Merge topic 'cuda_add_12.8_new_sm_support'
[kiteware-cmake.git] / Modules / CMakeDetermineCUDACompiler.cmake
blob70528da3d0eeee1887237eef0991d65a61fa2224
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 include(${CMAKE_ROOT}/Modules/CMakeDetermineCompiler.cmake)
5 include(${CMAKE_ROOT}/Modules/CMakeParseImplicitLinkInfo.cmake)
7 if(NOT ((CMAKE_GENERATOR MATCHES "Make") OR
8         (CMAKE_GENERATOR MATCHES "Ninja") OR
9         (CMAKE_GENERATOR MATCHES "Visual Studio (1|[9][0-9])")))
10   message(FATAL_ERROR "CUDA language not currently supported by \"${CMAKE_GENERATOR}\" generator")
11 endif()
13 if(CMAKE_GENERATOR MATCHES "Visual Studio")
14   if(DEFINED ENV{CUDAHOSTCXX} OR DEFINED CMAKE_CUDA_HOST_COMPILER)
15     message(WARNING "Visual Studio does not support specifying CUDAHOSTCXX or CMAKE_CUDA_HOST_COMPILER. Using the C++ compiler provided by Visual Studio.")
16   endif()
17 else()
18   if(NOT CMAKE_CUDA_COMPILER)
19     set(CMAKE_CUDA_COMPILER_INIT NOTFOUND)
21     # prefer the environment variable CUDACXX
22     if(NOT $ENV{CUDACXX} STREQUAL "")
23       get_filename_component(CMAKE_CUDA_COMPILER_INIT $ENV{CUDACXX} PROGRAM PROGRAM_ARGS CMAKE_CUDA_FLAGS_ENV_INIT)
24       if(CMAKE_CUDA_FLAGS_ENV_INIT)
25         set(CMAKE_CUDA_COMPILER_ARG1 "${CMAKE_CUDA_FLAGS_ENV_INIT}" CACHE STRING "Arguments to CUDA compiler")
26       endif()
27       if(NOT EXISTS ${CMAKE_CUDA_COMPILER_INIT})
28         message(FATAL_ERROR "Could not find compiler set in environment variable CUDACXX:\n$ENV{CUDACXX}.\n${CMAKE_CUDA_COMPILER_INIT}")
29       endif()
30     endif()
32     # finally list compilers to try
33     if(NOT CMAKE_CUDA_COMPILER_INIT)
34       set(CMAKE_CUDA_COMPILER_LIST nvcc)
35     endif()
37     set(_CMAKE_CUDA_COMPILER_PATHS "$ENV{CUDA_PATH}/bin")
38     _cmake_find_compiler(CUDA)
39     unset(_CMAKE_CUDA_COMPILER_PATHS)
40   else()
41     _cmake_find_compiler_path(CUDA)
42   endif()
44   mark_as_advanced(CMAKE_CUDA_COMPILER)
46   #Allow the user to specify a host compiler except for Visual Studio
47   if(NOT $ENV{CUDAHOSTCXX} STREQUAL "")
48     get_filename_component(CMAKE_CUDA_HOST_COMPILER $ENV{CUDAHOSTCXX} PROGRAM)
49     if(NOT EXISTS ${CMAKE_CUDA_HOST_COMPILER})
50       message(FATAL_ERROR "Could not find compiler set in environment variable CUDAHOSTCXX:\n$ENV{CUDAHOSTCXX}.\n${CMAKE_CUDA_HOST_COMPILER}")
51     endif()
52   endif()
53 endif()
55 if(NOT "$ENV{CUDAARCHS}" STREQUAL "")
56   set(CMAKE_CUDA_ARCHITECTURES "$ENV{CUDAARCHS}" CACHE STRING "CUDA architectures")
57 endif()
59 # Build a small source file to identify the compiler.
60 if(NOT CMAKE_CUDA_COMPILER_ID_RUN)
61   set(CMAKE_CUDA_COMPILER_ID_RUN 1)
63   include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
65   if(CMAKE_GENERATOR MATCHES "Visual Studio")
66     # We will not know CMAKE_CUDA_COMPILER until the main compiler id step
67     # below extracts it, but we do know that the compiler id will be NVIDIA.
68     set(CMAKE_CUDA_COMPILER_ID "NVIDIA")
69   else()
70     # We determine the vendor to help with find the toolkit and use the right flags for detection right away.
71     # The main compiler identification is still needed below to extract other information.
72     list(APPEND CMAKE_CUDA_COMPILER_ID_VENDORS NVIDIA Clang)
73     set(CMAKE_CUDA_COMPILER_ID_VENDOR_REGEX_NVIDIA "nvcc: NVIDIA \\(R\\) Cuda compiler driver")
74     set(CMAKE_CUDA_COMPILER_ID_VENDOR_REGEX_Clang "(clang version)")
75     CMAKE_DETERMINE_COMPILER_ID_VENDOR(CUDA "--version")
77     # Find the CUDA toolkit to get:
78     # - CMAKE_CUDA_COMPILER_TOOLKIT_VERSION
79     # - CMAKE_CUDA_COMPILER_TOOLKIT_ROOT
80     # - CMAKE_CUDA_COMPILER_LIBRARY_ROOT
81     # We save them in CMakeCUDACompiler.cmake so FindCUDAToolkit can
82     # avoid searching on future runs and the toolkit is the same.
83     # Match arguments with cmake_cuda_architectures_all call.
84     include(Internal/CMakeCUDAFindToolkit)
85     cmake_cuda_find_toolkit(CUDA CMAKE_CUDA_COMPILER_)
87     set(CMAKE_CUDA_DEVICE_LINKER "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}/bin/nvlink${CMAKE_EXECUTABLE_SUFFIX}")
88     set(CMAKE_CUDA_FATBINARY "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}/bin/fatbinary${CMAKE_EXECUTABLE_SUFFIX}")
89   endif()
91   set(CMAKE_CUDA_COMPILER_ID_FLAGS_ALWAYS "-v")
93   if(CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
94     set(nvcc_test_flags "--keep --keep-dir tmp")
95     if(CMAKE_CUDA_HOST_COMPILER)
96       string(APPEND nvcc_test_flags " -ccbin=\"${CMAKE_CUDA_HOST_COMPILER}\"")
97     endif()
98     # If we have extracted the vendor as NVIDIA we should require detection to
99     # work. If we don't, users will get confusing errors later about failure
100     # to detect a default value for CMAKE_CUDA_ARCHITECTURES
101     set(CMAKE_CUDA_COMPILER_ID_REQUIRE_SUCCESS ON)
102   elseif(CMAKE_CUDA_COMPILER_ID STREQUAL "Clang")
103     set(clang_test_flags "--cuda-path=\"${CMAKE_CUDA_COMPILER_LIBRARY_ROOT}\"")
104     if(CMAKE_CROSSCOMPILING)
105       # Need to pass the host target and include directories if we're crosscompiling.
106       string(APPEND clang_test_flags " --sysroot=\"${CMAKE_SYSROOT}\" --target=${CMAKE_CUDA_COMPILER_TARGET}")
107     endif()
108   endif()
110   # If the user set CMAKE_CUDA_ARCHITECTURES, validate its value.
111   include(Internal/CMakeCUDAArchitecturesValidate)
112   cmake_cuda_architectures_validate(CUDA)
114   if(CMAKE_CUDA_COMPILER_ID STREQUAL "Clang")
115     # Clang doesn't automatically select an architecture supported by the SDK.
116     # Try in reverse order of deprecation with the most recent at front (i.e. the most likely to work for new setups).
117     foreach(arch "52" "30" "20")
118       list(APPEND CMAKE_CUDA_COMPILER_ID_TEST_FLAGS_FIRST "${clang_test_flags} --cuda-gpu-arch=sm_${arch}")
119     endforeach()
120   elseif(CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
121     list(APPEND CMAKE_CUDA_COMPILER_ID_TEST_FLAGS_FIRST "${nvcc_test_flags}")
122   endif()
124   # We perform compiler identification for a second time to extract implicit linking info and host compiler for NVCC.
125   # We need to unset the compiler ID otherwise CMAKE_DETERMINE_COMPILER_ID() doesn't work.
126   set(CMAKE_CUDA_COMPILER_ID)
127   set(CMAKE_CUDA_PLATFORM_ID)
128   file(READ ${CMAKE_ROOT}/Modules/CMakePlatformId.h.in
129     CMAKE_CUDA_COMPILER_ID_PLATFORM_CONTENT)
131   CMAKE_DETERMINE_COMPILER_ID(CUDA CUDAFLAGS CMakeCUDACompilerId.cu)
133   if(CMAKE_GENERATOR MATCHES "Visual Studio")
134     # Now that we have the path to nvcc, we can compute the toolkit root.
135     get_filename_component(CMAKE_CUDA_COMPILER_TOOLKIT_ROOT "${CMAKE_CUDA_COMPILER}" DIRECTORY)
136     get_filename_component(CMAKE_CUDA_COMPILER_TOOLKIT_ROOT "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}" DIRECTORY)
137     set(CMAKE_CUDA_COMPILER_LIBRARY_ROOT "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}")
139     # The compiler comes with the toolkit, so the versions are the same.
140     set(CMAKE_CUDA_COMPILER_TOOLKIT_VERSION ${CMAKE_CUDA_COMPILER_VERSION})
141   endif()
143   include(Internal/CMakeCUDAArchitecturesAll)
144   # From CMAKE_CUDA_COMPILER_TOOLKIT_VERSION and CMAKE_CUDA_COMPILER_{ID,VERSION}, get:
145   # - CMAKE_CUDA_ARCHITECTURES_ALL
146   # - CMAKE_CUDA_ARCHITECTURES_ALL_MAJOR
147   # Match arguments with cmake_cuda_find_toolkit call.
148   cmake_cuda_architectures_all(CUDA CMAKE_CUDA_COMPILER_)
150   _cmake_find_compiler_sysroot(CUDA)
151 endif()
153 set(_CMAKE_PROCESSING_LANGUAGE "CUDA")
154 include(CMakeFindBinUtils)
155 include(Compiler/${CMAKE_CUDA_COMPILER_ID}-FindBinUtils OPTIONAL)
156 unset(_CMAKE_PROCESSING_LANGUAGE)
158 if(MSVC_CUDA_ARCHITECTURE_ID)
159   set(SET_MSVC_CUDA_ARCHITECTURE_ID
160     "set(MSVC_CUDA_ARCHITECTURE_ID ${MSVC_CUDA_ARCHITECTURE_ID})")
161 endif()
163 if(CMAKE_CUDA_COMPILER_ID STREQUAL "Clang")
164   string(REGEX MATCHALL "-target-cpu sm_([0-9]+)" _clang_target_cpus "${CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT}")
166   foreach(_clang_target_cpu ${_clang_target_cpus})
167     if(_clang_target_cpu MATCHES "-target-cpu sm_([0-9]+)")
168       list(APPEND CMAKE_CUDA_ARCHITECTURES_DEFAULT "${CMAKE_MATCH_1}")
169     endif()
170   endforeach()
172   # Find target directory when crosscompiling.
173   if(CMAKE_CROSSCOMPILING)
174     if(CMAKE_SYSTEM_PROCESSOR STREQUAL "armv7-a")
175       # Support for NVPACK
176       set(_CUDA_TARGET_NAME "armv7-linux-androideabi")
177     elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm")
178       set(_CUDA_TARGET_NAME "armv7-linux-gnueabihf")
179     elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
180       if(ANDROID_ARCH_NAME STREQUAL "arm64")
181         set(_CUDA_TARGET_NAME "aarch64-linux-androideabi")
182       else()
183         set(_CUDA_TARGET_NAME "aarch64-linux")
184       endif()
185     elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
186       set(_CUDA_TARGET_NAME "x86_64-linux")
187     endif()
189     if(EXISTS "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}/targets/${_CUDA_TARGET_NAME}")
190       set(_CUDA_TARGET_DIR "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}/targets/${_CUDA_TARGET_NAME}")
191     endif()
192   endif()
194   # If not already set we can simply use the toolkit root or it's a scattered installation.
195   if(NOT _CUDA_TARGET_DIR)
196     set(_CUDA_TARGET_DIR "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}")
197   endif()
199   # We can't use find_library() yet at this point, so try a few guesses.
200   if(EXISTS "${_CUDA_TARGET_DIR}/lib64")
201     set(_CUDA_LIBRARY_DIR "${_CUDA_TARGET_DIR}/lib64")
202   elseif(EXISTS "${_CUDA_TARGET_DIR}/lib/x64")
203     set(_CUDA_LIBRARY_DIR "${_CUDA_TARGET_DIR}/lib/x64")
204   elseif(EXISTS "${_CUDA_TARGET_DIR}/lib")
205     set(_CUDA_LIBRARY_DIR "${_CUDA_TARGET_DIR}/lib")
206   else()
207     message(FATAL_ERROR "Unable to find _CUDA_LIBRARY_DIR based on _CUDA_TARGET_DIR=${_CUDA_TARGET_DIR}")
208   endif()
210   # _CUDA_TARGET_DIR always points to the directory containing the include directory.
211   # On a scattered installation /usr, on a non-scattered something like /usr/local/cuda or /usr/local/cuda-10.2/targets/aarch64-linux.
212   if(EXISTS "${_CUDA_TARGET_DIR}/include/cuda_runtime.h")
213     set(_CUDA_INCLUDE_DIR "${_CUDA_TARGET_DIR}/include")
214   else()
215     message(FATAL_ERROR "Unable to find cuda_runtime.h in \"${_CUDA_TARGET_DIR}/include\" for _CUDA_INCLUDE_DIR.")
216   endif()
218   # Clang does not add any CUDA SDK libraries or directories when invoking the host linker.
219   # Add the CUDA toolkit library directory ourselves so that linking works.
220   # The CUDA runtime libraries are handled elsewhere by CMAKE_CUDA_RUNTIME_LIBRARY.
221   set(CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES "${_CUDA_INCLUDE_DIR}")
222   set(CMAKE_CUDA_HOST_IMPLICIT_LINK_DIRECTORIES "${_CUDA_LIBRARY_DIR}")
223   set(CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES "")
224   set(CMAKE_CUDA_HOST_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "")
226   # Don't leak variables unnecessarily to user code.
227   unset(_CUDA_INCLUDE_DIR)
228   unset(_CUDA_LIBRARY_DIR)
229   unset(_CUDA_TARGET_DIR)
230   unset(_CUDA_TARGET_NAME)
231 elseif(CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
232   include(Internal/CMakeNVCCParseImplicitInfo)
233   # Parse CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT to get:
234   # - CMAKE_CUDA_ARCHITECTURES_DEFAULT
235   # - CMAKE_CUDA_HOST_IMPLICIT_LINK_DIRECTORIES
236   # - CMAKE_CUDA_HOST_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES
237   # - CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES
238   # - CMAKE_CUDA_HOST_LINK_LAUNCHER
239   # - CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT
240   # - CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES
241   # Match arguments with cmake_nvcc_filter_implicit_info call in CMakeTestCUDACompiler.
242   cmake_nvcc_parse_implicit_info(CUDA CMAKE_CUDA_)
244   set(_SET_CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT
245     "set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT \"${CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT}\")")
246 endif()
248 include(Internal/CMakeCUDAFilterImplicitLibs)
249 # Filter out implicit link libraries that should not be passed unconditionally.
250 cmake_cuda_filter_implicit_libs(CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES)
252 if(CMAKE_CUDA_COMPILER_SYSROOT)
253   string(CONCAT _SET_CMAKE_CUDA_COMPILER_SYSROOT
254     "set(CMAKE_CUDA_COMPILER_SYSROOT \"${CMAKE_CUDA_COMPILER_SYSROOT}\")\n"
255     "set(CMAKE_COMPILER_SYSROOT \"${CMAKE_CUDA_COMPILER_SYSROOT}\")")
256 else()
257   set(_SET_CMAKE_CUDA_COMPILER_SYSROOT "")
258 endif()
260 # If the user did not set CMAKE_CUDA_ARCHITECTURES, use the compiler's default.
261 if("${CMAKE_CUDA_ARCHITECTURES}" STREQUAL "")
262   cmake_policy(GET CMP0104 _CUDA_CMP0104)
263   if(NOT CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA" OR _CUDA_CMP0104 STREQUAL "NEW")
264     set(CMAKE_CUDA_ARCHITECTURES "${CMAKE_CUDA_ARCHITECTURES_DEFAULT}" CACHE STRING "CUDA architectures")
265     if(NOT CMAKE_CUDA_ARCHITECTURES)
266       message(FATAL_ERROR "Failed to detect a default CUDA architecture.\n\nCompiler output:\n${CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT}")
267     endif()
268   endif()
269 endif()
270 unset(CMAKE_CUDA_ARCHITECTURES_DEFAULT)
272 # configure all variables set in this file
273 configure_file(${CMAKE_ROOT}/Modules/CMakeCUDACompiler.cmake.in
274   ${CMAKE_PLATFORM_INFO_DIR}/CMakeCUDACompiler.cmake
275   @ONLY
278 set(CMAKE_CUDA_COMPILER_ENV_VAR "CUDACXX")
279 set(CMAKE_CUDA_HOST_COMPILER_ENV_VAR "CUDAHOSTCXX")