CMakeDetermineCUDACompiler.cmake 26 KB

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