CMakeDetermineCUDACompiler.cmake 29 KB

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