CMakeDetermineCUDACompiler.cmake 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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(DEFINED 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. # Rest of the code treats an empty value as equivalent to "use the defaults".
  243. # Error out early to prevent confusing errors as a result of this.
  244. # Note that this also catches invalid non-numerical values such as "a".
  245. if(architectures_mode STREQUAL "explicit" AND "${tested_architectures}" STREQUAL "")
  246. message(FATAL_ERROR "CMAKE_CUDA_ARCHITECTURES must be valid if set.")
  247. endif()
  248. if(CMAKE_CUDA_COMPILER_ID STREQUAL "Clang")
  249. if(NOT CMAKE_CUDA_ARCHITECTURES)
  250. # Clang doesn't automatically select an architecture supported by the SDK.
  251. # Try in reverse order of deprecation with the most recent at front (i.e. the most likely to work for new setups).
  252. foreach(arch "52" "30" "20")
  253. list(APPEND CMAKE_CUDA_COMPILER_ID_TEST_FLAGS_FIRST "${clang_test_flags} --cuda-gpu-arch=sm_${arch}")
  254. endforeach()
  255. endif()
  256. # If the user specified CMAKE_CUDA_ARCHITECTURES this will include all the architecture flags.
  257. # Otherwise this won't include any architecture flags and we'll fallback to Clang's defaults.
  258. list(APPEND CMAKE_CUDA_COMPILER_ID_TEST_FLAGS_FIRST "${clang_test_flags}")
  259. elseif(CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
  260. list(APPEND CMAKE_CUDA_COMPILER_ID_TEST_FLAGS_FIRST "${nvcc_test_flags}")
  261. endif()
  262. # We perform compiler identification for a second time to extract implicit linking info and host compiler for NVCC.
  263. # We also use it to verify that CMAKE_CUDA_ARCHITECTURES and additionally on Clang that CUDA toolkit path works.
  264. # The latter could be done during compiler testing in the future to avoid doing this for Clang.
  265. # We need to unset the compiler ID otherwise CMAKE_DETERMINE_COMPILER_ID() doesn't work.
  266. set(CMAKE_CUDA_COMPILER_ID)
  267. set(CMAKE_CUDA_PLATFORM_ID)
  268. file(READ ${CMAKE_ROOT}/Modules/CMakePlatformId.h.in
  269. CMAKE_CUDA_COMPILER_ID_PLATFORM_CONTENT)
  270. CMAKE_DETERMINE_COMPILER_ID(CUDA CUDAFLAGS CMakeCUDACompilerId.cu)
  271. if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
  272. # Now that we have the path to nvcc, we can compute the toolkit root.
  273. get_filename_component(CMAKE_CUDA_COMPILER_TOOLKIT_ROOT "${CMAKE_CUDA_COMPILER}" DIRECTORY)
  274. get_filename_component(CMAKE_CUDA_COMPILER_TOOLKIT_ROOT "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}" DIRECTORY)
  275. set(CMAKE_CUDA_COMPILER_LIBRARY_ROOT "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}")
  276. endif()
  277. _cmake_find_compiler_sysroot(CUDA)
  278. endif()
  279. set(_CMAKE_PROCESSING_LANGUAGE "CUDA")
  280. include(CMakeFindBinUtils)
  281. include(Compiler/${CMAKE_CUDA_COMPILER_ID}-FindBinUtils OPTIONAL)
  282. unset(_CMAKE_PROCESSING_LANGUAGE)
  283. if(MSVC_CUDA_ARCHITECTURE_ID)
  284. set(SET_MSVC_CUDA_ARCHITECTURE_ID
  285. "set(MSVC_CUDA_ARCHITECTURE_ID ${MSVC_CUDA_ARCHITECTURE_ID})")
  286. endif()
  287. if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
  288. set(CMAKE_CUDA_HOST_LINK_LAUNCHER "${CMAKE_LINKER}")
  289. set(CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES "")
  290. set(CMAKE_CUDA_HOST_IMPLICIT_LINK_DIRECTORIES "")
  291. set(CMAKE_CUDA_HOST_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "")
  292. # We do not currently detect CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES but we
  293. # do need to detect CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT from the compiler by
  294. # looking at which cudart library exists in the implicit link libraries passed
  295. # to the host linker.
  296. if(CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT MATCHES "link\\.exe [^\n]*cudart_static\\.lib")
  297. set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "STATIC")
  298. elseif(CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT MATCHES "link\\.exe [^\n]*cudart\\.lib")
  299. set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "SHARED")
  300. else()
  301. set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "NONE")
  302. endif()
  303. set(_SET_CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT
  304. "set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT \"${CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT}\")")
  305. elseif(CMAKE_CUDA_COMPILER_ID STREQUAL "Clang")
  306. if(NOT CMAKE_CUDA_ARCHITECTURES)
  307. # Find the architecture that we successfully compiled using and set it as the default.
  308. string(REGEX MATCH "-target-cpu sm_([0-9]+)" dont_care "${CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT}")
  309. set(detected_architecture "${CMAKE_MATCH_1}")
  310. else()
  311. string(REGEX MATCHALL "-target-cpu sm_([0-9]+)" target_cpus "${CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT}")
  312. foreach(cpu ${target_cpus})
  313. string(REGEX MATCH "-target-cpu sm_([0-9]+)" dont_care "${cpu}")
  314. list(APPEND architectures "${CMAKE_MATCH_1}")
  315. endforeach()
  316. endif()
  317. # Find target directory when crosscompiling.
  318. if(CMAKE_CROSSCOMPILING)
  319. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "armv7-a")
  320. # Support for NVPACK
  321. set(_CUDA_TARGET_NAME "armv7-linux-androideabi")
  322. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm")
  323. set(_CUDA_TARGET_NAME "armv7-linux-gnueabihf")
  324. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
  325. if(ANDROID_ARCH_NAME STREQUAL "arm64")
  326. set(_CUDA_TARGET_NAME "aarch64-linux-androideabi")
  327. else()
  328. set(_CUDA_TARGET_NAME "aarch64-linux")
  329. endif()
  330. elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
  331. set(_CUDA_TARGET_NAME "x86_64-linux")
  332. endif()
  333. if(EXISTS "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}/targets/${_CUDA_TARGET_NAME}")
  334. set(_CUDA_TARGET_DIR "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}/targets/${_CUDA_TARGET_NAME}")
  335. endif()
  336. endif()
  337. # If not already set we can simply use the toolkit root or it's a scattered installation.
  338. if(NOT _CUDA_TARGET_DIR)
  339. set(_CUDA_TARGET_DIR "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}")
  340. endif()
  341. # We can't use find_library() yet at this point, so try a few guesses.
  342. if(EXISTS "${_CUDA_TARGET_DIR}/lib64")
  343. set(_CUDA_LIBRARY_DIR "${_CUDA_TARGET_DIR}/lib64")
  344. elseif(EXISTS "${_CUDA_TARGET_DIR}/lib/x64")
  345. set(_CUDA_LIBRARY_DIR "${_CUDA_TARGET_DIR}/lib/x64")
  346. elseif(EXISTS "${_CUDA_TARGET_DIR}/lib")
  347. set(_CUDA_LIBRARY_DIR "${_CUDA_TARGET_DIR}/lib")
  348. else()
  349. message(FATAL_ERROR "Unable to find _CUDA_LIBRARY_DIR based on _CUDA_TARGET_DIR=${_CUDA_TARGET_DIR}")
  350. endif()
  351. # _CUDA_TARGET_DIR always points to the directory containing the include directory.
  352. # On a scattered installation /usr, on a non-scattered something like /usr/local/cuda or /usr/local/cuda-10.2/targets/aarch64-linux.
  353. if(EXISTS "${_CUDA_TARGET_DIR}/include/cuda_runtime.h")
  354. set(_CUDA_INCLUDE_DIR "${_CUDA_TARGET_DIR}/include")
  355. else()
  356. message(FATAL_ERROR "Unable to find cuda_runtime.h in \"${_CUDA_TARGET_DIR}/include\" for _CUDA_INCLUDE_DIR.")
  357. endif()
  358. # Clang does not add any CUDA SDK libraries or directories when invoking the host linker.
  359. # Add the CUDA toolkit library directory ourselves so that linking works.
  360. # The CUDA runtime libraries are handled elsewhere by CMAKE_CUDA_RUNTIME_LIBRARY.
  361. set(CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES "${_CUDA_INCLUDE_DIR}")
  362. set(CMAKE_CUDA_HOST_IMPLICIT_LINK_DIRECTORIES "${_CUDA_LIBRARY_DIR}")
  363. set(CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES "")
  364. set(CMAKE_CUDA_HOST_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "")
  365. elseif(CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
  366. set(_nvcc_log "")
  367. string(REPLACE "\r" "" _nvcc_output_orig "${CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT}")
  368. if(_nvcc_output_orig MATCHES "#\\\$ +PATH= *([^\n]*)\n")
  369. set(_nvcc_path "${CMAKE_MATCH_1}")
  370. string(APPEND _nvcc_log " found 'PATH=' string: [${_nvcc_path}]\n")
  371. string(REPLACE ":" ";" _nvcc_path "${_nvcc_path}")
  372. else()
  373. set(_nvcc_path "")
  374. string(REPLACE "\n" "\n " _nvcc_output_log "\n${_nvcc_output_orig}")
  375. string(APPEND _nvcc_log " no 'PATH=' string found in nvcc output:${_nvcc_output_log}\n")
  376. endif()
  377. if(_nvcc_output_orig MATCHES "#\\\$ +LIBRARIES= *([^\n]*)\n")
  378. set(_nvcc_libraries "${CMAKE_MATCH_1}")
  379. string(APPEND _nvcc_log " found 'LIBRARIES=' string: [${_nvcc_libraries}]\n")
  380. else()
  381. set(_nvcc_libraries "")
  382. string(REPLACE "\n" "\n " _nvcc_output_log "\n${_nvcc_output_orig}")
  383. string(APPEND _nvcc_log " no 'LIBRARIES=' string found in nvcc output:${_nvcc_output_log}\n")
  384. endif()
  385. set(_nvcc_link_line "")
  386. if(_nvcc_libraries)
  387. # Remove variable assignments.
  388. string(REGEX REPLACE "#\\\$ *[^= ]+=[^\n]*\n" "" _nvcc_output "${_nvcc_output_orig}")
  389. # Encode [] characters that break list expansion.
  390. string(REPLACE "[" "{==={" _nvcc_output "${_nvcc_output}")
  391. string(REPLACE "]" "}===}" _nvcc_output "${_nvcc_output}")
  392. # Split lines.
  393. string(REGEX REPLACE "\n+(#\\\$ )?" ";" _nvcc_output "${_nvcc_output}")
  394. foreach(line IN LISTS _nvcc_output)
  395. set(_nvcc_output_line "${line}")
  396. string(REPLACE "{==={" "[" _nvcc_output_line "${_nvcc_output_line}")
  397. string(REPLACE "}===}" "]" _nvcc_output_line "${_nvcc_output_line}")
  398. string(APPEND _nvcc_log " considering line: [${_nvcc_output_line}]\n")
  399. if("${_nvcc_output_line}" MATCHES "^ *nvlink")
  400. string(APPEND _nvcc_log " ignoring nvlink line\n")
  401. elseif(_nvcc_libraries)
  402. if("${_nvcc_output_line}" MATCHES "(@\"?((tmp/)?a\\.exe\\.res)\"?)")
  403. set(_nvcc_link_res_arg "${CMAKE_MATCH_1}")
  404. set(_nvcc_link_res_file "${CMAKE_MATCH_2}")
  405. set(_nvcc_link_res "${CMAKE_PLATFORM_INFO_DIR}/CompilerIdCUDA/${_nvcc_link_res_file}")
  406. if(EXISTS "${_nvcc_link_res}")
  407. file(READ "${_nvcc_link_res}" _nvcc_link_res_content)
  408. string(REPLACE "${_nvcc_link_res_arg}" "${_nvcc_link_res_content}" _nvcc_output_line "${_nvcc_output_line}")
  409. endif()
  410. endif()
  411. string(FIND "${_nvcc_output_line}" "${_nvcc_libraries}" _nvcc_libraries_pos)
  412. if(NOT _nvcc_libraries_pos EQUAL -1)
  413. set(_nvcc_link_line "${_nvcc_output_line}")
  414. string(APPEND _nvcc_log " extracted link line: [${_nvcc_link_line}]\n")
  415. endif()
  416. endif()
  417. endforeach()
  418. endif()
  419. if(_nvcc_link_line)
  420. if("x${CMAKE_CUDA_SIMULATE_ID}" STREQUAL "xMSVC")
  421. set(CMAKE_CUDA_HOST_LINK_LAUNCHER "${CMAKE_LINKER}")
  422. else()
  423. #extract the compiler that is being used for linking
  424. separate_arguments(_nvcc_link_line_args UNIX_COMMAND "${_nvcc_link_line}")
  425. list(GET _nvcc_link_line_args 0 _nvcc_host_link_launcher)
  426. if(IS_ABSOLUTE "${_nvcc_host_link_launcher}")
  427. string(APPEND _nvcc_log " extracted link launcher absolute path: [${_nvcc_host_link_launcher}]\n")
  428. set(CMAKE_CUDA_HOST_LINK_LAUNCHER "${_nvcc_host_link_launcher}")
  429. else()
  430. string(APPEND _nvcc_log " extracted link launcher name: [${_nvcc_host_link_launcher}]\n")
  431. find_program(_nvcc_find_host_link_launcher
  432. NAMES ${_nvcc_host_link_launcher}
  433. PATHS ${_nvcc_path} NO_DEFAULT_PATH)
  434. find_program(_nvcc_find_host_link_launcher
  435. NAMES ${_nvcc_host_link_launcher})
  436. if(_nvcc_find_host_link_launcher)
  437. string(APPEND _nvcc_log " found link launcher absolute path: [${_nvcc_find_host_link_launcher}]\n")
  438. set(CMAKE_CUDA_HOST_LINK_LAUNCHER "${_nvcc_find_host_link_launcher}")
  439. else()
  440. string(APPEND _nvcc_log " could not find link launcher absolute path\n")
  441. set(CMAKE_CUDA_HOST_LINK_LAUNCHER "${_nvcc_host_link_launcher}")
  442. endif()
  443. unset(_nvcc_find_host_link_launcher CACHE)
  444. endif()
  445. endif()
  446. #prefix the line with cuda-fake-ld so that implicit link info believes it is
  447. #a link line
  448. set(_nvcc_link_line "cuda-fake-ld ${_nvcc_link_line}")
  449. CMAKE_PARSE_IMPLICIT_LINK_INFO("${_nvcc_link_line}"
  450. CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES
  451. CMAKE_CUDA_HOST_IMPLICIT_LINK_DIRECTORIES
  452. CMAKE_CUDA_HOST_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES
  453. log
  454. "${CMAKE_CUDA_IMPLICIT_OBJECT_REGEX}")
  455. # Detect CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT from the compiler by looking at which
  456. # cudart library exists in the implicit link libraries passed to the host linker.
  457. # This is required when a project sets the cuda runtime library as part of the
  458. # initial flags.
  459. if(";${CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES};" MATCHES [[;cudart_static(\.lib)?;]])
  460. set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "STATIC")
  461. elseif(";${CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES};" MATCHES [[;cudart(\.lib)?;]])
  462. set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "SHARED")
  463. else()
  464. set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "NONE")
  465. endif()
  466. set(_SET_CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT
  467. "set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT \"${CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT}\")")
  468. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  469. "Parsed CUDA nvcc implicit link information from above output:\n${_nvcc_log}\n${log}\n\n")
  470. else()
  471. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  472. "Failed to parse CUDA nvcc implicit link information:\n${_nvcc_log}\n\n")
  473. message(FATAL_ERROR "Failed to extract nvcc implicit link line.")
  474. endif()
  475. endif()
  476. # CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES is detected above as the list of
  477. # libraries that the CUDA compiler implicitly passes to the host linker.
  478. # CMake invokes the host linker directly and so needs to pass these libraries.
  479. # We filter out those that should not be passed unconditionally both here
  480. # and from CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES in CMakeTestCUDACompiler.
  481. set(CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES_EXCLUDE
  482. # The CUDA runtime libraries are controlled by CMAKE_CUDA_RUNTIME_LIBRARY.
  483. cudart cudart.lib
  484. cudart_static cudart_static.lib
  485. cudadevrt cudadevrt.lib
  486. # Dependencies of the CUDA static runtime library on Linux hosts.
  487. rt
  488. pthread
  489. dl
  490. )
  491. list(REMOVE_ITEM CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES ${CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES_EXCLUDE})
  492. if(CMAKE_CUDA_COMPILER_SYSROOT)
  493. string(CONCAT _SET_CMAKE_CUDA_COMPILER_SYSROOT
  494. "set(CMAKE_CUDA_COMPILER_SYSROOT \"${CMAKE_CUDA_COMPILER_SYSROOT}\")\n"
  495. "set(CMAKE_COMPILER_SYSROOT \"${CMAKE_CUDA_COMPILER_SYSROOT}\")")
  496. else()
  497. set(_SET_CMAKE_CUDA_COMPILER_SYSROOT "")
  498. endif()
  499. # Determine CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES
  500. if(CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
  501. set(CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES)
  502. string(REPLACE "\r" "" _nvcc_output_orig "${CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT}")
  503. if(_nvcc_output_orig MATCHES "#\\\$ +INCLUDES= *([^\n]*)\n")
  504. set(_nvcc_includes "${CMAKE_MATCH_1}")
  505. string(APPEND _nvcc_log " found 'INCLUDES=' string: [${_nvcc_includes}]\n")
  506. else()
  507. set(_nvcc_includes "")
  508. string(REPLACE "\n" "\n " _nvcc_output_log "\n${_nvcc_output_orig}")
  509. string(APPEND _nvcc_log " no 'INCLUDES=' string found in nvcc output:${_nvcc_output_log}\n")
  510. endif()
  511. if(_nvcc_includes)
  512. # across all operating system each include directory is prefixed with -I
  513. separate_arguments(_nvcc_output NATIVE_COMMAND "${_nvcc_includes}")
  514. foreach(line IN LISTS _nvcc_output)
  515. string(REGEX REPLACE "^-I" "" line "${line}")
  516. get_filename_component(line "${line}" ABSOLUTE)
  517. list(APPEND CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES "${line}")
  518. endforeach()
  519. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  520. "Parsed CUDA nvcc include information from above output:\n${_nvcc_log}\n${log}\n\n")
  521. else()
  522. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  523. "Failed to detect CUDA nvcc include information:\n${_nvcc_log}\n\n")
  524. endif()
  525. # Parse default CUDA architecture.
  526. cmake_policy(GET CMP0104 _CUDA_CMP0104)
  527. if(NOT CMAKE_CUDA_ARCHITECTURES AND _CUDA_CMP0104 STREQUAL "NEW")
  528. string(REGEX MATCH "arch[ =]compute_([0-9]+)" dont_care "${CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT}")
  529. set(detected_architecture "${CMAKE_MATCH_1}")
  530. elseif(CMAKE_CUDA_ARCHITECTURES)
  531. string(REGEX MATCHALL "-arch compute_([0-9]+)" target_cpus "${CMAKE_CUDA_COMPILER_PRODUCED_OUTPUT}")
  532. foreach(cpu ${target_cpus})
  533. string(REGEX MATCH "-arch compute_([0-9]+)" dont_care "${cpu}")
  534. list(APPEND architectures "${CMAKE_MATCH_1}")
  535. endforeach()
  536. endif()
  537. endif()
  538. # If the user didn't set the architectures, then set them to a default.
  539. # If the user did, then make sure those architectures worked.
  540. if(DEFINED detected_architecture AND "${CMAKE_CUDA_ARCHITECTURES}" STREQUAL "")
  541. set(CMAKE_CUDA_ARCHITECTURES "${detected_architecture}" CACHE STRING "CUDA architectures")
  542. if(NOT CMAKE_CUDA_ARCHITECTURES)
  543. message(FATAL_ERROR "Failed to find a working CUDA architecture.")
  544. endif()
  545. elseif(architectures AND (architectures_mode STREQUAL "xall" OR
  546. architectures_mode STREQUAL "xall-major"))
  547. if(NOT CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
  548. message(FATAL_ERROR
  549. "The CMAKE_CUDA_ARCHITECTURES:\n"
  550. " ${CMAKE_CUDA_ARCHITECTURES}\n"
  551. "is not supported with the ${CMAKE_CUDA_COMPILER_ID} compiler. Try:\n"
  552. " ${architectures}\n"
  553. "instead.")
  554. endif()
  555. elseif(architectures AND architectures_mode STREQUAL "xexplicit")
  556. # Sort since order mustn't matter.
  557. list(SORT architectures)
  558. list(SORT tested_architectures)
  559. # We don't distinguish real/virtual architectures during testing.
  560. # For "70-real;70-virtual" we detect "70" as working and tested_architectures is "70;70".
  561. # Thus we need to remove duplicates before checking if they're equal.
  562. list(REMOVE_DUPLICATES tested_architectures)
  563. if(NOT "${architectures}" STREQUAL "${tested_architectures}")
  564. message(FATAL_ERROR
  565. "The CMAKE_CUDA_ARCHITECTURES:\n"
  566. " ${CMAKE_CUDA_ARCHITECTURES}\n"
  567. "do not all work with this compiler. Try:\n"
  568. " ${architectures}\n"
  569. "instead.")
  570. endif()
  571. endif()
  572. # configure all variables set in this file
  573. configure_file(${CMAKE_ROOT}/Modules/CMakeCUDACompiler.cmake.in
  574. ${CMAKE_PLATFORM_INFO_DIR}/CMakeCUDACompiler.cmake
  575. @ONLY
  576. )
  577. # Don't leak variables unnecessarily to user code.
  578. unset(_CUDA_INCLUDE_DIR CACHE)
  579. unset(_CUDA_NVCC_EXECUTABLE CACHE)
  580. unset(_CUDA_LIBRARY_DIR)
  581. unset(_CUDA_TARGET_DIR)
  582. unset(_CUDA_TARGET_NAME)
  583. unset(architectures_mode)
  584. set(CMAKE_CUDA_COMPILER_ENV_VAR "CUDACXX")
  585. set(CMAKE_CUDA_HOST_COMPILER_ENV_VAR "CUDAHOSTCXX")