Description: Ship utest_run testing tool

Use ICD mode (to allow testing any ICD, not just beignet), but
include builtin_kernel_block_motion_estimate_intel when testing beignet.

Allow testing CPU devices (the "only GPU device is supported right now"
comment appears to refer to beignet-the-ICD, not this test tool).

Give meaningful errors when there is nothing to test.

Author: Rebecca N. Palmer <rebecca_palmer@zoho.com>
Forwarded: partly https://lists.freedesktop.org/archives/beignet/2016-October/008057.html https://lists.freedesktop.org/archives/beignet/2016-October/008059.html

--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -23,7 +23,6 @@ elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL
 elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL "Intel")
   set(COMPILER "ICC")
 endif()
-set (NOT_BUILD_STAND_ALONE_UTEST 1)
 
 INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
                     ${CMAKE_CURRENT_SOURCE_DIR}/include)
@@ -328,7 +327,7 @@ ENDIF(BUILD_EXAMPLES)
 ADD_SUBDIRECTORY(include)
 ADD_SUBDIRECTORY(backend)
 ADD_SUBDIRECTORY(src)
-ADD_SUBDIRECTORY(utests EXCLUDE_FROM_ALL)
+ADD_SUBDIRECTORY(utests)
 
 # compile benchmark only if standalone compiler is not provided
 IF (NOT (USE_STANDALONE_GBE_COMPILER STREQUAL "true"))
--- a/utests/utest_helper.cpp
+++ b/utests/utest_helper.cpp
@@ -221,7 +221,7 @@ cl_do_kiss_path(const char *file, cl_device_id device)
   sub_path = "";
 
   if (kiss_path == NULL)
-    clpanic("set OCL_KERNEL_PATH. This is where the kiss kernels are", -1);
+    kiss_path="/usr/share/beignet/test_kernels";
   sz += strlen(kiss_path) + strlen(sub_path) + 2; /* +1 for end of string, +1 for '/' */
   if ((ker_path = (char*) malloc(sz)) == NULL)
     clpanic("Allocation failed", -1);
@@ -443,9 +443,19 @@ cl_ocl_init(void)
   cl_context_properties *props = NULL;
 
   /* Get the platform number */
-  OCL_CALL (clGetPlatformIDs, 0, NULL, &platform_n);
-  printf("platform number %u\n", platform_n);
-  assert(platform_n >= 1);
+  status = clGetPlatformIDs(0, NULL, &platform_n);
+  if (platform_n < 1) {
+    fprintf(stderr, "No platforms found (check that the ICD to be tested is installed)\n");
+    status = CL_DEVICE_NOT_FOUND;
+    goto error;
+  }
+  if (status != CL_SUCCESS) {
+    fprintf(stderr, "error calling clGetPlatformIDs\n");
+    goto error;
+  }
+  if (platform_n > 1) {
+    fprintf(stderr, "%u platforms found - testing the first one (if this is not the one you want to test, uninstall any other ICDs)\n", platform_n);
+  }
 
   /* Get a valid platform */
   OCL_CALL (clGetPlatformIDs, 1, &platform, &platform_n);
@@ -456,8 +466,22 @@ cl_ocl_init(void)
   GET_PLATFORM_STR_INFO(extensions, EXTENSIONS);
 
   /* Get the device (only GPU device is supported right now) */
+  cl_uint device_n;
+  status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 0, NULL, &device_n);
+  if (device_n < 1) {
+    fprintf(stderr, "No devices found in first platform (uninstall any ICDs that are not compatible with your hardware, check that you have permission to access the hardware)\n");
+    status = CL_DEVICE_NOT_FOUND;
+    goto error;
+  }
+  if (status != CL_SUCCESS) {
+    fprintf(stderr, "error calling clGetDeviceIDs\n");
+    goto error;
+  }
+  if (device_n > 1) {
+    fprintf(stderr, "%u devices found - testing the first one\n", device_n);
+  }
   try {
-    OCL_CALL (clGetDeviceIDs, platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);
+    OCL_CALL (clGetDeviceIDs, platform, CL_DEVICE_TYPE_ALL, 1, &device, NULL);
     {
       size_t param_value_size;
       GET_DEVICE_STR_INFO(profile, PROFILE);
--- a/utests/CMakeLists.txt
+++ b/utests/CMakeLists.txt
@@ -341,9 +341,7 @@ else(GEN_PCI_ID)
   DEPENDS ${GBE_BIN_FILE} ${kernel_bin}.cl)
 endif(GEN_PCI_ID)
 
-if (NOT_BUILD_STAND_ALONE_UTEST)
   ADD_CUSTOM_TARGET(kernel_bin.bin DEPENDS ${kernel_bin}.bin)
-endif (NOT_BUILD_STAND_ALONE_UTEST)
 
 add_custom_command(OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/generated
     COMMAND mkdir ${CMAKE_CURRENT_SOURCE_DIR}/generated -p
@@ -392,6 +390,9 @@ if (NOT_BUILD_STAND_ALONE_UTEST)
 endif (NOT_BUILD_STAND_ALONE_UTEST)
 
 ADD_DEPENDENCIES (utests utest_generator)
+set_target_properties(utest_run PROPERTIES INSTALL_RPATH ${BEIGNET_INSTALL_DIR})
+install (TARGETS utests LIBRARY DESTINATION ${BEIGNET_INSTALL_DIR})
+install (TARGETS utest_run RUNTIME DESTINATION ${BEIGNET_INSTALL_DIR})
 
 ADD_EXECUTABLE(flat_address_space runtime_flat_address_space.cpp)
 TARGET_LINK_LIBRARIES(flat_address_space utests)
