FindCUDAToolkit.cmake 45 KB

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