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")
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.")
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")
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}")
32 # finally list compilers to try
33 if(NOT CMAKE_CUDA_COMPILER_INIT)
34 set(CMAKE_CUDA_COMPILER_LIST nvcc)
37 set(_CMAKE_CUDA_COMPILER_PATHS "$ENV{CUDA_PATH}/bin")
38 _cmake_find_compiler(CUDA)
39 unset(_CMAKE_CUDA_COMPILER_PATHS)
41 _cmake_find_compiler_path(CUDA)
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}")
55 if(NOT "$ENV{CUDAARCHS}" STREQUAL "")
56 set(CMAKE_CUDA_ARCHITECTURES "$ENV{CUDAARCHS}" CACHE STRING "CUDA architectures")
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")
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 if(CMAKE_CUDA_COMPILER_ID STREQUAL "Clang" AND WIN32)
78 message(FATAL_ERROR "Clang with CUDA is not yet supported on Windows. See CMake issue #20776.")
81 # Find the CUDA toolkit. We store the CMAKE_CUDA_COMPILER_TOOLKIT_ROOT, CMAKE_CUDA_COMPILER_TOOLKIT_VERSION and
82 # CMAKE_CUDA_COMPILER_LIBRARY_ROOT in CMakeCUDACompiler.cmake so FindCUDAToolkit can avoid searching on future
83 # runs and the toolkit is the same.
84 # This is very similar to FindCUDAToolkit, but somewhat simplified since we can issue fatal errors
85 # if we fail and we don't need to account for searching the libraries.
87 # For NVCC we can easily deduce the SDK binary directory from the compiler path.
88 if(CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
89 set(_CUDA_NVCC_EXECUTABLE "${CMAKE_CUDA_COMPILER}")
91 # Search using CUDAToolkit_ROOT and then CUDA_PATH for equivalence with FindCUDAToolkit.
92 # In FindCUDAToolkit CUDAToolkit_ROOT is searched automatically due to being in a find_package().
93 # First we search candidate non-default paths to give them priority.
94 find_program(_CUDA_NVCC_EXECUTABLE
96 PATHS ${CUDAToolkit_ROOT}
103 # If we didn't find NVCC, then try the default paths.
104 find_program(_CUDA_NVCC_EXECUTABLE
109 # If the user specified CUDAToolkit_ROOT but nvcc could not be found, this is an error.
110 if(NOT _CUDA_NVCC_EXECUTABLE AND (DEFINED CUDAToolkit_ROOT OR DEFINED ENV{CUDAToolkit_ROOT}))
111 set(fail_base "Could not find nvcc executable in path specified by")
113 if(DEFINED CUDAToolkit_ROOT)
114 message(FATAL_ERROR "${fail_base} CUDAToolkit_ROOT=${CUDAToolkit_ROOT}")
115 elseif(DEFINED ENV{CUDAToolkit_ROOT})
116 message(FATAL_ERROR "${fail_base} environment variable CUDAToolkit_ROOT=$ENV{CUDAToolkit_ROOT}")
120 # CUDAToolkit_ROOT cmake/env variable not specified, try platform defaults.
122 # - Linux: /usr/local/cuda-X.Y
123 # - macOS: /Developer/NVIDIA/CUDA-X.Y
124 # - Windows: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vX.Y
126 # We will also search the default symlink location /usr/local/cuda first since
127 # if CUDAToolkit_ROOT is not specified, it is assumed that the symlinked
128 # directory is the desired location.
129 if(NOT _CUDA_NVCC_EXECUTABLE)
132 set(platform_base "/usr/local/cuda-")
134 set(platform_base "/Developer/NVIDIA/CUDA-")
137 set(platform_base "C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v")
140 # Build out a descending list of possible cuda installations, e.g.
141 file(GLOB possible_paths "${platform_base}*")
142 # Iterate the glob results and create a descending list.
144 foreach(p ${possible_paths})
145 # Extract version number from end of string
146 string(REGEX MATCH "[0-9][0-9]?\\.[0-9]$" p_version ${p})
147 if(IS_DIRECTORY ${p} AND p_version)
148 list(APPEND versions ${p_version})
152 # Sort numerically in descending order, so we try the newest versions first.
153 list(SORT versions COMPARE NATURAL ORDER DESCENDING)
155 # With a descending list of versions, populate possible paths to search.
157 foreach(v ${versions})
158 list(APPEND search_paths "${platform_base}${v}")
161 # Force the global default /usr/local/cuda to the front on Unix.
163 list(INSERT search_paths 0 "/usr/local/cuda")
166 # Now search for nvcc again using the platform default search paths.
167 find_program(_CUDA_NVCC_EXECUTABLE
169 PATHS ${search_paths}
173 # We are done with these variables now, cleanup.
175 unset(possible_paths)
179 if(NOT _CUDA_NVCC_EXECUTABLE)
180 message(FATAL_ERROR "Failed to find nvcc.\nCompiler ${CMAKE_CUDA_COMPILER_ID} requires the CUDA toolkit. Please set the CUDAToolkit_ROOT variable.")
185 # Given that NVCC can be provided by multiple different sources (NVIDIA HPC SDK, CUDA Toolkit, distro)
186 # each of which has a different layout, we need to extract the CUDA toolkit root from the compiler
187 # itself, allowing us to support numerous different scattered toolkit layouts
188 execute_process(COMMAND ${_CUDA_NVCC_EXECUTABLE} "-v" "__cmake_determine_cuda"
189 OUTPUT_VARIABLE _CUDA_NVCC_OUT ERROR_VARIABLE _CUDA_NVCC_OUT)
190 if(_CUDA_NVCC_OUT MATCHES "\\#\\$ TOP=([^\r\n]*)")
191 get_filename_component(CMAKE_CUDA_COMPILER_TOOLKIT_ROOT "${CMAKE_MATCH_1}" ABSOLUTE)
193 get_filename_component(CMAKE_CUDA_COMPILER_TOOLKIT_ROOT "${_CUDA_NVCC_EXECUTABLE}" DIRECTORY)
194 get_filename_component(CMAKE_CUDA_COMPILER_TOOLKIT_ROOT "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}" DIRECTORY)
197 if(_CUDA_NVCC_OUT MATCHES "\\#\\$ NVVMIR_LIBRARY_DIR=([^\r\n]*)")
198 get_filename_component(_CUDA_NVVMIR_LIBRARY_DIR "${CMAKE_MATCH_1}" ABSOLUTE)
200 #We require the path to end in `/nvvm/libdevice'
201 if(_CUDA_NVVMIR_LIBRARY_DIR MATCHES "nvvm/libdevice$")
202 get_filename_component(_CUDA_NVVMIR_LIBRARY_DIR "${_CUDA_NVVMIR_LIBRARY_DIR}/../.." ABSOLUTE)
203 set(CMAKE_CUDA_COMPILER_LIBRARY_ROOT_FROM_NVVMIR_LIBRARY_DIR "${_CUDA_NVVMIR_LIBRARY_DIR}")
206 unset(_CUDA_NVVMIR_LIBRARY_DIR)
207 unset(_cuda_nvvmir_dir_name)
209 unset(_CUDA_NVCC_OUT)
211 set(CMAKE_CUDA_DEVICE_LINKER "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}/bin/nvlink${CMAKE_EXECUTABLE_SUFFIX}")
212 set(CMAKE_CUDA_FATBINARY "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}/bin/fatbinary${CMAKE_EXECUTABLE_SUFFIX}")
214 # In a non-scattered installation the following are equivalent to CMAKE_CUDA_COMPILER_TOOLKIT_ROOT.
215 # We first check for a non-scattered installation to prefer it over a scattered installation.
217 # CMAKE_CUDA_COMPILER_LIBRARY_ROOT contains the device library.
218 if(DEFINED CMAKE_CUDA_COMPILER_LIBRARY_ROOT_FROM_NVVMIR_LIBRARY_DIR)
219 set(CMAKE_CUDA_COMPILER_LIBRARY_ROOT "${CMAKE_CUDA_COMPILER_LIBRARY_ROOT_FROM_NVVMIR_LIBRARY_DIR}")
220 elseif(EXISTS "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}/nvvm/libdevice")
221 set(CMAKE_CUDA_COMPILER_LIBRARY_ROOT "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}")
222 elseif(CMAKE_SYSROOT_LINK AND EXISTS "${CMAKE_SYSROOT_LINK}/usr/lib/cuda/nvvm/libdevice")
223 set(CMAKE_CUDA_COMPILER_LIBRARY_ROOT "${CMAKE_SYSROOT_LINK}/usr/lib/cuda")
224 elseif(EXISTS "${CMAKE_SYSROOT}/usr/lib/cuda/nvvm/libdevice")
225 set(CMAKE_CUDA_COMPILER_LIBRARY_ROOT "${CMAKE_SYSROOT}/usr/lib/cuda")
227 message(FATAL_ERROR "Couldn't find CUDA library root.")
229 unset(CMAKE_CUDA_COMPILER_LIBRARY_ROOT_FROM_NVVMIR_LIBRARY_DIR)
231 # CMAKE_CUDA_COMPILER_TOOLKIT_LIBRARY_ROOT contains the linking stubs necessary for device linking and other low-level library files.
232 if(CMAKE_SYSROOT_LINK AND EXISTS "${CMAKE_SYSROOT_LINK}/usr/lib/nvidia-cuda-toolkit/bin/crt/link.stub")
233 set(CMAKE_CUDA_COMPILER_TOOLKIT_LIBRARY_ROOT "${CMAKE_SYSROOT_LINK}/usr/lib/nvidia-cuda-toolkit")
234 elseif(EXISTS "${CMAKE_SYSROOT}/usr/lib/nvidia-cuda-toolkit/bin/crt/link.stub")
235 set(CMAKE_CUDA_COMPILER_TOOLKIT_LIBRARY_ROOT "${CMAKE_SYSROOT}/usr/lib/nvidia-cuda-toolkit")
237 set(CMAKE_CUDA_COMPILER_TOOLKIT_LIBRARY_ROOT "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}")
241 # For regular nvcc we the toolkit version is the same as the compiler version and we can parse it from the vendor test output.
242 # For Clang we need to invoke nvcc to get version output.
243 if(NOT CMAKE_GENERATOR MATCHES "Visual Studio")
244 if(CMAKE_CUDA_COMPILER_ID STREQUAL "Clang")
245 execute_process(COMMAND ${_CUDA_NVCC_EXECUTABLE} "--version" OUTPUT_VARIABLE CMAKE_CUDA_COMPILER_ID_OUTPUT)
248 if(CMAKE_CUDA_COMPILER_ID_OUTPUT MATCHES [=[V([0-9]+\.[0-9]+\.[0-9]+)]=])
249 set(CMAKE_CUDA_COMPILER_TOOLKIT_VERSION "${CMAKE_MATCH_1}")
253 set(CMAKE_CUDA_COMPILER_ID_FLAGS_ALWAYS "-v")
255 if(CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
256 set(nvcc_test_flags "--keep --keep-dir tmp")
257 if(CMAKE_CUDA_HOST_COMPILER)
258 string(APPEND nvcc_test_flags " -ccbin=\"${CMAKE_CUDA_HOST_COMPILER}\"")
260 # If we have extracted the vendor as NVIDIA we should require detection to
261 # work. If we don't, users will get confusing errors later about failure
262 # to detect a default value for CMAKE_CUDA_ARCHITECTURES
263 set(CMAKE_CUDA_COMPILER_ID_REQUIRE_SUCCESS ON)
264 elseif(CMAKE_CUDA_COMPILER_ID STREQUAL "Clang")
265 set(clang_test_flags "--cuda-path=\"${CMAKE_CUDA_COMPILER_LIBRARY_ROOT}\"")
266 if(CMAKE_CROSSCOMPILING)
267 # Need to pass the host target and include directories if we're crosscompiling.
268 string(APPEND clang_test_flags " --sysroot=\"${CMAKE_SYSROOT}\" --target=${CMAKE_CUDA_COMPILER_TARGET}")
272 # Rest of the code treats an empty value as equivalent to "use the defaults".
273 # Error out early to prevent confusing errors as a result of this.
274 # Note that this also catches invalid non-numerical values such as "a".
275 if(DEFINED CMAKE_CUDA_ARCHITECTURES)
276 if(CMAKE_CUDA_ARCHITECTURES STREQUAL "")
277 message(FATAL_ERROR "CMAKE_CUDA_ARCHITECTURES must be non-empty if set.")
278 elseif(CMAKE_CUDA_ARCHITECTURES AND NOT CMAKE_CUDA_ARCHITECTURES MATCHES "^([0-9]+a?(-real|-virtual)?(;[0-9]+a?(-real|-virtual)?|;)*|all|all-major|native)$")
280 "CMAKE_CUDA_ARCHITECTURES:\n"
281 " ${CMAKE_CUDA_ARCHITECTURES}\n"
282 "is not one of the following:\n"
283 " * a semicolon-separated list of integers, each optionally\n"
284 " followed by '-real' or '-virtual'\n"
285 " * a special value: all, all-major, native\n"
290 if(CMAKE_CUDA_COMPILER_ID STREQUAL "Clang")
291 # Clang doesn't automatically select an architecture supported by the SDK.
292 # Try in reverse order of deprecation with the most recent at front (i.e. the most likely to work for new setups).
293 foreach(arch "52" "30" "20")
294 list(APPEND CMAKE_CUDA_COMPILER_ID_TEST_FLAGS_FIRST "${clang_test_flags} --cuda-gpu-arch=sm_${arch}")
296 elseif(CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
297 list(APPEND CMAKE_CUDA_COMPILER_ID_TEST_FLAGS_FIRST "${nvcc_test_flags}")
300 # We perform compiler identification for a second time to extract implicit linking info and host compiler for NVCC.
301 # We need to unset the compiler ID otherwise CMAKE_DETERMINE_COMPILER_ID() doesn't work.
302 set(CMAKE_CUDA_COMPILER_ID)
303 set(CMAKE_CUDA_PLATFORM_ID)
304 file(READ ${CMAKE_ROOT}/Modules/CMakePlatformId.h.in
305 CMAKE_CUDA_COMPILER_ID_PLATFORM_CONTENT)
307 CMAKE_DETERMINE_COMPILER_ID(CUDA CUDAFLAGS CMakeCUDACompilerId.cu)
309 if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
310 # Now that we have the path to nvcc, we can compute the toolkit root.
311 get_filename_component(CMAKE_CUDA_COMPILER_TOOLKIT_ROOT "${CMAKE_CUDA_COMPILER}" DIRECTORY)
312 get_filename_component(CMAKE_CUDA_COMPILER_TOOLKIT_ROOT "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}" DIRECTORY)
313 set(CMAKE_CUDA_COMPILER_LIBRARY_ROOT "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}")
315 # The compiler comes with the toolkit, so the versions are the same.
316 set(CMAKE_CUDA_COMPILER_TOOLKIT_VERSION ${CMAKE_CUDA_COMPILER_VERSION})
319 include(${CMAKE_ROOT}/Modules/CUDA/architectures.cmake)
321 _cmake_find_compiler_sysroot(CUDA)
324 set(_CMAKE_PROCESSING_LANGUAGE "CUDA")
325 include(CMakeFindBinUtils)
326 include(Compiler/${CMAKE_CUDA_COMPILER_ID}-FindBinUtils OPTIONAL)
327 unset(_CMAKE_PROCESSING_LANGUAGE)
329 if(MSVC_CUDA_ARCHITECTURE_ID)
330 set(SET_MSVC_CUDA_ARCHITECTURE_ID
331 "set(MSVC_CUDA_ARCHITECTURE_ID ${MSVC_CUDA_ARCHITECTURE_ID})")
334 if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
335 set(CMAKE_CUDA_HOST_LINK_LAUNCHER "${CMAKE_LINKER}")
336 set(CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES "")
337 set(CMAKE_CUDA_HOST_IMPLICIT_LINK_DIRECTORIES "")
338 set(CMAKE_CUDA_HOST_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "")
340 # We do not currently detect CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES but we
341 # do need to detect CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT from the compiler by
342 # looking at which cudart library exists in the implicit link libraries passed
343 # to the host linker.
344 if(CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT MATCHES "link\\.exe [^\n]*cudart_static\\.lib")
345 set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "STATIC")
346 elseif(CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT MATCHES "link\\.exe [^\n]*cudart\\.lib")
347 set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "SHARED")
349 set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "NONE")
351 set(_SET_CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT
352 "set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT \"${CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT}\")")
353 elseif(CMAKE_CUDA_COMPILER_ID STREQUAL "Clang")
354 string(REGEX MATCHALL "-target-cpu sm_([0-9]+)" target_cpus "${CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT}")
356 foreach(cpu ${target_cpus})
357 string(REGEX MATCH "-target-cpu sm_([0-9]+)" dont_care "${cpu}")
358 list(APPEND architectures_detected "${CMAKE_MATCH_1}")
361 # Find target directory when crosscompiling.
362 if(CMAKE_CROSSCOMPILING)
363 if(CMAKE_SYSTEM_PROCESSOR STREQUAL "armv7-a")
365 set(_CUDA_TARGET_NAME "armv7-linux-androideabi")
366 elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm")
367 set(_CUDA_TARGET_NAME "armv7-linux-gnueabihf")
368 elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
369 if(ANDROID_ARCH_NAME STREQUAL "arm64")
370 set(_CUDA_TARGET_NAME "aarch64-linux-androideabi")
372 set(_CUDA_TARGET_NAME "aarch64-linux")
374 elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
375 set(_CUDA_TARGET_NAME "x86_64-linux")
378 if(EXISTS "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}/targets/${_CUDA_TARGET_NAME}")
379 set(_CUDA_TARGET_DIR "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}/targets/${_CUDA_TARGET_NAME}")
383 # If not already set we can simply use the toolkit root or it's a scattered installation.
384 if(NOT _CUDA_TARGET_DIR)
385 set(_CUDA_TARGET_DIR "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}")
388 # We can't use find_library() yet at this point, so try a few guesses.
389 if(EXISTS "${_CUDA_TARGET_DIR}/lib64")
390 set(_CUDA_LIBRARY_DIR "${_CUDA_TARGET_DIR}/lib64")
391 elseif(EXISTS "${_CUDA_TARGET_DIR}/lib/x64")
392 set(_CUDA_LIBRARY_DIR "${_CUDA_TARGET_DIR}/lib/x64")
393 elseif(EXISTS "${_CUDA_TARGET_DIR}/lib")
394 set(_CUDA_LIBRARY_DIR "${_CUDA_TARGET_DIR}/lib")
396 message(FATAL_ERROR "Unable to find _CUDA_LIBRARY_DIR based on _CUDA_TARGET_DIR=${_CUDA_TARGET_DIR}")
399 # _CUDA_TARGET_DIR always points to the directory containing the include directory.
400 # On a scattered installation /usr, on a non-scattered something like /usr/local/cuda or /usr/local/cuda-10.2/targets/aarch64-linux.
401 if(EXISTS "${_CUDA_TARGET_DIR}/include/cuda_runtime.h")
402 set(_CUDA_INCLUDE_DIR "${_CUDA_TARGET_DIR}/include")
404 message(FATAL_ERROR "Unable to find cuda_runtime.h in \"${_CUDA_TARGET_DIR}/include\" for _CUDA_INCLUDE_DIR.")
407 # Clang does not add any CUDA SDK libraries or directories when invoking the host linker.
408 # Add the CUDA toolkit library directory ourselves so that linking works.
409 # The CUDA runtime libraries are handled elsewhere by CMAKE_CUDA_RUNTIME_LIBRARY.
410 set(CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES "${_CUDA_INCLUDE_DIR}")
411 set(CMAKE_CUDA_HOST_IMPLICIT_LINK_DIRECTORIES "${_CUDA_LIBRARY_DIR}")
412 set(CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES "")
413 set(CMAKE_CUDA_HOST_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "")
414 elseif(CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
416 string(REPLACE "\r" "" _nvcc_output_orig "${CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT}")
417 if(_nvcc_output_orig MATCHES "#\\\$ +PATH= *([^\n]*)\n")
418 set(_nvcc_path "${CMAKE_MATCH_1}")
419 string(APPEND _nvcc_log " found 'PATH=' string: [${_nvcc_path}]\n")
420 string(REPLACE ":" ";" _nvcc_path "${_nvcc_path}")
423 string(REPLACE "\n" "\n " _nvcc_output_log "\n${_nvcc_output_orig}")
424 string(APPEND _nvcc_log " no 'PATH=' string found in nvcc output:${_nvcc_output_log}\n")
426 if(_nvcc_output_orig MATCHES "#\\\$ +LIBRARIES= *([^\n]*)\n")
427 set(_nvcc_libraries "${CMAKE_MATCH_1}")
428 string(APPEND _nvcc_log " found 'LIBRARIES=' string: [${_nvcc_libraries}]\n")
430 set(_nvcc_libraries "")
431 string(REPLACE "\n" "\n " _nvcc_output_log "\n${_nvcc_output_orig}")
432 string(APPEND _nvcc_log " no 'LIBRARIES=' string found in nvcc output:${_nvcc_output_log}\n")
435 set(_nvcc_link_line "")
437 # Remove variable assignments.
438 string(REGEX REPLACE "#\\\$ *[^= ]+=[^\n]*\n" "" _nvcc_output "${_nvcc_output_orig}")
439 # Encode [] characters that break list expansion.
440 string(REPLACE "[" "{==={" _nvcc_output "${_nvcc_output}")
441 string(REPLACE "]" "}===}" _nvcc_output "${_nvcc_output}")
443 string(REGEX REPLACE "\n+(#\\\$ )?" ";" _nvcc_output "${_nvcc_output}")
444 foreach(line IN LISTS _nvcc_output)
445 set(_nvcc_output_line "${line}")
446 string(REPLACE "{==={" "[" _nvcc_output_line "${_nvcc_output_line}")
447 string(REPLACE "}===}" "]" _nvcc_output_line "${_nvcc_output_line}")
448 string(APPEND _nvcc_log " considering line: [${_nvcc_output_line}]\n")
449 if("${_nvcc_output_line}" MATCHES "^ *nvlink")
450 string(APPEND _nvcc_log " ignoring nvlink line\n")
451 elseif(_nvcc_libraries)
452 if("${_nvcc_output_line}" MATCHES "(@\"?((tmp/)?a\\.exe\\.res)\"?)")
453 set(_nvcc_link_res_arg "${CMAKE_MATCH_1}")
454 set(_nvcc_link_res_file "${CMAKE_MATCH_2}")
455 set(_nvcc_link_res "${CMAKE_PLATFORM_INFO_DIR}/CompilerIdCUDA/${_nvcc_link_res_file}")
456 if(EXISTS "${_nvcc_link_res}")
457 file(READ "${_nvcc_link_res}" _nvcc_link_res_content)
458 string(REPLACE "${_nvcc_link_res_arg}" "${_nvcc_link_res_content}" _nvcc_output_line "${_nvcc_output_line}")
461 string(FIND "${_nvcc_output_line}" "${_nvcc_libraries}" _nvcc_libraries_pos)
462 if(NOT _nvcc_libraries_pos EQUAL -1)
463 set(_nvcc_link_line "${_nvcc_output_line}")
464 string(APPEND _nvcc_log " extracted link line: [${_nvcc_link_line}]\n")
471 if("x${CMAKE_CUDA_SIMULATE_ID}" STREQUAL "xMSVC")
472 set(CMAKE_CUDA_HOST_LINK_LAUNCHER "${CMAKE_LINKER}")
474 #extract the compiler that is being used for linking
475 separate_arguments(_nvcc_link_line_args UNIX_COMMAND "${_nvcc_link_line}")
476 list(GET _nvcc_link_line_args 0 _nvcc_host_link_launcher)
477 if(IS_ABSOLUTE "${_nvcc_host_link_launcher}")
478 string(APPEND _nvcc_log " extracted link launcher absolute path: [${_nvcc_host_link_launcher}]\n")
479 set(CMAKE_CUDA_HOST_LINK_LAUNCHER "${_nvcc_host_link_launcher}")
481 string(APPEND _nvcc_log " extracted link launcher name: [${_nvcc_host_link_launcher}]\n")
482 find_program(_nvcc_find_host_link_launcher
483 NAMES ${_nvcc_host_link_launcher}
484 PATHS ${_nvcc_path} NO_DEFAULT_PATH)
485 find_program(_nvcc_find_host_link_launcher
486 NAMES ${_nvcc_host_link_launcher})
487 if(_nvcc_find_host_link_launcher)
488 string(APPEND _nvcc_log " found link launcher absolute path: [${_nvcc_find_host_link_launcher}]\n")
489 set(CMAKE_CUDA_HOST_LINK_LAUNCHER "${_nvcc_find_host_link_launcher}")
491 string(APPEND _nvcc_log " could not find link launcher absolute path\n")
492 set(CMAKE_CUDA_HOST_LINK_LAUNCHER "${_nvcc_host_link_launcher}")
494 unset(_nvcc_find_host_link_launcher CACHE)
498 #prefix the line with cuda-fake-ld so that implicit link info believes it is
500 set(_nvcc_link_line "cuda-fake-ld ${_nvcc_link_line}")
501 CMAKE_PARSE_IMPLICIT_LINK_INFO("${_nvcc_link_line}"
502 CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES
503 CMAKE_CUDA_HOST_IMPLICIT_LINK_DIRECTORIES
504 CMAKE_CUDA_HOST_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES
506 "${CMAKE_CUDA_IMPLICIT_OBJECT_REGEX}"
509 # Detect CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT from the compiler by looking at which
510 # cudart library exists in the implicit link libraries passed to the host linker.
511 # This is required when a project sets the cuda runtime library as part of the
513 if(";${CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES};" MATCHES [[;cudart_static(\.lib)?;]])
514 set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "STATIC")
515 elseif(";${CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES};" MATCHES [[;cudart(\.lib)?;]])
516 set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "SHARED")
518 set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "NONE")
520 set(_SET_CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT
521 "set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT \"${CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT}\")")
523 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
524 "Parsed CUDA nvcc implicit link information from above output:\n${_nvcc_log}\n${log}\n\n")
526 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
527 "Failed to parse CUDA nvcc implicit link information:\n${_nvcc_log}\n\n")
528 message(FATAL_ERROR "Failed to extract nvcc implicit link line.")
532 # CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES is detected above as the list of
533 # libraries that the CUDA compiler implicitly passes to the host linker.
534 # CMake invokes the host linker directly and so needs to pass these libraries.
535 # We filter out those that should not be passed unconditionally both here
536 # and from CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES in CMakeTestCUDACompiler.
537 set(CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES_EXCLUDE
538 # The CUDA runtime libraries are controlled by CMAKE_CUDA_RUNTIME_LIBRARY.
540 cudart_static cudart_static.lib
541 cudadevrt cudadevrt.lib
543 # Dependencies of the CUDA static runtime library on Linux hosts.
548 list(REMOVE_ITEM CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES ${CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES_EXCLUDE})
550 if(CMAKE_CUDA_COMPILER_SYSROOT)
551 string(CONCAT _SET_CMAKE_CUDA_COMPILER_SYSROOT
552 "set(CMAKE_CUDA_COMPILER_SYSROOT \"${CMAKE_CUDA_COMPILER_SYSROOT}\")\n"
553 "set(CMAKE_COMPILER_SYSROOT \"${CMAKE_CUDA_COMPILER_SYSROOT}\")")
555 set(_SET_CMAKE_CUDA_COMPILER_SYSROOT "")
558 # Determine CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES
559 if(CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
560 set(CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES)
561 string(REPLACE "\r" "" _nvcc_output_orig "${CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT}")
562 if(_nvcc_output_orig MATCHES "#\\\$ +INCLUDES= *([^\n]*)\n")
563 set(_nvcc_includes "${CMAKE_MATCH_1}")
564 string(APPEND _nvcc_log " found 'INCLUDES=' string: [${_nvcc_includes}]\n")
566 set(_nvcc_includes "")
567 string(REPLACE "\n" "\n " _nvcc_output_log "\n${_nvcc_output_orig}")
568 string(APPEND _nvcc_log " no 'INCLUDES=' string found in nvcc output:${_nvcc_output_log}\n")
571 # across all operating system each include directory is prefixed with -I
572 separate_arguments(_nvcc_output NATIVE_COMMAND "${_nvcc_includes}")
573 foreach(line IN LISTS _nvcc_output)
574 string(REGEX REPLACE "^-I" "" line "${line}")
575 get_filename_component(line "${line}" ABSOLUTE)
576 list(APPEND CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES "${line}")
579 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
580 "Parsed CUDA nvcc include information from above output:\n${_nvcc_log}\n${log}\n\n")
582 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
583 "Failed to detect CUDA nvcc include information:\n${_nvcc_log}\n\n")
586 string(REGEX MATCHALL "-arch compute_([0-9]+)" target_cpus "${CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT}")
588 foreach(cpu ${target_cpus})
589 string(REGEX MATCH "-arch compute_([0-9]+)" dont_care "${cpu}")
590 list(APPEND architectures_detected "${CMAKE_MATCH_1}")
594 # If the user didn't set the architectures, then set them to a default.
595 # If the user did, then make sure those architectures worked.
596 if("${CMAKE_CUDA_ARCHITECTURES}" STREQUAL "")
597 cmake_policy(GET CMP0104 _CUDA_CMP0104)
599 if(NOT CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA" OR _CUDA_CMP0104 STREQUAL "NEW")
600 set(CMAKE_CUDA_ARCHITECTURES "${architectures_detected}" CACHE STRING "CUDA architectures")
602 if(NOT CMAKE_CUDA_ARCHITECTURES)
603 message(FATAL_ERROR "Failed to detect a default CUDA architecture.\n\nCompiler output:\n${CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT}")
608 # configure all variables set in this file
609 configure_file(${CMAKE_ROOT}/Modules/CMakeCUDACompiler.cmake.in
610 ${CMAKE_PLATFORM_INFO_DIR}/CMakeCUDACompiler.cmake
614 # Don't leak variables unnecessarily to user code.
615 unset(_CUDA_INCLUDE_DIR CACHE)
616 unset(_CUDA_NVCC_EXECUTABLE CACHE)
617 unset(_CUDA_LIBRARY_DIR)
618 unset(_CUDA_TARGET_DIR)
619 unset(_CUDA_TARGET_NAME)
621 unset(architectures_detected)
623 set(CMAKE_CUDA_COMPILER_ENV_VAR "CUDACXX")
624 set(CMAKE_CUDA_HOST_COMPILER_ENV_VAR "CUDAHOSTCXX")