CMakeDetermineCUDACompiler.cmake 26 KB

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