FindCUDAToolkit.cmake 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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_TARGET_DIR``
  277. The path to the CUDA Toolkit directory including the target architecture
  278. when cross-compiling. When not cross-compiling this will be equivalant to
  279. ``CUDAToolkit_ROOT_DIR``.
  280. ``CUDAToolkit_NVCC_EXECUTABLE``
  281. The path to the NVIDIA CUDA compiler ``nvcc``. Note that this path may
  282. **not** be the same as
  283. :variable:`CMAKE_CUDA_COMPILER <CMAKE_<LANG>_COMPILER>`. ``nvcc`` must be
  284. found to determine the CUDA Toolkit version as well as determining other
  285. features of the Toolkit. This variable is set for the convenience of
  286. modules that depend on this one.
  287. #]=======================================================================]
  288. # NOTE: much of this was simply extracted from FindCUDA.cmake.
  289. # James Bigler, NVIDIA Corp (nvidia.com - jbigler)
  290. # Abe Stephens, SCI Institute -- http://www.sci.utah.edu/~abe/FindCuda.html
  291. #
  292. # Copyright (c) 2008 - 2009 NVIDIA Corporation. All rights reserved.
  293. #
  294. # Copyright (c) 2007-2009
  295. # Scientific Computing and Imaging Institute, University of Utah
  296. #
  297. # This code is licensed under the MIT License. See the FindCUDA.cmake script
  298. # for the text of the license.
  299. # The MIT License
  300. #
  301. # License for the specific language governing rights and limitations under
  302. # Permission is hereby granted, free of charge, to any person obtaining a
  303. # copy of this software and associated documentation files (the "Software"),
  304. # to deal in the Software without restriction, including without limitation
  305. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  306. # and/or sell copies of the Software, and to permit persons to whom the
  307. # Software is furnished to do so, subject to the following conditions:
  308. #
  309. # The above copyright notice and this permission notice shall be included
  310. # in all copies or substantial portions of the Software.
  311. #
  312. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  313. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  314. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  315. # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  316. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  317. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  318. # DEALINGS IN THE SOFTWARE.
  319. #
  320. ###############################################################################
  321. if(CMAKE_CUDA_COMPILER_LOADED AND NOT CUDAToolkit_BIN_DIR)
  322. get_filename_component(cuda_dir "${CMAKE_CUDA_COMPILER}" DIRECTORY)
  323. # use the already detected cuda compiler
  324. set(CUDAToolkit_BIN_DIR "${cuda_dir}" CACHE PATH "")
  325. mark_as_advanced(CUDAToolkit_BIN_DIR)
  326. unset(cuda_dir)
  327. endif()
  328. # Try language- or user-provided path first.
  329. if(CUDAToolkit_BIN_DIR)
  330. find_program(CUDAToolkit_NVCC_EXECUTABLE
  331. NAMES nvcc nvcc.exe
  332. PATHS ${CUDAToolkit_BIN_DIR}
  333. NO_DEFAULT_PATH
  334. )
  335. endif()
  336. # Search using CUDAToolkit_ROOT
  337. find_program(CUDAToolkit_NVCC_EXECUTABLE
  338. NAMES nvcc nvcc.exe
  339. PATHS ENV CUDA_PATH
  340. PATH_SUFFIXES bin
  341. )
  342. # If the user specified CUDAToolkit_ROOT but nvcc could not be found, this is an error.
  343. if (NOT CUDAToolkit_NVCC_EXECUTABLE AND (DEFINED CUDAToolkit_ROOT OR DEFINED ENV{CUDAToolkit_ROOT}))
  344. # Declare error messages now, print later depending on find_package args.
  345. set(fail_base "Could not find nvcc executable in path specified by")
  346. set(cuda_root_fail "${fail_base} CUDAToolkit_ROOT=${CUDAToolkit_ROOT}")
  347. set(env_cuda_root_fail "${fail_base} environment variable CUDAToolkit_ROOT=$ENV{CUDAToolkit_ROOT}")
  348. if (CUDAToolkit_FIND_REQUIRED)
  349. if (DEFINED CUDAToolkit_ROOT)
  350. message(FATAL_ERROR ${cuda_root_fail})
  351. elseif (DEFINED ENV{CUDAToolkit_ROOT})
  352. message(FATAL_ERROR ${env_cuda_root_fail})
  353. endif()
  354. else()
  355. if (NOT CUDAToolkit_FIND_QUIETLY)
  356. if (DEFINED CUDAToolkit_ROOT)
  357. message(STATUS ${cuda_root_fail})
  358. elseif (DEFINED ENV{CUDAToolkit_ROOT})
  359. message(STATUS ${env_cuda_root_fail})
  360. endif()
  361. endif()
  362. set(CUDAToolkit_FOUND FALSE)
  363. unset(fail_base)
  364. unset(cuda_root_fail)
  365. unset(env_cuda_root_fail)
  366. return()
  367. endif()
  368. endif()
  369. # CUDAToolkit_ROOT cmake / env variable not specified, try platform defaults.
  370. #
  371. # - Linux: /usr/local/cuda-X.Y
  372. # - macOS: /Developer/NVIDIA/CUDA-X.Y
  373. # - Windows: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vX.Y
  374. #
  375. # We will also search the default symlink location /usr/local/cuda first since
  376. # if CUDAToolkit_ROOT is not specified, it is assumed that the symlinked
  377. # directory is the desired location.
  378. if (NOT CUDAToolkit_NVCC_EXECUTABLE)
  379. if (UNIX)
  380. if (NOT APPLE)
  381. set(platform_base "/usr/local/cuda-")
  382. else()
  383. set(platform_base "/Developer/NVIDIA/CUDA-")
  384. endif()
  385. else()
  386. set(platform_base "C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v")
  387. endif()
  388. # Build out a descending list of possible cuda installations, e.g.
  389. file(GLOB possible_paths "${platform_base}*")
  390. # Iterate the glob results and create a descending list.
  391. set(possible_versions)
  392. foreach (p ${possible_paths})
  393. # Extract version number from end of string
  394. string(REGEX MATCH "[0-9][0-9]?\\.[0-9]$" p_version ${p})
  395. if (IS_DIRECTORY ${p} AND p_version)
  396. list(APPEND possible_versions ${p_version})
  397. endif()
  398. endforeach()
  399. # Cannot use list(SORT) because that is alphabetical, we need numerical.
  400. # NOTE: this is not an efficient sorting strategy. But even if a user had
  401. # every possible version of CUDA installed, this wouldn't create any
  402. # significant overhead.
  403. set(versions)
  404. foreach (v ${possible_versions})
  405. list(LENGTH versions num_versions)
  406. # First version, nothing to compare with so just append.
  407. if (num_versions EQUAL 0)
  408. list(APPEND versions ${v})
  409. else()
  410. # Loop through list. Insert at an index when comparison is
  411. # VERSION_GREATER since we want a descending list. Duplicates will not
  412. # happen since this came from a glob list of directories.
  413. set(i 0)
  414. set(early_terminate FALSE)
  415. while (i LESS num_versions)
  416. list(GET versions ${i} curr)
  417. if (v VERSION_GREATER curr)
  418. list(INSERT versions ${i} ${v})
  419. set(early_terminate TRUE)
  420. break()
  421. endif()
  422. math(EXPR i "${i} + 1")
  423. endwhile()
  424. # If it did not get inserted, place it at the end.
  425. if (NOT early_terminate)
  426. list(APPEND versions ${v})
  427. endif()
  428. endif()
  429. endforeach()
  430. # With a descending list of versions, populate possible paths to search.
  431. set(search_paths)
  432. foreach (v ${versions})
  433. list(APPEND search_paths "${platform_base}${v}")
  434. endforeach()
  435. # Force the global default /usr/local/cuda to the front on Unix.
  436. if (UNIX)
  437. list(INSERT search_paths 0 "/usr/local/cuda")
  438. endif()
  439. # Now search for nvcc again using the platform default search paths.
  440. find_program(CUDAToolkit_NVCC_EXECUTABLE
  441. NAMES nvcc nvcc.exe
  442. PATHS ${search_paths}
  443. PATH_SUFFIXES bin
  444. )
  445. # We are done with these variables now, cleanup for caller.
  446. unset(platform_base)
  447. unset(possible_paths)
  448. unset(possible_versions)
  449. unset(versions)
  450. unset(i)
  451. unset(early_terminate)
  452. unset(search_paths)
  453. if (NOT CUDAToolkit_NVCC_EXECUTABLE)
  454. if (CUDAToolkit_FIND_REQUIRED)
  455. message(FATAL_ERROR "Could not find nvcc, please set CUDAToolkit_ROOT.")
  456. elseif(NOT CUDAToolkit_FIND_QUIETLY)
  457. message(STATUS "Could not find nvcc, please set CUDAToolkit_ROOT.")
  458. endif()
  459. set(CUDAToolkit_FOUND FALSE)
  460. return()
  461. endif()
  462. endif()
  463. if(NOT CUDAToolkit_BIN_DIR AND CUDAToolkit_NVCC_EXECUTABLE)
  464. get_filename_component(cuda_dir "${CUDAToolkit_NVCC_EXECUTABLE}" DIRECTORY)
  465. set(CUDAToolkit_BIN_DIR "${cuda_dir}" CACHE PATH "" FORCE)
  466. mark_as_advanced(CUDAToolkit_BIN_DIR)
  467. unset(cuda_dir)
  468. endif()
  469. if(CUDAToolkit_NVCC_EXECUTABLE AND
  470. CUDAToolkit_NVCC_EXECUTABLE STREQUAL CMAKE_CUDA_COMPILER)
  471. # Need to set these based off the already computed CMAKE_CUDA_COMPILER_VERSION value
  472. # This if statement will always match, but is used to provide variables for MATCH 1,2,3...
  473. if(CMAKE_CUDA_COMPILER_VERSION MATCHES [=[([0-9]+)\.([0-9]+)\.([0-9]+)]=])
  474. set(CUDAToolkit_VERSION_MAJOR "${CMAKE_MATCH_1}")
  475. set(CUDAToolkit_VERSION_MINOR "${CMAKE_MATCH_2}")
  476. set(CUDAToolkit_VERSION_PATCH "${CMAKE_MATCH_3}")
  477. set(CUDAToolkit_VERSION "${CMAKE_CUDA_COMPILER_VERSION}")
  478. endif()
  479. else()
  480. # Compute the version by invoking nvcc
  481. execute_process (COMMAND ${CUDAToolkit_NVCC_EXECUTABLE} "--version" OUTPUT_VARIABLE NVCC_OUT)
  482. if(NVCC_OUT MATCHES [=[ V([0-9]+)\.([0-9]+)\.([0-9]+)]=])
  483. set(CUDAToolkit_VERSION_MAJOR "${CMAKE_MATCH_1}")
  484. set(CUDAToolkit_VERSION_MINOR "${CMAKE_MATCH_2}")
  485. set(CUDAToolkit_VERSION_PATCH "${CMAKE_MATCH_3}")
  486. set(CUDAToolkit_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}")
  487. endif()
  488. unset(NVCC_OUT)
  489. endif()
  490. get_filename_component(CUDAToolkit_ROOT_DIR ${CUDAToolkit_BIN_DIR} DIRECTORY ABSOLUTE)
  491. # Handle cross compilation
  492. if(CMAKE_CROSSCOMPILING)
  493. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "armv7-a")
  494. # Support for NVPACK
  495. set (CUDAToolkit_TARGET_NAME "armv7-linux-androideabi")
  496. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm")
  497. # Support for arm cross compilation
  498. set(CUDAToolkit_TARGET_NAME "armv7-linux-gnueabihf")
  499. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
  500. # Support for aarch64 cross compilation
  501. if (ANDROID_ARCH_NAME STREQUAL "arm64")
  502. set(CUDAToolkit_TARGET_NAME "aarch64-linux-androideabi")
  503. else()
  504. set(CUDAToolkit_TARGET_NAME "aarch64-linux")
  505. endif (ANDROID_ARCH_NAME STREQUAL "arm64")
  506. elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
  507. set(CUDAToolkit_TARGET_NAME "x86_64-linux")
  508. endif()
  509. if (EXISTS "${CUDAToolkit_ROOT_DIR}/targets/${CUDAToolkit_TARGET_NAME}")
  510. set(CUDAToolkit_TARGET_DIR "${CUDAToolkit_ROOT_DIR}/targets/${CUDAToolkit_TARGET_NAME}")
  511. # add known CUDA target root path to the set of directories we search for programs, libraries and headers
  512. list(PREPEND CMAKE_FIND_ROOT_PATH "${CUDAToolkit_TARGET_DIR}")
  513. # Mark that we need to pop the root search path changes after we have
  514. # found all cuda libraries so that searches for our cross-compilation
  515. # libraries work when another cuda sdk is in CMAKE_PREFIX_PATH or
  516. # PATh
  517. set(_CUDAToolkit_Pop_ROOT_PATH True)
  518. endif()
  519. else()
  520. # Not cross compiling
  521. set(CUDAToolkit_TARGET_DIR "${CUDAToolkit_ROOT_DIR}")
  522. # Now that we have the real ROOT_DIR, find components inside it.
  523. list(APPEND CMAKE_PREFIX_PATH ${CUDAToolkit_ROOT_DIR})
  524. # Mark that we need to pop the prefix path changes after we have
  525. # found the cudart library.
  526. set(_CUDAToolkit_Pop_Prefix True)
  527. endif()
  528. # Find the include/ directory
  529. find_path(CUDAToolkit_INCLUDE_DIR
  530. NAMES cuda_runtime.h
  531. )
  532. # And find the CUDA Runtime Library libcudart
  533. find_library(CUDA_CUDART
  534. NAMES cudart
  535. PATH_SUFFIXES lib64 lib64/stubs lib/x64
  536. )
  537. if (NOT CUDA_CUDART AND NOT CUDAToolkit_FIND_QUIETLY)
  538. message(STATUS "Unable to find cudart library.")
  539. endif()
  540. unset(CUDAToolkit_ROOT_DIR)
  541. if(_CUDAToolkit_Pop_Prefix)
  542. list(REMOVE_AT CMAKE_PREFIX_PATH -1)
  543. unset(_CUDAToolkit_Pop_Prefix)
  544. endif()
  545. #-----------------------------------------------------------------------------
  546. # Perform version comparison and validate all required variables are set.
  547. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  548. find_package_handle_standard_args(CUDAToolkit
  549. REQUIRED_VARS
  550. CUDAToolkit_INCLUDE_DIR
  551. CUDA_CUDART
  552. CUDAToolkit_NVCC_EXECUTABLE
  553. VERSION_VAR
  554. CUDAToolkit_VERSION
  555. )
  556. mark_as_advanced(CUDA_CUDART
  557. CUDAToolkit_INCLUDE_DIR
  558. CUDAToolkit_NVCC_EXECUTABLE
  559. )
  560. #-----------------------------------------------------------------------------
  561. # Construct result variables
  562. if(CUDAToolkit_FOUND)
  563. set(CUDAToolkit_INCLUDE_DIRS ${CUDAToolkit_INCLUDE_DIR})
  564. get_filename_component(CUDAToolkit_LIBRARY_DIR ${CUDA_CUDART} DIRECTORY ABSOLUTE)
  565. endif()
  566. #-----------------------------------------------------------------------------
  567. # Construct import targets
  568. if(CUDAToolkit_FOUND)
  569. function(_CUDAToolkit_find_and_add_import_lib lib_name)
  570. cmake_parse_arguments(arg "" "" "ALT;DEPS;EXTRA_PATH_SUFFIXES" ${ARGN})
  571. set(search_names ${lib_name} ${arg_ALT})
  572. find_library(CUDA_${lib_name}_LIBRARY
  573. NAMES ${search_names}
  574. HINTS ${CUDAToolkit_LIBRARY_DIR}
  575. ENV CUDA_PATH
  576. PATH_SUFFIXES nvidia/current lib64 lib64/stubs lib/x64 lib lib/stubs
  577. ${arg_EXTRA_PATH_SUFFIXES}
  578. )
  579. mark_as_advanced(CUDA_${lib_name}_LIBRARY)
  580. if (NOT TARGET CUDA::${lib_name} AND CUDA_${lib_name}_LIBRARY)
  581. add_library(CUDA::${lib_name} IMPORTED INTERFACE)
  582. target_include_directories(CUDA::${lib_name} SYSTEM INTERFACE "${CUDAToolkit_INCLUDE_DIRS}")
  583. target_link_libraries(CUDA::${lib_name} INTERFACE "${CUDA_${lib_name}_LIBRARY}")
  584. foreach(dep ${arg_DEPS})
  585. if(TARGET CUDA::${dep})
  586. target_link_libraries(CUDA::${lib_name} INTERFACE CUDA::${dep})
  587. endif()
  588. endforeach()
  589. endif()
  590. endfunction()
  591. if(NOT TARGET CUDA::toolkit)
  592. add_library(CUDA::toolkit IMPORTED INTERFACE)
  593. target_include_directories(CUDA::toolkit SYSTEM INTERFACE "${CUDAToolkit_INCLUDE_DIRS}")
  594. target_link_directories(CUDA::toolkit INTERFACE "${CUDAToolkit_LIBRARY_DIR}")
  595. endif()
  596. _CUDAToolkit_find_and_add_import_lib(cuda_driver ALT cuda)
  597. _CUDAToolkit_find_and_add_import_lib(cudart)
  598. _CUDAToolkit_find_and_add_import_lib(cudart_static)
  599. # setup dependencies that are required for cudart_static when building
  600. # on linux. These are generally only required when using the CUDA toolkit
  601. # when CUDA language is disabled
  602. if(NOT TARGET CUDA::cudart_static_deps
  603. AND TARGET CUDA::cudart_static)
  604. add_library(CUDA::cudart_static_deps IMPORTED INTERFACE)
  605. target_link_libraries(CUDA::cudart_static INTERFACE CUDA::cudart_static_deps)
  606. if(UNIX AND (CMAKE_C_COMPILER OR CMAKE_CXX_COMPILER))
  607. find_package(Threads REQUIRED)
  608. target_link_libraries(CUDA::cudart_static_deps INTERFACE Threads::Threads ${CMAKE_DL_LIBS})
  609. endif()
  610. if(UNIX AND NOT APPLE)
  611. # On Linux, you must link against librt when using the static cuda runtime.
  612. find_library(CUDAToolkit_rt_LIBRARY rt)
  613. mark_as_advanced(CUDAToolkit_rt_LIBRARY)
  614. if(NOT CUDAToolkit_rt_LIBRARY)
  615. message(WARNING "Could not find librt library, needed by CUDA::cudart_static")
  616. else()
  617. target_link_libraries(CUDA::cudart_static_deps INTERFACE ${CUDAToolkit_rt_LIBRARY})
  618. endif()
  619. endif()
  620. endif()
  621. _CUDAToolkit_find_and_add_import_lib(culibos) # it's a static library
  622. foreach (cuda_lib cublas cufft curand cusparse nppc nvjpeg)
  623. _CUDAToolkit_find_and_add_import_lib(${cuda_lib})
  624. _CUDAToolkit_find_and_add_import_lib(${cuda_lib}_static DEPS culibos)
  625. endforeach()
  626. # cuFFTW depends on cuFFT
  627. _CUDAToolkit_find_and_add_import_lib(cufftw DEPS cufft)
  628. _CUDAToolkit_find_and_add_import_lib(cufftw DEPS cufft_static)
  629. # cuSOLVER depends on cuBLAS, and cuSPARSE
  630. _CUDAToolkit_find_and_add_import_lib(cusolver DEPS cublas cusparse)
  631. _CUDAToolkit_find_and_add_import_lib(cusolver_static DEPS cublas_static cusparse_static culibos)
  632. # nvGRAPH depends on cuRAND, and cuSOLVER.
  633. _CUDAToolkit_find_and_add_import_lib(nvgraph DEPS curand cusolver)
  634. _CUDAToolkit_find_and_add_import_lib(nvgraph_static DEPS curand_static cusolver_static)
  635. # Process the majority of the NPP libraries.
  636. foreach (cuda_lib nppial nppicc nppidei nppif nppig nppim nppist nppitc npps nppicom nppisu)
  637. _CUDAToolkit_find_and_add_import_lib(${cuda_lib} DEPS nppc)
  638. _CUDAToolkit_find_and_add_import_lib(${cuda_lib}_static DEPS nppc_static)
  639. endforeach()
  640. _CUDAToolkit_find_and_add_import_lib(cupti
  641. EXTRA_PATH_SUFFIXES ../extras/CUPTI/lib64/
  642. ../extras/CUPTI/lib/)
  643. _CUDAToolkit_find_and_add_import_lib(cupti_static
  644. EXTRA_PATH_SUFFIXES ../extras/CUPTI/lib64/
  645. ../extras/CUPTI/lib/)
  646. _CUDAToolkit_find_and_add_import_lib(nvrtc DEPS cuda_driver)
  647. _CUDAToolkit_find_and_add_import_lib(nvml ALT nvidia-ml nvml)
  648. if(WIN32)
  649. # nvtools can be installed outside the CUDA toolkit directory
  650. # so prefer the NVTOOLSEXT_PATH windows only environment variable
  651. # In addition on windows the most common name is nvToolsExt64_1
  652. find_library(CUDA_nvToolsExt_LIBRARY
  653. NAMES nvToolsExt64_1 nvToolsExt64 nvToolsExt
  654. PATHS ENV NVTOOLSEXT_PATH
  655. ENV CUDA_PATH
  656. PATH_SUFFIXES lib/x64 lib
  657. )
  658. endif()
  659. _CUDAToolkit_find_and_add_import_lib(nvToolsExt ALT nvToolsExt64)
  660. _CUDAToolkit_find_and_add_import_lib(OpenCL)
  661. endif()
  662. if(_CUDAToolkit_Pop_ROOT_PATH)
  663. list(REMOVE_AT CMAKE_FIND_ROOT_PATH 0)
  664. unset(_CUDAToolkit_Pop_ROOT_PATH)
  665. endif()