FindCUDAToolkit.cmake 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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. .. versionadded:: 3.17
  7. This script locates the NVIDIA CUDA toolkit and the associated libraries, but
  8. does not require the ``CUDA`` language be enabled for a given project. This
  9. module does not search for the NVIDIA CUDA Samples.
  10. .. versionadded:: 3.19
  11. QNX support.
  12. Search Behavior
  13. ^^^^^^^^^^^^^^^
  14. The CUDA Toolkit search behavior uses the following order:
  15. 1. If the ``CUDA`` language has been enabled we will use the directory
  16. containing the compiler as the first search location for ``nvcc``.
  17. 2. If the ``CUDAToolkit_ROOT`` cmake configuration variable (e.g.,
  18. ``-DCUDAToolkit_ROOT=/some/path``) *or* environment variable is defined, it
  19. will be searched. If both an environment variable **and** a
  20. configuration variable are specified, the *configuration* variable takes
  21. precedence.
  22. The directory specified here must be such that the executable ``nvcc`` or
  23. the appropriate ``version.txt`` file can be found underneath the specified
  24. directory.
  25. 3. If the CUDA_PATH environment variable is defined, it will be searched
  26. for ``nvcc``.
  27. 4. The user's path is searched for ``nvcc`` using :command:`find_program`. If
  28. this is found, no subsequent search attempts are performed. Users are
  29. responsible for ensuring that the first ``nvcc`` to show up in the path is
  30. the desired path in the event that multiple CUDA Toolkits are installed.
  31. 5. On Unix systems, if the symbolic link ``/usr/local/cuda`` exists, this is
  32. used. No subsequent search attempts are performed. No default symbolic link
  33. location exists for the Windows platform.
  34. 6. The platform specific default install locations are searched. If exactly one
  35. candidate is found, this is used. The default CUDA Toolkit install locations
  36. searched are:
  37. +-------------+-------------------------------------------------------------+
  38. | Platform | Search Pattern |
  39. +=============+=============================================================+
  40. | macOS | ``/Developer/NVIDIA/CUDA-X.Y`` |
  41. +-------------+-------------------------------------------------------------+
  42. | Other Unix | ``/usr/local/cuda-X.Y`` |
  43. +-------------+-------------------------------------------------------------+
  44. | Windows | ``C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vX.Y`` |
  45. +-------------+-------------------------------------------------------------+
  46. Where ``X.Y`` would be a specific version of the CUDA Toolkit, such as
  47. ``/usr/local/cuda-9.0`` or
  48. ``C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0``
  49. .. note::
  50. When multiple CUDA Toolkits are installed in the default location of a
  51. system (e.g., both ``/usr/local/cuda-9.0`` and ``/usr/local/cuda-10.0``
  52. exist but the ``/usr/local/cuda`` symbolic link does **not** exist), this
  53. package is marked as **not** found.
  54. There are too many factors involved in making an automatic decision in
  55. the presence of multiple CUDA Toolkits being installed. In this
  56. situation, users are encouraged to either (1) set ``CUDAToolkit_ROOT`` or
  57. (2) ensure that the correct ``nvcc`` executable shows up in ``$PATH`` for
  58. :command:`find_program` to find.
  59. Arguments
  60. ^^^^^^^^^
  61. ``[<version>]``
  62. The ``[<version>]`` argument requests a version with which the package found
  63. should be compatible. See :ref:`find_package version format <FIND_PACKAGE_VERSION_FORMAT>`
  64. for more details.
  65. Options
  66. ^^^^^^^
  67. ``REQUIRED``
  68. If specified, configuration will error if a suitable CUDA Toolkit is not
  69. found.
  70. ``QUIET``
  71. If specified, the search for a suitable CUDA Toolkit will not produce any
  72. messages.
  73. ``EXACT``
  74. If specified, the CUDA Toolkit is considered found only if the exact
  75. ``VERSION`` specified is recovered.
  76. Imported targets
  77. ^^^^^^^^^^^^^^^^
  78. An :ref:`imported target <Imported targets>` named ``CUDA::toolkit`` is provided.
  79. This module defines :prop_tgt:`IMPORTED` targets for each
  80. of the following libraries that are part of the CUDAToolkit:
  81. - :ref:`CUDA Runtime Library<cuda_toolkit_rt_lib>`
  82. - :ref:`CUDA Driver Library<cuda_toolkit_driver_lib>`
  83. - :ref:`cuBLAS<cuda_toolkit_cuBLAS>`
  84. - :ref:`cuFile<cuda_toolkit_cuFile>`
  85. - :ref:`cuFFT<cuda_toolkit_cuFFT>`
  86. - :ref:`cuRAND<cuda_toolkit_cuRAND>`
  87. - :ref:`cuSOLVER<cuda_toolkit_cuSOLVER>`
  88. - :ref:`cuSPARSE<cuda_toolkit_cuSPARSE>`
  89. - :ref:`cuPTI<cuda_toolkit_cupti>`
  90. - :ref:`NPP<cuda_toolkit_NPP>`
  91. - :ref:`nvBLAS<cuda_toolkit_nvBLAS>`
  92. - :ref:`nvGRAPH<cuda_toolkit_nvGRAPH>`
  93. - :ref:`nvJPEG<cuda_toolkit_nvJPEG>`
  94. - :ref:`nvidia-ML<cuda_toolkit_nvML>`
  95. - :ref:`nvPTX Compiler<cuda_toolkit_nvptx>`
  96. - :ref:`nvRTC<cuda_toolkit_nvRTC>`
  97. - :ref:`nvToolsExt<cuda_toolkit_nvToolsExt>`
  98. - :ref:`nvtx3<cuda_toolkit_nvtx3>`
  99. - :ref:`OpenCL<cuda_toolkit_opencl>`
  100. - :ref:`cuLIBOS<cuda_toolkit_cuLIBOS>`
  101. .. _`cuda_toolkit_rt_lib`:
  102. CUDA Runtime Library
  103. """"""""""""""""""""
  104. The CUDA Runtime library (cudart) are what most applications will typically
  105. need to link against to make any calls such as `cudaMalloc`, and `cudaFree`.
  106. Targets Created:
  107. - ``CUDA::cudart``
  108. - ``CUDA::cudart_static``
  109. .. _`cuda_toolkit_driver_lib`:
  110. CUDA Driver Library
  111. """"""""""""""""""""
  112. The CUDA Driver library (cuda) are used by applications that use calls
  113. such as `cuMemAlloc`, and `cuMemFree`.
  114. Targets Created:
  115. - ``CUDA::cuda_driver``
  116. .. _`cuda_toolkit_cuBLAS`:
  117. cuBLAS
  118. """"""
  119. The `cuBLAS <https://docs.nvidia.com/cuda/cublas/index.html>`_ library.
  120. Targets Created:
  121. - ``CUDA::cublas``
  122. - ``CUDA::cublas_static``
  123. - ``CUDA::cublasLt`` starting in CUDA 10.1
  124. - ``CUDA::cublasLt_static`` starting in CUDA 10.1
  125. .. _`cuda_toolkit_cuFile`:
  126. cuFile
  127. """"""
  128. .. versionadded:: 3.25
  129. The NVIDIA GPUDirect Storage `cuFile <https://docs.nvidia.com/cuda/cufile-api/index.html>`_ library.
  130. Targets Created:
  131. - ``CUDA::cuFile`` starting in CUDA 11.4
  132. - ``CUDA::cuFile_static`` starting in CUDA 11.4
  133. - ``CUDA::cuFile_rdma`` starting in CUDA 11.4
  134. - ``CUDA::cuFile_rdma_static`` starting in CUDA 11.4
  135. .. _`cuda_toolkit_cuFFT`:
  136. cuFFT
  137. """""
  138. The `cuFFT <https://docs.nvidia.com/cuda/cufft/index.html>`_ library.
  139. Targets Created:
  140. - ``CUDA::cufft``
  141. - ``CUDA::cufftw``
  142. - ``CUDA::cufft_static``
  143. - ``CUDA::cufft_static_nocallback`` starting in CUDA 9.2, requires CMake 3.23+
  144. - ``CUDA::cufftw_static``
  145. cuRAND
  146. """"""
  147. The `cuRAND <https://docs.nvidia.com/cuda/curand/index.html>`_ library.
  148. Targets Created:
  149. - ``CUDA::curand``
  150. - ``CUDA::curand_static``
  151. .. _`cuda_toolkit_cuSOLVER`:
  152. cuSOLVER
  153. """"""""
  154. The `cuSOLVER <https://docs.nvidia.com/cuda/cusolver/index.html>`_ library.
  155. Targets Created:
  156. - ``CUDA::cusolver``
  157. - ``CUDA::cusolver_static``
  158. .. _`cuda_toolkit_cuSPARSE`:
  159. cuSPARSE
  160. """"""""
  161. The `cuSPARSE <https://docs.nvidia.com/cuda/cusparse/index.html>`_ library.
  162. Targets Created:
  163. - ``CUDA::cusparse``
  164. - ``CUDA::cusparse_static``
  165. .. _`cuda_toolkit_cupti`:
  166. cupti
  167. """""
  168. The `NVIDIA CUDA Profiling Tools Interface <https://developer.nvidia.com/CUPTI>`_.
  169. Targets Created:
  170. - ``CUDA::cupti``
  171. - ``CUDA::cupti_static``
  172. .. _`cuda_toolkit_NPP`:
  173. NPP
  174. """
  175. The `NPP <https://docs.nvidia.com/cuda/npp/index.html>`_ libraries.
  176. Targets Created:
  177. - `nppc`:
  178. - ``CUDA::nppc``
  179. - ``CUDA::nppc_static``
  180. - `nppial`: Arithmetic and logical operation functions in `nppi_arithmetic_and_logical_operations.h`
  181. - ``CUDA::nppial``
  182. - ``CUDA::nppial_static``
  183. - `nppicc`: Color conversion and sampling functions in `nppi_color_conversion.h`
  184. - ``CUDA::nppicc``
  185. - ``CUDA::nppicc_static``
  186. - `nppicom`: JPEG compression and decompression functions in `nppi_compression_functions.h`
  187. Removed starting in CUDA 11.0, use :ref:`nvJPEG<cuda_toolkit_nvJPEG>` instead.
  188. - ``CUDA::nppicom``
  189. - ``CUDA::nppicom_static``
  190. - `nppidei`: Data exchange and initialization functions in `nppi_data_exchange_and_initialization.h`
  191. - ``CUDA::nppidei``
  192. - ``CUDA::nppidei_static``
  193. - `nppif`: Filtering and computer vision functions in `nppi_filter_functions.h`
  194. - ``CUDA::nppif``
  195. - ``CUDA::nppif_static``
  196. - `nppig`: Geometry transformation functions found in `nppi_geometry_transforms.h`
  197. - ``CUDA::nppig``
  198. - ``CUDA::nppig_static``
  199. - `nppim`: Morphological operation functions found in `nppi_morphological_operations.h`
  200. - ``CUDA::nppim``
  201. - ``CUDA::nppim_static``
  202. - `nppist`: Statistics and linear transform in `nppi_statistics_functions.h` and `nppi_linear_transforms.h`
  203. - ``CUDA::nppist``
  204. - ``CUDA::nppist_static``
  205. - `nppisu`: Memory support functions in `nppi_support_functions.h`
  206. - ``CUDA::nppisu``
  207. - ``CUDA::nppisu_static``
  208. - `nppitc`: Threshold and compare operation functions in `nppi_threshold_and_compare_operations.h`
  209. - ``CUDA::nppitc``
  210. - ``CUDA::nppitc_static``
  211. - `npps`:
  212. - ``CUDA::npps``
  213. - ``CUDA::npps_static``
  214. .. _`cuda_toolkit_nvBLAS`:
  215. nvBLAS
  216. """"""
  217. The `nvBLAS <https://docs.nvidia.com/cuda/nvblas/index.html>`_ libraries.
  218. This is a shared library only.
  219. Targets Created:
  220. - ``CUDA::nvblas``
  221. .. _`cuda_toolkit_nvGRAPH`:
  222. nvGRAPH
  223. """""""
  224. The `nvGRAPH <https://docs.nvidia.com/cuda/nvgraph/index.html>`_ library.
  225. Removed starting in CUDA 11.0
  226. Targets Created:
  227. - ``CUDA::nvgraph``
  228. - ``CUDA::nvgraph_static``
  229. .. _`cuda_toolkit_nvJPEG`:
  230. nvJPEG
  231. """"""
  232. The `nvJPEG <https://docs.nvidia.com/cuda/nvjpeg/index.html>`_ library.
  233. Introduced in CUDA 10.
  234. Targets Created:
  235. - ``CUDA::nvjpeg``
  236. - ``CUDA::nvjpeg_static``
  237. .. _`cuda_toolkit_nvPTX`:
  238. nvPTX Compiler
  239. """"""""""""""
  240. .. versionadded:: 3.25
  241. The `nvPTX <https://docs.nvidia.com/cuda/ptx-compiler-api/index.html>`_ (PTX Compilation) library.
  242. The PTX Compiler APIs are a set of APIs which can be used to compile a PTX program into GPU assembly code.
  243. Introduced in CUDA 11.1
  244. This is a static library only.
  245. Targets Created:
  246. - ``CUDA::nvptxcompiler_static`` starting in CUDA 11.1
  247. .. _`cuda_toolkit_nvRTC`:
  248. nvRTC
  249. """""
  250. The `nvRTC <https://docs.nvidia.com/cuda/nvrtc/index.html>`_ (Runtime Compilation) library.
  251. This is a shared library only.
  252. Targets Created:
  253. - ``CUDA::nvrtc``
  254. .. _`cuda_toolkit_nvjitlink`:
  255. nvJitLink
  256. """""""""
  257. The `nvJItLink <https://docs.nvidia.com/cuda/>`_ (Runtime LTO Linking) library.
  258. Targets Created:
  259. - ``CUDA::nvJitLink`` starting in CUDA 12.0
  260. - ``CUDA::nvJitLink_static`` starting in CUDA 12.0
  261. .. _`cuda_toolkit_nvml`:
  262. nvidia-ML
  263. """""""""
  264. The `NVIDIA Management Library <https://developer.nvidia.com/nvidia-management-library-nvml>`_.
  265. This is a shared library only.
  266. Targets Created:
  267. - ``CUDA::nvml``
  268. .. _`cuda_toolkit_nvToolsExt`:
  269. nvToolsExt
  270. """"""""""
  271. .. deprecated:: 3.25 With CUDA 10.0+, use :ref:`nvtx3 <cuda_toolkit_nvtx3>`.
  272. The `NVIDIA Tools Extension <https://docs.nvidia.com/gameworks/content/gameworkslibrary/nvtx/nvidia_tools_extension_library_nvtx.htm>`_.
  273. This is a shared library only.
  274. Targets Created:
  275. - ``CUDA::nvToolsExt``
  276. .. _`cuda_toolkit_nvtx3`:
  277. nvtx3
  278. """""
  279. .. versionadded:: 3.25
  280. The header-only `NVIDIA Tools Extension Library <https://nvidia.github.io/NVTX/doxygen/index.html>`_.
  281. Introduced in CUDA 10.0.
  282. Targets created:
  283. - ``CUDA::nvtx3``
  284. .. _`cuda_toolkit_opencl`:
  285. OpenCL
  286. """"""
  287. The `NVIDIA OpenCL Library <https://developer.nvidia.com/opencl>`_.
  288. This is a shared library only.
  289. Targets Created:
  290. - ``CUDA::OpenCL``
  291. .. _`cuda_toolkit_cuLIBOS`:
  292. cuLIBOS
  293. """""""
  294. The cuLIBOS library is a backend thread abstraction layer library which is
  295. static only. The ``CUDA::cublas_static``, ``CUDA::cusparse_static``,
  296. ``CUDA::cufft_static``, ``CUDA::curand_static``, and (when implemented) NPP
  297. libraries all automatically have this dependency linked.
  298. Target Created:
  299. - ``CUDA::culibos``
  300. **Note**: direct usage of this target by consumers should not be necessary.
  301. .. _`cuda_toolkit_cuRAND`:
  302. Result variables
  303. ^^^^^^^^^^^^^^^^
  304. ``CUDAToolkit_FOUND``
  305. A boolean specifying whether or not the CUDA Toolkit was found.
  306. ``CUDAToolkit_VERSION``
  307. The exact version of the CUDA Toolkit found (as reported by
  308. ``nvcc --version`` or ``version.txt``).
  309. ``CUDAToolkit_VERSION_MAJOR``
  310. The major version of the CUDA Toolkit.
  311. ``CUDAToolkit_VERSION_MINOR``
  312. The minor version of the CUDA Toolkit.
  313. ``CUDAToolkit_VERSION_PATCH``
  314. The patch version of the CUDA Toolkit.
  315. ``CUDAToolkit_BIN_DIR``
  316. The path to the CUDA Toolkit library directory that contains the CUDA
  317. executable ``nvcc``.
  318. ``CUDAToolkit_INCLUDE_DIRS``
  319. The path to the CUDA Toolkit ``include`` folder containing the header files
  320. required to compile a project linking against CUDA.
  321. ``CUDAToolkit_LIBRARY_DIR``
  322. The path to the CUDA Toolkit library directory that contains the CUDA
  323. Runtime library ``cudart``.
  324. ``CUDAToolkit_LIBRARY_ROOT``
  325. .. versionadded:: 3.18
  326. The path to the CUDA Toolkit directory containing the nvvm directory and
  327. version.txt.
  328. ``CUDAToolkit_TARGET_DIR``
  329. The path to the CUDA Toolkit directory including the target architecture
  330. when cross-compiling. When not cross-compiling this will be equivalent to
  331. the parent directory of ``CUDAToolkit_BIN_DIR``.
  332. ``CUDAToolkit_NVCC_EXECUTABLE``
  333. The path to the NVIDIA CUDA compiler ``nvcc``. Note that this path may
  334. **not** be the same as
  335. :variable:`CMAKE_CUDA_COMPILER <CMAKE_<LANG>_COMPILER>`. ``nvcc`` must be
  336. found to determine the CUDA Toolkit version as well as determining other
  337. features of the Toolkit. This variable is set for the convenience of
  338. modules that depend on this one.
  339. #]=======================================================================]
  340. # NOTE: much of this was simply extracted from FindCUDA.cmake.
  341. # James Bigler, NVIDIA Corp (nvidia.com - jbigler)
  342. # Abe Stephens, SCI Institute -- http://www.sci.utah.edu/~abe/FindCuda.html
  343. #
  344. # Copyright (c) 2008 - 2009 NVIDIA Corporation. All rights reserved.
  345. #
  346. # Copyright (c) 2007-2009
  347. # Scientific Computing and Imaging Institute, University of Utah
  348. #
  349. # This code is licensed under the MIT License. See the FindCUDA.cmake script
  350. # for the text of the license.
  351. # The MIT License
  352. #
  353. # License for the specific language governing rights and limitations under
  354. # Permission is hereby granted, free of charge, to any person obtaining a
  355. # copy of this software and associated documentation files (the "Software"),
  356. # to deal in the Software without restriction, including without limitation
  357. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  358. # and/or sell copies of the Software, and to permit persons to whom the
  359. # Software is furnished to do so, subject to the following conditions:
  360. #
  361. # The above copyright notice and this permission notice shall be included
  362. # in all copies or substantial portions of the Software.
  363. #
  364. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  365. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  366. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  367. # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  368. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  369. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  370. # DEALINGS IN THE SOFTWARE.
  371. #
  372. ###############################################################################
  373. # The toolkit is located during compiler detection for CUDA and stored in CMakeCUDACompiler.cmake as
  374. # CMAKE_CUDA_COMPILER_TOOLKIT_ROOT and CMAKE_CUDA_COMPILER_LIBRARY_ROOT.
  375. # We compute the rest based on those here to avoid re-searching and to avoid finding a possibly
  376. # different installation.
  377. if(CMAKE_CUDA_COMPILER_TOOLKIT_ROOT)
  378. set(CUDAToolkit_ROOT_DIR "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}")
  379. set(CUDAToolkit_LIBRARY_ROOT "${CMAKE_CUDA_COMPILER_LIBRARY_ROOT}")
  380. set(CUDAToolkit_BIN_DIR "${CUDAToolkit_ROOT_DIR}/bin")
  381. set(CUDAToolkit_NVCC_EXECUTABLE "${CUDAToolkit_BIN_DIR}/nvcc${CMAKE_EXECUTABLE_SUFFIX}")
  382. set(CUDAToolkit_VERSION "${CMAKE_CUDA_COMPILER_TOOLKIT_VERSION}")
  383. if(CUDAToolkit_VERSION MATCHES [=[([0-9]+)\.([0-9]+)\.([0-9]+)]=])
  384. set(CUDAToolkit_VERSION_MAJOR "${CMAKE_MATCH_1}")
  385. set(CUDAToolkit_VERSION_MINOR "${CMAKE_MATCH_2}")
  386. set(CUDAToolkit_VERSION_PATCH "${CMAKE_MATCH_3}")
  387. endif()
  388. else()
  389. function(_CUDAToolkit_find_root_dir )
  390. cmake_parse_arguments(arg "" "" "SEARCH_PATHS;FIND_FLAGS" ${ARGN})
  391. if(NOT CUDAToolkit_BIN_DIR)
  392. if(NOT CUDAToolkit_SENTINEL_FILE)
  393. find_program(CUDAToolkit_NVCC_EXECUTABLE
  394. NAMES nvcc nvcc.exe
  395. PATHS ${arg_SEARCH_PATHS}
  396. ${arg_FIND_FLAGS}
  397. )
  398. endif()
  399. if(NOT CUDAToolkit_NVCC_EXECUTABLE)
  400. find_file(CUDAToolkit_SENTINEL_FILE
  401. NAMES version.txt
  402. PATHS ${arg_SEARCH_PATHS}
  403. NO_DEFAULT_PATH
  404. )
  405. endif()
  406. if(EXISTS "${CUDAToolkit_NVCC_EXECUTABLE}")
  407. # If NVCC exists then invoke it to find the toolkit location.
  408. # This allows us to support wrapper scripts (e.g. ccache or colornvcc), CUDA Toolkit,
  409. # NVIDIA HPC SDK, and distro's splayed layouts
  410. execute_process(COMMAND ${CUDAToolkit_NVCC_EXECUTABLE} "-v" "__cmake_determine_cuda"
  411. OUTPUT_VARIABLE _CUDA_NVCC_OUT ERROR_VARIABLE _CUDA_NVCC_OUT)
  412. if(_CUDA_NVCC_OUT MATCHES "\\#\\$ TOP=([^\r\n]*)")
  413. get_filename_component(CUDAToolkit_BIN_DIR "${CMAKE_MATCH_1}/bin" ABSOLUTE)
  414. else()
  415. get_filename_component(CUDAToolkit_BIN_DIR "${CUDAToolkit_NVCC_EXECUTABLE}" DIRECTORY)
  416. endif()
  417. unset(_CUDA_NVCC_OUT)
  418. mark_as_advanced(CUDAToolkit_BIN_DIR)
  419. set(CUDAToolkit_BIN_DIR "${CUDAToolkit_BIN_DIR}" CACHE PATH "" FORCE)
  420. endif()
  421. if(CUDAToolkit_SENTINEL_FILE)
  422. get_filename_component(CUDAToolkit_BIN_DIR ${CUDAToolkit_SENTINEL_FILE} DIRECTORY ABSOLUTE)
  423. set(CUDAToolkit_BIN_DIR "${CUDAToolkit_BIN_DIR}/bin")
  424. set(CUDAToolkit_BIN_DIR "${CUDAToolkit_BIN_DIR}" CACHE PATH "" FORCE)
  425. mark_as_advanced(CUDAToolkit_BIN_DIR)
  426. endif()
  427. endif()
  428. if(CUDAToolkit_BIN_DIR)
  429. get_filename_component(CUDAToolkit_ROOT_DIR ${CUDAToolkit_BIN_DIR} DIRECTORY ABSOLUTE)
  430. set(CUDAToolkit_ROOT_DIR "${CUDAToolkit_ROOT_DIR}" PARENT_SCOPE)
  431. endif()
  432. endfunction()
  433. function(_CUDAToolkit_find_version_file result_variable)
  434. # We first check for a non-scattered installation to prefer it over a scattered installation.
  435. if(CUDAToolkit_ROOT AND EXISTS "${CUDAToolkit_ROOT}/version.txt")
  436. set(${result_variable} "${CUDAToolkit_ROOT}/version.txt" PARENT_SCOPE)
  437. elseif(CUDAToolkit_ROOT_DIR AND EXISTS "${CUDAToolkit_ROOT_DIR}/version.txt")
  438. set(${result_variable} "${CUDAToolkit_ROOT_DIR}/version.txt" PARENT_SCOPE)
  439. elseif(CMAKE_SYSROOT_LINK AND EXISTS "${CMAKE_SYSROOT_LINK}/usr/lib/cuda/version.txt")
  440. set(${result_variable} "${CMAKE_SYSROOT_LINK}/usr/lib/cuda/version.txt" PARENT_SCOPE)
  441. elseif(EXISTS "${CMAKE_SYSROOT}/usr/lib/cuda/version.txt")
  442. set(${result_variable} "${CMAKE_SYSROOT}/usr/lib/cuda/version.txt" PARENT_SCOPE)
  443. endif()
  444. endfunction()
  445. # For NVCC we can easily deduce the SDK binary directory from the compiler path.
  446. if(CMAKE_CUDA_COMPILER_LOADED AND NOT CUDAToolkit_BIN_DIR AND CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
  447. get_filename_component(CUDAToolkit_BIN_DIR "${CMAKE_CUDA_COMPILER}" DIRECTORY)
  448. set(CUDAToolkit_BIN_DIR "${CUDAToolkit_BIN_DIR}" CACHE PATH "")
  449. # Try language provided path first.
  450. _CUDAToolkit_find_root_dir(SEARCH_PATHS "${CUDAToolkit_BIN_DIR}" FIND_FLAGS NO_DEFAULT_PATH)
  451. mark_as_advanced(CUDAToolkit_BIN_DIR)
  452. endif()
  453. # Try user provided path
  454. if(NOT CUDAToolkit_ROOT_DIR AND CUDAToolkit_ROOT)
  455. _CUDAToolkit_find_root_dir(SEARCH_PATHS "${CUDAToolkit_ROOT}" FIND_FLAGS PATH_SUFFIXES bin NO_DEFAULT_PATH)
  456. endif()
  457. if(NOT CUDAToolkit_ROOT_DIR)
  458. _CUDAToolkit_find_root_dir(FIND_FLAGS PATHS ENV CUDA_PATH PATH_SUFFIXES bin)
  459. endif()
  460. # If the user specified CUDAToolkit_ROOT but the toolkit could not be found, this is an error.
  461. if(NOT CUDAToolkit_ROOT_DIR AND (DEFINED CUDAToolkit_ROOT OR DEFINED ENV{CUDAToolkit_ROOT}))
  462. # Declare error messages now, print later depending on find_package args.
  463. set(fail_base "Could not find nvcc executable in path specified by")
  464. set(cuda_root_fail "${fail_base} CUDAToolkit_ROOT=${CUDAToolkit_ROOT}")
  465. set(env_cuda_root_fail "${fail_base} environment variable CUDAToolkit_ROOT=$ENV{CUDAToolkit_ROOT}")
  466. if(CUDAToolkit_FIND_REQUIRED)
  467. if(DEFINED CUDAToolkit_ROOT)
  468. message(FATAL_ERROR ${cuda_root_fail})
  469. elseif(DEFINED ENV{CUDAToolkit_ROOT})
  470. message(FATAL_ERROR ${env_cuda_root_fail})
  471. endif()
  472. else()
  473. if(NOT CUDAToolkit_FIND_QUIETLY)
  474. if(DEFINED CUDAToolkit_ROOT)
  475. message(STATUS ${cuda_root_fail})
  476. elseif(DEFINED ENV{CUDAToolkit_ROOT})
  477. message(STATUS ${env_cuda_root_fail})
  478. endif()
  479. endif()
  480. set(CUDAToolkit_FOUND FALSE)
  481. unset(fail_base)
  482. unset(cuda_root_fail)
  483. unset(env_cuda_root_fail)
  484. return()
  485. endif()
  486. endif()
  487. # CUDAToolkit_ROOT cmake / env variable not specified, try platform defaults.
  488. #
  489. # - Linux: /usr/local/cuda-X.Y
  490. # - macOS: /Developer/NVIDIA/CUDA-X.Y
  491. # - Windows: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vX.Y
  492. #
  493. # We will also search the default symlink location /usr/local/cuda first since
  494. # if CUDAToolkit_ROOT is not specified, it is assumed that the symlinked
  495. # directory is the desired location.
  496. if(NOT CUDAToolkit_ROOT_DIR)
  497. if(UNIX)
  498. if(NOT APPLE)
  499. set(platform_base "/usr/local/cuda-")
  500. else()
  501. set(platform_base "/Developer/NVIDIA/CUDA-")
  502. endif()
  503. else()
  504. set(platform_base "C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v")
  505. endif()
  506. # Build out a descending list of possible cuda installations, e.g.
  507. file(GLOB possible_paths "${platform_base}*")
  508. # Iterate the glob results and create a descending list.
  509. set(versions)
  510. foreach(p ${possible_paths})
  511. # Extract version number from end of string
  512. string(REGEX MATCH "[0-9][0-9]?\\.[0-9]$" p_version ${p})
  513. if(IS_DIRECTORY ${p} AND p_version)
  514. list(APPEND versions ${p_version})
  515. endif()
  516. endforeach()
  517. # Sort numerically in descending order, so we try the newest versions first.
  518. list(SORT versions COMPARE NATURAL ORDER DESCENDING)
  519. # With a descending list of versions, populate possible paths to search.
  520. set(search_paths)
  521. foreach(v ${versions})
  522. list(APPEND search_paths "${platform_base}${v}")
  523. endforeach()
  524. # Force the global default /usr/local/cuda to the front on Unix.
  525. if(UNIX)
  526. list(INSERT search_paths 0 "/usr/local/cuda")
  527. endif()
  528. # Now search for the toolkit again using the platform default search paths.
  529. _CUDAToolkit_find_root_dir(SEARCH_PATHS "${search_paths}" FIND_FLAGS PATH_SUFFIXES bin)
  530. # We are done with these variables now, cleanup for caller.
  531. unset(platform_base)
  532. unset(possible_paths)
  533. unset(versions)
  534. unset(search_paths)
  535. if(NOT CUDAToolkit_ROOT_DIR)
  536. if(CUDAToolkit_FIND_REQUIRED)
  537. message(FATAL_ERROR "Could not find nvcc, please set CUDAToolkit_ROOT.")
  538. elseif(NOT CUDAToolkit_FIND_QUIETLY)
  539. message(STATUS "Could not find nvcc, please set CUDAToolkit_ROOT.")
  540. endif()
  541. set(CUDAToolkit_FOUND FALSE)
  542. return()
  543. endif()
  544. endif()
  545. _CUDAToolkit_find_version_file( _CUDAToolkit_version_file )
  546. if(_CUDAToolkit_version_file)
  547. # CUDAToolkit_LIBRARY_ROOT contains the device library and version file.
  548. get_filename_component(CUDAToolkit_LIBRARY_ROOT "${_CUDAToolkit_version_file}" DIRECTORY ABSOLUTE)
  549. endif()
  550. unset(_CUDAToolkit_version_file)
  551. if(CUDAToolkit_NVCC_EXECUTABLE AND
  552. CMAKE_CUDA_COMPILER_VERSION AND
  553. CUDAToolkit_NVCC_EXECUTABLE STREQUAL CMAKE_CUDA_COMPILER)
  554. # Need to set these based off the already computed CMAKE_CUDA_COMPILER_VERSION value
  555. # This if statement will always match, but is used to provide variables for MATCH 1,2,3...
  556. if(CMAKE_CUDA_COMPILER_VERSION MATCHES [=[([0-9]+)\.([0-9]+)\.([0-9]+)]=])
  557. set(CUDAToolkit_VERSION_MAJOR "${CMAKE_MATCH_1}")
  558. set(CUDAToolkit_VERSION_MINOR "${CMAKE_MATCH_2}")
  559. set(CUDAToolkit_VERSION_PATCH "${CMAKE_MATCH_3}")
  560. set(CUDAToolkit_VERSION "${CMAKE_CUDA_COMPILER_VERSION}")
  561. endif()
  562. elseif(CUDAToolkit_NVCC_EXECUTABLE)
  563. # Compute the version by invoking nvcc
  564. execute_process(COMMAND ${CUDAToolkit_NVCC_EXECUTABLE} "--version" OUTPUT_VARIABLE NVCC_OUT)
  565. if(NVCC_OUT MATCHES [=[ V([0-9]+)\.([0-9]+)\.([0-9]+)]=])
  566. set(CUDAToolkit_VERSION_MAJOR "${CMAKE_MATCH_1}")
  567. set(CUDAToolkit_VERSION_MINOR "${CMAKE_MATCH_2}")
  568. set(CUDAToolkit_VERSION_PATCH "${CMAKE_MATCH_3}")
  569. set(CUDAToolkit_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}")
  570. endif()
  571. unset(NVCC_OUT)
  572. else()
  573. _CUDAToolkit_find_version_file(version_file)
  574. if(version_file)
  575. file(READ "${version_file}" VERSION_INFO)
  576. if(VERSION_INFO MATCHES [=[CUDA Version ([0-9]+)\.([0-9]+)\.([0-9]+)]=])
  577. set(CUDAToolkit_VERSION_MAJOR "${CMAKE_MATCH_1}")
  578. set(CUDAToolkit_VERSION_MINOR "${CMAKE_MATCH_2}")
  579. set(CUDAToolkit_VERSION_PATCH "${CMAKE_MATCH_3}")
  580. set(CUDAToolkit_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}")
  581. endif()
  582. endif()
  583. endif()
  584. endif()
  585. # Find target directory when crosscompiling.
  586. if(CMAKE_CROSSCOMPILING)
  587. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "armv7-a")
  588. # Support for NVPACK
  589. set(CUDAToolkit_TARGET_NAMES "armv7-linux-androideabi")
  590. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm")
  591. set(CUDAToolkit_TARGET_NAMES "armv7-linux-gnueabihf")
  592. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
  593. if(ANDROID_ARCH_NAME STREQUAL "arm64")
  594. set(CUDAToolkit_TARGET_NAMES "aarch64-linux-androideabi")
  595. elseif (CMAKE_SYSTEM_NAME STREQUAL "QNX")
  596. set(CUDAToolkit_TARGET_NAMES "aarch64-qnx")
  597. else()
  598. set(CUDAToolkit_TARGET_NAMES "aarch64-linux" "sbsa-linux")
  599. endif()
  600. elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
  601. set(CUDAToolkit_TARGET_NAMES "x86_64-linux")
  602. endif()
  603. foreach(CUDAToolkit_TARGET_NAME IN LISTS CUDAToolkit_TARGET_NAMES)
  604. if(EXISTS "${CUDAToolkit_ROOT_DIR}/targets/${CUDAToolkit_TARGET_NAME}")
  605. set(CUDAToolkit_TARGET_DIR "${CUDAToolkit_ROOT_DIR}/targets/${CUDAToolkit_TARGET_NAME}")
  606. # add known CUDA target root path to the set of directories we search for programs, libraries and headers
  607. list(PREPEND CMAKE_FIND_ROOT_PATH "${CUDAToolkit_TARGET_DIR}")
  608. # Mark that we need to pop the root search path changes after we have
  609. # found all cuda libraries so that searches for our cross-compilation
  610. # libraries work when another cuda sdk is in CMAKE_PREFIX_PATH or
  611. # PATh
  612. set(_CUDAToolkit_Pop_ROOT_PATH True)
  613. break()
  614. endif()
  615. endforeach()
  616. endif()
  617. # If not already set we can simply use the toolkit root or it's a scattered installation.
  618. if(NOT CUDAToolkit_TARGET_DIR)
  619. # Not cross compiling
  620. set(CUDAToolkit_TARGET_DIR "${CUDAToolkit_ROOT_DIR}")
  621. # Now that we have the real ROOT_DIR, find components inside it.
  622. list(APPEND CMAKE_PREFIX_PATH ${CUDAToolkit_ROOT_DIR})
  623. # Mark that we need to pop the prefix path changes after we have
  624. # found the cudart library.
  625. set(_CUDAToolkit_Pop_Prefix True)
  626. endif()
  627. # CUDAToolkit_TARGET_DIR always points to the directory containing the include directory.
  628. # On a scattered installation /usr, on a non-scattered something like /usr/local/cuda or /usr/local/cuda-10.2/targets/aarch64-linux.
  629. if(EXISTS "${CUDAToolkit_TARGET_DIR}/include/cuda_runtime.h")
  630. set(CUDAToolkit_INCLUDE_DIR "${CUDAToolkit_TARGET_DIR}/include")
  631. elseif(NOT CUDAToolkit_FIND_QUIETLY)
  632. message(STATUS "Unable to find cuda_runtime.h in \"${CUDAToolkit_TARGET_DIR}/include\" for CUDAToolkit_INCLUDE_DIR.")
  633. endif()
  634. # The NVHPC layout moves math library headers and libraries to a sibling directory and it could be nested under
  635. # the version of the CUDA toolchain
  636. # Create a separate variable so this directory can be selectively added to math targets.
  637. if(NOT EXISTS "${CUDAToolkit_INCLUDE_DIR}/cublas_v2.h")
  638. file(REAL_PATH "${CUDAToolkit_TARGET_DIR}" CUDAToolkit_MATH_INCLUDE_DIR)
  639. cmake_path(APPEND CUDAToolkit_MATH_INCLUDE_DIR "../../math_libs/")
  640. if(EXISTS "${CUDAToolkit_MATH_INCLUDE_DIR}/${CUDAToolkit_VERSION_MAJOR}.${CUDAToolkit_VERSION_MINOR}/")
  641. cmake_path(APPEND CUDAToolkit_MATH_INCLUDE_DIR "${CUDAToolkit_VERSION_MAJOR}.${CUDAToolkit_VERSION_MINOR}/")
  642. endif()
  643. cmake_path(APPEND CUDAToolkit_MATH_INCLUDE_DIR "include")
  644. cmake_path(NORMAL_PATH CUDAToolkit_MATH_INCLUDE_DIR)
  645. if(NOT EXISTS "${CUDAToolkit_MATH_INCLUDE_DIR}/cublas_v2.h")
  646. if(NOT CUDAToolkit_FIND_QUIETLY)
  647. message(STATUS "Unable to find cublas_v2.h in either \"${CUDAToolkit_INCLUDE_DIR}\" or \"${CUDAToolkit_MATH_INCLUDE_DIR}\"")
  648. endif()
  649. unset(CUDAToolkit_MATH_INCLUDE_DIR)
  650. endif()
  651. endif()
  652. # Find the CUDA Runtime Library libcudart
  653. find_library(CUDA_CUDART
  654. NAMES cudart
  655. PATH_SUFFIXES lib64 lib/x64
  656. )
  657. find_library(CUDA_CUDART
  658. NAMES cudart
  659. PATH_SUFFIXES lib64/stubs lib/x64/stubs
  660. )
  661. if(NOT CUDA_CUDART AND NOT CUDAToolkit_FIND_QUIETLY)
  662. message(STATUS "Unable to find cudart library.")
  663. endif()
  664. if(_CUDAToolkit_Pop_Prefix)
  665. list(REMOVE_AT CMAKE_PREFIX_PATH -1)
  666. unset(_CUDAToolkit_Pop_Prefix)
  667. endif()
  668. #-----------------------------------------------------------------------------
  669. # Perform version comparison and validate all required variables are set.
  670. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  671. find_package_handle_standard_args(CUDAToolkit
  672. REQUIRED_VARS
  673. CUDAToolkit_INCLUDE_DIR
  674. CUDA_CUDART
  675. CUDAToolkit_BIN_DIR
  676. VERSION_VAR
  677. CUDAToolkit_VERSION
  678. )
  679. unset(CUDAToolkit_ROOT_DIR)
  680. mark_as_advanced(CUDA_CUDART
  681. CUDAToolkit_INCLUDE_DIR
  682. CUDAToolkit_NVCC_EXECUTABLE
  683. CUDAToolkit_SENTINEL_FILE
  684. )
  685. #-----------------------------------------------------------------------------
  686. # Construct result variables
  687. if(CUDAToolkit_FOUND)
  688. set(CUDAToolkit_INCLUDE_DIRS ${CUDAToolkit_INCLUDE_DIR})
  689. get_filename_component(CUDAToolkit_LIBRARY_DIR ${CUDA_CUDART} DIRECTORY ABSOLUTE)
  690. # Build search paths without any symlinks
  691. file(REAL_PATH "${CUDAToolkit_LIBRARY_DIR}" _cmake_search_dir)
  692. set(CUDAToolkit_LIBRARY_SEARCH_DIRS "${_cmake_search_dir}")
  693. # Detect we are in a splayed nvhpc toolkit layout and add extra
  694. # search paths without symlinks
  695. if(CUDAToolkit_LIBRARY_DIR MATCHES ".*/cuda/${CUDAToolkit_VERSION_MAJOR}.${CUDAToolkit_VERSION_MINOR}/lib64$")
  696. # Search location for math_libs/
  697. file(REAL_PATH "${CUDAToolkit_LIBRARY_DIR}/../../../" _cmake_search_dir)
  698. list(APPEND CUDAToolkit_LIBRARY_SEARCH_DIRS "${_cmake_search_dir}")
  699. # Search location for extras like cupti
  700. file(REAL_PATH "${CUDAToolkit_LIBRARY_DIR}/../" _cmake_search_dir)
  701. list(APPEND CUDAToolkit_LIBRARY_SEARCH_DIRS "${_cmake_search_dir}")
  702. endif()
  703. endif()
  704. #-----------------------------------------------------------------------------
  705. # Construct import targets
  706. if(CUDAToolkit_FOUND)
  707. function(_CUDAToolkit_find_and_add_import_lib lib_name)
  708. cmake_parse_arguments(arg "" "" "ALT;DEPS;EXTRA_PATH_SUFFIXES;EXTRA_INCLUDE_DIRS" ${ARGN})
  709. set(search_names ${lib_name} ${arg_ALT})
  710. find_library(CUDA_${lib_name}_LIBRARY
  711. NAMES ${search_names}
  712. HINTS ${CUDAToolkit_LIBRARY_SEARCH_DIRS}
  713. ENV CUDA_PATH
  714. PATH_SUFFIXES nvidia/current lib64 lib/x64 lib
  715. # Support NVHPC splayed math library layout
  716. math_libs/${CUDAToolkit_VERSION_MAJOR}.${CUDAToolkit_VERSION_MINOR}/lib64
  717. math_libs/lib64
  718. ${arg_EXTRA_PATH_SUFFIXES}
  719. )
  720. # Don't try any stub directories until we have exhausted all other
  721. # search locations.
  722. find_library(CUDA_${lib_name}_LIBRARY
  723. NAMES ${search_names}
  724. HINTS ${CUDAToolkit_LIBRARY_SEARCH_DIRS}
  725. ENV CUDA_PATH
  726. PATH_SUFFIXES lib64/stubs lib/x64/stubs lib/stubs stubs
  727. )
  728. mark_as_advanced(CUDA_${lib_name}_LIBRARY)
  729. if (NOT TARGET CUDA::${lib_name} AND CUDA_${lib_name}_LIBRARY)
  730. add_library(CUDA::${lib_name} UNKNOWN IMPORTED)
  731. target_include_directories(CUDA::${lib_name} SYSTEM INTERFACE "${CUDAToolkit_INCLUDE_DIRS}")
  732. if(DEFINED CUDAToolkit_MATH_INCLUDE_DIR)
  733. string(FIND ${CUDA_${lib_name}_LIBRARY} "math_libs" math_libs)
  734. if(NOT ${math_libs} EQUAL -1)
  735. target_include_directories(CUDA::${lib_name} SYSTEM INTERFACE "${CUDAToolkit_MATH_INCLUDE_DIR}")
  736. endif()
  737. endif()
  738. set_property(TARGET CUDA::${lib_name} PROPERTY IMPORTED_LOCATION "${CUDA_${lib_name}_LIBRARY}")
  739. foreach(dep ${arg_DEPS})
  740. if(TARGET CUDA::${dep})
  741. target_link_libraries(CUDA::${lib_name} INTERFACE CUDA::${dep})
  742. endif()
  743. endforeach()
  744. if(arg_EXTRA_INCLUDE_DIRS)
  745. target_include_directories(CUDA::${lib_name} SYSTEM INTERFACE "${arg_EXTRA_INCLUDE_DIRS}")
  746. endif()
  747. endif()
  748. endfunction()
  749. if(NOT TARGET CUDA::toolkit)
  750. add_library(CUDA::toolkit IMPORTED INTERFACE)
  751. target_include_directories(CUDA::toolkit SYSTEM INTERFACE "${CUDAToolkit_INCLUDE_DIRS}")
  752. target_link_directories(CUDA::toolkit INTERFACE "${CUDAToolkit_LIBRARY_DIR}")
  753. endif()
  754. _CUDAToolkit_find_and_add_import_lib(cuda_driver ALT cuda)
  755. _CUDAToolkit_find_and_add_import_lib(cudart)
  756. _CUDAToolkit_find_and_add_import_lib(cudart_static)
  757. # setup dependencies that are required for cudart_static when building
  758. # on linux. These are generally only required when using the CUDA toolkit
  759. # when CUDA language is disabled
  760. if(NOT TARGET CUDA::cudart_static_deps
  761. AND TARGET CUDA::cudart_static)
  762. add_library(CUDA::cudart_static_deps IMPORTED INTERFACE)
  763. target_link_libraries(CUDA::cudart_static INTERFACE CUDA::cudart_static_deps)
  764. if(UNIX AND (CMAKE_C_COMPILER OR CMAKE_CXX_COMPILER))
  765. find_package(Threads REQUIRED)
  766. target_link_libraries(CUDA::cudart_static_deps INTERFACE Threads::Threads ${CMAKE_DL_LIBS})
  767. endif()
  768. if(UNIX AND NOT APPLE AND NOT (CMAKE_SYSTEM_NAME STREQUAL "QNX"))
  769. # On Linux, you must link against librt when using the static cuda runtime.
  770. find_library(CUDAToolkit_rt_LIBRARY rt)
  771. mark_as_advanced(CUDAToolkit_rt_LIBRARY)
  772. if(NOT CUDAToolkit_rt_LIBRARY)
  773. message(WARNING "Could not find librt library, needed by CUDA::cudart_static")
  774. else()
  775. target_link_libraries(CUDA::cudart_static_deps INTERFACE ${CUDAToolkit_rt_LIBRARY})
  776. endif()
  777. endif()
  778. endif()
  779. _CUDAToolkit_find_and_add_import_lib(culibos) # it's a static library
  780. foreach (cuda_lib cublasLt cufft curand cusparse nppc nvjpeg)
  781. _CUDAToolkit_find_and_add_import_lib(${cuda_lib})
  782. _CUDAToolkit_find_and_add_import_lib(${cuda_lib}_static DEPS culibos)
  783. endforeach()
  784. if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 11.0.0)
  785. # cublas depends on cublasLt
  786. # https://docs.nvidia.com/cuda/archive/11.0/cublas/index.html#static-library
  787. _CUDAToolkit_find_and_add_import_lib(cublas DEPS cublasLt culibos)
  788. _CUDAToolkit_find_and_add_import_lib(cublas_static DEPS cublasLt_static culibos)
  789. else()
  790. _CUDAToolkit_find_and_add_import_lib(cublas DEPS culibos)
  791. _CUDAToolkit_find_and_add_import_lib(cublas_static DEPS culibos)
  792. endif()
  793. if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 11.4)
  794. _CUDAToolkit_find_and_add_import_lib(cuFile DEPS culibos)
  795. _CUDAToolkit_find_and_add_import_lib(cuFile_static DEPS culibos)
  796. _CUDAToolkit_find_and_add_import_lib(cuFile_rdma DEPS cuFile culibos)
  797. _CUDAToolkit_find_and_add_import_lib(cuFile_rdma_static DEPS cuFile_static culibos)
  798. endif()
  799. # cuFFTW depends on cuFFT
  800. _CUDAToolkit_find_and_add_import_lib(cufftw DEPS cufft)
  801. _CUDAToolkit_find_and_add_import_lib(cufftw_static DEPS cufft_static)
  802. if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 9.2)
  803. _CUDAToolkit_find_and_add_import_lib(cufft_static_nocallback DEPS culibos)
  804. endif()
  805. # cuSOLVER depends on cuBLAS, and cuSPARSE
  806. set(cusolver_deps cublas cusparse)
  807. set(cusolver_static_deps cublas_static cusparse_static culibos)
  808. if(CUDAToolkit_VERSION VERSION_GREATER 11.2.1)
  809. # cusolver depends on libcusolver_metis and cublasLt
  810. # https://docs.nvidia.com/cuda/archive/11.2.2/cusolver/index.html#link-dependency
  811. list(APPEND cusolver_deps cublasLt)
  812. _CUDAToolkit_find_and_add_import_lib(cusolver_metis_static ALT metis_static) # implementation detail static lib
  813. list(APPEND cusolver_static_deps cusolver_metis_static cublasLt_static)
  814. endif()
  815. if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 10.1.2)
  816. # cusolver depends on liblapack_static.a starting with CUDA 10.1 update 2,
  817. # https://docs.nvidia.com/cuda/archive/11.5.0/cusolver/index.html#static-link-lapack
  818. _CUDAToolkit_find_and_add_import_lib(cusolver_lapack_static ALT lapack_static) # implementation detail static lib
  819. list(APPEND cusolver_static_deps cusolver_lapack_static)
  820. endif()
  821. _CUDAToolkit_find_and_add_import_lib(cusolver DEPS ${cusolver_deps})
  822. _CUDAToolkit_find_and_add_import_lib(cusolver_static DEPS ${cusolver_static_deps})
  823. unset(cusolver_deps)
  824. unset(cusolver_static_deps)
  825. # nvGRAPH depends on cuRAND, and cuSOLVER.
  826. _CUDAToolkit_find_and_add_import_lib(nvgraph DEPS curand cusolver)
  827. _CUDAToolkit_find_and_add_import_lib(nvgraph_static DEPS curand_static cusolver_static)
  828. # Process the majority of the NPP libraries.
  829. foreach (cuda_lib nppial nppicc nppidei nppif nppig nppim nppist nppitc npps nppicom nppisu)
  830. _CUDAToolkit_find_and_add_import_lib(${cuda_lib} DEPS nppc)
  831. _CUDAToolkit_find_and_add_import_lib(${cuda_lib}_static DEPS nppc_static)
  832. endforeach()
  833. find_path(CUDAToolkit_CUPTI_INCLUDE_DIR cupti.h PATHS
  834. "${CUDAToolkit_ROOT_DIR}/extras/CUPTI/include"
  835. "${CUDAToolkit_INCLUDE_DIR}/../extras/CUPTI/include"
  836. "${CUDAToolkit_INCLUDE_DIR}"
  837. NO_DEFAULT_PATH)
  838. if(CUDAToolkit_CUPTI_INCLUDE_DIR)
  839. _CUDAToolkit_find_and_add_import_lib(cupti
  840. EXTRA_PATH_SUFFIXES extras/CUPTI/lib64/
  841. extras/CUPTI/lib/
  842. ../extras/CUPTI/lib64/
  843. ../extras/CUPTI/lib/
  844. EXTRA_INCLUDE_DIRS "${CUDAToolkit_CUPTI_INCLUDE_DIR}")
  845. _CUDAToolkit_find_and_add_import_lib(cupti_static
  846. EXTRA_PATH_SUFFIXES extras/CUPTI/lib64/
  847. extras/CUPTI/lib/
  848. ../extras/CUPTI/lib64/
  849. ../extras/CUPTI/lib/
  850. EXTRA_INCLUDE_DIRS "${CUDAToolkit_CUPTI_INCLUDE_DIR}")
  851. endif()
  852. _CUDAToolkit_find_and_add_import_lib(nvrtc DEPS cuda_driver)
  853. if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 11.1.0)
  854. if(NOT TARGET CUDA::nvptxcompiler_static)
  855. _CUDAToolkit_find_and_add_import_lib(nvptxcompiler_static DEPS cuda_driver)
  856. if(TARGET CUDA::nvptxcompiler_static)
  857. target_link_libraries(CUDA::nvptxcompiler_static INTERFACE Threads::Threads)
  858. endif()
  859. endif()
  860. endif()
  861. if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 12.0.0)
  862. _CUDAToolkit_find_and_add_import_lib(nvJitLink DEPS cuda_driver)
  863. _CUDAToolkit_find_and_add_import_lib(nvJitLink_static DEPS cuda_driver)
  864. endif()
  865. _CUDAToolkit_find_and_add_import_lib(nvml ALT nvidia-ml nvml)
  866. if(WIN32)
  867. # nvtools can be installed outside the CUDA toolkit directory
  868. # so prefer the NVTOOLSEXT_PATH windows only environment variable
  869. # In addition on windows the most common name is nvToolsExt64_1
  870. find_library(CUDA_nvToolsExt_LIBRARY
  871. NAMES nvToolsExt64_1 nvToolsExt64 nvToolsExt
  872. PATHS ENV NVTOOLSEXT_PATH
  873. ENV CUDA_PATH
  874. PATH_SUFFIXES lib/x64 lib
  875. )
  876. endif()
  877. _CUDAToolkit_find_and_add_import_lib(nvToolsExt ALT nvToolsExt64)
  878. if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 10.0)
  879. # nvToolsExt is deprecated since nvtx3 introduction.
  880. # Warn only if the project requires a sufficiently new CMake to make migration possible.
  881. if(TARGET CUDA::nvToolsExt AND CMAKE_MINIMUM_REQUIRED_VERSION VERSION_GREATER_EQUAL 3.25)
  882. set_property(TARGET CUDA::nvToolsExt PROPERTY DEPRECATION "nvToolsExt has been superseded by nvtx3 since CUDA 10.0 and CMake 3.25. Use CUDA::nvtx3 and include <nvtx3/nvToolsExt.h> instead.")
  883. endif()
  884. # Header-only variant. Uses dlopen().
  885. if(NOT TARGET CUDA::nvtx3)
  886. add_library(CUDA::nvtx3 INTERFACE IMPORTED)
  887. target_include_directories(CUDA::nvtx3 SYSTEM INTERFACE "${CUDAToolkit_INCLUDE_DIRS}")
  888. target_link_libraries(CUDA::nvtx3 INTERFACE ${CMAKE_DL_LIBS})
  889. endif()
  890. endif()
  891. _CUDAToolkit_find_and_add_import_lib(OpenCL)
  892. endif()
  893. if(_CUDAToolkit_Pop_ROOT_PATH)
  894. list(REMOVE_AT CMAKE_FIND_ROOT_PATH 0)
  895. unset(_CUDAToolkit_Pop_ROOT_PATH)
  896. endif()