Merge topic 'cxx-checks-tolerate-unused-arguments'
[kiteware-cmake.git] / Modules / FindOpenCL.cmake
blob55be667a86d796658a108b6cf2cc66f8522ce4c1
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:
5 FindOpenCL
6 ----------
8 .. versionadded:: 3.1
10 Finds Open Computing Language (OpenCL)
12 .. versionadded:: 3.10
13   Detection of OpenCL 2.1 and 2.2.
15 IMPORTED Targets
16 ^^^^^^^^^^^^^^^^
18 .. versionadded:: 3.7
20 This module defines :prop_tgt:`IMPORTED` target ``OpenCL::OpenCL``, if
21 OpenCL has been found.
23 Result Variables
24 ^^^^^^^^^^^^^^^^
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)
54       CHECK_SYMBOL_EXISTS(
55         CL_VERSION_${VERSION}
56         "${OpenCL_INCLUDE_DIR}/Headers/cl.h"
57         OPENCL_VERSION_${VERSION})
58     else()
59       CHECK_SYMBOL_EXISTS(
60         CL_VERSION_${VERSION}
61         "${OpenCL_INCLUDE_DIR}/CL/cl.h"
62         OPENCL_VERSION_${VERSION})
63     endif()
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)
73       break()
74     endif()
75   endforeach()
76   CMAKE_POP_CHECK_STATE()
77 endfunction()
79 find_path(OpenCL_INCLUDE_DIR
80   NAMES
81     CL/cl.h OpenCL/cl.h
82   PATHS
83     ENV "PROGRAMFILES(X86)"
84     ENV "PROGRAMFILES"
85     $ENV{PROGRAMFILES${_OPENCL_x86}}/OpenCLHeaders
86     $ENV{PROGRAMFILES}/OpenCLHeaders
87     ENV AMDAPPSDKROOT
88     ENV INTELOCLSDKROOT
89     ENV NVSDKCOMPUTE_ROOT
90     ENV CUDA_PATH
91     ENV ATISTREAMSDKROOT
92     ENV OCL_ROOT
93     /usr/local/cuda
94     /opt/cuda
95   PATH_SUFFIXES
96     include
97     OpenCL/common/inc
98     "AMD APP/include")
100 _FIND_OPENCL_VERSION()
102 if(WIN32)
103   if(CMAKE_SIZEOF_VOID_P EQUAL 4)
104     find_library(OpenCL_LIBRARY
105       NAMES OpenCL
106       PATHS
107         ENV "PROGRAMFILES(X86)"
108         ENV "PROGRAMFILES"
109         $ENV{PROGRAMFILES${_OPENCL_x86}}/OpenCL-ICD-Loader
110         $ENV{PROGRAMFILES}/OpenCL-ICD-Loader
111         ENV AMDAPPSDKROOT
112         ENV INTELOCLSDKROOT
113         ENV CUDA_PATH
114         ENV NVSDKCOMPUTE_ROOT
115         ENV ATISTREAMSDKROOT
116         ENV OCL_ROOT
117       PATH_SUFFIXES
118         "AMD APP/lib/x86"
119         lib/x86
120         lib/Win32
121         OpenCL/common/lib/Win32)
122   elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
123     find_library(OpenCL_LIBRARY
124       NAMES OpenCL
125       PATHS
126         ENV "PROGRAMFILES(X86)"
127         ENV "PROGRAMFILES"
128         $ENV{PROGRAMFILES${_OPENCL_x86}}/OpenCL-ICD-Loader
129         $ENV{PROGRAMFILES}/OpenCL-ICD-Loader
130         ENV AMDAPPSDKROOT
131         ENV INTELOCLSDKROOT
132         ENV CUDA_PATH
133         ENV NVSDKCOMPUTE_ROOT
134         ENV ATISTREAMSDKROOT
135         ENV OCL_ROOT
136       PATH_SUFFIXES
137         "AMD APP/lib/x86_64"
138         lib/x86_64
139         lib/x64
140         lib
141         OpenCL/common/lib/x64)
142   endif()
143 else()
144   if(CMAKE_SIZEOF_VOID_P EQUAL 4)
145     find_library(OpenCL_LIBRARY
146       NAMES OpenCL
147       PATHS
148         ENV AMDAPPSDKROOT
149         ENV CUDA_PATH
150         /usr/local/cuda
151         /opt/cuda
152       PATH_SUFFIXES
153         lib/x86
154         lib)
155   elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
156     find_library(OpenCL_LIBRARY
157       NAMES OpenCL
158       PATHS
159         ENV AMDAPPSDKROOT
160         ENV CUDA_PATH
161         /usr/local/cuda
162         /opt/cuda
163       PATH_SUFFIXES
164         lib/x86_64
165         lib/x64
166         lib
167         lib64)
168   endif()
169 endif()
171 unset(_OPENCL_x86)
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(
178   OpenCL
179   FOUND_VAR OpenCL_FOUND
180   REQUIRED_VARS OpenCL_LIBRARY OpenCL_INCLUDE_DIR
181   VERSION_VAR OpenCL_VERSION_STRING)
183 mark_as_advanced(
184   OpenCL_INCLUDE_DIR
185   OpenCL_LIBRARY)
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}")
192   else()
193     add_library(OpenCL::OpenCL UNKNOWN IMPORTED)
194     set_target_properties(OpenCL::OpenCL PROPERTIES
195       IMPORTED_LOCATION "${OpenCL_LIBRARY}")
196   endif()
197   set_target_properties(OpenCL::OpenCL PROPERTIES
198     INTERFACE_INCLUDE_DIRECTORIES "${OpenCL_INCLUDE_DIRS}")
199 endif()