Debian Codegraph @

tensilelite/Tensile/Source/FindOpenCL.cmake

raw
1 ################################################################################
2 #
3 # Copyright (C) 2022 Advanced Micro Devices, Inc. All rights reserved.
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 # SOFTWARE.
22 #
23 ################################################################################
24
25 # Author: Kent Knox <kent.knox@amd dot com>
26 # Locate an OpenCL implementation.
27 # Currently supports AMD APP SDK (http://developer.amd.com/sdks/AMDAPPSDK/Pages/default.aspx/)
28 #
29 # Defines the following variables:
30 #
31 # OPENCL_FOUND - Found the OPENCL framework
32 # OPENCL_INCLUDE_DIRS - Include directories
33 #
34 # Also defines the library variables below as normal
35 # variables. These contain debug/optimized keywords when
36 # a debugging library is found.
37 #
38 # OPENCL_LIBRARIES - libopencl
39 #
40 # Accepts the following variables as input:
41 #
42 # OPENCL_ROOT - (as a CMake or environment variable)
43 # The root directory of the OpenCL implementation found
44 #
45 # FIND_LIBRARY_USE_LIB64_PATHS - Global property that controls whether findOpenCL should search for
46 # 64bit or 32bit libs
47 #-----------------------
48 # Example Usage:
49 #
50 # find_package(OPENCL REQUIRED)
51 # include_directories(${OPENCL_INCLUDE_DIRS})
52 #
53 # add_executable(foo foo.cc)
54 # target_link_libraries(foo ${OPENCL_LIBRARIES})
55 #
56 #-----------------------
57 include( CheckSymbolExists )
58 include( CMakePushCheckState )
59
60 if( DEFINED OPENCL_ROOT OR DEFINED ENV{OPENCL_ROOT})
61 message( STATUS "Defined OPENCL_ROOT: ${OPENCL_ROOT}, ENV{OPENCL_ROOT}: $ENV{OPENCL_ROOT}" )
62 endif( )
63
64 find_path(OPENCL_INCLUDE_DIRS
65 NAMES OpenCL/cl.h CL/cl.h
66 HINTS
67 ${OPENCL_ROOT}/include
68 $ENV{OPENCL_ROOT}/include
69 $ENV{AMDAPPSDKROOT}/include
70 $ENV{CUDA_PATH}/include
71 PATHS
72 /usr/include
73 /usr/local/include
74 /usr/local/cuda/include
75 /opt/rocm/opencl/include
76 DOC "OpenCL header file path"
77 )
78 mark_as_advanced( OPENCL_INCLUDE_DIRS )
79 message( STATUS "OPENCL_INCLUDE_DIRS: ${OPENCL_INCLUDE_DIRS}" )
80
81 set( OpenCL_VERSION "0.0" )
82
83 cmake_push_check_state( RESET )
84 set( CMAKE_REQUIRED_INCLUDES "${OPENCL_INCLUDE_DIRS}" )
85
86 # Bug in check_symbol_exists prevents us from specifying a list of files, so we loop
87 # Only 1 of these files will exist on a system, so the other file will not clobber the output variable
88 if( APPLE )
89 set( CL_HEADER_FILE "OpenCL/cl.h" )
90 else( )
91 set( CL_HEADER_FILE "CL/cl.h" )
92 endif( )
93
94 check_symbol_exists( CL_VERSION_2_0 ${CL_HEADER_FILE} HAVE_CL_2_0 )
95 check_symbol_exists( CL_VERSION_1_2 ${CL_HEADER_FILE} HAVE_CL_1_2 )
96 check_symbol_exists( CL_VERSION_1_1 ${CL_HEADER_FILE} HAVE_CL_1_1 )
97 # message( STATUS "HAVE_CL_2_0: ${HAVE_CL_2_0}" )
98 # message( STATUS "HAVE_CL_1_2: ${HAVE_CL_1_2}" )
99 # message( STATUS "HAVE_CL_1_1: ${HAVE_CL_1_1}" )
100
101 # set OpenCL_VERSION to the highest detected version
102 if( HAVE_CL_2_0 )
103 set( OpenCL_VERSION "2.0" )
104 elseif( HAVE_CL_1_2 )
105 set( OpenCL_VERSION "1.2" )
106 elseif( HAVE_CL_1_1 )
107 set( OpenCL_VERSION "1.1" )
108 endif( )
109
110 cmake_pop_check_state( )
111
112 # Search for 64bit/32bit libs
113 if( CMAKE_SIZEOF_VOID_P EQUAL 8 )
114 set(LIB64 ON)
115 message( STATUS "FindOpenCL searching for 64-bit libraries" )
116 else( )
117 set(LIB64 OFF)
118 message( STATUS "FindOpenCL searching for 32-bit libraries" )
119 endif( )
120
121 if( LIB64 )
122 find_library( OPENCL_LIBRARIES
123 NAMES OpenCL
124 HINTS
125 ${OPENCL_ROOT}/lib
126 $ENV{OPENCL_ROOT}/lib
127 $ENV{AMDAPPSDKROOT}/lib
128 $ENV{CUDA_PATH}/lib
129 DOC "OpenCL dynamic library path"
130 PATH_SUFFIXES x86_64 x64
131 PATHS
132 /usr/lib
133 /usr/local/cuda/lib
134 /opt/rocm/opencl/lib
135 )
136 else( )
137 find_library( OPENCL_LIBRARIES
138 NAMES OpenCL
139 HINTS
140 ${OPENCL_ROOT}/lib
141 $ENV{OPENCL_ROOT}/lib
142 $ENV{AMDAPPSDKROOT}/lib
143 $ENV{CUDA_PATH}/lib
144 DOC "OpenCL dynamic library path"
145 PATH_SUFFIXES x86 Win32
146 PATHS
147 /usr/lib
148 /usr/local/cuda/lib
149 /opt/rocm/opencl/lib
150 )
151 endif( )
152 mark_as_advanced( OPENCL_LIBRARIES )
153
154 # If we asked for OpenCL 1.2, and we found a version installed greater than that, pass the 'use deprecated' flag
155 if( (OpenCL_FIND_VERSION VERSION_LESS "2.0") AND (OpenCL_VERSION VERSION_GREATER OpenCL_FIND_VERSION) )
156 add_definitions( -DCL_USE_DEPRECATED_OPENCL_2_0_APIS )
157 add_definitions( -DCL_USE_DEPRECATED_OPENCL_1_2_APIS )
158
159 # If we asked for OpenCL 1.1, and we found a version installed greater than that, pass the 'use deprecated' flag
160 if( (OpenCL_FIND_VERSION VERSION_LESS "1.2") AND (OpenCL_VERSION VERSION_GREATER OpenCL_FIND_VERSION) )
161 add_definitions( -DCL_USE_DEPRECATED_OPENCL_1_1_APIS )
162 endif( )
163 endif( )
164
165 include( FindPackageHandleStandardArgs )
166 FIND_PACKAGE_HANDLE_STANDARD_ARGS( OPENCL
167 REQUIRED_VARS OPENCL_LIBRARIES OPENCL_INCLUDE_DIRS
168 VERSION_VAR OpenCL_VERSION
169 )
170
171 if( NOT OPENCL_FOUND )
172 message( STATUS "FindOpenCL looked for libraries named: OpenCL" )
173 else( )
174 message(STATUS "FindOpenCL ${OPENCL_LIBRARIES}, ${OPENCL_INCLUDE_DIRS}")
175
176 # UNKNOWN implies on windows I can use the .lib file for IMPORTED_LOCATION
177 # we don't care about the location of opencl.dll, it's installed in system directories
178 if (WIN32)
179 add_library( opencl UNKNOWN IMPORTED )
180 endif()
181
182 set_target_properties( opencl PROPERTIES
183 INTERFACE_INCLUDE_DIRECTORIES "${OPENCL_INCLUDE_DIRS}"
184 IMPORTED_LOCATION "${OPENCL_LIBRARIES}"
185 )
186
187 endif()