diff --git a/opencl/CMakeLists.txt b/opencl/CMakeLists.txt index 0223ffd0fa..14ae06c477 100644 --- a/opencl/CMakeLists.txt +++ b/opencl/CMakeLists.txt @@ -15,7 +15,7 @@ set(CMAKE_INSTALL_LIBDIR "lib" CACHE STRING "Library install directory") include(GNUInstallDirs) option(BUILD_TESTS "Enable building OpenCL tests" OFF) -option(BUILD_ICD "Enable building OpenCL ICD Loader" ON) +option(BUILD_ICD "Enable building OpenCL ICD Loader" OFF) option(EMU_ENV "Enable building for emulation environment" OFF) option(FILE_REORG_BACKWARD_COMPATIBILITY "Enable File Reorganization backward compatibility" OFF) option(ENABLE_ADDRESS_SANITIZER "Option to enable ASAN build" OFF) @@ -39,8 +39,6 @@ endif() set(OPENCL_ICD_LOADER_HEADERS_DIR "${CMAKE_CURRENT_LIST_DIR}/khronos/headers/opencl2.2" CACHE PATH "") if(BUILD_ICD) add_subdirectory(khronos/icd) -else() - find_package(OpenCL REQUIRED) endif() add_subdirectory(amdocl) add_subdirectory(tools/clinfo) diff --git a/opencl/cmake/FindAMD_ICD.cmake b/opencl/cmake/FindAMD_ICD.cmake new file mode 100644 index 0000000000..7954fea0a8 --- /dev/null +++ b/opencl/cmake/FindAMD_ICD.cmake @@ -0,0 +1,66 @@ +# Copyright (c) 2020 - 2024 Advanced Micro Devices, Inc. All rights reserved. +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +if(AMD_ICD_FOUND) + return() +endif() + +if(WIN32) + find_path(AMD_ICD_LIBRARY_DIR OpenCL.lib + HINTS + ${CMAKE_CURRENT_BINARY_DIR}/../../../../icd #for clinfo + ${CMAKE_CURRENT_BINARY_DIR}/../../../../../icd #for ocltst + ${CMAKE_CURRENT_BINARY_DIR}/../../../../../../icd #for ocltst modules + NO_DEFAULT_PATH) +else() +# The following line is used by AMD python script to build this repo in Linux. +# +# If people only use cmake to build this repo, ICD repo may not be built yet. +# As a result, ICD cannot be found because ICD does not exist. +# Take ocltst as an example how to build this repo under such scenario. +# ocltst cmake uses find_library command to find OpenCL. If find_library can't +# find ICD built by AMD, find_library command continue to search OpenCL +# installed in system. For example, find_library can find OpenCL installed by +# AMD rocm driver, /opt/rocm/lib/libOpenCL.so. +# +# Another solution is to build ICD loader first. Then install ICD loader into +# system. This solution is used by ROCM stack currently. + + find_path(AMD_ICD_LIBRARY_DIR libOpenCL.so + HINTS + # python build script + ${CMAKE_CURRENT_BINARY_DIR}/../../../../icd #for clinfo + ${CMAKE_CURRENT_BINARY_DIR}/../../../../../icd #for ocltst + ${CMAKE_CURRENT_BINARY_DIR}/../../../../../../icd #for ocltst modules + # rocm stack build scripts + ${CMAKE_CURRENT_BINARY_DIR}/../../../../OpenCL-ICD-Loader #for clinfo + ${CMAKE_CURRENT_BINARY_DIR}/../../../../../OpenCL-ICD-Loader #for ocltst + ${CMAKE_CURRENT_BINARY_DIR}/../../../../../../OpenCL-ICD-Loader #for ocltst modules + # pure cmake method + /opt/rocm/lib + NO_DEFAULT_PATH) +endif() + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(AMD_ICD + "\nICD not found. Build may succeed if OpenCL ICD is installed in the system" + AMD_ICD_LIBRARY_DIR) + +mark_as_advanced(AMD_ICD_LIBRARY_DIR) diff --git a/opencl/tests/ocltst/env/CMakeLists.txt b/opencl/tests/ocltst/env/CMakeLists.txt index 308acc7d6c..b483d28ce6 100644 --- a/opencl/tests/ocltst/env/CMakeLists.txt +++ b/opencl/tests/ocltst/env/CMakeLists.txt @@ -34,10 +34,16 @@ target_include_directories(ocltst PRIVATE $) -target_link_libraries(ocltst - PRIVATE - OpenCL - ) +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake") +find_package(AMD_ICD) +find_library(AMD_ICD_LIBRARY OpenCL HINTS "${AMD_ICD_LIBRARY_DIR}") +target_link_libraries(ocltst PRIVATE ${AMD_ICD_LIBRARY}) + +if (NOT WIN32) + set(THREADS_PREFER_PTHREAD_FLAG ON) + find_package(Threads REQUIRED) + target_link_libraries(ocltst PRIVATE Threads::Threads ${CMAKE_DL_LIBS}) +endif() set_target_properties(ocltst PROPERTIES INSTALL_RPATH "$ORIGIN") diff --git a/opencl/tests/ocltst/module/gl/CMakeLists.txt b/opencl/tests/ocltst/module/gl/CMakeLists.txt index 115879cf15..d5fba4c527 100644 --- a/opencl/tests/ocltst/module/gl/CMakeLists.txt +++ b/opencl/tests/ocltst/module/gl/CMakeLists.txt @@ -47,10 +47,20 @@ target_include_directories(oclgl target_link_libraries(oclgl PRIVATE - OpenCL ${GLEW_LIBRARIES} ${OPENGL_LIBRARIES}) +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../../cmake") +find_package(AMD_ICD) +find_library(AMD_ICD_LIBRARY OpenCL HINTS "${AMD_ICD_LIBRARY_DIR}") +target_link_libraries(oclgl PRIVATE ${AMD_ICD_LIBRARY}) + +if (NOT WIN32) + set(THREADS_PREFER_PTHREAD_FLAG ON) + find_package(Threads REQUIRED) + target_link_libraries(oclgl PRIVATE Threads::Threads) +endif() + add_custom_command( TARGET oclgl POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy diff --git a/opencl/tests/ocltst/module/perf/CMakeLists.txt b/opencl/tests/ocltst/module/perf/CMakeLists.txt index b9780283c6..1a266d1fd0 100644 --- a/opencl/tests/ocltst/module/perf/CMakeLists.txt +++ b/opencl/tests/ocltst/module/perf/CMakeLists.txt @@ -95,9 +95,15 @@ target_include_directories(oclperf PRIVATE $) -target_link_libraries(oclperf - PRIVATE - OpenCL) +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../../cmake") +find_package(AMD_ICD) +find_library(AMD_ICD_LIBRARY OpenCL HINTS "${AMD_ICD_LIBRARY_DIR}") +target_link_libraries(oclperf PRIVATE ${AMD_ICD_LIBRARY}) +if (NOT WIN32) + set(THREADS_PREFER_PTHREAD_FLAG ON) + find_package(Threads REQUIRED) + target_link_libraries(oclperf PRIVATE Threads::Threads) +endif() add_custom_command( TARGET oclperf POST_BUILD diff --git a/opencl/tests/ocltst/module/runtime/CMakeLists.txt b/opencl/tests/ocltst/module/runtime/CMakeLists.txt index 0b5de94176..e7db5cf7c5 100644 --- a/opencl/tests/ocltst/module/runtime/CMakeLists.txt +++ b/opencl/tests/ocltst/module/runtime/CMakeLists.txt @@ -68,9 +68,17 @@ target_include_directories(oclruntime PRIVATE $) -target_link_libraries(oclruntime - PRIVATE - OpenCL) + +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../../cmake") +find_package(AMD_ICD) +find_library(AMD_ICD_LIBRARY OpenCL HINTS "${AMD_ICD_LIBRARY_DIR}") +target_link_libraries(oclruntime PRIVATE ${AMD_ICD_LIBRARY}) + +if (NOT WIN32) + set(THREADS_PREFER_PTHREAD_FLAG ON) + find_package(Threads REQUIRED) + target_link_libraries(oclruntime PRIVATE Threads::Threads) +endif() add_custom_command( TARGET oclruntime POST_BUILD diff --git a/opencl/tools/clinfo/CMakeLists.txt b/opencl/tools/clinfo/CMakeLists.txt index 217f18225b..0af9f1eab1 100644 --- a/opencl/tools/clinfo/CMakeLists.txt +++ b/opencl/tools/clinfo/CMakeLists.txt @@ -2,9 +2,14 @@ add_executable(clinfo clinfo.cpp) target_compile_definitions(clinfo PRIVATE CL_TARGET_OPENCL_VERSION=220 HAVE_CL2_HPP) -target_include_directories(clinfo PRIVATE ${OPENCL_ICD_LOADER_HEADERS_DIR}) +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../cmake") +find_package(AMD_ICD) -target_link_libraries(clinfo OpenCL) +#todo: to be updated to use header files from other repos +target_include_directories(clinfo PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../../khronos/headers/opencl2.2") + +find_library(AMD_ICD_LIBRARY OpenCL HINTS "${AMD_ICD_LIBRARY_DIR}") +target_link_libraries(clinfo PRIVATE ${AMD_ICD_LIBRARY}) INSTALL(TARGETS clinfo RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})