FindOpenCL.cmake 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindOpenCL
  5. ----------
  6. Finds Open Computing Language (OpenCL)
  7. IMPORTED Targets
  8. ^^^^^^^^^^^^^^^^
  9. This module defines :prop_tgt:`IMPORTED` target ``OpenCL::OpenCL``, if
  10. OpenCL has been found.
  11. Result Variables
  12. ^^^^^^^^^^^^^^^^
  13. This module defines the following variables::
  14. OpenCL_FOUND - True if OpenCL was found
  15. OpenCL_INCLUDE_DIRS - include directories for OpenCL
  16. OpenCL_LIBRARIES - link against this library to use OpenCL
  17. OpenCL_VERSION_STRING - Highest supported OpenCL version (eg. 1.2)
  18. OpenCL_VERSION_MAJOR - The major version of the OpenCL implementation
  19. OpenCL_VERSION_MINOR - The minor version of the OpenCL implementation
  20. The module will also define two cache variables::
  21. OpenCL_INCLUDE_DIR - the OpenCL include directory
  22. OpenCL_LIBRARY - the path to the OpenCL library
  23. #]=======================================================================]
  24. function(_FIND_OPENCL_VERSION)
  25. include(CheckSymbolExists)
  26. include(CMakePushCheckState)
  27. set(CMAKE_REQUIRED_QUIET ${OpenCL_FIND_QUIETLY})
  28. CMAKE_PUSH_CHECK_STATE()
  29. foreach(VERSION "2_2" "2_1" "2_0" "1_2" "1_1" "1_0")
  30. set(CMAKE_REQUIRED_INCLUDES "${OpenCL_INCLUDE_DIR}")
  31. if(APPLE)
  32. CHECK_SYMBOL_EXISTS(
  33. CL_VERSION_${VERSION}
  34. "${OpenCL_INCLUDE_DIR}/Headers/cl.h"
  35. OPENCL_VERSION_${VERSION})
  36. else()
  37. CHECK_SYMBOL_EXISTS(
  38. CL_VERSION_${VERSION}
  39. "${OpenCL_INCLUDE_DIR}/CL/cl.h"
  40. OPENCL_VERSION_${VERSION})
  41. endif()
  42. if(OPENCL_VERSION_${VERSION})
  43. string(REPLACE "_" "." VERSION "${VERSION}")
  44. set(OpenCL_VERSION_STRING ${VERSION} PARENT_SCOPE)
  45. string(REGEX MATCHALL "[0-9]+" version_components "${VERSION}")
  46. list(GET version_components 0 major_version)
  47. list(GET version_components 1 minor_version)
  48. set(OpenCL_VERSION_MAJOR ${major_version} PARENT_SCOPE)
  49. set(OpenCL_VERSION_MINOR ${minor_version} PARENT_SCOPE)
  50. break()
  51. endif()
  52. endforeach()
  53. CMAKE_POP_CHECK_STATE()
  54. endfunction()
  55. find_path(OpenCL_INCLUDE_DIR
  56. NAMES
  57. CL/cl.h OpenCL/cl.h
  58. PATHS
  59. ENV "PROGRAMFILES(X86)"
  60. ENV AMDAPPSDKROOT
  61. ENV INTELOCLSDKROOT
  62. ENV NVSDKCOMPUTE_ROOT
  63. ENV CUDA_PATH
  64. ENV ATISTREAMSDKROOT
  65. ENV OCL_ROOT
  66. PATH_SUFFIXES
  67. include
  68. OpenCL/common/inc
  69. "AMD APP/include")
  70. _FIND_OPENCL_VERSION()
  71. if(WIN32)
  72. if(CMAKE_SIZEOF_VOID_P EQUAL 4)
  73. find_library(OpenCL_LIBRARY
  74. NAMES OpenCL
  75. PATHS
  76. ENV "PROGRAMFILES(X86)"
  77. ENV AMDAPPSDKROOT
  78. ENV INTELOCLSDKROOT
  79. ENV CUDA_PATH
  80. ENV NVSDKCOMPUTE_ROOT
  81. ENV ATISTREAMSDKROOT
  82. ENV OCL_ROOT
  83. PATH_SUFFIXES
  84. "AMD APP/lib/x86"
  85. lib/x86
  86. lib/Win32
  87. OpenCL/common/lib/Win32)
  88. elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
  89. find_library(OpenCL_LIBRARY
  90. NAMES OpenCL
  91. PATHS
  92. ENV "PROGRAMFILES(X86)"
  93. ENV AMDAPPSDKROOT
  94. ENV INTELOCLSDKROOT
  95. ENV CUDA_PATH
  96. ENV NVSDKCOMPUTE_ROOT
  97. ENV ATISTREAMSDKROOT
  98. ENV OCL_ROOT
  99. PATH_SUFFIXES
  100. "AMD APP/lib/x86_64"
  101. lib/x86_64
  102. lib/x64
  103. OpenCL/common/lib/x64)
  104. endif()
  105. else()
  106. if(CMAKE_SIZEOF_VOID_P EQUAL 4)
  107. find_library(OpenCL_LIBRARY
  108. NAMES OpenCL
  109. PATHS
  110. ENV AMDAPPSDKROOT
  111. ENV CUDA_PATH
  112. PATH_SUFFIXES
  113. lib/x86
  114. lib)
  115. elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
  116. find_library(OpenCL_LIBRARY
  117. NAMES OpenCL
  118. PATHS
  119. ENV AMDAPPSDKROOT
  120. ENV CUDA_PATH
  121. PATH_SUFFIXES
  122. lib/x86_64
  123. lib/x64
  124. lib
  125. lib64)
  126. endif()
  127. endif()
  128. set(OpenCL_LIBRARIES ${OpenCL_LIBRARY})
  129. set(OpenCL_INCLUDE_DIRS ${OpenCL_INCLUDE_DIR})
  130. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  131. find_package_handle_standard_args(
  132. OpenCL
  133. FOUND_VAR OpenCL_FOUND
  134. REQUIRED_VARS OpenCL_LIBRARY OpenCL_INCLUDE_DIR
  135. VERSION_VAR OpenCL_VERSION_STRING)
  136. mark_as_advanced(
  137. OpenCL_INCLUDE_DIR
  138. OpenCL_LIBRARY)
  139. if(OpenCL_FOUND AND NOT TARGET OpenCL::OpenCL)
  140. if(OpenCL_LIBRARY MATCHES "/([^/]+)\\.framework$")
  141. add_library(OpenCL::OpenCL INTERFACE IMPORTED)
  142. set_target_properties(OpenCL::OpenCL PROPERTIES
  143. INTERFACE_LINK_LIBRARIES "${OpenCL_LIBRARY}")
  144. else()
  145. add_library(OpenCL::OpenCL UNKNOWN IMPORTED)
  146. set_target_properties(OpenCL::OpenCL PROPERTIES
  147. IMPORTED_LOCATION "${OpenCL_LIBRARY}")
  148. endif()
  149. set_target_properties(OpenCL::OpenCL PROPERTIES
  150. INTERFACE_INCLUDE_DIRECTORIES "${OpenCL_INCLUDE_DIRS}")
  151. endif()