FindCUDAToolkit.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindCUDAToolkit
  5. ---------------
  6. This script locates the NVIDIA CUDA toolkit, but does not require the ``CUDA``
  7. language be enabled for a given project. This module does not search for the
  8. NVIDIA CUDA Samples or any specific CUDA libraries. This module only searches
  9. for the CUDA Toolkit root directory.
  10. Search Behavior
  11. ^^^^^^^^^^^^^^^
  12. Finding the CUDA Toolkit requires finding the ``nvcc`` executable, which is
  13. searched for in the following order:
  14. 1. If the ``CUDAToolkit_ROOT`` cmake configuration variable (e.g.,
  15. ``-DCUDAToolkit_ROOT=/some/path``) *or* environment variable is defined, it
  16. will be searched first. If both an environment variable **and** a
  17. configuration variable are specified, the *configuration* variable takes
  18. precedence.
  19. The directory specified here must be such that the executable ``nvcc`` can be
  20. found underneath the directory specified by ``CUDAToolkit_ROOT``. If
  21. ``CUDAToolkit_ROOT`` is specified, but no ``nvcc`` is found underneath, this
  22. package is marked as **not** found. No subsequent search attempts are
  23. performed.
  24. 2. The user's path is searched for ``nvcc`` using :command:`find_program`. If
  25. this is found, no subsequent search attempts are performed. Users are
  26. responsible for ensuring that the first ``nvcc`` to show up in the path is
  27. the desired path in the event that multiple CUDA Toolkits are installed.
  28. 3. On Unix systems, if the symbolic link ``/usr/local/cuda`` exists, this is
  29. used. No subsequent search attempts are performed. No default symbolic link
  30. location exists for the Windows platform.
  31. 4. The platform specific default install locations are searched. If exactly one
  32. candidate is found, this is used. The default CUDA Toolkit install locations
  33. searched are:
  34. +-------------+-------------------------------------------------------------+
  35. | Platform | Search Pattern |
  36. +=============+=============================================================+
  37. | macOS | ``/Developer/NVIDIA/CUDA-X.Y`` |
  38. +-------------+-------------------------------------------------------------+
  39. | Other Unix | ``/usr/local/cuda-X.Y`` |
  40. +-------------+-------------------------------------------------------------+
  41. | Windows | ``C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vX.Y`` |
  42. +-------------+-------------------------------------------------------------+
  43. Where ``X.Y`` would be a specific version of the CUDA Toolkit, such as
  44. ``/usr/local/cuda-9.0`` or
  45. ``C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0``
  46. .. note::
  47. When multiple CUDA Toolkits are installed in the default location of a
  48. system (e.g., both ``/usr/local/cuda-9.0`` and ``/usr/local/cuda-10.0``
  49. exist but the ``/usr/local/cuda`` symbolic link does **not** exist), this
  50. package is marked as **not** found.
  51. There are too many factors involved in making an automatic decision in
  52. the presence of multiple CUDA Toolkits being installed. In this
  53. situation, users are encouraged to either (1) set ``CUDAToolkit_ROOT`` or
  54. (2) ensure that the correct ``nvcc`` executable shows up in ``$PATH`` for
  55. :command:`find_program` to find.
  56. Options
  57. ^^^^^^^
  58. ``VERSION``
  59. If specified, describes the version of the CUDA Toolkit to search for.
  60. ``REQUIRED``
  61. If specified, configuration will error if a suitable CUDA Toolkit is not
  62. found.
  63. ``QUIET``
  64. If specified, the search for a suitable CUDA Toolkit will not produce any
  65. messages.
  66. ``EXACT``
  67. If specified, the CUDA Toolkit is considered found only if the exact
  68. ``VERSION`` specified is recovered.
  69. Output Variables
  70. ^^^^^^^^^^^^^^^^
  71. ``CUDAToolkit_FOUND``
  72. A boolean specifying whether or not the CUDA Toolkit was found.
  73. ``CUDAToolkit_VERSION``
  74. The exact version of the CUDA Toolkit found (as reported by
  75. ``nvcc --version``).
  76. ``CUDAToolkit_ROOT_DIR``
  77. The root directory of the CUDA Toolkit found. Note that this variable will
  78. be the same as ``CUDAToolkit_ROOT`` when specified *and* a suitable toolkit was
  79. found.
  80. ``CUDAToolkit_INCLUDE_DIRS``
  81. The path to the CUDA Toolkit ``include`` folder containing the header files
  82. required to compile a project linking against CUDA.
  83. ``CUDAToolkit_LIBRARY_DIR``
  84. The path to the CUDA Toolkit library directory that contains the CUDA
  85. Runtime library ``cudart``.
  86. ``CUDAToolkit_NVCC_EXECUTABLE``
  87. The path to the NVIDIA CUDA compiler ``nvcc``. Note that this path may not
  88. **not** be the same as
  89. :variable:`CMAKE_CUDA_COMPILER <CMAKE_<LANG>_COMPILER>`. ``nvcc`` must be
  90. found to determine the CUDA Toolkit version as well as determining other
  91. features of the Toolkit. This variable is set for the convenience of
  92. modules that depend on this one.
  93. #]=======================================================================]
  94. # NOTE: much of this was simply extracted from FindCUDA.cmake.
  95. # James Bigler, NVIDIA Corp (nvidia.com - jbigler)
  96. # Abe Stephens, SCI Institute -- http://www.sci.utah.edu/~abe/FindCuda.html
  97. #
  98. # Copyright (c) 2008 - 2009 NVIDIA Corporation. All rights reserved.
  99. #
  100. # Copyright (c) 2007-2009
  101. # Scientific Computing and Imaging Institute, University of Utah
  102. #
  103. # This code is licensed under the MIT License. See the FindCUDA.cmake script
  104. # for the text of the license.
  105. # The MIT License
  106. #
  107. # License for the specific language governing rights and limitations under
  108. # Permission is hereby granted, free of charge, to any person obtaining a
  109. # copy of this software and associated documentation files (the "Software"),
  110. # to deal in the Software without restriction, including without limitation
  111. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  112. # and/or sell copies of the Software, and to permit persons to whom the
  113. # Software is furnished to do so, subject to the following conditions:
  114. #
  115. # The above copyright notice and this permission notice shall be included
  116. # in all copies or substantial portions of the Software.
  117. #
  118. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  119. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  120. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  121. # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  122. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  123. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  124. # DEALINGS IN THE SOFTWARE.
  125. #
  126. ###############################################################################
  127. # Attempt 1: try user provided paths first.
  128. find_path(CUDAToolkit_ROOT_DIR
  129. NAMES nvcc nvcc.exe
  130. PATHS
  131. ${CUDAToolkit_ROOT}
  132. ENV CUDAToolkit_ROOT
  133. PATH_SUFFIXES bin bin64
  134. NO_DEFAULT_PATH
  135. )
  136. message(FATAL_ERROR "CUDAToolkit_ROOT_DIR: ${CUDAToolkit_ROOT_DIR}")
  137. # If the user specified CUDAToolkit_ROOT but nvcc could not be found, this is an error.
  138. if (NOT CUDAToolkit_ROOT_DIR AND (DEFINED CUDAToolkit_ROOT OR DEFINED ENV{CUDAToolkit_ROOT}))
  139. # Declare error messages now, print later depending on find_package args.
  140. set(fail_base "Could not find nvcc executable in path specified by")
  141. set(cuda_root_fail "${fail_base} CUDAToolkit_ROOT=${CUDAToolkit_ROOT}")
  142. set(env_cuda_root_fail "${fail_base} environment variable CUDAToolkit_ROOT=$ENV{CUDAToolkit_ROOT}")
  143. if (CUDAToolkit_FIND_REQUIRED)
  144. if (DEFINED CUDAToolkit_ROOT)
  145. message(FATAL_ERROR ${cuda_root_fail})
  146. elseif (DEFINED ENV{CUDAToolkit_ROOT})
  147. message(FATAL_ERROR ${env_cuda_root_fail})
  148. endif()
  149. else()
  150. if (NOT CUDAToolkit_FIND_QUIETLY)
  151. if (DEFINED CUDAToolkit_ROOT)
  152. message(STATUS ${cuda_root_fail})
  153. elseif (DEFINED ENV{CUDAToolkit_ROOT})
  154. message(STATUS ${env_cuda_root_fail})
  155. endif()
  156. endif()
  157. set(CUDAToolkit_FOUND FALSE)
  158. unset(fail_base)
  159. unset(cuda_root_fail)
  160. unset(env_cuda_root_fail)
  161. return()
  162. endif()
  163. endif()
  164. # CUDAToolkit_ROOT cmake / env variable not specified, try platform defaults.
  165. #
  166. # - Linux: /usr/local/cuda-X.Y
  167. # - macOS: /Developer/NVIDIA/CUDA-X.Y
  168. # - Windows: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vX.Y
  169. #
  170. # We will also search the default symlink location /usr/local/cuda first since
  171. # if CUDAToolkit_ROOT is not specified, it is assumed that the symlinked
  172. # directory is the desired location.
  173. if (NOT CUDAToolkit_ROOT_DIR)
  174. if (UNIX)
  175. if (NOT APPLE)
  176. set(platform_base "/usr/local/cuda-")
  177. else()
  178. set(platform_base "/Developer/NVIDIA/CUDA-")
  179. endif()
  180. else()
  181. set(platform_base "C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v")
  182. endif()
  183. # Build out a descending list of possible cuda installations, e.g.
  184. file(GLOB possible_paths "${platform_base}*")
  185. # Iterate the glob results and create a descending list.
  186. set(possible_versions)
  187. foreach (p ${possible_paths})
  188. # Extract version number from end of string
  189. string(REGEX MATCH "[0-9][0-9]?\\.[0-9]$" p_version ${p})
  190. if (IS_DIRECTORY ${p} AND p_version)
  191. list(APPEND possible_versions ${p_version})
  192. endif()
  193. endforeach()
  194. # Cannot use list(SORT) because that is alphabetical, we need numerical.
  195. # NOTE: this is not an efficient sorting strategy. But even if a user had
  196. # every possible version of CUDA installed, this wouldn't create any
  197. # significant overhead.
  198. set(versions)
  199. foreach (v ${possible_versions})
  200. list(LENGTH versions num_versions)
  201. # First version, nothing to compare with so just append.
  202. if (num_versions EQUAL 0)
  203. list(APPEND versions ${v})
  204. else()
  205. # Loop through list. Insert at an index when comparison is
  206. # VERSION_GREATER since we want a descending list. Duplicates will not
  207. # happen since this came from a glob list of directories.
  208. set(i 0)
  209. set(early_terminate FALSE)
  210. while (i LESS num_versions)
  211. list(GET versions ${i} curr)
  212. if (v VERSION_GREATER curr)
  213. list(INSERT versions ${i} ${v})
  214. set(early_terminate TRUE)
  215. break()
  216. endif()
  217. math(EXPR i "${i} + 1")
  218. endwhile()
  219. # If it did not get inserted, place it at the end.
  220. if (NOT early_terminate)
  221. list(APPEND versions ${v})
  222. endif()
  223. endif()
  224. endforeach()
  225. # With a descending list of versions, populate possible paths to search.
  226. set(search_paths)
  227. foreach (v ${versions})
  228. list(APPEND search_paths "${platform_base}${v}")
  229. endforeach()
  230. # Force the global default /usr/local/cuda to the front on Unix.
  231. if (UNIX)
  232. list(INSERT search_paths 0 "/usr/local/cuda")
  233. endif()
  234. # Now search for nvcc again using the platform default search paths.
  235. find_path(CUDAToolkit_ROOT_DIR
  236. NAMES nvcc nvcc.exe
  237. PATHS ${search_paths}
  238. PATH_SUFFIXES bin bin64
  239. NO_DEFAULT_PATH
  240. )
  241. # We are done with these variables now, cleanup for caller.
  242. unset(platform_base)
  243. unset(possible_paths)
  244. unset(possible_versions)
  245. unset(versions)
  246. unset(i)
  247. unset(early_terminate)
  248. unset(search_paths)
  249. if (NOT CUDAToolkit_ROOT_DIR)
  250. if (CUDAToolkit_FIND_REQUIRED)
  251. message(FATAL_ERROR "Could not find nvcc, please set CUDAToolkit_ROOT.")
  252. elseif(NOT CUDAToolkit_FIND_QUIETLY)
  253. message(STATUS "Could not find nvcc, please set CUDAToolkit_ROOT.")
  254. endif()
  255. set(CUDAToolkit_FOUND FALSE)
  256. return()
  257. endif()
  258. endif()
  259. # TODO: why does FindCUDA.cmake go through effor tof `cuda_find_host_program`
  260. # when CMAKE_CROSSCOMPILING // is that still relevant?
  261. # https://gitlab.kitware.com/cmake/cmake/issues/16509
  262. # NOTE: search before trimming bin / bin64 from CUDAToolkit_ROOT_DIR
  263. find_program(CUDAToolkit_NVCC_EXECUTABLE
  264. NAMES nvcc nvcc.exe
  265. PATHS ${CUDAToolkit_ROOT_DIR}
  266. NO_DEFAULT_PATH
  267. )
  268. # Compute the version.
  269. execute_process(
  270. COMMAND ${CUDAToolkit_NVCC_EXECUTABLE} "--version"
  271. OUTPUT_VARIABLE NVCC_OUT
  272. )
  273. string(
  274. REGEX REPLACE ".*release ([0-9]+)\\.([0-9]+).*" "\\1"
  275. CUDAToolkit_VERSION_MAJOR ${NVCC_OUT}
  276. )
  277. string(
  278. REGEX REPLACE ".*release ([0-9]+)\\.([0-9]+).*" "\\2"
  279. CUDAToolkit_VERSION_MINOR ${NVCC_OUT}
  280. )
  281. set(
  282. CUDAToolkit_VERSION "${CUDAToolkit_VERSION_MAJOR}.${CUDAToolkit_VERSION_MINOR}"
  283. CACHE STRING "Version of CUDA as computed from nvcc."
  284. )
  285. unset(NVCC_OUT)
  286. # CUDAToolkit_ROOT_DIR should have the path to the bin or bin64 folder from the
  287. # find_path calls above. Failure to find nvcc should have had an early return.
  288. # So now we need to remove bin / bin64, as well as reset the cache entry that
  289. # find_path creates.
  290. string(REGEX REPLACE "[/\\\\]?bin[64]*[/\\\\]?$" "" CUDAToolkit_ROOT_DIR ${CUDAToolkit_ROOT_DIR})
  291. set(CUDAToolkit_ROOT_DIR ${CUDAToolkit_ROOT_DIR} CACHE PATH "Toolkit location." FORCE)
  292. # Now that we have the real ROOT_DIR, find the include/ directory
  293. find_path(CUDAToolkit_INCLUDE_DIRS
  294. cuda_runtime.h
  295. # TODO: FindCUDA.cmake has special TARGET_DIR for cross compiling, is that needed?
  296. PATHS ${CUDAToolkit_ROOT_DIR}
  297. PATH_SUFFIXES include
  298. NO_DEFAULT_PATH
  299. )
  300. # And find the CUDA Runtime Library libcudart
  301. find_library(libcudart
  302. cudart
  303. PATHS ${CUDAToolkit_ROOT_DIR}
  304. PATH_SUFFIXES lib lib64
  305. NO_DEFAULT_PATH
  306. )
  307. if (libcudart)
  308. get_filename_component(CUDAToolkit_LIBRARY_DIR ${libcudart} DIRECTORY ABSOLUTE)
  309. else()
  310. if (NOT CUDAToolkit_FIND_QUIETLY)
  311. message(STATUS "Unable to find cudart library under ${CUDAToolkit_ROOT_DIR}/lib[64].")
  312. endif()
  313. set(CUDAToolkit_LIBRARY_DIR CUDAToolkit_LIBRARY_DIR-NOTFOUND)
  314. endif()
  315. unset(libcudart)
  316. # Perform version comparison and validate all required variables are set.
  317. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  318. find_package_handle_standard_args(CUDAToolkit
  319. REQUIRED_VARS
  320. CUDAToolkit_ROOT_DIR
  321. CUDAToolkit_INCLUDE_DIRS
  322. CUDAToolkit_LIBRARY_DIR
  323. CUDAToolkit_NVCC_EXECUTABLE
  324. VERSION_VAR
  325. CUDAToolkit_VERSION
  326. )