FindCUDAToolkit.cmake 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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 and the associated libraries, but
  7. does not require the ``CUDA`` language be enabled for a given project. This
  8. module does not search for the NVIDIA CUDA Samples.
  9. Search Behavior
  10. ^^^^^^^^^^^^^^^
  11. Finding the CUDA Toolkit requires finding the ``nvcc`` executable, which is
  12. searched for in the following order:
  13. 1. If the ``CUDA`` language has been enabled we will use the directory
  14. containing the compiler as the first search location for ``nvcc``.
  15. 2. If the ``CUDAToolkit_ROOT`` cmake configuration variable (e.g.,
  16. ``-DCUDAToolkit_ROOT=/some/path``) *or* environment variable is defined, it
  17. will be searched. If both an environment variable **and** a
  18. configuration variable are specified, the *configuration* variable takes
  19. precedence.
  20. The directory specified here must be such that the executable ``nvcc`` can be
  21. found underneath the directory specified by ``CUDAToolkit_ROOT``. If
  22. ``CUDAToolkit_ROOT`` is specified, but no ``nvcc`` is found underneath, this
  23. package is marked as **not** found. No subsequent search attempts are
  24. performed.
  25. 3. If the CUDA_PATH environment variable is defined, it will be searched.
  26. 4. The user's path is searched for ``nvcc`` using :command:`find_program`. If
  27. this is found, no subsequent search attempts are performed. Users are
  28. responsible for ensuring that the first ``nvcc`` to show up in the path is
  29. the desired path in the event that multiple CUDA Toolkits are installed.
  30. 5. On Unix systems, if the symbolic link ``/usr/local/cuda`` exists, this is
  31. used. No subsequent search attempts are performed. No default symbolic link
  32. location exists for the Windows platform.
  33. 6. The platform specific default install locations are searched. If exactly one
  34. candidate is found, this is used. The default CUDA Toolkit install locations
  35. searched are:
  36. +-------------+-------------------------------------------------------------+
  37. | Platform | Search Pattern |
  38. +=============+=============================================================+
  39. | macOS | ``/Developer/NVIDIA/CUDA-X.Y`` |
  40. +-------------+-------------------------------------------------------------+
  41. | Other Unix | ``/usr/local/cuda-X.Y`` |
  42. +-------------+-------------------------------------------------------------+
  43. | Windows | ``C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vX.Y`` |
  44. +-------------+-------------------------------------------------------------+
  45. Where ``X.Y`` would be a specific version of the CUDA Toolkit, such as
  46. ``/usr/local/cuda-9.0`` or
  47. ``C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0``
  48. .. note::
  49. When multiple CUDA Toolkits are installed in the default location of a
  50. system (e.g., both ``/usr/local/cuda-9.0`` and ``/usr/local/cuda-10.0``
  51. exist but the ``/usr/local/cuda`` symbolic link does **not** exist), this
  52. package is marked as **not** found.
  53. There are too many factors involved in making an automatic decision in
  54. the presence of multiple CUDA Toolkits being installed. In this
  55. situation, users are encouraged to either (1) set ``CUDAToolkit_ROOT`` or
  56. (2) ensure that the correct ``nvcc`` executable shows up in ``$PATH`` for
  57. :command:`find_program` to find.
  58. Options
  59. ^^^^^^^
  60. ``VERSION``
  61. If specified, describes the version of the CUDA Toolkit to search for.
  62. ``REQUIRED``
  63. If specified, configuration will error if a suitable CUDA Toolkit is not
  64. found.
  65. ``QUIET``
  66. If specified, the search for a suitable CUDA Toolkit will not produce any
  67. messages.
  68. ``EXACT``
  69. If specified, the CUDA Toolkit is considered found only if the exact
  70. ``VERSION`` specified is recovered.
  71. Imported targets
  72. ^^^^^^^^^^^^^^^^
  73. An :ref:`imported target <Imported targets>` named ``CUDA::toolkit`` is provided.
  74. This module defines :prop_tgt:`IMPORTED` targets for each
  75. of the following libraries that are part of the CUDAToolkit:
  76. - :ref:`CUDA Runtime Library<cuda_toolkit_rt_lib>`
  77. - :ref:`CUDA Driver Library<cuda_toolkit_driver_lib>`
  78. - :ref:`cuBLAS<cuda_toolkit_cuBLAS>`
  79. - :ref:`cuFFT<cuda_toolkit_cuFFT>`
  80. - :ref:`cuRAND<cuda_toolkit_cuRAND>`
  81. - :ref:`cuSOLVER<cuda_toolkit_cuSOLVER>`
  82. - :ref:`cuSPARSE<cuda_toolkit_cuSPARSE>`
  83. - :ref:`cuPTI<cuda_toolkit_cupti>`
  84. - :ref:`NPP<cuda_toolkit_NPP>`
  85. - :ref:`nvBLAS<cuda_toolkit_nvBLAS>`
  86. - :ref:`nvGRAPH<cuda_toolkit_nvGRAPH>`
  87. - :ref:`nvJPEG<cuda_toolkit_nvJPEG>`
  88. - :ref:`nvidia-ML<cuda_toolkit_nvML>`
  89. - :ref:`nvRTC<cuda_toolkit_nvRTC>`
  90. - :ref:`nvToolsExt<cuda_toolkit_nvToolsExt>`
  91. - :ref:`OpenCL<cuda_toolkit_opencl>`
  92. - :ref:`cuLIBOS<cuda_toolkit_cuLIBOS>`
  93. .. _`cuda_toolkit_rt_lib`:
  94. CUDA Runtime Library
  95. """"""""""""""""""""
  96. The CUDA Runtime library (cudart) are what most applications will typically
  97. need to link against to make any calls such as `cudaMalloc`, and `cudaFree`.
  98. Targets Created:
  99. - ``CUDA::cudart``
  100. - ``CUDA::cudart_static``
  101. .. _`cuda_toolkit_driver_lib`:
  102. CUDA Driver Library
  103. """"""""""""""""""""
  104. The CUDA Driver library (cuda) are used by applications that use calls
  105. such as `cuMemAlloc`, and `cuMemFree`. This is generally used by advanced
  106. Targets Created:
  107. - ``CUDA::cuda_driver``
  108. - ``CUDA::cuda_driver``
  109. .. _`cuda_toolkit_cuBLAS`:
  110. cuBLAS
  111. """"""
  112. The `cuBLAS <https://docs.nvidia.com/cuda/cublas/index.html>`_ library.
  113. Targets Created:
  114. - ``CUDA::cublas``
  115. - ``CUDA::cublas_static``
  116. .. _`cuda_toolkit_cuFFT`:
  117. cuFFT
  118. """""
  119. The `cuFFT <https://docs.nvidia.com/cuda/cufft/index.html>`_ library.
  120. Targets Created:
  121. - ``CUDA::cufft``
  122. - ``CUDA::cufftw``
  123. - ``CUDA::cufft_static``
  124. - ``CUDA::cufftw_static``
  125. cuRAND
  126. """"""
  127. The `cuRAND <https://docs.nvidia.com/cuda/curand/index.html>`_ library.
  128. Targets Created:
  129. - ``CUDA::curand``
  130. - ``CUDA::curand_static``
  131. .. _`cuda_toolkit_cuSOLVER`:
  132. cuSOLVER
  133. """"""""
  134. The `cuSOLVER <https://docs.nvidia.com/cuda/cusolver/index.html>`_ library.
  135. Targets Created:
  136. - ``CUDA::cusolver``
  137. - ``CUDA::cusolver_static``
  138. .. _`cuda_toolkit_cuSPARSE`:
  139. cuSPARSE
  140. """"""""
  141. The `cuSPARSE <https://docs.nvidia.com/cuda/cusparse/index.html>`_ library.
  142. Targets Created:
  143. - ``CUDA::cusparse``
  144. - ``CUDA::cusparse_static``
  145. .. _`cuda_toolkit_cupti`:
  146. cupti
  147. """""
  148. The `NVIDIA CUDA Profiling Tools Interface <https://developer.nvidia.com/CUPTI>`_.
  149. Targets Created:
  150. - ``CUDA::cupti``
  151. - ``CUDA::cupti_static``
  152. .. _`cuda_toolkit_NPP`:
  153. NPP
  154. """
  155. The `NPP <https://docs.nvidia.com/cuda/npp/index.html>`_ libraries.
  156. Targets Created:
  157. - `nppc`:
  158. - ``CUDA::nppc``
  159. - ``CUDA::nppc_static``
  160. - `nppial`: Arithmetic and logical operation functions in `nppi_arithmetic_and_logical_operations.h`
  161. - ``CUDA::nppial``
  162. - ``CUDA::nppial_static``
  163. - `nppicc`: Color conversion and sampling functions in `nppi_color_conversion.h`
  164. - ``CUDA::nppicc``
  165. - ``CUDA::nppicc_static``
  166. - `nppicom`: JPEG compression and decompression functions in `nppi_compression_functions.h`
  167. - ``CUDA::nppicom``
  168. - ``CUDA::nppicom_static``
  169. - `nppidei`: Data exchange and initialization functions in `nppi_data_exchange_and_initialization.h`
  170. - ``CUDA::nppidei``
  171. - ``CUDA::nppidei_static``
  172. - `nppif`: Filtering and computer vision functions in `nppi_filter_functions.h`
  173. - ``CUDA::nppif``
  174. - ``CUDA::nppif_static``
  175. - `nppig`: Geometry transformation functions found in `nppi_geometry_transforms.h`
  176. - ``CUDA::nppig``
  177. - ``CUDA::nppig_static``
  178. - `nppim`: Morphological operation functions found in `nppi_morphological_operations.h`
  179. - ``CUDA::nppim``
  180. - ``CUDA::nppim_static``
  181. - `nppist`: Statistics and linear transform in `nppi_statistics_functions.h` and `nppi_linear_transforms.h`
  182. - ``CUDA::nppist``
  183. - ``CUDA::nppist_static``
  184. - `nppisu`: Memory support functions in `nppi_support_functions.h`
  185. - ``CUDA::nppisu``
  186. - ``CUDA::nppisu_static``
  187. - `nppitc`: Threshold and compare operation functions in `nppi_threshold_and_compare_operations.h`
  188. - ``CUDA::nppitc``
  189. - ``CUDA::nppitc_static``
  190. - `npps`:
  191. - ``CUDA::npps``
  192. - ``CUDA::npps_static``
  193. .. _`cuda_toolkit_nvBLAS`:
  194. nvBLAS
  195. """"""
  196. The `nvBLAS <https://docs.nvidia.com/cuda/nvblas/index.html>`_ libraries.
  197. This is a shared library only.
  198. Targets Created:
  199. - ``CUDA::nvblas``
  200. .. _`cuda_toolkit_nvGRAPH`:
  201. nvGRAPH
  202. """""""
  203. The `nvGRAPH <https://docs.nvidia.com/cuda/nvgraph/index.html>`_ library.
  204. Targets Created:
  205. - ``CUDA::nvgraph``
  206. - ``CUDA::nvgraph_static``
  207. .. _`cuda_toolkit_nvJPEG`:
  208. nvJPEG
  209. """"""
  210. The `nvJPEG <https://docs.nvidia.com/cuda/nvjpeg/index.html>`_ library.
  211. Introduced in CUDA 10.
  212. Targets Created:
  213. - ``CUDA::nvjpeg``
  214. - ``CUDA::nvjpeg_static``
  215. .. _`cuda_toolkit_nvRTC`:
  216. nvRTC
  217. """""
  218. The `nvRTC <https://docs.nvidia.com/cuda/nvrtc/index.html>`_ (Runtime Compilation) library.
  219. This is a shared library only.
  220. Targets Created:
  221. - ``CUDA::nvrtc``
  222. .. _`cuda_toolkit_nvml`:
  223. nvidia-ML
  224. """""""""
  225. The `NVIDIA Management Library <https://developer.nvidia.com/nvidia-management-library-nvml>`_.
  226. This is a shared library only.
  227. Targets Created:
  228. - ``CUDA::nvml``
  229. .. _`cuda_toolkit_nvToolsExt`:
  230. nvToolsExt
  231. """"""""""
  232. The `NVIDIA Tools Extension <https://docs.nvidia.com/gameworks/content/gameworkslibrary/nvtx/nvidia_tools_extension_library_nvtx.htm>`_.
  233. This is a shared library only.
  234. Targets Created:
  235. - ``CUDA::nvToolsExt``
  236. .. _`cuda_toolkit_opencl`:
  237. OpenCL
  238. """"""
  239. The `NVIDIA OpenCL Library <https://developer.nvidia.com/opencl>`_.
  240. This is a shared library only.
  241. Targets Created:
  242. - ``CUDA::OpenCL``
  243. .. _`cuda_toolkit_cuLIBOS`:
  244. cuLIBOS
  245. """""""
  246. The cuLIBOS library is a backend thread abstraction layer library which is
  247. static only. The ``CUDA::cublas_static``, ``CUDA::cusparse_static``,
  248. ``CUDA::cufft_static``, ``CUDA::curand_static``, and (when implemented) NPP
  249. libraries all automatically have this dependency linked.
  250. Target Created:
  251. - ``CUDA::culibos``
  252. **Note**: direct usage of this target by consumers should not be necessary.
  253. .. _`cuda_toolkit_cuRAND`:
  254. Result variables
  255. ^^^^^^^^^^^^^^^^
  256. ``CUDAToolkit_FOUND``
  257. A boolean specifying whether or not the CUDA Toolkit was found.
  258. ``CUDAToolkit_VERSION``
  259. The exact version of the CUDA Toolkit found (as reported by
  260. ``nvcc --version``).
  261. ``CUDAToolkit_VERSION_MAJOR``
  262. The major version of the CUDA Toolkit.
  263. ``CUDAToolkit_VERSION_MAJOR``
  264. The minor version of the CUDA Toolkit.
  265. ``CUDAToolkit_VERSION_PATCH``
  266. The patch version of the CUDA Toolkit.
  267. ``CUDAToolkit_BIN_DIR``
  268. The path to the CUDA Toolkit library directory that contains the CUDA
  269. executable ``nvcc``.
  270. ``CUDAToolkit_INCLUDE_DIRS``
  271. The path to the CUDA Toolkit ``include`` folder containing the header files
  272. required to compile a project linking against CUDA.
  273. ``CUDAToolkit_LIBRARY_DIR``
  274. The path to the CUDA Toolkit library directory that contains the CUDA
  275. Runtime library ``cudart``.
  276. ``CUDAToolkit_LIBRARY_ROOT``
  277. The path to the CUDA Toolkit directory containing the nvvm directory and
  278. version.txt.
  279. ``CUDAToolkit_TARGET_DIR``
  280. The path to the CUDA Toolkit directory including the target architecture
  281. when cross-compiling. When not cross-compiling this will be equivalant to
  282. ``CUDAToolkit_ROOT_DIR``.
  283. ``CUDAToolkit_NVCC_EXECUTABLE``
  284. The path to the NVIDIA CUDA compiler ``nvcc``. Note that this path may
  285. **not** be the same as
  286. :variable:`CMAKE_CUDA_COMPILER <CMAKE_<LANG>_COMPILER>`. ``nvcc`` must be
  287. found to determine the CUDA Toolkit version as well as determining other
  288. features of the Toolkit. This variable is set for the convenience of
  289. modules that depend on this one.
  290. #]=======================================================================]
  291. # NOTE: much of this was simply extracted from FindCUDA.cmake.
  292. # James Bigler, NVIDIA Corp (nvidia.com - jbigler)
  293. # Abe Stephens, SCI Institute -- http://www.sci.utah.edu/~abe/FindCuda.html
  294. #
  295. # Copyright (c) 2008 - 2009 NVIDIA Corporation. All rights reserved.
  296. #
  297. # Copyright (c) 2007-2009
  298. # Scientific Computing and Imaging Institute, University of Utah
  299. #
  300. # This code is licensed under the MIT License. See the FindCUDA.cmake script
  301. # for the text of the license.
  302. # The MIT License
  303. #
  304. # License for the specific language governing rights and limitations under
  305. # Permission is hereby granted, free of charge, to any person obtaining a
  306. # copy of this software and associated documentation files (the "Software"),
  307. # to deal in the Software without restriction, including without limitation
  308. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  309. # and/or sell copies of the Software, and to permit persons to whom the
  310. # Software is furnished to do so, subject to the following conditions:
  311. #
  312. # The above copyright notice and this permission notice shall be included
  313. # in all copies or substantial portions of the Software.
  314. #
  315. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  316. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  317. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  318. # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  319. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  320. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  321. # DEALINGS IN THE SOFTWARE.
  322. #
  323. ###############################################################################
  324. # On Clang the toolkit is found during compiler detection and stored in CMakeCUDACompiler.cmake as
  325. # CMAKE_CUDA_COMPILER_TOOLKIT_ROOT and CMAKE_CUDA_COMPILER_LIBRARY_ROOT.
  326. # We compute the rest based on those here to avoid re-searching and to avoid finding a possibly
  327. # different installation.
  328. if(CMAKE_CUDA_COMPILER_TOOLKIT_ROOT)
  329. set(CUDAToolkit_ROOT_DIR "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}")
  330. set(CUDAToolkit_LIBRARY_ROOT "${CMAKE_CUDA_COMPILER_LIBRARY_ROOT}")
  331. set(CUDAToolkit_BIN_DIR "${CUDAToolkit_ROOT_DIR}/bin")
  332. set(CUDAToolkit_NVCC_EXECUTABLE "${CUDAToolkit_BIN_DIR}/nvcc${CMAKE_EXECUTABLE_SUFFIX}")
  333. else()
  334. # For NVCC we can easily deduce the SDK binary directory from the compiler path.
  335. if(CMAKE_CUDA_COMPILER_LOADED AND NOT CUDAToolkit_BIN_DIR AND CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
  336. get_filename_component(CUDAToolkit_BIN_DIR "${CMAKE_CUDA_COMPILER}" DIRECTORY)
  337. set(CUDAToolkit_BIN_DIR "${CUDAToolkit_BIN_DIR}" CACHE PATH "")
  338. mark_as_advanced(CUDAToolkit_BIN_DIR)
  339. endif()
  340. # Try language- or user-provided path first.
  341. if(CUDAToolkit_BIN_DIR)
  342. find_program(CUDAToolkit_NVCC_EXECUTABLE
  343. NAMES nvcc nvcc.exe
  344. PATHS ${CUDAToolkit_BIN_DIR}
  345. NO_DEFAULT_PATH
  346. )
  347. endif()
  348. # Search using CUDAToolkit_ROOT
  349. find_program(CUDAToolkit_NVCC_EXECUTABLE
  350. NAMES nvcc nvcc.exe
  351. PATHS ENV CUDA_PATH
  352. PATH_SUFFIXES bin
  353. )
  354. # If the user specified CUDAToolkit_ROOT but nvcc could not be found, this is an error.
  355. if(NOT CUDAToolkit_NVCC_EXECUTABLE AND (DEFINED CUDAToolkit_ROOT OR DEFINED ENV{CUDAToolkit_ROOT}))
  356. # Declare error messages now, print later depending on find_package args.
  357. set(fail_base "Could not find nvcc executable in path specified by")
  358. set(cuda_root_fail "${fail_base} CUDAToolkit_ROOT=${CUDAToolkit_ROOT}")
  359. set(env_cuda_root_fail "${fail_base} environment variable CUDAToolkit_ROOT=$ENV{CUDAToolkit_ROOT}")
  360. if(CUDAToolkit_FIND_REQUIRED)
  361. if(DEFINED CUDAToolkit_ROOT)
  362. message(FATAL_ERROR ${cuda_root_fail})
  363. elseif(DEFINED ENV{CUDAToolkit_ROOT})
  364. message(FATAL_ERROR ${env_cuda_root_fail})
  365. endif()
  366. else()
  367. if(NOT CUDAToolkit_FIND_QUIETLY)
  368. if(DEFINED CUDAToolkit_ROOT)
  369. message(STATUS ${cuda_root_fail})
  370. elseif(DEFINED ENV{CUDAToolkit_ROOT})
  371. message(STATUS ${env_cuda_root_fail})
  372. endif()
  373. endif()
  374. set(CUDAToolkit_FOUND FALSE)
  375. unset(fail_base)
  376. unset(cuda_root_fail)
  377. unset(env_cuda_root_fail)
  378. return()
  379. endif()
  380. endif()
  381. # CUDAToolkit_ROOT cmake / env variable not specified, try platform defaults.
  382. #
  383. # - Linux: /usr/local/cuda-X.Y
  384. # - macOS: /Developer/NVIDIA/CUDA-X.Y
  385. # - Windows: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vX.Y
  386. #
  387. # We will also search the default symlink location /usr/local/cuda first since
  388. # if CUDAToolkit_ROOT is not specified, it is assumed that the symlinked
  389. # directory is the desired location.
  390. if(NOT CUDAToolkit_NVCC_EXECUTABLE)
  391. if(UNIX)
  392. if(NOT APPLE)
  393. set(platform_base "/usr/local/cuda-")
  394. else()
  395. set(platform_base "/Developer/NVIDIA/CUDA-")
  396. endif()
  397. else()
  398. set(platform_base "C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v")
  399. endif()
  400. # Build out a descending list of possible cuda installations, e.g.
  401. file(GLOB possible_paths "${platform_base}*")
  402. # Iterate the glob results and create a descending list.
  403. set(versions)
  404. foreach(p ${possible_paths})
  405. # Extract version number from end of string
  406. string(REGEX MATCH "[0-9][0-9]?\\.[0-9]$" p_version ${p})
  407. if(IS_DIRECTORY ${p} AND p_version)
  408. list(APPEND versions ${p_version})
  409. endif()
  410. endforeach()
  411. # Sort numerically in descending order, so we try the newest versions first.
  412. list(SORT versions COMPARE NATURAL ORDER DESCENDING)
  413. # With a descending list of versions, populate possible paths to search.
  414. set(search_paths)
  415. foreach(v ${versions})
  416. list(APPEND search_paths "${platform_base}${v}")
  417. endforeach()
  418. # Force the global default /usr/local/cuda to the front on Unix.
  419. if(UNIX)
  420. list(INSERT search_paths 0 "/usr/local/cuda")
  421. endif()
  422. # Now search for nvcc again using the platform default search paths.
  423. find_program(CUDAToolkit_NVCC_EXECUTABLE
  424. NAMES nvcc nvcc.exe
  425. PATHS ${search_paths}
  426. PATH_SUFFIXES bin
  427. )
  428. # We are done with these variables now, cleanup for caller.
  429. unset(platform_base)
  430. unset(possible_paths)
  431. unset(versions)
  432. unset(search_paths)
  433. if(NOT CUDAToolkit_NVCC_EXECUTABLE)
  434. if(CUDAToolkit_FIND_REQUIRED)
  435. message(FATAL_ERROR "Could not find nvcc, please set CUDAToolkit_ROOT.")
  436. elseif(NOT CUDAToolkit_FIND_QUIETLY)
  437. message(STATUS "Could not find nvcc, please set CUDAToolkit_ROOT.")
  438. endif()
  439. set(CUDAToolkit_FOUND FALSE)
  440. return()
  441. endif()
  442. endif()
  443. if(NOT CUDAToolkit_BIN_DIR AND CUDAToolkit_NVCC_EXECUTABLE)
  444. get_filename_component(CUDAToolkit_BIN_DIR "${CUDAToolkit_NVCC_EXECUTABLE}" DIRECTORY)
  445. set(CUDAToolkit_BIN_DIR "${CUDAToolkit_BIN_DIR}" CACHE PATH "" FORCE)
  446. mark_as_advanced(CUDAToolkit_BIN_DIR)
  447. endif()
  448. get_filename_component(CUDAToolkit_ROOT_DIR ${CUDAToolkit_BIN_DIR} DIRECTORY ABSOLUTE)
  449. # CUDAToolkit_LIBRARY_ROOT contains the device library and version file.
  450. # In a non-scattered installation this is equivalent to CUDAToolkit_ROOT_DIR.
  451. # We first check for a non-scattered installation to prefer it over a scattered installation.
  452. if(EXISTS "${CUDAToolkit_ROOT_DIR}/version.txt")
  453. set(CUDAToolkit_LIBRARY_ROOT "${CUDAToolkit_ROOT_DIR}")
  454. elseif(CMAKE_SYSROOT_LINK AND EXISTS "${CMAKE_SYSROOT_LINK}/usr/lib/cuda/version.txt")
  455. set(CUDAToolkit_LIBRARY_ROOT "${CMAKE_SYSROOT_LINK}/usr/lib/cuda")
  456. elseif(EXISTS "${CMAKE_SYSROOT}/usr/lib/cuda/version.txt")
  457. set(CUDAToolkit_LIBRARY_ROOT "${CMAKE_SYSROOT}/usr/lib/cuda")
  458. endif()
  459. endif()
  460. # Handle cross compilation
  461. if(CMAKE_CROSSCOMPILING)
  462. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "armv7-a")
  463. # Support for NVPACK
  464. set(CUDAToolkit_TARGET_NAME "armv7-linux-androideabi")
  465. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm")
  466. # Support for arm cross compilation
  467. set(CUDAToolkit_TARGET_NAME "armv7-linux-gnueabihf")
  468. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
  469. # Support for aarch64 cross compilation
  470. if(ANDROID_ARCH_NAME STREQUAL "arm64")
  471. set(CUDAToolkit_TARGET_NAME "aarch64-linux-androideabi")
  472. else()
  473. set(CUDAToolkit_TARGET_NAME "aarch64-linux")
  474. endif(ANDROID_ARCH_NAME STREQUAL "arm64")
  475. elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
  476. set(CUDAToolkit_TARGET_NAME "x86_64-linux")
  477. endif()
  478. if(EXISTS "${CUDAToolkit_ROOT_DIR}/targets/${CUDAToolkit_TARGET_NAME}")
  479. set(CUDAToolkit_TARGET_DIR "${CUDAToolkit_ROOT_DIR}/targets/${CUDAToolkit_TARGET_NAME}")
  480. # add known CUDA target root path to the set of directories we search for programs, libraries and headers
  481. list(PREPEND CMAKE_FIND_ROOT_PATH "${CUDAToolkit_TARGET_DIR}")
  482. # Mark that we need to pop the root search path changes after we have
  483. # found all cuda libraries so that searches for our cross-compilation
  484. # libraries work when another cuda sdk is in CMAKE_PREFIX_PATH or
  485. # PATh
  486. set(_CUDAToolkit_Pop_ROOT_PATH True)
  487. endif()
  488. else()
  489. # Not cross compiling
  490. set(CUDAToolkit_TARGET_DIR "${CUDAToolkit_ROOT_DIR}")
  491. # Now that we have the real ROOT_DIR, find components inside it.
  492. list(APPEND CMAKE_PREFIX_PATH ${CUDAToolkit_ROOT_DIR})
  493. # Mark that we need to pop the prefix path changes after we have
  494. # found the cudart library.
  495. set(_CUDAToolkit_Pop_Prefix True)
  496. endif()
  497. # CUDAToolkit_TARGET_DIR always points to the directory containing the include directory.
  498. # On a scattered installation /usr, on a non-scattered something like /usr/local/cuda or /usr/local/cuda-10.2/targets/aarch64-linux.
  499. if(EXISTS "${CUDAToolkit_TARGET_DIR}/include/cuda_runtime.h")
  500. set(CUDAToolkit_INCLUDE_DIR "${CUDAToolkit_TARGET_DIR}/include")
  501. elseif(NOT CUDAToolkit_FIND_QUIETLY)
  502. message(STATUS "Unable to find cuda_runtime.h in \"${CUDAToolkit_TARGET_DIR}/include\" for CUDAToolkit_INCLUDE_DIR.")
  503. endif()
  504. if(CUDAToolkit_NVCC_EXECUTABLE AND
  505. CUDAToolkit_NVCC_EXECUTABLE STREQUAL CMAKE_CUDA_COMPILER)
  506. # Need to set these based off the already computed CMAKE_CUDA_COMPILER_VERSION value
  507. # This if statement will always match, but is used to provide variables for MATCH 1,2,3...
  508. if(CMAKE_CUDA_COMPILER_VERSION MATCHES [=[([0-9]+)\.([0-9]+)\.([0-9]+)]=])
  509. set(CUDAToolkit_VERSION_MAJOR "${CMAKE_MATCH_1}")
  510. set(CUDAToolkit_VERSION_MINOR "${CMAKE_MATCH_2}")
  511. set(CUDAToolkit_VERSION_PATCH "${CMAKE_MATCH_3}")
  512. set(CUDAToolkit_VERSION "${CMAKE_CUDA_COMPILER_VERSION}")
  513. endif()
  514. else()
  515. # Compute the version by invoking nvcc
  516. execute_process(COMMAND ${CUDAToolkit_NVCC_EXECUTABLE} "--version" OUTPUT_VARIABLE NVCC_OUT)
  517. if(NVCC_OUT MATCHES [=[ V([0-9]+)\.([0-9]+)\.([0-9]+)]=])
  518. set(CUDAToolkit_VERSION_MAJOR "${CMAKE_MATCH_1}")
  519. set(CUDAToolkit_VERSION_MINOR "${CMAKE_MATCH_2}")
  520. set(CUDAToolkit_VERSION_PATCH "${CMAKE_MATCH_3}")
  521. set(CUDAToolkit_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}")
  522. endif()
  523. unset(NVCC_OUT)
  524. endif()
  525. # Find the CUDA Runtime Library libcudart
  526. find_library(CUDA_CUDART
  527. NAMES cudart
  528. PATH_SUFFIXES lib64 lib/x64
  529. )
  530. find_library(CUDA_CUDART
  531. NAMES cudart
  532. PATH_SUFFIXES lib64/stubs lib/x64/stubs
  533. )
  534. if(NOT CUDA_CUDART AND NOT CUDAToolkit_FIND_QUIETLY)
  535. message(STATUS "Unable to find cudart library.")
  536. endif()
  537. unset(CUDAToolkit_ROOT_DIR)
  538. if(_CUDAToolkit_Pop_Prefix)
  539. list(REMOVE_AT CMAKE_PREFIX_PATH -1)
  540. unset(_CUDAToolkit_Pop_Prefix)
  541. endif()
  542. #-----------------------------------------------------------------------------
  543. # Perform version comparison and validate all required variables are set.
  544. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  545. find_package_handle_standard_args(CUDAToolkit
  546. REQUIRED_VARS
  547. CUDAToolkit_INCLUDE_DIR
  548. CUDA_CUDART
  549. CUDAToolkit_NVCC_EXECUTABLE
  550. VERSION_VAR
  551. CUDAToolkit_VERSION
  552. )
  553. mark_as_advanced(CUDA_CUDART
  554. CUDAToolkit_INCLUDE_DIR
  555. CUDAToolkit_NVCC_EXECUTABLE
  556. )
  557. #-----------------------------------------------------------------------------
  558. # Construct result variables
  559. if(CUDAToolkit_FOUND)
  560. set(CUDAToolkit_INCLUDE_DIRS ${CUDAToolkit_INCLUDE_DIR})
  561. get_filename_component(CUDAToolkit_LIBRARY_DIR ${CUDA_CUDART} DIRECTORY ABSOLUTE)
  562. endif()
  563. #-----------------------------------------------------------------------------
  564. # Construct import targets
  565. if(CUDAToolkit_FOUND)
  566. function(_CUDAToolkit_find_and_add_import_lib lib_name)
  567. cmake_parse_arguments(arg "" "" "ALT;DEPS;EXTRA_PATH_SUFFIXES" ${ARGN})
  568. set(search_names ${lib_name} ${arg_ALT})
  569. find_library(CUDA_${lib_name}_LIBRARY
  570. NAMES ${search_names}
  571. HINTS ${CUDAToolkit_LIBRARY_DIR}
  572. ENV CUDA_PATH
  573. PATH_SUFFIXES nvidia/current lib64 lib/x64 lib
  574. ${arg_EXTRA_PATH_SUFFIXES}
  575. )
  576. # Don't try any stub directories until we have exhausted all other
  577. # search locations.
  578. find_library(CUDA_${lib_name}_LIBRARY
  579. NAMES ${search_names}
  580. HINTS ${CUDAToolkit_LIBRARY_DIR}
  581. ENV CUDA_PATH
  582. PATH_SUFFIXES lib64/stubs lib/x64/stubs lib/stubs stubs
  583. )
  584. mark_as_advanced(CUDA_${lib_name}_LIBRARY)
  585. if (NOT TARGET CUDA::${lib_name} AND CUDA_${lib_name}_LIBRARY)
  586. add_library(CUDA::${lib_name} IMPORTED INTERFACE)
  587. target_include_directories(CUDA::${lib_name} SYSTEM INTERFACE "${CUDAToolkit_INCLUDE_DIRS}")
  588. target_link_libraries(CUDA::${lib_name} INTERFACE "${CUDA_${lib_name}_LIBRARY}")
  589. foreach(dep ${arg_DEPS})
  590. if(TARGET CUDA::${dep})
  591. target_link_libraries(CUDA::${lib_name} INTERFACE CUDA::${dep})
  592. endif()
  593. endforeach()
  594. endif()
  595. endfunction()
  596. if(NOT TARGET CUDA::toolkit)
  597. add_library(CUDA::toolkit IMPORTED INTERFACE)
  598. target_include_directories(CUDA::toolkit SYSTEM INTERFACE "${CUDAToolkit_INCLUDE_DIRS}")
  599. target_link_directories(CUDA::toolkit INTERFACE "${CUDAToolkit_LIBRARY_DIR}")
  600. endif()
  601. _CUDAToolkit_find_and_add_import_lib(cuda_driver ALT cuda)
  602. _CUDAToolkit_find_and_add_import_lib(cudart)
  603. _CUDAToolkit_find_and_add_import_lib(cudart_static)
  604. # setup dependencies that are required for cudart_static when building
  605. # on linux. These are generally only required when using the CUDA toolkit
  606. # when CUDA language is disabled
  607. if(NOT TARGET CUDA::cudart_static_deps
  608. AND TARGET CUDA::cudart_static)
  609. add_library(CUDA::cudart_static_deps IMPORTED INTERFACE)
  610. target_link_libraries(CUDA::cudart_static INTERFACE CUDA::cudart_static_deps)
  611. if(UNIX AND (CMAKE_C_COMPILER OR CMAKE_CXX_COMPILER))
  612. find_package(Threads REQUIRED)
  613. target_link_libraries(CUDA::cudart_static_deps INTERFACE Threads::Threads ${CMAKE_DL_LIBS})
  614. endif()
  615. if(UNIX AND NOT APPLE)
  616. # On Linux, you must link against librt when using the static cuda runtime.
  617. find_library(CUDAToolkit_rt_LIBRARY rt)
  618. mark_as_advanced(CUDAToolkit_rt_LIBRARY)
  619. if(NOT CUDAToolkit_rt_LIBRARY)
  620. message(WARNING "Could not find librt library, needed by CUDA::cudart_static")
  621. else()
  622. target_link_libraries(CUDA::cudart_static_deps INTERFACE ${CUDAToolkit_rt_LIBRARY})
  623. endif()
  624. endif()
  625. endif()
  626. _CUDAToolkit_find_and_add_import_lib(culibos) # it's a static library
  627. foreach (cuda_lib cublas cufft curand cusparse nppc nvjpeg)
  628. _CUDAToolkit_find_and_add_import_lib(${cuda_lib})
  629. _CUDAToolkit_find_and_add_import_lib(${cuda_lib}_static DEPS culibos)
  630. endforeach()
  631. # cuFFTW depends on cuFFT
  632. _CUDAToolkit_find_and_add_import_lib(cufftw DEPS cufft)
  633. _CUDAToolkit_find_and_add_import_lib(cufftw DEPS cufft_static)
  634. # cuSOLVER depends on cuBLAS, and cuSPARSE
  635. _CUDAToolkit_find_and_add_import_lib(cusolver DEPS cublas cusparse)
  636. _CUDAToolkit_find_and_add_import_lib(cusolver_static DEPS cublas_static cusparse_static culibos)
  637. # nvGRAPH depends on cuRAND, and cuSOLVER.
  638. _CUDAToolkit_find_and_add_import_lib(nvgraph DEPS curand cusolver)
  639. _CUDAToolkit_find_and_add_import_lib(nvgraph_static DEPS curand_static cusolver_static)
  640. # Process the majority of the NPP libraries.
  641. foreach (cuda_lib nppial nppicc nppidei nppif nppig nppim nppist nppitc npps nppicom nppisu)
  642. _CUDAToolkit_find_and_add_import_lib(${cuda_lib} DEPS nppc)
  643. _CUDAToolkit_find_and_add_import_lib(${cuda_lib}_static DEPS nppc_static)
  644. endforeach()
  645. _CUDAToolkit_find_and_add_import_lib(cupti
  646. EXTRA_PATH_SUFFIXES ../extras/CUPTI/lib64/
  647. ../extras/CUPTI/lib/)
  648. _CUDAToolkit_find_and_add_import_lib(cupti_static
  649. EXTRA_PATH_SUFFIXES ../extras/CUPTI/lib64/
  650. ../extras/CUPTI/lib/)
  651. _CUDAToolkit_find_and_add_import_lib(nvrtc DEPS cuda_driver)
  652. _CUDAToolkit_find_and_add_import_lib(nvml ALT nvidia-ml nvml)
  653. if(WIN32)
  654. # nvtools can be installed outside the CUDA toolkit directory
  655. # so prefer the NVTOOLSEXT_PATH windows only environment variable
  656. # In addition on windows the most common name is nvToolsExt64_1
  657. find_library(CUDA_nvToolsExt_LIBRARY
  658. NAMES nvToolsExt64_1 nvToolsExt64 nvToolsExt
  659. PATHS ENV NVTOOLSEXT_PATH
  660. ENV CUDA_PATH
  661. PATH_SUFFIXES lib/x64 lib
  662. )
  663. endif()
  664. _CUDAToolkit_find_and_add_import_lib(nvToolsExt ALT nvToolsExt64)
  665. _CUDAToolkit_find_and_add_import_lib(OpenCL)
  666. endif()
  667. if(_CUDAToolkit_Pop_ROOT_PATH)
  668. list(REMOVE_AT CMAKE_FIND_ROOT_PATH 0)
  669. unset(_CUDAToolkit_Pop_ROOT_PATH)
  670. endif()