FindCUDAToolkit.cmake 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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:`NPP<cuda_toolkit_NPP>`
  84. - :ref:`nvBLAS<cuda_toolkit_nvBLAS>`
  85. - :ref:`nvGRAPH<cuda_toolkit_nvGRAPH>`
  86. - :ref:`nvJPEG<cuda_toolkit_nvJPEG>`
  87. - :ref:`nvidia-ML<cuda_toolkit_nvML>`
  88. - :ref:`nvRTC<cuda_toolkit_nvRTC>`
  89. - :ref:`nvToolsExt<cuda_toolkit_nvToolsExt>`
  90. - :ref:`OpenCL<cuda_toolkit_opencl>`
  91. - :ref:`cuLIBOS<cuda_toolkit_cuLIBOS>`
  92. .. _`cuda_toolkit_rt_lib`:
  93. CUDA Runtime Library
  94. """"""""""""""""""""
  95. The CUDA Runtime library (cudart) are what most applications will typically
  96. need to link against to make any calls such as `cudaMalloc`, and `cudaFree`.
  97. Targets Created:
  98. - ``CUDA::cudart``
  99. - ``CUDA::cudart_static``
  100. .. _`cuda_toolkit_driver_lib`:
  101. CUDA Driver Library
  102. """"""""""""""""""""
  103. The CUDA Driver library (cuda) are used by applications that use calls
  104. such as `cuMemAlloc`, and `cuMemFree`. This is generally used by advanced
  105. Targets Created:
  106. - ``CUDA::cuda_driver``
  107. - ``CUDA::cuda_driver``
  108. .. _`cuda_toolkit_cuBLAS`:
  109. cuBLAS
  110. """"""
  111. The `cuBLAS <https://docs.nvidia.com/cuda/cublas/index.html>`_ library.
  112. Targets Created:
  113. - ``CUDA::cublas``
  114. - ``CUDA::cublas_static``
  115. .. _`cuda_toolkit_cuFFT`:
  116. cuFFT
  117. """""
  118. The `cuFFT <https://docs.nvidia.com/cuda/cufft/index.html>`_ library.
  119. Targets Created:
  120. - ``CUDA::cufft``
  121. - ``CUDA::cufftw``
  122. - ``CUDA::cufft_static``
  123. - ``CUDA::cufftw_static``
  124. cuRAND
  125. """"""
  126. The `cuRAND <https://docs.nvidia.com/cuda/curand/index.html>`_ library.
  127. Targets Created:
  128. - ``CUDA::curand``
  129. - ``CUDA::curand_static``
  130. .. _`cuda_toolkit_cuSOLVER`:
  131. cuSOLVER
  132. """"""""
  133. The `cuSOLVER <https://docs.nvidia.com/cuda/cusolver/index.html>`_ library.
  134. Targets Created:
  135. - ``CUDA::cusolver``
  136. - ``CUDA::cusolver_static``
  137. .. _`cuda_toolkit_cuSPARSE`:
  138. cuSPARSE
  139. """"""""
  140. The `cuSPARSE <https://docs.nvidia.com/cuda/cusparse/index.html>`_ library.
  141. Targets Created:
  142. - ``CUDA::cusparse``
  143. - ``CUDA::cusparse_static``
  144. .. _`cuda_toolkit_NPP`:
  145. NPP
  146. """
  147. The `NPP <https://docs.nvidia.com/cuda/npp/index.html>`_ libraries.
  148. Targets Created:
  149. - `nppc`:
  150. - ``CUDA::nppc``
  151. - ``CUDA::nppc_static``
  152. - `nppial`: Arithmetic and logical operation functions in `nppi_arithmetic_and_logical_operations.h`
  153. - ``CUDA::nppial``
  154. - ``CUDA::nppial_static``
  155. - `nppicc`: Color conversion and sampling functions in `nppi_color_conversion.h`
  156. - ``CUDA::nppicc``
  157. - ``CUDA::nppicc_static``
  158. - `nppicom`: JPEG compression and decompression functions in `nppi_compression_functions.h`
  159. - ``CUDA::nppicom``
  160. - ``CUDA::nppicom_static``
  161. - `nppidei`: Data exchange and initialization functions in `nppi_data_exchange_and_initialization.h`
  162. - ``CUDA::nppidei``
  163. - ``CUDA::nppidei_static``
  164. - `nppif`: Filtering and computer vision functions in `nppi_filter_functions.h`
  165. - ``CUDA::nppif``
  166. - ``CUDA::nppif_static``
  167. - `nppig`: Geometry transformation functions found in `nppi_geometry_transforms.h`
  168. - ``CUDA::nppig``
  169. - ``CUDA::nppig_static``
  170. - `nppim`: Morphological operation functions found in `nppi_morphological_operations.h`
  171. - ``CUDA::nppim``
  172. - ``CUDA::nppim_static``
  173. - `nppist`: Statistics and linear transform in `nppi_statistics_functions.h` and `nppi_linear_transforms.h`
  174. - ``CUDA::nppist``
  175. - ``CUDA::nppist_static``
  176. - `nppisu`: Memory support functions in `nppi_support_functions.h`
  177. - ``CUDA::nppisu``
  178. - ``CUDA::nppisu_static``
  179. - `nppitc`: Threshold and compare operation functions in `nppi_threshold_and_compare_operations.h`
  180. - ``CUDA::nppitc``
  181. - ``CUDA::nppitc_static``
  182. - `npps`:
  183. - ``CUDA::npps``
  184. - ``CUDA::npps_static``
  185. .. _`cuda_toolkit_nvBLAS`:
  186. nvBLAS
  187. """"""
  188. The `nvBLAS <https://docs.nvidia.com/cuda/nvblas/index.html>`_ libraries.
  189. This is a shared library only.
  190. Targets Created:
  191. - ``CUDA::nvblas``
  192. .. _`cuda_toolkit_nvGRAPH`:
  193. nvGRAPH
  194. """""""
  195. The `nvGRAPH <https://docs.nvidia.com/cuda/nvgraph/index.html>`_ library.
  196. Targets Created:
  197. - ``CUDA::nvgraph``
  198. - ``CUDA::nvgraph_static``
  199. .. _`cuda_toolkit_nvJPEG`:
  200. nvJPEG
  201. """"""
  202. The `nvJPEG <https://docs.nvidia.com/cuda/nvjpeg/index.html>`_ library.
  203. Introduced in CUDA 10.
  204. Targets Created:
  205. - ``CUDA::nvjpeg``
  206. - ``CUDA::nvjpeg_static``
  207. .. _`cuda_toolkit_nvRTC`:
  208. nvRTC
  209. """""
  210. The `nvRTC <https://docs.nvidia.com/cuda/nvrtc/index.html>`_ (Runtime Compilation) library.
  211. This is a shared library only.
  212. Targets Created:
  213. - ``CUDA::nvrtc``
  214. .. _`cuda_toolkit_nvml`:
  215. nvidia-ML
  216. """""""""
  217. The `NVIDIA Management Library <https://developer.nvidia.com/nvidia-management-library-nvml>`_.
  218. This is a shared library only.
  219. Targets Created:
  220. - ``CUDA::nvml``
  221. .. _`cuda_toolkit_opencl`:
  222. .. _`cuda_toolkit_nvToolsExt`:
  223. nvToolsExt
  224. """"""""""
  225. The `NVIDIA Tools Extension <https://docs.nvidia.com/gameworks/content/gameworkslibrary/nvtx/nvidia_tools_extension_library_nvtx.htm>`_.
  226. This is a shared library only.
  227. Targets Created:
  228. - ``CUDA::nvToolsExt``
  229. OpenCL
  230. """"""
  231. The `NVIDIA OpenCL Library <https://developer.nvidia.com/opencl>`_.
  232. This is a shared library only.
  233. Targets Created:
  234. - ``CUDA::OpenCL``
  235. .. _`cuda_toolkit_cuLIBOS`:
  236. cuLIBOS
  237. """""""
  238. The cuLIBOS library is a backend thread abstraction layer library which is
  239. static only. The ``CUDA::cublas_static``, ``CUDA::cusparse_static``,
  240. ``CUDA::cufft_static``, ``CUDA::curand_static``, and (when implemented) NPP
  241. libraries all automatically have this dependency linked.
  242. Target Created:
  243. - ``CUDA::culibos``
  244. **Note**: direct usage of this target by consumers should not be necessary.
  245. .. _`cuda_toolkit_cuRAND`:
  246. Result variables
  247. ^^^^^^^^^^^^^^^^
  248. ``CUDAToolkit_FOUND``
  249. A boolean specifying whether or not the CUDA Toolkit was found.
  250. ``CUDAToolkit_VERSION``
  251. The exact version of the CUDA Toolkit found (as reported by
  252. ``nvcc --version``).
  253. ``CUDAToolkit_VERSION_MAJOR``
  254. The major version of the CUDA Toolkit.
  255. ``CUDAToolkit_VERSION_MAJOR``
  256. The minor version of the CUDA Toolkit.
  257. ``CUDAToolkit_VERSION_PATCH``
  258. The patch version of the CUDA Toolkit.
  259. ``CUDAToolkit_BIN_DIR``
  260. The path to the CUDA Toolkit library directory that contains the CUDA
  261. executable ``nvcc``.
  262. ``CUDAToolkit_INCLUDE_DIRS``
  263. The path to the CUDA Toolkit ``include`` folder containing the header files
  264. required to compile a project linking against CUDA.
  265. ``CUDAToolkit_LIBRARY_DIR``
  266. The path to the CUDA Toolkit library directory that contains the CUDA
  267. Runtime library ``cudart``.
  268. ``CUDAToolkit_TARGET_DIR``
  269. The path to the CUDA Toolkit directory including the target architecture
  270. when cross-compiling. When not cross-compiling this will be equivalant to
  271. ``CUDAToolkit_ROOT_DIR``.
  272. ``CUDAToolkit_NVCC_EXECUTABLE``
  273. The path to the NVIDIA CUDA compiler ``nvcc``. Note that this path may
  274. **not** be the same as
  275. :variable:`CMAKE_CUDA_COMPILER <CMAKE_<LANG>_COMPILER>`. ``nvcc`` must be
  276. found to determine the CUDA Toolkit version as well as determining other
  277. features of the Toolkit. This variable is set for the convenience of
  278. modules that depend on this one.
  279. #]=======================================================================]
  280. # NOTE: much of this was simply extracted from FindCUDA.cmake.
  281. # James Bigler, NVIDIA Corp (nvidia.com - jbigler)
  282. # Abe Stephens, SCI Institute -- http://www.sci.utah.edu/~abe/FindCuda.html
  283. #
  284. # Copyright (c) 2008 - 2009 NVIDIA Corporation. All rights reserved.
  285. #
  286. # Copyright (c) 2007-2009
  287. # Scientific Computing and Imaging Institute, University of Utah
  288. #
  289. # This code is licensed under the MIT License. See the FindCUDA.cmake script
  290. # for the text of the license.
  291. # The MIT License
  292. #
  293. # License for the specific language governing rights and limitations under
  294. # Permission is hereby granted, free of charge, to any person obtaining a
  295. # copy of this software and associated documentation files (the "Software"),
  296. # to deal in the Software without restriction, including without limitation
  297. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  298. # and/or sell copies of the Software, and to permit persons to whom the
  299. # Software is furnished to do so, subject to the following conditions:
  300. #
  301. # The above copyright notice and this permission notice shall be included
  302. # in all copies or substantial portions of the Software.
  303. #
  304. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  305. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  306. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  307. # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  308. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  309. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  310. # DEALINGS IN THE SOFTWARE.
  311. #
  312. ###############################################################################
  313. if(CMAKE_CUDA_COMPILER_LOADED AND NOT CUDAToolkit_BIN_DIR)
  314. get_filename_component(cuda_dir "${CMAKE_CUDA_COMPILER}" DIRECTORY)
  315. # use the already detected cuda compiler
  316. set(CUDAToolkit_BIN_DIR "${cuda_dir}" CACHE PATH "")
  317. unset(cuda_dir)
  318. endif()
  319. # Try language- or user-provided path first.
  320. if(CUDAToolkit_BIN_DIR)
  321. find_program(CUDAToolkit_NVCC_EXECUTABLE
  322. NAMES nvcc nvcc.exe
  323. PATHS ${CUDAToolkit_BIN_DIR}
  324. NO_DEFAULT_PATH
  325. )
  326. endif()
  327. # Search using CUDAToolkit_ROOT
  328. find_program(CUDAToolkit_NVCC_EXECUTABLE
  329. NAMES nvcc nvcc.exe
  330. PATHS ENV CUDA_PATH
  331. PATH_SUFFIXES bin
  332. )
  333. # If the user specified CUDAToolkit_ROOT but nvcc could not be found, this is an error.
  334. if (NOT CUDAToolkit_NVCC_EXECUTABLE AND (DEFINED CUDAToolkit_ROOT OR DEFINED ENV{CUDAToolkit_ROOT}))
  335. # Declare error messages now, print later depending on find_package args.
  336. set(fail_base "Could not find nvcc executable in path specified by")
  337. set(cuda_root_fail "${fail_base} CUDAToolkit_ROOT=${CUDAToolkit_ROOT}")
  338. set(env_cuda_root_fail "${fail_base} environment variable CUDAToolkit_ROOT=$ENV{CUDAToolkit_ROOT}")
  339. if (CUDAToolkit_FIND_REQUIRED)
  340. if (DEFINED CUDAToolkit_ROOT)
  341. message(FATAL_ERROR ${cuda_root_fail})
  342. elseif (DEFINED ENV{CUDAToolkit_ROOT})
  343. message(FATAL_ERROR ${env_cuda_root_fail})
  344. endif()
  345. else()
  346. if (NOT CUDAToolkit_FIND_QUIETLY)
  347. if (DEFINED CUDAToolkit_ROOT)
  348. message(STATUS ${cuda_root_fail})
  349. elseif (DEFINED ENV{CUDAToolkit_ROOT})
  350. message(STATUS ${env_cuda_root_fail})
  351. endif()
  352. endif()
  353. set(CUDAToolkit_FOUND FALSE)
  354. unset(fail_base)
  355. unset(cuda_root_fail)
  356. unset(env_cuda_root_fail)
  357. return()
  358. endif()
  359. endif()
  360. # CUDAToolkit_ROOT cmake / env variable not specified, try platform defaults.
  361. #
  362. # - Linux: /usr/local/cuda-X.Y
  363. # - macOS: /Developer/NVIDIA/CUDA-X.Y
  364. # - Windows: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vX.Y
  365. #
  366. # We will also search the default symlink location /usr/local/cuda first since
  367. # if CUDAToolkit_ROOT is not specified, it is assumed that the symlinked
  368. # directory is the desired location.
  369. if (NOT CUDAToolkit_NVCC_EXECUTABLE)
  370. if (UNIX)
  371. if (NOT APPLE)
  372. set(platform_base "/usr/local/cuda-")
  373. else()
  374. set(platform_base "/Developer/NVIDIA/CUDA-")
  375. endif()
  376. else()
  377. set(platform_base "C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v")
  378. endif()
  379. # Build out a descending list of possible cuda installations, e.g.
  380. file(GLOB possible_paths "${platform_base}*")
  381. # Iterate the glob results and create a descending list.
  382. set(possible_versions)
  383. foreach (p ${possible_paths})
  384. # Extract version number from end of string
  385. string(REGEX MATCH "[0-9][0-9]?\\.[0-9]$" p_version ${p})
  386. if (IS_DIRECTORY ${p} AND p_version)
  387. list(APPEND possible_versions ${p_version})
  388. endif()
  389. endforeach()
  390. # Cannot use list(SORT) because that is alphabetical, we need numerical.
  391. # NOTE: this is not an efficient sorting strategy. But even if a user had
  392. # every possible version of CUDA installed, this wouldn't create any
  393. # significant overhead.
  394. set(versions)
  395. foreach (v ${possible_versions})
  396. list(LENGTH versions num_versions)
  397. # First version, nothing to compare with so just append.
  398. if (num_versions EQUAL 0)
  399. list(APPEND versions ${v})
  400. else()
  401. # Loop through list. Insert at an index when comparison is
  402. # VERSION_GREATER since we want a descending list. Duplicates will not
  403. # happen since this came from a glob list of directories.
  404. set(i 0)
  405. set(early_terminate FALSE)
  406. while (i LESS num_versions)
  407. list(GET versions ${i} curr)
  408. if (v VERSION_GREATER curr)
  409. list(INSERT versions ${i} ${v})
  410. set(early_terminate TRUE)
  411. break()
  412. endif()
  413. math(EXPR i "${i} + 1")
  414. endwhile()
  415. # If it did not get inserted, place it at the end.
  416. if (NOT early_terminate)
  417. list(APPEND versions ${v})
  418. endif()
  419. endif()
  420. endforeach()
  421. # With a descending list of versions, populate possible paths to search.
  422. set(search_paths)
  423. foreach (v ${versions})
  424. list(APPEND search_paths "${platform_base}${v}")
  425. endforeach()
  426. # Force the global default /usr/local/cuda to the front on Unix.
  427. if (UNIX)
  428. list(INSERT search_paths 0 "/usr/local/cuda")
  429. endif()
  430. # Now search for nvcc again using the platform default search paths.
  431. find_program(CUDAToolkit_NVCC_EXECUTABLE
  432. NAMES nvcc nvcc.exe
  433. PATHS ${search_paths}
  434. PATH_SUFFIXES bin
  435. )
  436. # We are done with these variables now, cleanup for caller.
  437. unset(platform_base)
  438. unset(possible_paths)
  439. unset(possible_versions)
  440. unset(versions)
  441. unset(i)
  442. unset(early_terminate)
  443. unset(search_paths)
  444. if (NOT CUDAToolkit_NVCC_EXECUTABLE)
  445. if (CUDAToolkit_FIND_REQUIRED)
  446. message(FATAL_ERROR "Could not find nvcc, please set CUDAToolkit_ROOT.")
  447. elseif(NOT CUDAToolkit_FIND_QUIETLY)
  448. message(STATUS "Could not find nvcc, please set CUDAToolkit_ROOT.")
  449. endif()
  450. set(CUDAToolkit_FOUND FALSE)
  451. return()
  452. endif()
  453. endif()
  454. if(NOT CUDAToolkit_BIN_DIR AND CUDAToolkit_NVCC_EXECUTABLE)
  455. get_filename_component(cuda_dir "${CUDAToolkit_NVCC_EXECUTABLE}" DIRECTORY)
  456. set(CUDAToolkit_BIN_DIR "${cuda_dir}" CACHE PATH "" FORCE)
  457. unset(cuda_dir)
  458. endif()
  459. if(CUDAToolkit_NVCC_EXECUTABLE AND
  460. CUDAToolkit_NVCC_EXECUTABLE STREQUAL CMAKE_CUDA_COMPILER)
  461. # Need to set these based off the already computed CMAKE_CUDA_COMPILER_VERSION value
  462. # This if statement will always match, but is used to provide variables for MATCH 1,2,3...
  463. if(CMAKE_CUDA_COMPILER_VERSION MATCHES [=[([0-9]+)\.([0-9]+)\.([0-9]+)]=])
  464. set(CUDAToolkit_VERSION_MAJOR "${CMAKE_MATCH_1}")
  465. set(CUDAToolkit_VERSION_MINOR "${CMAKE_MATCH_2}")
  466. set(CUDAToolkit_VERSION_PATCH "${CMAKE_MATCH_3}")
  467. set(CUDAToolkit_VERSION "${CMAKE_CUDA_COMPILER_VERSION}")
  468. endif()
  469. else()
  470. # Compute the version by invoking nvcc
  471. execute_process (COMMAND ${CUDAToolkit_NVCC_EXECUTABLE} "--version" OUTPUT_VARIABLE NVCC_OUT)
  472. if(NVCC_OUT MATCHES [=[ V([0-9]+)\.([0-9]+)\.([0-9]+)]=])
  473. set(CUDAToolkit_VERSION_MAJOR "${CMAKE_MATCH_1}")
  474. set(CUDAToolkit_VERSION_MINOR "${CMAKE_MATCH_2}")
  475. set(CUDAToolkit_VERSION_PATCH "${CMAKE_MATCH_3}")
  476. set(CUDAToolkit_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}")
  477. endif()
  478. unset(NVCC_OUT)
  479. endif()
  480. get_filename_component(CUDAToolkit_ROOT_DIR ${CUDAToolkit_BIN_DIR} DIRECTORY ABSOLUTE)
  481. # Handle cross compilation
  482. if(CMAKE_CROSSCOMPILING)
  483. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "armv7-a")
  484. # Support for NVPACK
  485. set (CUDAToolkit_TARGET_NAME "armv7-linux-androideabi")
  486. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm")
  487. # Support for arm cross compilation
  488. set(CUDAToolkit_TARGET_NAME "armv7-linux-gnueabihf")
  489. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
  490. # Support for aarch64 cross compilation
  491. if (ANDROID_ARCH_NAME STREQUAL "arm64")
  492. set(CUDAToolkit_TARGET_NAME "aarch64-linux-androideabi")
  493. else()
  494. set(CUDAToolkit_TARGET_NAME "aarch64-linux")
  495. endif (ANDROID_ARCH_NAME STREQUAL "arm64")
  496. elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
  497. set(CUDAToolkit_TARGET_NAME "x86_64-linux")
  498. endif()
  499. if (EXISTS "${CUDAToolkit_ROOT_DIR}/targets/${CUDAToolkit_TARGET_NAME}")
  500. set(CUDAToolkit_TARGET_DIR "${CUDAToolkit_ROOT_DIR}/targets/${CUDAToolkit_TARGET_NAME}")
  501. # add known CUDA target root path to the set of directories we search for programs, libraries and headers
  502. list(APPEND CMAKE_FIND_ROOT_PATH "${CUDAToolkit_TARGET_DIR}")
  503. endif()
  504. else()
  505. # Not cross compiling
  506. set(CUDAToolkit_TARGET_DIR "${CUDAToolkit_ROOT_DIR}")
  507. # Now that we have the real ROOT_DIR, find components inside it.
  508. list(APPEND CMAKE_PREFIX_PATH ${CUDAToolkit_ROOT_DIR})
  509. endif()
  510. # Find the include/ directory
  511. find_path(CUDAToolkit_INCLUDE_DIR
  512. NAMES cuda_runtime.h
  513. )
  514. # And find the CUDA Runtime Library libcudart
  515. find_library(CUDA_CUDART
  516. NAMES cudart
  517. PATH_SUFFIXES lib64 lib64/stubs lib/x64
  518. )
  519. if (NOT CUDA_CUDART AND NOT CUDAToolkit_FIND_QUIETLY)
  520. message(STATUS "Unable to find cudart library.")
  521. endif()
  522. unset(CUDAToolkit_ROOT_DIR)
  523. if(CMAKE_CROSSCOMPILING)
  524. if(CUDAToolkit_TARGET_DIR)
  525. list(REMOVE_AT CMAKE_FIND_ROOT_PATH -1)
  526. endif()
  527. else()
  528. list(REMOVE_AT CMAKE_PREFIX_PATH -1)
  529. endif()
  530. #-----------------------------------------------------------------------------
  531. # Perform version comparison and validate all required variables are set.
  532. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  533. find_package_handle_standard_args(CUDAToolkit
  534. REQUIRED_VARS
  535. CUDAToolkit_INCLUDE_DIR
  536. CUDA_CUDART
  537. CUDAToolkit_NVCC_EXECUTABLE
  538. VERSION_VAR
  539. CUDAToolkit_VERSION
  540. )
  541. #-----------------------------------------------------------------------------
  542. # Construct result variables
  543. if(CUDAToolkit_FOUND)
  544. set(CUDAToolkit_INCLUDE_DIRS ${CUDAToolkit_INCLUDE_DIR})
  545. get_filename_component(CUDAToolkit_LIBRARY_DIR ${CUDA_CUDART} DIRECTORY ABSOLUTE)
  546. endif()
  547. #-----------------------------------------------------------------------------
  548. # Construct import targets
  549. if(CUDAToolkit_FOUND)
  550. function(find_and_add_cuda_import_lib lib_name)
  551. if(ARGC GREATER 1)
  552. set(search_names ${ARGN})
  553. else()
  554. set(search_names ${lib_name})
  555. endif()
  556. find_library(CUDA_${lib_name}_LIBRARY
  557. NAMES ${search_names}
  558. PATHS ${CUDAToolkit_LIBRARY_DIR}
  559. ENV CUDA_PATH
  560. PATH_SUFFIXES nvidia/current lib64 lib64/stubs lib/x64 lib lib/stubs
  561. )
  562. if (NOT CUDA::${lib_name} AND CUDA_${lib_name}_LIBRARY)
  563. add_library(CUDA::${lib_name} IMPORTED INTERFACE)
  564. target_include_directories(CUDA::${lib_name} SYSTEM INTERFACE "${CUDAToolkit_INCLUDE_DIRS}")
  565. target_link_libraries(CUDA::${lib_name} INTERFACE "${CUDA_${lib_name}_LIBRARY}")
  566. endif()
  567. endfunction()
  568. function(add_cuda_link_dependency lib_name)
  569. if(TARGET CUDA::${lib_name})
  570. foreach(dependency IN LISTS ARGN)
  571. if(TARGET CUDA::${dependency})
  572. target_link_libraries(CUDA::${lib_name} INTERFACE CUDA::${dependency})
  573. endif()
  574. endforeach()
  575. endif()
  576. endfunction()
  577. add_library(CUDA::toolkit IMPORTED INTERFACE)
  578. target_include_directories(CUDA::toolkit SYSTEM INTERFACE "${CUDAToolkit_INCLUDE_DIRS}")
  579. target_link_directories(CUDA::toolkit INTERFACE "${CUDAToolkit_LIBRARY_DIR}")
  580. find_and_add_cuda_import_lib(cuda_driver cuda)
  581. find_and_add_cuda_import_lib(cudart)
  582. find_and_add_cuda_import_lib(cudart_static)
  583. foreach (cuda_lib cublas cufft cufftw curand cusolver cusparse nvgraph nvjpeg)
  584. find_and_add_cuda_import_lib(${cuda_lib})
  585. find_and_add_cuda_import_lib(${cuda_lib}_static)
  586. endforeach()
  587. # cuSOLVER depends on cuBLAS, and cuSPARSE
  588. add_cuda_link_dependency(cusolver cublas cusparse)
  589. add_cuda_link_dependency(cusolver_static cublas_static cusparse)
  590. # nvGRAPH depends on cuRAND, and cuSOLVER.
  591. add_cuda_link_dependency(nvgraph curand cusolver)
  592. add_cuda_link_dependency(nvgraph_static curand_static cusolver_static)
  593. find_and_add_cuda_import_lib(nppc)
  594. find_and_add_cuda_import_lib(nppc_static)
  595. # Process the majority of the NPP libraries.
  596. foreach (cuda_lib nppial nppicc nppidei nppif nppig nppim nppist nppitc npps nppicom nppisu)
  597. find_and_add_cuda_import_lib(${cuda_lib})
  598. find_and_add_cuda_import_lib(${cuda_lib}_static)
  599. add_cuda_link_dependency(${cuda_lib} nppc)
  600. add_cuda_link_dependency(${cuda_lib}_static nppc_static)
  601. endforeach()
  602. find_and_add_cuda_import_lib(nvrtc)
  603. add_cuda_link_dependency(nvrtc cuda_driver)
  604. find_and_add_cuda_import_lib(nvml nvidia-ml nvml)
  605. if(WIN32)
  606. # nvtools can be installed outside the CUDA toolkit directory
  607. # so prefer the NVTOOLSEXT_PATH windows only environment variable
  608. # In addition on windows the most common name is nvToolsExt64_1
  609. find_library(CUDA_nvToolsExt_LIBRARY
  610. NAMES nvToolsExt64_1 nvToolsExt64 nvToolsExt
  611. PATHS ENV NVTOOLSEXT_PATH
  612. ENV CUDA_PATH
  613. PATH_SUFFIXES lib/x64 lib
  614. )
  615. endif()
  616. find_and_add_cuda_import_lib(nvToolsExt nvToolsExt nvToolsExt64)
  617. find_and_add_cuda_import_lib(OpenCL)
  618. find_and_add_cuda_import_lib(culibos)
  619. if(TARGET CUDA::culibos)
  620. foreach (cuda_lib cublas cufft cusparse curand nppc nvjpeg)
  621. add_cuda_link_dependency(${cuda_lib}_static culibos)
  622. endforeach()
  623. endif()
  624. endif()