CMakeDetermineCUDACompiler.cmake 31 KB

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