CMakeCUDAFindToolkit.cmake 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. macro(cmake_cuda_find_toolkit lang lang_var_)
  4. # This is very similar to FindCUDAToolkit, but somewhat simplified since we can issue fatal errors
  5. # if we fail and we don't need to account for searching the libraries.
  6. # For NVCC we can easily deduce the SDK binary directory from the compiler path.
  7. if(CMAKE_${lang}_COMPILER_ID STREQUAL "NVIDIA")
  8. set(_CUDA_NVCC_EXECUTABLE "${CMAKE_${lang}_COMPILER}")
  9. else()
  10. # Search using CUDAToolkit_ROOT and then CUDA_PATH for equivalence with FindCUDAToolkit.
  11. # In FindCUDAToolkit CUDAToolkit_ROOT is searched automatically due to being in a find_package().
  12. # First we search candidate non-default paths to give them priority.
  13. find_program(_CUDA_NVCC_EXECUTABLE
  14. NAMES nvcc nvcc.exe
  15. PATHS ${CUDAToolkit_ROOT}
  16. ENV CUDAToolkit_ROOT
  17. ENV CUDA_PATH
  18. PATH_SUFFIXES bin
  19. NO_DEFAULT_PATH
  20. NO_CACHE
  21. )
  22. # If we didn't find NVCC, then try the default paths.
  23. find_program(_CUDA_NVCC_EXECUTABLE
  24. NAMES nvcc nvcc.exe
  25. PATH_SUFFIXES bin
  26. NO_CACHE
  27. )
  28. # If the user specified CUDAToolkit_ROOT but nvcc could not be found, this is an error.
  29. if(NOT _CUDA_NVCC_EXECUTABLE AND (DEFINED CUDAToolkit_ROOT OR DEFINED ENV{CUDAToolkit_ROOT}))
  30. set(fail_base "Could not find nvcc executable in path specified by")
  31. if(DEFINED CUDAToolkit_ROOT)
  32. message(FATAL_ERROR "${fail_base} CUDAToolkit_ROOT=${CUDAToolkit_ROOT}")
  33. elseif(DEFINED ENV{CUDAToolkit_ROOT})
  34. message(FATAL_ERROR "${fail_base} environment variable CUDAToolkit_ROOT=$ENV{CUDAToolkit_ROOT}")
  35. endif()
  36. endif()
  37. # CUDAToolkit_ROOT cmake/env variable not specified, try platform defaults.
  38. #
  39. # - Linux: /usr/local/cuda-X.Y
  40. # - macOS: /Developer/NVIDIA/CUDA-X.Y
  41. # - Windows: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vX.Y
  42. #
  43. # We will also search the default symlink location /usr/local/cuda first since
  44. # if CUDAToolkit_ROOT is not specified, it is assumed that the symlinked
  45. # directory is the desired location.
  46. if(NOT _CUDA_NVCC_EXECUTABLE)
  47. if(UNIX)
  48. if(NOT APPLE)
  49. set(platform_base "/usr/local/cuda-")
  50. else()
  51. set(platform_base "/Developer/NVIDIA/CUDA-")
  52. endif()
  53. else()
  54. set(platform_base "C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v")
  55. endif()
  56. # Build out a descending list of possible cuda installations, e.g.
  57. file(GLOB possible_paths "${platform_base}*")
  58. # Iterate the glob results and create a descending list.
  59. set(versions)
  60. foreach(p ${possible_paths})
  61. # Extract version number from end of string
  62. string(REGEX MATCH "[0-9][0-9]?\\.[0-9]$" p_version ${p})
  63. if(IS_DIRECTORY ${p} AND p_version)
  64. list(APPEND versions ${p_version})
  65. endif()
  66. endforeach()
  67. # Sort numerically in descending order, so we try the newest versions first.
  68. list(SORT versions COMPARE NATURAL ORDER DESCENDING)
  69. # With a descending list of versions, populate possible paths to search.
  70. set(search_paths)
  71. foreach(v ${versions})
  72. list(APPEND search_paths "${platform_base}${v}")
  73. endforeach()
  74. # Force the global default /usr/local/cuda to the front on Unix.
  75. if(UNIX)
  76. list(INSERT search_paths 0 "/usr/local/cuda")
  77. endif()
  78. # Now search for nvcc again using the platform default search paths.
  79. find_program(_CUDA_NVCC_EXECUTABLE
  80. NAMES nvcc nvcc.exe
  81. PATHS ${search_paths}
  82. PATH_SUFFIXES bin
  83. NO_CACHE
  84. )
  85. # We are done with these variables now, cleanup.
  86. unset(platform_base)
  87. unset(possible_paths)
  88. unset(versions)
  89. unset(search_paths)
  90. if(NOT _CUDA_NVCC_EXECUTABLE)
  91. message(FATAL_ERROR "Failed to find nvcc.\nCompiler ${CMAKE_${lang}_COMPILER_ID} requires the CUDA toolkit. Please set the CUDAToolkit_ROOT variable.")
  92. endif()
  93. endif()
  94. endif()
  95. # Given that NVCC can be provided by multiple different sources (NVIDIA HPC SDK, CUDA Toolkit, distro)
  96. # each of which has a different layout, we need to extract the CUDA toolkit root from the compiler
  97. # itself, allowing us to support numerous different scattered toolkit layouts
  98. execute_process(COMMAND ${_CUDA_NVCC_EXECUTABLE} "-v" "__cmake_determine_cuda"
  99. OUTPUT_VARIABLE _CUDA_NVCC_OUT ERROR_VARIABLE _CUDA_NVCC_OUT)
  100. if(_CUDA_NVCC_OUT MATCHES "\\#\\$ TOP=([^\r\n]*)")
  101. get_filename_component(${lang_var_}TOOLKIT_ROOT "${CMAKE_MATCH_1}" ABSOLUTE)
  102. else()
  103. get_filename_component(${lang_var_}TOOLKIT_ROOT "${_CUDA_NVCC_EXECUTABLE}" DIRECTORY)
  104. get_filename_component(${lang_var_}TOOLKIT_ROOT "${${lang_var_}TOOLKIT_ROOT}" DIRECTORY)
  105. endif()
  106. if(_CUDA_NVCC_OUT MATCHES "\\#\\$ NVVMIR_LIBRARY_DIR=([^\r\n]*)")
  107. get_filename_component(_CUDA_NVVMIR_LIBRARY_DIR "${CMAKE_MATCH_1}" ABSOLUTE)
  108. #We require the path to end in `/nvvm/libdevice'
  109. if(_CUDA_NVVMIR_LIBRARY_DIR MATCHES "nvvm/libdevice$")
  110. get_filename_component(_CUDA_NVVMIR_LIBRARY_DIR "${_CUDA_NVVMIR_LIBRARY_DIR}/../.." ABSOLUTE)
  111. set(_CUDA_COMPILER_LIBRARY_ROOT_FROM_NVVMIR_LIBRARY_DIR "${_CUDA_NVVMIR_LIBRARY_DIR}")
  112. endif()
  113. unset(_CUDA_NVVMIR_LIBRARY_DIR)
  114. unset(_cuda_nvvmir_dir_name)
  115. endif()
  116. unset(_CUDA_NVCC_OUT)
  117. # In a non-scattered installation the following are equivalent to ${lang_var_}TOOLKIT_ROOT.
  118. # We first check for a non-scattered installation to prefer it over a scattered installation.
  119. # ${lang_var_}LIBRARY_ROOT contains the device library.
  120. if(DEFINED _CUDA_COMPILER_LIBRARY_ROOT_FROM_NVVMIR_LIBRARY_DIR)
  121. set(${lang_var_}LIBRARY_ROOT "${_CUDA_COMPILER_LIBRARY_ROOT_FROM_NVVMIR_LIBRARY_DIR}")
  122. elseif(EXISTS "${${lang_var_}TOOLKIT_ROOT}/nvvm/libdevice")
  123. set(${lang_var_}LIBRARY_ROOT "${${lang_var_}TOOLKIT_ROOT}")
  124. elseif(CMAKE_SYSROOT_LINK AND EXISTS "${CMAKE_SYSROOT_LINK}/usr/lib/cuda/nvvm/libdevice")
  125. set(${lang_var_}LIBRARY_ROOT "${CMAKE_SYSROOT_LINK}/usr/lib/cuda")
  126. elseif(EXISTS "${CMAKE_SYSROOT}/usr/lib/cuda/nvvm/libdevice")
  127. set(${lang_var_}LIBRARY_ROOT "${CMAKE_SYSROOT}/usr/lib/cuda")
  128. else()
  129. message(FATAL_ERROR "Couldn't find CUDA library root.")
  130. endif()
  131. unset(_CUDA_COMPILER_LIBRARY_ROOT_FROM_NVVMIR_LIBRARY_DIR)
  132. # ${lang_var_}TOOLKIT_LIBRARY_ROOT contains the linking stubs necessary for device linking and other low-level library files.
  133. if(CMAKE_SYSROOT_LINK AND EXISTS "${CMAKE_SYSROOT_LINK}/usr/lib/nvidia-cuda-toolkit/bin/crt/link.stub")
  134. set(${lang_var_}TOOLKIT_LIBRARY_ROOT "${CMAKE_SYSROOT_LINK}/usr/lib/nvidia-cuda-toolkit")
  135. elseif(EXISTS "${CMAKE_SYSROOT}/usr/lib/nvidia-cuda-toolkit/bin/crt/link.stub")
  136. set(${lang_var_}TOOLKIT_LIBRARY_ROOT "${CMAKE_SYSROOT}/usr/lib/nvidia-cuda-toolkit")
  137. else()
  138. set(${lang_var_}TOOLKIT_LIBRARY_ROOT "${${lang_var_}TOOLKIT_ROOT}")
  139. endif()
  140. # For regular nvcc we the toolkit version is the same as the compiler version and we can parse it from the vendor test output.
  141. # For Clang we need to invoke nvcc to get version output.
  142. if(CMAKE_${lang}_COMPILER_ID STREQUAL "Clang")
  143. execute_process(COMMAND ${_CUDA_NVCC_EXECUTABLE} "--version" OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT)
  144. endif()
  145. if(CMAKE_${lang}_COMPILER_ID_OUTPUT MATCHES [=[V([0-9]+\.[0-9]+\.[0-9]+)]=])
  146. set(${lang_var_}TOOLKIT_VERSION "${CMAKE_MATCH_1}")
  147. endif()
  148. # Don't leak variables unnecessarily to user code.
  149. unset(_CUDA_NVCC_EXECUTABLE)
  150. endmacro()