CMakeDetermineCUDACompiler.cmake 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompiler.cmake)
  4. include(${CMAKE_ROOT}/Modules/CMakeParseImplicitLinkInfo.cmake)
  5. if( NOT ( ("${CMAKE_GENERATOR}" MATCHES "Make") OR
  6. ("${CMAKE_GENERATOR}" MATCHES "Ninja") OR
  7. ("${CMAKE_GENERATOR}" MATCHES "Visual Studio (1|[9][0-9])") ) )
  8. message(FATAL_ERROR "CUDA language not currently supported by \"${CMAKE_GENERATOR}\" generator")
  9. endif()
  10. if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
  11. if(DEFINED ENV{CUDAHOSTCXX} OR DEFINED CMAKE_CUDA_HOST_COMPILER)
  12. message(WARNING "Visual Studio does not support specifying CUDAHOSTCXX or CMAKE_CUDA_HOST_COMPILER. Using the C++ compiler provided by Visual Studio.")
  13. endif()
  14. else()
  15. if(NOT CMAKE_CUDA_COMPILER)
  16. set(CMAKE_CUDA_COMPILER_INIT NOTFOUND)
  17. # prefer the environment variable CUDACXX
  18. if(NOT $ENV{CUDACXX} STREQUAL "")
  19. get_filename_component(CMAKE_CUDA_COMPILER_INIT $ENV{CUDACXX} PROGRAM PROGRAM_ARGS CMAKE_CUDA_FLAGS_ENV_INIT)
  20. if(CMAKE_CUDA_FLAGS_ENV_INIT)
  21. set(CMAKE_CUDA_COMPILER_ARG1 "${CMAKE_CUDA_FLAGS_ENV_INIT}" CACHE STRING "Arguments to CUDA compiler")
  22. endif()
  23. if(NOT EXISTS ${CMAKE_CUDA_COMPILER_INIT})
  24. message(FATAL_ERROR "Could not find compiler set in environment variable CUDACXX:\n$ENV{CUDACXX}.\n${CMAKE_CUDA_COMPILER_INIT}")
  25. endif()
  26. endif()
  27. # finally list compilers to try
  28. if(NOT CMAKE_CUDA_COMPILER_INIT)
  29. set(CMAKE_CUDA_COMPILER_LIST nvcc)
  30. endif()
  31. set(_CMAKE_CUDA_COMPILER_PATHS "$ENV{CUDA_PATH}/bin")
  32. _cmake_find_compiler(CUDA)
  33. unset(_CMAKE_CUDA_COMPILER_PATHS)
  34. else()
  35. _cmake_find_compiler_path(CUDA)
  36. endif()
  37. mark_as_advanced(CMAKE_CUDA_COMPILER)
  38. #Allow the user to specify a host compiler except for Visual Studio
  39. if(NOT $ENV{CUDAHOSTCXX} STREQUAL "")
  40. get_filename_component(CMAKE_CUDA_HOST_COMPILER $ENV{CUDAHOSTCXX} PROGRAM)
  41. if(NOT EXISTS ${CMAKE_CUDA_HOST_COMPILER})
  42. message(FATAL_ERROR "Could not find compiler set in environment variable CUDAHOSTCXX:\n$ENV{CUDAHOSTCXX}.\n${CMAKE_CUDA_HOST_COMPILER}")
  43. endif()
  44. endif()
  45. endif()
  46. if(NOT "$ENV{CUDAARCHS}" STREQUAL "")
  47. set(CMAKE_CUDA_ARCHITECTURES "$ENV{CUDAARCHS}" CACHE STRING "CUDA architectures")
  48. endif()
  49. # Build a small source file to identify the compiler.
  50. if(NOT CMAKE_CUDA_COMPILER_ID_RUN)
  51. set(CMAKE_CUDA_COMPILER_ID_RUN 1)
  52. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
  53. if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
  54. # We will not know CMAKE_CUDA_COMPILER until the main compiler id step
  55. # below extracts it, but we do know that the compiler id will be NVIDIA.
  56. set(CMAKE_CUDA_COMPILER_ID "NVIDIA")
  57. else()
  58. # We determine the vendor to help with find the toolkit and use the right flags for detection right away.
  59. # The main compiler identification is still needed below to extract other information.
  60. list(APPEND CMAKE_CUDA_COMPILER_ID_VENDORS NVIDIA Clang)
  61. set(CMAKE_CUDA_COMPILER_ID_VENDOR_REGEX_NVIDIA "nvcc: NVIDIA \\(R\\) Cuda compiler driver")
  62. set(CMAKE_CUDA_COMPILER_ID_VENDOR_REGEX_Clang "(clang version)")
  63. CMAKE_DETERMINE_COMPILER_ID_VENDOR(CUDA "--version")
  64. if(CMAKE_CUDA_COMPILER_ID STREQUAL "Clang" AND WIN32)
  65. message(FATAL_ERROR "Clang with CUDA is not yet supported on Windows. See CMake issue #20776.")
  66. endif()
  67. # Find the CUDA toolkit. We store the CMAKE_CUDA_COMPILER_TOOLKIT_ROOT and CMAKE_CUDA_COMPILER_LIBRARY_ROOT
  68. # in CMakeCUDACompiler.cmake, so FindCUDAToolkit can avoid searching on future runs and the toolkit stays the same.
  69. # This is very similar to FindCUDAToolkit, but somewhat simplified since we can issue fatal errors
  70. # if we fail to find things we need and we don't need to account for searching the libraries.
  71. # For NVCC we can easily deduce the SDK binary directory from the compiler path.
  72. if(CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
  73. set(_CUDA_NVCC_EXECUTABLE "${CMAKE_CUDA_COMPILER}")
  74. else()
  75. # Search using CUDAToolkit_ROOT and then CUDA_PATH for equivalence with FindCUDAToolkit.
  76. # In FindCUDAToolkit CUDAToolkit_ROOT is searched automatically due to being in a find_package().
  77. # First we search candidate non-default paths to give them priority.
  78. find_program(_CUDA_NVCC_EXECUTABLE
  79. NAMES nvcc nvcc.exe
  80. PATHS ${CUDAToolkit_ROOT}
  81. ENV CUDAToolkit_ROOT
  82. ENV CUDA_PATH
  83. PATH_SUFFIXES bin
  84. NO_DEFAULT_PATH
  85. )
  86. # If we didn't find NVCC, then try the default paths.
  87. find_program(_CUDA_NVCC_EXECUTABLE
  88. NAMES nvcc nvcc.exe
  89. PATH_SUFFIXES bin
  90. )
  91. # If the user specified CUDAToolkit_ROOT but nvcc could not be found, this is an error.
  92. if(NOT _CUDA_NVCC_EXECUTABLE AND (DEFINED CUDAToolkit_ROOT OR DEFINED ENV{CUDAToolkit_ROOT}))
  93. set(fail_base "Could not find nvcc executable in path specified by")
  94. if(DEFINED CUDAToolkit_ROOT)
  95. message(FATAL_ERROR "${fail_base} CUDAToolkit_ROOT=${CUDAToolkit_ROOT}")
  96. elseif(DEFINED ENV{CUDAToolkit_ROOT})
  97. message(FATAL_ERROR "${fail_base} environment variable CUDAToolkit_ROOT=$ENV{CUDAToolkit_ROOT}")
  98. endif()
  99. endif()
  100. # CUDAToolkit_ROOT cmake/env variable not specified, try platform defaults.
  101. #
  102. # - Linux: /usr/local/cuda-X.Y
  103. # - macOS: /Developer/NVIDIA/CUDA-X.Y
  104. # - Windows: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vX.Y
  105. #
  106. # We will also search the default symlink location /usr/local/cuda first since
  107. # if CUDAToolkit_ROOT is not specified, it is assumed that the symlinked
  108. # directory is the desired location.
  109. if(NOT _CUDA_NVCC_EXECUTABLE)
  110. if(UNIX)
  111. if(NOT APPLE)
  112. set(platform_base "/usr/local/cuda-")
  113. else()
  114. set(platform_base "/Developer/NVIDIA/CUDA-")
  115. endif()
  116. else()
  117. set(platform_base "C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v")
  118. endif()
  119. # Build out a descending list of possible cuda installations, e.g.
  120. file(GLOB possible_paths "${platform_base}*")
  121. # Iterate the glob results and create a descending list.
  122. set(versions)
  123. foreach(p ${possible_paths})
  124. # Extract version number from end of string
  125. string(REGEX MATCH "[0-9][0-9]?\\.[0-9]$" p_version ${p})
  126. if(IS_DIRECTORY ${p} AND p_version)
  127. list(APPEND versions ${p_version})
  128. endif()
  129. endforeach()
  130. # Sort numerically in descending order, so we try the newest versions first.
  131. list(SORT versions COMPARE NATURAL ORDER DESCENDING)
  132. # With a descending list of versions, populate possible paths to search.
  133. set(search_paths)
  134. foreach(v ${versions})
  135. list(APPEND search_paths "${platform_base}${v}")
  136. endforeach()
  137. # Force the global default /usr/local/cuda to the front on Unix.
  138. if(UNIX)
  139. list(INSERT search_paths 0 "/usr/local/cuda")
  140. endif()
  141. # Now search for nvcc again using the platform default search paths.
  142. find_program(_CUDA_NVCC_EXECUTABLE
  143. NAMES nvcc nvcc.exe
  144. PATHS ${search_paths}
  145. PATH_SUFFIXES bin
  146. )
  147. # We are done with these variables now, cleanup.
  148. unset(platform_base)
  149. unset(possible_paths)
  150. unset(versions)
  151. unset(search_paths)
  152. if(NOT _CUDA_NVCC_EXECUTABLE)
  153. message(FATAL_ERROR "Failed to find nvcc.\nCompiler ${CMAKE_CUDA_COMPILER_ID} requires the CUDA toolkit. Please set the CUDAToolkit_ROOT variable.")
  154. endif()
  155. endif()
  156. endif()
  157. # Given that NVCC can be provided by multiple different sources (NVIDIA HPC SDK, CUDA Toolkit, distro)
  158. # each of which has a different layout, we need to extract the CUDA toolkit root from the compiler
  159. # itself, allowing us to support numerous different scattered toolkit layouts
  160. execute_process(COMMAND ${_CUDA_NVCC_EXECUTABLE} "-v" "__cmake_determine_cuda"
  161. OUTPUT_VARIABLE _CUDA_NVCC_OUT ERROR_VARIABLE _CUDA_NVCC_OUT)
  162. if(_CUDA_NVCC_OUT MATCHES "\\#\\$ TOP=([^\r\n]*)")
  163. get_filename_component(CMAKE_CUDA_COMPILER_TOOLKIT_ROOT "${CMAKE_MATCH_1}" ABSOLUTE)
  164. else()
  165. get_filename_component(CMAKE_CUDA_COMPILER_TOOLKIT_ROOT "${_CUDA_NVCC_EXECUTABLE}" DIRECTORY)
  166. get_filename_component(CMAKE_CUDA_COMPILER_TOOLKIT_ROOT "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}" DIRECTORY)
  167. endif()
  168. if(_CUDA_NVCC_OUT MATCHES "\\#\\$ NVVMIR_LIBRARY_DIR=([^\r\n]*)")
  169. get_filename_component(_CUDA_NVVMIR_LIBRARY_DIR "${CMAKE_MATCH_1}" ABSOLUTE)
  170. #We require the path to end in `/nvvm/libdevice'
  171. if(_CUDA_NVVMIR_LIBRARY_DIR MATCHES "nvvm/libdevice$")
  172. get_filename_component(_CUDA_NVVMIR_LIBRARY_DIR "${_CUDA_NVVMIR_LIBRARY_DIR}/../.." ABSOLUTE)
  173. set(CMAKE_CUDA_COMPILER_LIBRARY_ROOT_FROM_NVVMIR_LIBRARY_DIR "${_CUDA_NVVMIR_LIBRARY_DIR}")
  174. endif()
  175. unset(_CUDA_NVVMIR_LIBRARY_DIR)
  176. unset(_cuda_nvvmir_dir_name)
  177. endif()
  178. unset(_CUDA_NVCC_OUT)
  179. set(CMAKE_CUDA_DEVICE_LINKER "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}/bin/nvlink${CMAKE_EXECUTABLE_SUFFIX}")
  180. set(CMAKE_CUDA_FATBINARY "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}/bin/fatbinary${CMAKE_EXECUTABLE_SUFFIX}")
  181. # In a non-scattered installation the following are equivalent to CMAKE_CUDA_COMPILER_TOOLKIT_ROOT.
  182. # We first check for a non-scattered installation to prefer it over a scattered installation.
  183. # CMAKE_CUDA_COMPILER_LIBRARY_ROOT contains the device library.
  184. if(DEFINED CMAKE_CUDA_COMPILER_LIBRARY_ROOT_FROM_NVVMIR_LIBRARY_DIR)
  185. set(CMAKE_CUDA_COMPILER_LIBRARY_ROOT "${CMAKE_CUDA_COMPILER_LIBRARY_ROOT_FROM_NVVMIR_LIBRARY_DIR}")
  186. elseif(EXISTS "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}/nvvm/libdevice")
  187. set(CMAKE_CUDA_COMPILER_LIBRARY_ROOT "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}")
  188. elseif(CMAKE_SYSROOT_LINK AND EXISTS "${CMAKE_SYSROOT_LINK}/usr/lib/cuda/nvvm/libdevice")
  189. set(CMAKE_CUDA_COMPILER_LIBRARY_ROOT "${CMAKE_SYSROOT_LINK}/usr/lib/cuda")
  190. elseif(EXISTS "${CMAKE_SYSROOT}/usr/lib/cuda/nvvm/libdevice")
  191. set(CMAKE_CUDA_COMPILER_LIBRARY_ROOT "${CMAKE_SYSROOT}/usr/lib/cuda")
  192. else()
  193. message(FATAL_ERROR "Couldn't find CUDA library root.")
  194. endif()
  195. unset(CMAKE_CUDA_COMPILER_LIBRARY_ROOT_FROM_NVVMIR_LIBRARY_DIR)
  196. # CMAKE_CUDA_COMPILER_TOOLKIT_LIBRARY_ROOT contains the linking stubs necessary for device linking and other low-level library files.
  197. if(CMAKE_SYSROOT_LINK AND EXISTS "${CMAKE_SYSROOT_LINK}/usr/lib/nvidia-cuda-toolkit/bin/crt/link.stub")
  198. set(CMAKE_CUDA_COMPILER_TOOLKIT_LIBRARY_ROOT "${CMAKE_SYSROOT_LINK}/usr/lib/nvidia-cuda-toolkit")
  199. elseif(EXISTS "${CMAKE_SYSROOT}/usr/lib/nvidia-cuda-toolkit/bin/crt/link.stub")
  200. set(CMAKE_CUDA_COMPILER_TOOLKIT_LIBRARY_ROOT "${CMAKE_SYSROOT}/usr/lib/nvidia-cuda-toolkit")
  201. else()
  202. set(CMAKE_CUDA_COMPILER_TOOLKIT_LIBRARY_ROOT "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}")
  203. endif()
  204. endif()
  205. set(CMAKE_CUDA_COMPILER_ID_FLAGS_ALWAYS "-v")
  206. if(CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
  207. set(nvcc_test_flags "--keep --keep-dir tmp")
  208. if(CMAKE_CUDA_HOST_COMPILER)
  209. string(APPEND nvcc_test_flags " -ccbin=\"${CMAKE_CUDA_HOST_COMPILER}\"")
  210. # If the user has specified a host compiler we should fail instead of trying without.
  211. # Succeeding detection without may result in confusing errors later on, see #21076.
  212. set(CMAKE_CUDA_COMPILER_ID_REQUIRE_SUCCESS ON)
  213. endif()
  214. elseif(CMAKE_CUDA_COMPILER_ID STREQUAL "Clang")
  215. set(clang_test_flags "--cuda-path=\"${CMAKE_CUDA_COMPILER_LIBRARY_ROOT}\"")
  216. if(CMAKE_CROSSCOMPILING)
  217. # Need to pass the host target and include directories if we're crosscompiling.
  218. string(APPEND clang_test_flags " --sysroot=\"${CMAKE_SYSROOT}\" --target=${CMAKE_CUDA_COMPILER_TARGET}")
  219. endif()
  220. endif()
  221. # Append user-specified architectures.
  222. if(CMAKE_CUDA_ARCHITECTURES)
  223. if("x${CMAKE_CUDA_ARCHITECTURES}" STREQUAL "xall")
  224. string(APPEND nvcc_test_flags " -arch=all")
  225. set(architectures_mode all)
  226. elseif("x${CMAKE_CUDA_ARCHITECTURES}" STREQUAL "xall-major")
  227. string(APPEND nvcc_test_flags " -arch=all-major")
  228. set(architectures_mode all-major)
  229. else()
  230. set(architectures_mode explicit)
  231. foreach(arch ${CMAKE_CUDA_ARCHITECTURES})
  232. # Strip specifiers as PTX vs binary doesn't matter.
  233. string(REGEX MATCH "[0-9]+" arch_name "${arch}")
  234. string(APPEND clang_test_flags " --cuda-gpu-arch=sm_${arch_name}")
  235. string(APPEND nvcc_test_flags " -gencode=arch=compute_${arch_name},code=sm_${arch_name}")
  236. list(APPEND tested_architectures "${arch_name}")
  237. endforeach()
  238. endif()
  239. # If the user has specified architectures we'll want to fail during compiler detection if they don't work.
  240. set(CMAKE_CUDA_COMPILER_ID_REQUIRE_SUCCESS ON)
  241. endif()
  242. if(CMAKE_CUDA_COMPILER_ID STREQUAL "Clang")
  243. if(NOT CMAKE_CUDA_ARCHITECTURES)
  244. # Clang doesn't automatically select an architecture supported by the SDK.
  245. # Try in reverse order of deprecation with the most recent at front (i.e. the most likely to work for new setups).
  246. foreach(arch "20" "30" "52")
  247. list(APPEND CMAKE_CUDA_COMPILER_ID_TEST_FLAGS_FIRST "${clang_test_flags} --cuda-gpu-arch=sm_${arch}")
  248. endforeach()
  249. endif()
  250. # If the user specified CMAKE_CUDA_ARCHITECTURES this will include all the architecture flags.
  251. # Otherwise this won't include any architecture flags and we'll fallback to Clang's defaults.
  252. list(APPEND CMAKE_CUDA_COMPILER_ID_TEST_FLAGS_FIRST "${clang_test_flags}")
  253. elseif(CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
  254. list(APPEND CMAKE_CUDA_COMPILER_ID_TEST_FLAGS_FIRST "${nvcc_test_flags}")
  255. endif()
  256. # We perform compiler identification for a second time to extract implicit linking info and host compiler for NVCC.
  257. # We also use it to verify that CMAKE_CUDA_ARCHITECTURES and additionally on Clang that CUDA toolkit path works.
  258. # The latter could be done during compiler testing in the future to avoid doing this for Clang.
  259. # We need to unset the compiler ID otherwise CMAKE_DETERMINE_COMPILER_ID() doesn't work.
  260. set(CMAKE_CUDA_COMPILER_ID)
  261. set(CMAKE_CUDA_PLATFORM_ID)
  262. file(READ ${CMAKE_ROOT}/Modules/CMakePlatformId.h.in
  263. CMAKE_CUDA_COMPILER_ID_PLATFORM_CONTENT)
  264. CMAKE_DETERMINE_COMPILER_ID(CUDA CUDAFLAGS CMakeCUDACompilerId.cu)
  265. if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
  266. # Now that we have the path to nvcc, we can compute the toolkit root.
  267. get_filename_component(CMAKE_CUDA_COMPILER_TOOLKIT_ROOT "${CMAKE_CUDA_COMPILER}" DIRECTORY)
  268. get_filename_component(CMAKE_CUDA_COMPILER_TOOLKIT_ROOT "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}" DIRECTORY)
  269. set(CMAKE_CUDA_COMPILER_LIBRARY_ROOT "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}")
  270. endif()
  271. _cmake_find_compiler_sysroot(CUDA)
  272. endif()
  273. set(_CMAKE_PROCESSING_LANGUAGE "CUDA")
  274. include(CMakeFindBinUtils)
  275. include(Compiler/${CMAKE_CUDA_COMPILER_ID}-FindBinUtils OPTIONAL)
  276. unset(_CMAKE_PROCESSING_LANGUAGE)
  277. if(MSVC_CUDA_ARCHITECTURE_ID)
  278. set(SET_MSVC_CUDA_ARCHITECTURE_ID
  279. "set(MSVC_CUDA_ARCHITECTURE_ID ${MSVC_CUDA_ARCHITECTURE_ID})")
  280. endif()
  281. if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
  282. set(CMAKE_CUDA_HOST_LINK_LAUNCHER "${CMAKE_LINKER}")
  283. set(CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES "")
  284. set(CMAKE_CUDA_HOST_IMPLICIT_LINK_DIRECTORIES "")
  285. set(CMAKE_CUDA_HOST_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "")
  286. # We do not currently detect CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES but we
  287. # do need to detect CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT from the compiler by
  288. # looking at which cudart library exists in the implicit link libraries passed
  289. # to the host linker.
  290. if(CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT MATCHES "link\\.exe [^\n]*cudart_static\\.lib")
  291. set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "STATIC")
  292. elseif(CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT MATCHES "link\\.exe [^\n]*cudart\\.lib")
  293. set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "SHARED")
  294. else()
  295. set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "NONE")
  296. endif()
  297. set(_SET_CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT
  298. "set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT \"${CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT}\")")
  299. elseif(CMAKE_CUDA_COMPILER_ID STREQUAL "Clang")
  300. if(NOT CMAKE_CUDA_ARCHITECTURES)
  301. # Find the architecture that we successfully compiled using and set it as the default.
  302. string(REGEX MATCH "-target-cpu sm_([0-9]+)" dont_care "${CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT}")
  303. set(detected_architecture "${CMAKE_MATCH_1}")
  304. else()
  305. string(REGEX MATCHALL "-target-cpu sm_([0-9]+)" target_cpus "${CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT}")
  306. foreach(cpu ${target_cpus})
  307. string(REGEX MATCH "-target-cpu sm_([0-9]+)" dont_care "${cpu}")
  308. list(APPEND architectures "${CMAKE_MATCH_1}")
  309. endforeach()
  310. endif()
  311. # Find target directory when crosscompiling.
  312. if(CMAKE_CROSSCOMPILING)
  313. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "armv7-a")
  314. # Support for NVPACK
  315. set(_CUDA_TARGET_NAME "armv7-linux-androideabi")
  316. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm")
  317. set(_CUDA_TARGET_NAME "armv7-linux-gnueabihf")
  318. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
  319. if(ANDROID_ARCH_NAME STREQUAL "arm64")
  320. set(_CUDA_TARGET_NAME "aarch64-linux-androideabi")
  321. else()
  322. set(_CUDA_TARGET_NAME "aarch64-linux")
  323. endif()
  324. elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
  325. set(_CUDA_TARGET_NAME "x86_64-linux")
  326. endif()
  327. if(EXISTS "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}/targets/${_CUDA_TARGET_NAME}")
  328. set(_CUDA_TARGET_DIR "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}/targets/${_CUDA_TARGET_NAME}")
  329. endif()
  330. endif()
  331. # If not already set we can simply use the toolkit root or it's a scattered installation.
  332. if(NOT _CUDA_TARGET_DIR)
  333. set(_CUDA_TARGET_DIR "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}")
  334. endif()
  335. # We can't use find_library() yet at this point, so try a few guesses.
  336. if(EXISTS "${_CUDA_TARGET_DIR}/lib64")
  337. set(_CUDA_LIBRARY_DIR "${_CUDA_TARGET_DIR}/lib64")
  338. elseif(EXISTS "${_CUDA_TARGET_DIR}/lib/x64")
  339. set(_CUDA_LIBRARY_DIR "${_CUDA_TARGET_DIR}/lib/x64")
  340. elseif(EXISTS "${_CUDA_TARGET_DIR}/lib")
  341. set(_CUDA_LIBRARY_DIR "${_CUDA_TARGET_DIR}/lib")
  342. else()
  343. message(FATAL_ERROR "Unable to find _CUDA_LIBRARY_DIR based on _CUDA_TARGET_DIR=${_CUDA_TARGET_DIR}")
  344. endif()
  345. # _CUDA_TARGET_DIR always points to the directory containing the include directory.
  346. # On a scattered installation /usr, on a non-scattered something like /usr/local/cuda or /usr/local/cuda-10.2/targets/aarch64-linux.
  347. if(EXISTS "${_CUDA_TARGET_DIR}/include/cuda_runtime.h")
  348. set(_CUDA_INCLUDE_DIR "${_CUDA_TARGET_DIR}/include")
  349. else()
  350. message(FATAL_ERROR "Unable to find cuda_runtime.h in \"${_CUDA_TARGET_DIR}/include\" for _CUDA_INCLUDE_DIR.")
  351. endif()
  352. # Clang does not add any CUDA SDK libraries or directories when invoking the host linker.
  353. # Add the CUDA toolkit library directory ourselves so that linking works.
  354. # The CUDA runtime libraries are handled elsewhere by CMAKE_CUDA_RUNTIME_LIBRARY.
  355. set(CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES "${_CUDA_INCLUDE_DIR}")
  356. set(CMAKE_CUDA_HOST_IMPLICIT_LINK_DIRECTORIES "${_CUDA_LIBRARY_DIR}")
  357. set(CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES "")
  358. set(CMAKE_CUDA_HOST_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "")
  359. elseif(CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
  360. set(_nvcc_log "")
  361. string(REPLACE "\r" "" _nvcc_output_orig "${CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT}")
  362. if(_nvcc_output_orig MATCHES "#\\\$ +PATH= *([^\n]*)\n")
  363. set(_nvcc_path "${CMAKE_MATCH_1}")
  364. string(APPEND _nvcc_log " found 'PATH=' string: [${_nvcc_path}]\n")
  365. string(REPLACE ":" ";" _nvcc_path "${_nvcc_path}")
  366. else()
  367. set(_nvcc_path "")
  368. string(REPLACE "\n" "\n " _nvcc_output_log "\n${_nvcc_output_orig}")
  369. string(APPEND _nvcc_log " no 'PATH=' string found in nvcc output:${_nvcc_output_log}\n")
  370. endif()
  371. if(_nvcc_output_orig MATCHES "#\\\$ +LIBRARIES= *([^\n]*)\n")
  372. set(_nvcc_libraries "${CMAKE_MATCH_1}")
  373. string(APPEND _nvcc_log " found 'LIBRARIES=' string: [${_nvcc_libraries}]\n")
  374. else()
  375. set(_nvcc_libraries "")
  376. string(REPLACE "\n" "\n " _nvcc_output_log "\n${_nvcc_output_orig}")
  377. string(APPEND _nvcc_log " no 'LIBRARIES=' string found in nvcc output:${_nvcc_output_log}\n")
  378. endif()
  379. set(_nvcc_link_line "")
  380. if(_nvcc_libraries)
  381. # Remove variable assignments.
  382. string(REGEX REPLACE "#\\\$ *[^= ]+=[^\n]*\n" "" _nvcc_output "${_nvcc_output_orig}")
  383. # Encode [] characters that break list expansion.
  384. string(REPLACE "[" "{==={" _nvcc_output "${_nvcc_output}")
  385. string(REPLACE "]" "}===}" _nvcc_output "${_nvcc_output}")
  386. # Split lines.
  387. string(REGEX REPLACE "\n+(#\\\$ )?" ";" _nvcc_output "${_nvcc_output}")
  388. foreach(line IN LISTS _nvcc_output)
  389. set(_nvcc_output_line "${line}")
  390. string(REPLACE "{==={" "[" _nvcc_output_line "${_nvcc_output_line}")
  391. string(REPLACE "}===}" "]" _nvcc_output_line "${_nvcc_output_line}")
  392. string(APPEND _nvcc_log " considering line: [${_nvcc_output_line}]\n")
  393. if("${_nvcc_output_line}" MATCHES "^ *nvlink")
  394. string(APPEND _nvcc_log " ignoring nvlink line\n")
  395. elseif(_nvcc_libraries)
  396. if("${_nvcc_output_line}" MATCHES "(@\"?((tmp/)?a\\.exe\\.res)\"?)")
  397. set(_nvcc_link_res_arg "${CMAKE_MATCH_1}")
  398. set(_nvcc_link_res_file "${CMAKE_MATCH_2}")
  399. set(_nvcc_link_res "${CMAKE_PLATFORM_INFO_DIR}/CompilerIdCUDA/${_nvcc_link_res_file}")
  400. if(EXISTS "${_nvcc_link_res}")
  401. file(READ "${_nvcc_link_res}" _nvcc_link_res_content)
  402. string(REPLACE "${_nvcc_link_res_arg}" "${_nvcc_link_res_content}" _nvcc_output_line "${_nvcc_output_line}")
  403. endif()
  404. endif()
  405. string(FIND "${_nvcc_output_line}" "${_nvcc_libraries}" _nvcc_libraries_pos)
  406. if(NOT _nvcc_libraries_pos EQUAL -1)
  407. set(_nvcc_link_line "${_nvcc_output_line}")
  408. string(APPEND _nvcc_log " extracted link line: [${_nvcc_link_line}]\n")
  409. endif()
  410. endif()
  411. endforeach()
  412. endif()
  413. if(_nvcc_link_line)
  414. if("x${CMAKE_CUDA_SIMULATE_ID}" STREQUAL "xMSVC")
  415. set(CMAKE_CUDA_HOST_LINK_LAUNCHER "${CMAKE_LINKER}")
  416. else()
  417. #extract the compiler that is being used for linking
  418. separate_arguments(_nvcc_link_line_args UNIX_COMMAND "${_nvcc_link_line}")
  419. list(GET _nvcc_link_line_args 0 _nvcc_host_link_launcher)
  420. if(IS_ABSOLUTE "${_nvcc_host_link_launcher}")
  421. string(APPEND _nvcc_log " extracted link launcher absolute path: [${_nvcc_host_link_launcher}]\n")
  422. set(CMAKE_CUDA_HOST_LINK_LAUNCHER "${_nvcc_host_link_launcher}")
  423. else()
  424. string(APPEND _nvcc_log " extracted link launcher name: [${_nvcc_host_link_launcher}]\n")
  425. find_program(_nvcc_find_host_link_launcher
  426. NAMES ${_nvcc_host_link_launcher}
  427. PATHS ${_nvcc_path} NO_DEFAULT_PATH)
  428. find_program(_nvcc_find_host_link_launcher
  429. NAMES ${_nvcc_host_link_launcher})
  430. if(_nvcc_find_host_link_launcher)
  431. string(APPEND _nvcc_log " found link launcher absolute path: [${_nvcc_find_host_link_launcher}]\n")
  432. set(CMAKE_CUDA_HOST_LINK_LAUNCHER "${_nvcc_find_host_link_launcher}")
  433. else()
  434. string(APPEND _nvcc_log " could not find link launcher absolute path\n")
  435. set(CMAKE_CUDA_HOST_LINK_LAUNCHER "${_nvcc_host_link_launcher}")
  436. endif()
  437. unset(_nvcc_find_host_link_launcher CACHE)
  438. endif()
  439. endif()
  440. #prefix the line with cuda-fake-ld so that implicit link info believes it is
  441. #a link line
  442. set(_nvcc_link_line "cuda-fake-ld ${_nvcc_link_line}")
  443. CMAKE_PARSE_IMPLICIT_LINK_INFO("${_nvcc_link_line}"
  444. CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES
  445. CMAKE_CUDA_HOST_IMPLICIT_LINK_DIRECTORIES
  446. CMAKE_CUDA_HOST_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES
  447. log
  448. "${CMAKE_CUDA_IMPLICIT_OBJECT_REGEX}")
  449. # Detect CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT from the compiler by looking at which
  450. # cudart library exists in the implicit link libraries passed to the host linker.
  451. # This is required when a project sets the cuda runtime library as part of the
  452. # initial flags.
  453. if(";${CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES};" MATCHES [[;cudart_static(\.lib)?;]])
  454. set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "STATIC")
  455. elseif(";${CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES};" MATCHES [[;cudart(\.lib)?;]])
  456. set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "SHARED")
  457. else()
  458. set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "NONE")
  459. endif()
  460. set(_SET_CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT
  461. "set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT \"${CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT}\")")
  462. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  463. "Parsed CUDA nvcc implicit link information from above output:\n${_nvcc_log}\n${log}\n\n")
  464. else()
  465. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  466. "Failed to parse CUDA nvcc implicit link information:\n${_nvcc_log}\n\n")
  467. message(FATAL_ERROR "Failed to extract nvcc implicit link line.")
  468. endif()
  469. endif()
  470. # CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES is detected above as the list of
  471. # libraries that the CUDA compiler implicitly passes to the host linker.
  472. # CMake invokes the host linker directly and so needs to pass these libraries.
  473. # We filter out those that should not be passed unconditionally both here
  474. # and from CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES in CMakeTestCUDACompiler.
  475. set(CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES_EXCLUDE
  476. # The CUDA runtime libraries are controlled by CMAKE_CUDA_RUNTIME_LIBRARY.
  477. cudart cudart.lib
  478. cudart_static cudart_static.lib
  479. cudadevrt cudadevrt.lib
  480. # Dependencies of the CUDA static runtime library on Linux hosts.
  481. rt
  482. pthread
  483. dl
  484. )
  485. list(REMOVE_ITEM CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES ${CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES_EXCLUDE})
  486. if(CMAKE_CUDA_COMPILER_SYSROOT)
  487. string(CONCAT _SET_CMAKE_CUDA_COMPILER_SYSROOT
  488. "set(CMAKE_CUDA_COMPILER_SYSROOT \"${CMAKE_CUDA_COMPILER_SYSROOT}\")\n"
  489. "set(CMAKE_COMPILER_SYSROOT \"${CMAKE_CUDA_COMPILER_SYSROOT}\")")
  490. else()
  491. set(_SET_CMAKE_CUDA_COMPILER_SYSROOT "")
  492. endif()
  493. # Determine CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES
  494. if(CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
  495. set(CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES)
  496. string(REPLACE "\r" "" _nvcc_output_orig "${CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT}")
  497. if(_nvcc_output_orig MATCHES "#\\\$ +INCLUDES= *([^\n]*)\n")
  498. set(_nvcc_includes "${CMAKE_MATCH_1}")
  499. string(APPEND _nvcc_log " found 'INCLUDES=' string: [${_nvcc_includes}]\n")
  500. else()
  501. set(_nvcc_includes "")
  502. string(REPLACE "\n" "\n " _nvcc_output_log "\n${_nvcc_output_orig}")
  503. string(APPEND _nvcc_log " no 'INCLUDES=' string found in nvcc output:${_nvcc_output_log}\n")
  504. endif()
  505. if(_nvcc_includes)
  506. # across all operating system each include directory is prefixed with -I
  507. separate_arguments(_nvcc_output NATIVE_COMMAND "${_nvcc_includes}")
  508. foreach(line IN LISTS _nvcc_output)
  509. string(REGEX REPLACE "^-I" "" line "${line}")
  510. get_filename_component(line "${line}" ABSOLUTE)
  511. list(APPEND CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES "${line}")
  512. endforeach()
  513. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  514. "Parsed CUDA nvcc include information from above output:\n${_nvcc_log}\n${log}\n\n")
  515. else()
  516. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  517. "Failed to detect CUDA nvcc include information:\n${_nvcc_log}\n\n")
  518. endif()
  519. # Parse default CUDA architecture.
  520. cmake_policy(GET CMP0104 _CUDA_CMP0104)
  521. if(NOT CMAKE_CUDA_ARCHITECTURES AND _CUDA_CMP0104 STREQUAL "NEW")
  522. string(REGEX MATCH "arch[ =]compute_([0-9]+)" dont_care "${CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT}")
  523. set(detected_architecture "${CMAKE_MATCH_1}")
  524. elseif(CMAKE_CUDA_ARCHITECTURES)
  525. string(REGEX MATCHALL "-arch compute_([0-9]+)" target_cpus "${CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT}")
  526. foreach(cpu ${target_cpus})
  527. string(REGEX MATCH "-arch compute_([0-9]+)" dont_care "${cpu}")
  528. list(APPEND architectures "${CMAKE_MATCH_1}")
  529. endforeach()
  530. endif()
  531. endif()
  532. # If the user didn't set the architectures, then set them to a default.
  533. # If the user did, then make sure those architectures worked.
  534. if(DEFINED detected_architecture AND "${CMAKE_CUDA_ARCHITECTURES}" STREQUAL "")
  535. set(CMAKE_CUDA_ARCHITECTURES "${detected_architecture}" CACHE STRING "CUDA architectures")
  536. if(NOT CMAKE_CUDA_ARCHITECTURES)
  537. message(FATAL_ERROR "Failed to find a working CUDA architecture.")
  538. endif()
  539. elseif(architectures AND (architectures_mode STREQUAL "xall" OR
  540. architectures_mode STREQUAL "xall-major"))
  541. if(NOT CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
  542. message(FATAL_ERROR
  543. "The CMAKE_CUDA_ARCHITECTURES:\n"
  544. " ${CMAKE_CUDA_ARCHITECTURES}\n"
  545. "is not supported with the ${CMAKE_CUDA_COMPILER_ID} compiler. Try:\n"
  546. " ${architectures}\n"
  547. "instead.")
  548. endif()
  549. elseif(architectures AND architectures_mode STREQUAL "xexplicit")
  550. # Sort since order mustn't matter.
  551. list(SORT architectures)
  552. list(SORT tested_architectures)
  553. # We don't distinguish real/virtual architectures during testing.
  554. # For "70-real;70-virtual" we detect "70" as working and tested_architectures is "70;70".
  555. # Thus we need to remove duplicates before checking if they're equal.
  556. list(REMOVE_DUPLICATES tested_architectures)
  557. if(NOT "${architectures}" STREQUAL "${tested_architectures}")
  558. message(FATAL_ERROR
  559. "The CMAKE_CUDA_ARCHITECTURES:\n"
  560. " ${CMAKE_CUDA_ARCHITECTURES}\n"
  561. "do not all work with this compiler. Try:\n"
  562. " ${architectures}\n"
  563. "instead.")
  564. endif()
  565. endif()
  566. # configure all variables set in this file
  567. configure_file(${CMAKE_ROOT}/Modules/CMakeCUDACompiler.cmake.in
  568. ${CMAKE_PLATFORM_INFO_DIR}/CMakeCUDACompiler.cmake
  569. @ONLY
  570. )
  571. # Don't leak variables unnecessarily to user code.
  572. unset(_CUDA_INCLUDE_DIR CACHE)
  573. unset(_CUDA_NVCC_EXECUTABLE CACHE)
  574. unset(_CUDA_LIBRARY_DIR)
  575. unset(_CUDA_TARGET_DIR)
  576. unset(_CUDA_TARGET_NAME)
  577. unset(architectures_mode)
  578. set(CMAKE_CUDA_COMPILER_ENV_VAR "CUDACXX")
  579. set(CMAKE_CUDA_HOST_COMPILER_ENV_VAR "CUDAHOSTCXX")