1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
10 Finds Open Computing Language (OpenCL)
12 .. versionadded:: 3.10
13 Detection of OpenCL 2.1 and 2.2.
20 This module defines :prop_tgt:`IMPORTED` target ``OpenCL::OpenCL``, if
21 OpenCL has been found.
26 This module defines the following variables::
28 OpenCL_FOUND - True if OpenCL was found
29 OpenCL_INCLUDE_DIRS - include directories for OpenCL
30 OpenCL_LIBRARIES - link against this library to use OpenCL
31 OpenCL_VERSION_STRING - Highest supported OpenCL version (eg. 1.2)
32 OpenCL_VERSION_MAJOR - The major version of the OpenCL implementation
33 OpenCL_VERSION_MINOR - The minor version of the OpenCL implementation
35 The module will also define two cache variables::
37 OpenCL_INCLUDE_DIR - the OpenCL include directory
38 OpenCL_LIBRARY - the path to the OpenCL library
40 #]=======================================================================]
42 set(_OPENCL_x86 "(x86)")
44 function(_FIND_OPENCL_VERSION)
45 include(CheckSymbolExists)
46 include(CMakePushCheckState)
47 set(CMAKE_REQUIRED_QUIET ${OpenCL_FIND_QUIETLY})
49 CMAKE_PUSH_CHECK_STATE()
50 foreach(VERSION "3_0" "2_2" "2_1" "2_0" "1_2" "1_1" "1_0")
51 set(CMAKE_REQUIRED_INCLUDES "${OpenCL_INCLUDE_DIR}")
53 if(EXISTS ${OpenCL_INCLUDE_DIR}/Headers/cl.h)
56 "${OpenCL_INCLUDE_DIR}/Headers/cl.h"
57 OPENCL_VERSION_${VERSION})
61 "${OpenCL_INCLUDE_DIR}/CL/cl.h"
62 OPENCL_VERSION_${VERSION})
65 if(OPENCL_VERSION_${VERSION})
66 string(REPLACE "_" "." VERSION "${VERSION}")
67 set(OpenCL_VERSION_STRING ${VERSION} PARENT_SCOPE)
68 string(REGEX MATCHALL "[0-9]+" version_components "${VERSION}")
69 list(GET version_components 0 major_version)
70 list(GET version_components 1 minor_version)
71 set(OpenCL_VERSION_MAJOR ${major_version} PARENT_SCOPE)
72 set(OpenCL_VERSION_MINOR ${minor_version} PARENT_SCOPE)
76 CMAKE_POP_CHECK_STATE()
79 find_path(OpenCL_INCLUDE_DIR
83 ENV "PROGRAMFILES(X86)"
85 $ENV{PROGRAMFILES${_OPENCL_x86}}/OpenCLHeaders
86 $ENV{PROGRAMFILES}/OpenCLHeaders
100 _FIND_OPENCL_VERSION()
103 if(CMAKE_SIZEOF_VOID_P EQUAL 4)
104 find_library(OpenCL_LIBRARY
107 ENV "PROGRAMFILES(X86)"
109 $ENV{PROGRAMFILES${_OPENCL_x86}}/OpenCL-ICD-Loader
110 $ENV{PROGRAMFILES}/OpenCL-ICD-Loader
114 ENV NVSDKCOMPUTE_ROOT
121 OpenCL/common/lib/Win32)
122 elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
123 find_library(OpenCL_LIBRARY
126 ENV "PROGRAMFILES(X86)"
128 $ENV{PROGRAMFILES${_OPENCL_x86}}/OpenCL-ICD-Loader
129 $ENV{PROGRAMFILES}/OpenCL-ICD-Loader
133 ENV NVSDKCOMPUTE_ROOT
141 OpenCL/common/lib/x64)
144 if(CMAKE_SIZEOF_VOID_P EQUAL 4)
145 find_library(OpenCL_LIBRARY
155 elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
156 find_library(OpenCL_LIBRARY
173 set(OpenCL_LIBRARIES ${OpenCL_LIBRARY})
174 set(OpenCL_INCLUDE_DIRS ${OpenCL_INCLUDE_DIR})
176 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
177 find_package_handle_standard_args(
179 FOUND_VAR OpenCL_FOUND
180 REQUIRED_VARS OpenCL_LIBRARY OpenCL_INCLUDE_DIR
181 VERSION_VAR OpenCL_VERSION_STRING)
187 if(OpenCL_FOUND AND NOT TARGET OpenCL::OpenCL)
188 if(OpenCL_LIBRARY MATCHES "/([^/]+)\\.framework$")
189 add_library(OpenCL::OpenCL INTERFACE IMPORTED)
190 set_target_properties(OpenCL::OpenCL PROPERTIES
191 INTERFACE_LINK_LIBRARIES "${OpenCL_LIBRARY}")
193 add_library(OpenCL::OpenCL UNKNOWN IMPORTED)
194 set_target_properties(OpenCL::OpenCL PROPERTIES
195 IMPORTED_LOCATION "${OpenCL_LIBRARY}")
197 set_target_properties(OpenCL::OpenCL PROPERTIES
198 INTERFACE_INCLUDE_DIRECTORIES "${OpenCL_INCLUDE_DIRS}")