FindCUDAToolkit.cmake 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindCUDAToolkit
  5. ---------------
  6. .. versionadded:: 3.17
  7. Finds the NVIDIA CUDA toolkit and the associated libraries, but does not
  8. require the ``CUDA`` language be enabled for a given project:
  9. .. code-block:: cmake
  10. find_package(CUDAToolkit [<version>] [QUIET] [REQUIRED] [EXACT] [...])
  11. This module does not search for the NVIDIA CUDA Samples.
  12. .. versionadded:: 3.19
  13. QNX support.
  14. Search Behavior
  15. ^^^^^^^^^^^^^^^
  16. The CUDA Toolkit search behavior uses the following order:
  17. 1. If the ``CUDA`` language has been enabled we will use the directory
  18. containing the compiler as the first search location for ``nvcc``.
  19. 2. If the variable :variable:`CMAKE_CUDA_COMPILER <CMAKE_<LANG>_COMPILER>` or
  20. the environment variable :envvar:`CUDACXX` is defined, it will be used
  21. as the path to the ``nvcc`` executable.
  22. 3. If the ``CUDAToolkit_ROOT`` cmake configuration variable (e.g.,
  23. ``-DCUDAToolkit_ROOT=/some/path``) *or* environment variable is defined, it
  24. will be searched. If both an environment variable **and** a
  25. configuration variable are specified, the *configuration* variable takes
  26. precedence.
  27. The directory specified here must be such that the executable ``nvcc`` or
  28. the appropriate ``version.txt`` or ``version.json`` file can be found
  29. underneath the specified directory.
  30. 4. If the CUDA_PATH environment variable is defined, it will be searched
  31. for ``nvcc``.
  32. 5. The user's path is searched for ``nvcc`` using :command:`find_program`. If
  33. this is found, no subsequent search attempts are performed. Users are
  34. responsible for ensuring that the first ``nvcc`` to show up in the path is
  35. the desired path in the event that multiple CUDA Toolkits are installed.
  36. 6. On Unix systems, if the symbolic link ``/usr/local/cuda`` exists, this is
  37. used. No subsequent search attempts are performed. No default symbolic link
  38. location exists for the Windows platform.
  39. 7. The platform specific default install locations are searched. If exactly one
  40. candidate is found, this is used. The default CUDA Toolkit install locations
  41. searched are:
  42. +-------------+-------------------------------------------------------------+
  43. | Platform | Search Pattern |
  44. +=============+=============================================================+
  45. | macOS | ``/Developer/NVIDIA/CUDA-X.Y`` |
  46. +-------------+-------------------------------------------------------------+
  47. | Other Unix | ``/usr/local/cuda-X.Y`` |
  48. +-------------+-------------------------------------------------------------+
  49. | Windows | ``C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vX.Y`` |
  50. +-------------+-------------------------------------------------------------+
  51. Where ``X.Y`` would be a specific version of the CUDA Toolkit, such as
  52. ``/usr/local/cuda-9.0`` or
  53. ``C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0``
  54. .. note::
  55. When multiple CUDA Toolkits are installed in the default location of a
  56. system (e.g., both ``/usr/local/cuda-9.0`` and ``/usr/local/cuda-10.0``
  57. exist but the ``/usr/local/cuda`` symbolic link does **not** exist), this
  58. package is marked as **not** found.
  59. There are too many factors involved in making an automatic decision in
  60. the presence of multiple CUDA Toolkits being installed. In this
  61. situation, users are encouraged to either (1) set ``CUDAToolkit_ROOT`` or
  62. (2) ensure that the correct ``nvcc`` executable shows up in ``$PATH`` for
  63. :command:`find_program` to find.
  64. Arguments
  65. ^^^^^^^^^
  66. ``[<version>]``
  67. The ``[<version>]`` argument requests a version with which the package found
  68. should be compatible. See :ref:`find_package version format <FIND_PACKAGE_VERSION_FORMAT>`
  69. for more details.
  70. Options
  71. ^^^^^^^
  72. ``REQUIRED``
  73. If specified, configuration will error if a suitable CUDA Toolkit is not
  74. found.
  75. ``QUIET``
  76. If specified, the search for a suitable CUDA Toolkit will not produce any
  77. messages.
  78. ``EXACT``
  79. If specified, the CUDA Toolkit is considered found only if the exact
  80. ``VERSION`` specified is recovered.
  81. Imported Targets
  82. ^^^^^^^^^^^^^^^^
  83. An :ref:`imported target <Imported targets>` named ``CUDA::toolkit`` is provided.
  84. This module provides :ref:`Imported Targets` for each
  85. of the following libraries that are part of the CUDAToolkit:
  86. - `CUDA Runtime Library`_
  87. - `CUDA Driver Library`_
  88. - `cuBLAS`_
  89. - `cuDLA`_
  90. - `cuFile`_
  91. - `cuFFT`_
  92. - `cuRAND`_
  93. - `cuSOLVER`_
  94. - `cuSPARSE`_
  95. - `cuPTI`_
  96. - `NPP`_
  97. - `nvBLAS`_
  98. - `nvGRAPH`_
  99. - `nvJPEG`_
  100. - `nvidia-ML`_
  101. - `nvPTX Compiler`_
  102. - `nvRTC`_
  103. - `nvJitLink`_
  104. - `nvFatBin`_
  105. - `nvToolsExt`_
  106. - `nvtx3`_
  107. - `OpenCL`_
  108. - `cuLIBOS`_
  109. CUDA Runtime Library
  110. """"""""""""""""""""
  111. The CUDA Runtime library (cudart) are what most applications will typically
  112. need to link against to make any calls such as `cudaMalloc`, and `cudaFree`.
  113. Targets Created:
  114. - ``CUDA::cudart``
  115. - ``CUDA::cudart_static``
  116. CUDA Driver Library
  117. """"""""""""""""""""
  118. The CUDA Driver library (cuda) are used by applications that use calls
  119. such as `cuMemAlloc`, and `cuMemFree`.
  120. Targets Created:
  121. - ``CUDA::cuda_driver``
  122. cuBLAS
  123. """"""
  124. The `CUDA Basic Linear Algebra Subroutine`_ library.
  125. Targets Created:
  126. - ``CUDA::cublas``
  127. - ``CUDA::cublas_static``
  128. - ``CUDA::cublasLt`` starting in CUDA 10.1
  129. - ``CUDA::cublasLt_static`` starting in CUDA 10.1
  130. .. _`CUDA Basic Linear Algebra Subroutine`: https://docs.nvidia.com/cuda/cublas
  131. cuDLA
  132. """"""
  133. .. versionadded:: 3.27
  134. The `NVIDIA Tegra Deep Learning Accelerator`_ library.
  135. Targets Created:
  136. - ``CUDA::cudla`` starting in CUDA 11.6
  137. .. _`NVIDIA Tegra Deep Learning Accelerator`: https://docs.nvidia.com/cuda/cuda-for-tegra-appnote#cudla
  138. cuFile
  139. """"""
  140. .. versionadded:: 3.25
  141. The `NVIDIA GPUDirect Storage cuFile`_ library.
  142. Targets Created:
  143. - ``CUDA::cuFile`` starting in CUDA 11.4
  144. - ``CUDA::cuFile_static`` starting in CUDA 11.4
  145. - ``CUDA::cuFile_rdma`` starting in CUDA 11.4
  146. - ``CUDA::cuFile_rdma_static`` starting in CUDA 11.4
  147. .. _`NVIDIA GPUDirect Storage cuFile`: https://docs.nvidia.com/gpudirect-storage/api-reference-guide
  148. cuFFT
  149. """""
  150. The `CUDA Fast Fourier Transform`_ library.
  151. Targets Created:
  152. - ``CUDA::cufft``
  153. - ``CUDA::cufftw``
  154. - ``CUDA::cufft_static``
  155. - ``CUDA::cufft_static_nocallback`` starting in CUDA 9.2, requires CMake 3.23+
  156. - ``CUDA::cufftw_static``
  157. .. _`CUDA Fast Fourier Transform`: https://docs.nvidia.com/cuda/cufft
  158. cuRAND
  159. """"""
  160. The `CUDA random number generation`_ library.
  161. Targets Created:
  162. - ``CUDA::curand``
  163. - ``CUDA::curand_static``
  164. .. _`CUDA random number generation`: https://docs.nvidia.com/cuda/curand
  165. cuSOLVER
  166. """"""""
  167. A `GPU accelerated linear system solver`_ library.
  168. Targets Created:
  169. - ``CUDA::cusolver``
  170. - ``CUDA::cusolver_static``
  171. .. _`GPU accelerated linear system solver`: https://docs.nvidia.com/cuda/cusolver
  172. cuSPARSE
  173. """"""""
  174. The `CUDA sparse matrix`_ library.
  175. Targets Created:
  176. - ``CUDA::cusparse``
  177. - ``CUDA::cusparse_static``
  178. .. _`CUDA sparse matrix`: https://docs.nvidia.com/cuda/cusparse
  179. cupti
  180. """""
  181. The `NVIDIA CUDA Profiling Tools Interface`_.
  182. Targets Created:
  183. - ``CUDA::cupti``
  184. - ``CUDA::cupti_static``
  185. .. versionadded:: 3.27
  186. - ``CUDA::nvperf_host`` starting in CUDA 10.2
  187. - ``CUDA::nvperf_host_static`` starting in CUDA 10.2
  188. - ``CUDA::nvperf_target`` starting in CUDA 10.2
  189. - ``CUDA::pcsamplingutil`` starting in CUDA 11.3
  190. .. _`NVIDIA CUDA Profiling Tools Interface`: https://developer.nvidia.com/cupti
  191. NPP
  192. """
  193. The `NVIDIA 2D Image and Signal Processing Performance Primitives`_ libraries.
  194. Targets Created:
  195. - `nppc`:
  196. - ``CUDA::nppc``
  197. - ``CUDA::nppc_static``
  198. - `nppial`: Arithmetic and logical operation functions in `nppi_arithmetic_and_logical_operations.h`
  199. - ``CUDA::nppial``
  200. - ``CUDA::nppial_static``
  201. - `nppicc`: Color conversion and sampling functions in `nppi_color_conversion.h`
  202. - ``CUDA::nppicc``
  203. - ``CUDA::nppicc_static``
  204. - `nppicom`: JPEG compression and decompression functions in `nppi_compression_functions.h`
  205. Removed starting in CUDA 11.0, use `nvJPEG`_ instead.
  206. - ``CUDA::nppicom``
  207. - ``CUDA::nppicom_static``
  208. - `nppidei`: Data exchange and initialization functions in `nppi_data_exchange_and_initialization.h`
  209. - ``CUDA::nppidei``
  210. - ``CUDA::nppidei_static``
  211. - `nppif`: Filtering and computer vision functions in `nppi_filter_functions.h`
  212. - ``CUDA::nppif``
  213. - ``CUDA::nppif_static``
  214. - `nppig`: Geometry transformation functions found in `nppi_geometry_transforms.h`
  215. - ``CUDA::nppig``
  216. - ``CUDA::nppig_static``
  217. - `nppim`: Morphological operation functions found in `nppi_morphological_operations.h`
  218. - ``CUDA::nppim``
  219. - ``CUDA::nppim_static``
  220. - `nppist`: Statistics and linear transform in `nppi_statistics_functions.h` and `nppi_linear_transforms.h`
  221. - ``CUDA::nppist``
  222. - ``CUDA::nppist_static``
  223. - `nppisu`: Memory support functions in `nppi_support_functions.h`
  224. - ``CUDA::nppisu``
  225. - ``CUDA::nppisu_static``
  226. - `nppitc`: Threshold and compare operation functions in `nppi_threshold_and_compare_operations.h`
  227. - ``CUDA::nppitc``
  228. - ``CUDA::nppitc_static``
  229. - `npps`:
  230. - ``CUDA::npps``
  231. - ``CUDA::npps_static``
  232. .. _`NVIDIA 2D Image and Signal Processing Performance Primitives`: https://docs.nvidia.com/cuda/npp
  233. nvBLAS
  234. """"""
  235. The `GPU-accelerated drop-in BLAS`_ library.
  236. This is a shared library only.
  237. Targets Created:
  238. - ``CUDA::nvblas``
  239. .. _`GPU-accelerated drop-in BLAS`: https://docs.nvidia.com/cuda/nvblas
  240. nvGRAPH
  241. """""""
  242. A `GPU-accelerated graph analytics`_ library.
  243. Removed starting in CUDA 11.0
  244. Targets Created:
  245. - ``CUDA::nvgraph``
  246. - ``CUDA::nvgraph_static``
  247. .. _`GPU-accelerated graph analytics`: https://docs.nvidia.com/cuda/archive/10.0/nvgraph
  248. nvJPEG
  249. """"""
  250. A `GPU-accelerated JPEG codec`_ library.
  251. Introduced in CUDA 10.
  252. Targets Created:
  253. - ``CUDA::nvjpeg``
  254. - ``CUDA::nvjpeg_static``
  255. .. _`GPU-accelerated JPEG codec`: https://docs.nvidia.com/cuda/nvjpeg
  256. nvPTX Compiler
  257. """"""""""""""
  258. .. versionadded:: 3.25
  259. The `PTX Compiler APIs`_.
  260. These are a set of APIs which can be used to compile a PTX program into GPU assembly code.
  261. Introduced in CUDA 11.1
  262. This is a static library only.
  263. Targets Created:
  264. - ``CUDA::nvptxcompiler_static`` starting in CUDA 11.1
  265. .. _`PTX Compiler APIs`: https://docs.nvidia.com/cuda/ptx-compiler-api
  266. nvRTC
  267. """""
  268. A `runtime compilation library for CUDA`_.
  269. Targets Created:
  270. - ``CUDA::nvrtc``
  271. .. versionadded:: 3.26
  272. - ``CUDA::nvrtc_builtins``
  273. - ``CUDA::nvrtc_static`` starting in CUDA 11.5
  274. - ``CUDA::nvrtc_builtins_static`` starting in CUDA 11.5
  275. .. _`runtime compilation library for CUDA`: https://docs.nvidia.com/cuda/nvrtc
  276. nvJitLink
  277. """""""""
  278. The `JIT Link APIs`_.
  279. Targets Created:
  280. - ``CUDA::nvJitLink`` starting in CUDA 12.0
  281. - ``CUDA::nvJitLink_static`` starting in CUDA 12.0
  282. .. _`JIT Link APIs`: https://docs.nvidia.com/cuda/nvjitlink
  283. nvFatBin
  284. """""""""
  285. .. versionadded:: 3.30
  286. The `Fatbin Creator APIs`_.
  287. Targets Created:
  288. - ``CUDA::nvfatbin`` starting in CUDA 12.4
  289. - ``CUDA::nvfatbin_static`` starting in CUDA 12.4
  290. .. _`Fatbin Creator APIs`: https://docs.nvidia.com/cuda/nvfatbin
  291. nvidia-ML
  292. """""""""
  293. The `NVIDIA Management Library`_.
  294. Targets Created:
  295. - ``CUDA::nvml``
  296. - ``CUDA::nvml_static`` starting in CUDA 12.4
  297. .. versionadded:: 3.31
  298. Added ``CUDA::nvml_static``.
  299. .. _`NVIDIA Management Library`: https://developer.nvidia.com/management-library-nvml
  300. .. _`FindCUDAToolkit_nvToolsExt`:
  301. nvToolsExt
  302. """"""""""
  303. .. deprecated:: 3.25
  304. With CUDA 10.0+, use `nvtx3`_.
  305. Starting in CUDA 12.9 the `nvToolsExt` library no longer exists
  306. The `legacy NVIDIA Tools Extension`_.
  307. This is a shared library only.
  308. Targets Created:
  309. - ``CUDA::nvToolsExt``
  310. .. _`legacy NVIDIA Tools Extension`: https://docs.nvidia.com/cuda/archive/9.0/profiler-users-guide#nvtx
  311. .. _`FindCUDAToolkit_nvtx3`:
  312. nvtx3
  313. """""
  314. .. versionadded:: 3.25
  315. The header-only `NVIDIA Tools Extension`_ library.
  316. Introduced in CUDA 10.0.
  317. Targets created:
  318. - ``CUDA::nvtx3``
  319. - ``CUDA::nvtx3_interop``
  320. .. versionadded:: 4.1
  321. This is provided by CUDA 12.9 and above for use by languages that
  322. cannot consume C++ header-only libraries, such as ``Fortran``.
  323. .. _`NVIDIA Tools Extension`: https://nvidia.github.io/NVTX/doxygen
  324. OpenCL
  325. """"""
  326. The `NVIDIA Open Computing Language`_ library.
  327. This is a shared library only.
  328. Targets Created:
  329. - ``CUDA::OpenCL``
  330. .. _`NVIDIA Open Computing Language`: https://developer.nvidia.com/opencl
  331. cuLIBOS
  332. """""""
  333. The cuLIBOS library is a backend thread abstraction layer library which is
  334. static only. The ``CUDA::cublas_static``, ``CUDA::cusparse_static``,
  335. ``CUDA::cufft_static``, ``CUDA::curand_static``, and (when implemented) NPP
  336. libraries all automatically have this dependency linked.
  337. Target Created:
  338. - ``CUDA::culibos``
  339. **Note**: direct usage of this target by consumers should not be necessary.
  340. .. _`FindCUDAToolkit_bin2c`:
  341. bin2c
  342. """""
  343. .. versionadded:: 4.3
  344. A utility that converts binary files to C files containing byte arrays.
  345. Target Created:
  346. - ``CUDA::bin2c``
  347. Result Variables
  348. ^^^^^^^^^^^^^^^^
  349. This module defines the following variables:
  350. ``CUDAToolkit_FOUND``
  351. A boolean specifying whether or not the CUDA Toolkit was found.
  352. ``CUDAToolkit_VERSION``
  353. The exact version of the CUDA Toolkit found (as reported by
  354. ``nvcc --version``, ``version.txt``, or ``version.json``).
  355. ``CUDAToolkit_VERSION_MAJOR``
  356. The major version of the CUDA Toolkit.
  357. ``CUDAToolkit_VERSION_MINOR``
  358. The minor version of the CUDA Toolkit.
  359. ``CUDAToolkit_VERSION_PATCH``
  360. The patch version of the CUDA Toolkit.
  361. ``CUDAToolkit_BIN_DIR``
  362. The path to the CUDA Toolkit library directory that contains the CUDA
  363. executable ``nvcc``.
  364. ``CUDAToolkit_INCLUDE_DIRS``
  365. List of paths to all the CUDA Toolkit folders containing header files
  366. required to compile a project linking against CUDA.
  367. ``CUDAToolkit_LIBRARY_DIR``
  368. The path to the CUDA Toolkit library directory that contains the CUDA
  369. Runtime library ``cudart``.
  370. ``CUDAToolkit_LIBRARY_ROOT``
  371. .. versionadded:: 3.18
  372. The path to the CUDA Toolkit directory containing the nvvm directory and
  373. either version.txt or version.json.
  374. ``CUDAToolkit_TARGET_DIR``
  375. The path to the CUDA Toolkit directory including the target architecture
  376. when cross-compiling. When not cross-compiling this will be equivalent to
  377. the parent directory of ``CUDAToolkit_BIN_DIR``.
  378. ``CUDAToolkit_NVCC_EXECUTABLE``
  379. The path to the NVIDIA CUDA compiler ``nvcc``. Note that this path may
  380. **not** be the same as
  381. :variable:`CMAKE_CUDA_COMPILER <CMAKE_<LANG>_COMPILER>`. ``nvcc`` must be
  382. found to determine the CUDA Toolkit version as well as determining other
  383. features of the Toolkit. This variable is set for the convenience of
  384. modules that depend on this one.
  385. #]=======================================================================]
  386. # NOTE: much of this was simply extracted from FindCUDA.cmake.
  387. # James Bigler, NVIDIA Corp (nvidia.com - jbigler)
  388. # Abe Stephens, SCI Institute -- http://www.sci.utah.edu/~abe/FindCuda.html
  389. #
  390. # Copyright (c) 2008 - 2009 NVIDIA Corporation. All rights reserved.
  391. #
  392. # Copyright (c) 2007-2009
  393. # Scientific Computing and Imaging Institute, University of Utah
  394. #
  395. # This code is licensed under the MIT License. See the FindCUDA.cmake script
  396. # for the text of the license.
  397. # The MIT License
  398. #
  399. # License for the specific language governing rights and limitations under
  400. # Permission is hereby granted, free of charge, to any person obtaining a
  401. # copy of this software and associated documentation files (the "Software"),
  402. # to deal in the Software without restriction, including without limitation
  403. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  404. # and/or sell copies of the Software, and to permit persons to whom the
  405. # Software is furnished to do so, subject to the following conditions:
  406. #
  407. # The above copyright notice and this permission notice shall be included
  408. # in all copies or substantial portions of the Software.
  409. #
  410. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  411. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  412. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  413. # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  414. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  415. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  416. # DEALINGS IN THE SOFTWARE.
  417. #
  418. ###############################################################################
  419. function(_CUDAToolkit_build_include_dirs result_variable default_paths_variable)
  420. set(content "${${default_paths_variable}}")
  421. set(${result_variable} "${content}" PARENT_SCOPE)
  422. endfunction()
  423. function(_CUDAToolkit_build_library_dirs result_variable default_paths_variable)
  424. set(content "${${default_paths_variable}}")
  425. set(${result_variable} "${content}" PARENT_SCOPE)
  426. endfunction()
  427. # The toolkit is located during compiler detection for CUDA and stored in CMakeCUDACompiler.cmake as
  428. # - CMAKE_CUDA_COMPILER_TOOLKIT_ROOT
  429. # - CMAKE_CUDA_COMPILER_LIBRARY_ROOT
  430. # - CMAKE_CUDA_COMPILER_LIBRARY_DIRECTORIES_FROM_IMPLICIT_LIBRARIES
  431. # - CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES
  432. # We compute the rest based on those here to avoid re-searching and to avoid finding a possibly
  433. # different installation.
  434. if(CMAKE_CUDA_COMPILER_TOOLKIT_ROOT)
  435. set(CUDAToolkit_ROOT_DIR "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}")
  436. set(CUDAToolkit_LIBRARY_ROOT "${CMAKE_CUDA_COMPILER_LIBRARY_ROOT}")
  437. _CUDAToolkit_build_library_dirs(CUDAToolkit_IMPLICIT_LIBRARY_DIRECTORIES CMAKE_CUDA_HOST_IMPLICIT_LINK_DIRECTORIES)
  438. _CUDAToolkit_build_include_dirs(CUDAToolkit_INCLUDE_DIRECTORIES CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES)
  439. set(CUDAToolkit_BIN_DIR "${CUDAToolkit_ROOT_DIR}/bin")
  440. set(CUDAToolkit_NVCC_EXECUTABLE "${CUDAToolkit_BIN_DIR}/nvcc${CMAKE_EXECUTABLE_SUFFIX}")
  441. set(CUDAToolkit_VERSION "${CMAKE_CUDA_COMPILER_TOOLKIT_VERSION}")
  442. if(CUDAToolkit_VERSION MATCHES [=[([0-9]+)\.([0-9]+)\.([0-9]+)]=])
  443. set(CUDAToolkit_VERSION_MAJOR "${CMAKE_MATCH_1}")
  444. set(CUDAToolkit_VERSION_MINOR "${CMAKE_MATCH_2}")
  445. set(CUDAToolkit_VERSION_PATCH "${CMAKE_MATCH_3}")
  446. endif()
  447. else()
  448. function(_CUDAToolkit_find_root_dir )
  449. cmake_parse_arguments(arg "COMPILER_PATHS" "" "SEARCH_PATHS;FIND_FLAGS" ${ARGN})
  450. if(NOT CUDAToolkit_BIN_DIR)
  451. if(arg_COMPILER_PATHS)
  452. # need to find parent dir, since this could clang and not nvcc
  453. if(EXISTS "${CMAKE_CUDA_COMPILER}")
  454. get_filename_component(possible_nvcc_path "${CMAKE_CUDA_COMPILER}" PROGRAM PROGRAM_ARGS CUDAToolkit_compiler_args)
  455. get_filename_component(possible_nvcc_path "${possible_nvcc_path}" DIRECTORY)
  456. elseif(EXISTS "$ENV{CUDACXX}")
  457. get_filename_component(possible_nvcc_path "$ENV{CUDACXX}" PROGRAM PROGRAM_ARGS CUDAToolkit_compiler_args)
  458. get_filename_component(possible_nvcc_path "${possible_nvcc_path}" DIRECTORY)
  459. endif()
  460. if(possible_nvcc_path)
  461. find_program(CUDAToolkit_NVCC_EXECUTABLE
  462. NAMES nvcc nvcc.exe
  463. NO_DEFAULT_PATH
  464. PATHS ${possible_nvcc_path}
  465. )
  466. endif()
  467. else()
  468. if(NOT CUDAToolkit_SENTINEL_FILE)
  469. find_program(CUDAToolkit_NVCC_EXECUTABLE
  470. NAMES nvcc nvcc.exe
  471. PATHS ${arg_SEARCH_PATHS}
  472. ${arg_FIND_FLAGS}
  473. )
  474. endif()
  475. if(NOT CUDAToolkit_NVCC_EXECUTABLE)
  476. find_file(CUDAToolkit_SENTINEL_FILE
  477. NAMES version.txt version.json
  478. PATHS ${arg_SEARCH_PATHS}
  479. NO_DEFAULT_PATH
  480. )
  481. endif()
  482. endif()
  483. if(EXISTS "${CUDAToolkit_NVCC_EXECUTABLE}")
  484. # If NVCC exists then invoke it to find the toolkit location.
  485. # This allows us to support wrapper scripts (e.g. ccache or colornvcc), CUDA Toolkit,
  486. # NVIDIA HPC SDK, and distro's splayed layouts
  487. #Allow the user to specify a host compiler except for Visual Studio
  488. if(NOT $ENV{CUDAHOSTCXX} STREQUAL "")
  489. get_filename_component(CUDAToolkit_CUDA_HOST_COMPILER $ENV{CUDAHOSTCXX} PROGRAM)
  490. if(NOT EXISTS ${CUDAToolkit_CUDA_HOST_COMPILER})
  491. message(FATAL_ERROR "Could not find compiler set in environment variable CUDAHOSTCXX:\n$ENV{CUDAHOSTCXX}.\n${CUDAToolkit_CUDA_HOST_COMPILER}")
  492. endif()
  493. elseif(CUDAToolkit_CUDA_HOST_COMPILER)
  494. # We get here if CUDAToolkit_CUDA_HOST_COMPILER was specified by the user or toolchain file.
  495. if(IS_ABSOLUTE "${CUDAToolkit_CUDA_HOST_COMPILER}")
  496. # Convert to forward slashes.
  497. cmake_path(CONVERT "${CUDAToolkit_CUDA_HOST_COMPILER}" TO_CMAKE_PATH_LIST CUDAToolkit_CUDA_HOST_COMPILER NORMALIZE)
  498. else()
  499. # Convert to absolute path so changes in `PATH` do not impact CUDA compilation.
  500. find_program(_CUDAToolkit_CUDA_HOST_COMPILER_PATH NO_CACHE NAMES "${CUDAToolkit_CUDA_HOST_COMPILER}")
  501. if(_CUDAToolkit_CUDA_HOST_COMPILER_PATH)
  502. set(CUDAToolkit_CUDA_HOST_COMPILER "${_CUDAToolkit_CUDA_HOST_COMPILER_PATH}")
  503. endif()
  504. unset(_CUDAToolkit_CUDA_HOST_COMPILER_PATH)
  505. endif()
  506. if(NOT EXISTS "${CUDAToolkit_CUDA_HOST_COMPILER}")
  507. message(FATAL_ERROR "Could not find compiler set in variable CUDAToolkit_CUDA_HOST_COMPILER:\n ${CUDAToolkit_CUDA_HOST_COMPILER}")
  508. endif()
  509. # If the value was cached, update the cache entry with our modifications.
  510. get_property(_CUDAToolkit_CUDA_HOST_COMPILER_CACHED CACHE CUDAToolkit_CUDA_HOST_COMPILER PROPERTY TYPE)
  511. if(_CUDAToolkit_CUDA_HOST_COMPILER_CACHED)
  512. set_property(CACHE CUDAToolkit_CUDA_HOST_COMPILER PROPERTY VALUE "${CUDAToolkit_CUDA_HOST_COMPILER}")
  513. mark_as_advanced(CUDAToolkit_CUDA_HOST_COMPILER)
  514. endif()
  515. unset(_CUDAToolkit_CUDA_HOST_COMPILER_CACHED)
  516. endif()
  517. if(CUDAToolkit_CUDA_HOST_COMPILER)
  518. set(nvcc_ccbin_flag "-ccbin=${CUDAToolkit_CUDA_HOST_COMPILER}")
  519. endif()
  520. execute_process(COMMAND ${CUDAToolkit_NVCC_EXECUTABLE} "${nvcc_ccbin_flag}" "-v" "__cmake_determine_cuda"
  521. OUTPUT_VARIABLE _CUDA_NVCC_OUT ERROR_VARIABLE _CUDA_NVCC_OUT)
  522. message(CONFIGURE_LOG
  523. "Executed nvcc to extract CUDAToolkit information:\n${_CUDA_NVCC_OUT}\n\n")
  524. if(_CUDA_NVCC_OUT MATCHES "\\#\\$ TOP=([^\r\n]*)")
  525. get_filename_component(CUDAToolkit_BIN_DIR "${CMAKE_MATCH_1}/bin" ABSOLUTE)
  526. message(CONFIGURE_LOG
  527. "Parsed CUDAToolkit nvcc location:\n${CUDAToolkit_BIN_DIR}\n\n")
  528. else()
  529. get_filename_component(CUDAToolkit_BIN_DIR "${CUDAToolkit_NVCC_EXECUTABLE}" DIRECTORY)
  530. endif()
  531. if(_CUDA_NVCC_OUT MATCHES "\\#\\$ INCLUDES=([^\r\n]*)")
  532. separate_arguments(_nvcc_output NATIVE_COMMAND "${CMAKE_MATCH_1}")
  533. foreach(line IN LISTS _nvcc_output)
  534. string(REGEX REPLACE "^-I" "" line "${line}")
  535. get_filename_component(line "${line}" ABSOLUTE)
  536. list(APPEND _cmake_CUDAToolkit_include_directories "${line}")
  537. endforeach()
  538. endif()
  539. if(_CUDA_NVCC_OUT MATCHES "\\#\\$ SYSTEM_INCLUDES=([^\r\n]*)")
  540. unset(_nvcc_output)
  541. separate_arguments(_nvcc_output NATIVE_COMMAND "${CMAKE_MATCH_1}")
  542. foreach(line IN LISTS _nvcc_output)
  543. string(REGEX REPLACE "^-isystem" "" line "${line}")
  544. if(line)
  545. get_filename_component(line "${line}" ABSOLUTE)
  546. list(APPEND _cmake_CUDAToolkit_include_directories "${line}")
  547. endif()
  548. endforeach()
  549. endif()
  550. if(DEFINED _cmake_CUDAToolkit_include_directories)
  551. message(CONFIGURE_LOG
  552. "Parsed CUDAToolkit nvcc implicit include information:\n${_cmake_CUDAToolkit_include_directories}\n\n")
  553. set(_cmake_CUDAToolkit_include_directories "${_cmake_CUDAToolkit_include_directories}" CACHE INTERNAL "CUDAToolkit internal list of include directories")
  554. endif()
  555. if(_CUDA_NVCC_OUT MATCHES "\\#\\$ LIBRARIES=([^\r\n]*)")
  556. include(${CMAKE_ROOT}/Modules/CMakeParseImplicitLinkInfo.cmake)
  557. set(_nvcc_link_line "cuda-fake-ld ${CMAKE_MATCH_1}")
  558. CMAKE_PARSE_IMPLICIT_LINK_INFO("${_nvcc_link_line}"
  559. _cmake_CUDAToolkit_implicit_link_libs
  560. _cmake_CUDAToolkit_implicit_link_directories
  561. _cmake_CUDAToolkit_implicit_frameworks
  562. _nvcc_log
  563. "${CMAKE_CUDA_IMPLICIT_OBJECT_REGEX}"
  564. LANGUAGE CUDA)
  565. message(CONFIGURE_LOG
  566. "Parsed CUDAToolkit nvcc implicit link information:\n${_nvcc_log}\n${_cmake_CUDAToolkit_implicit_link_directories}\n\n")
  567. unset(_nvcc_link_line)
  568. unset(_cmake_CUDAToolkit_implicit_link_libs)
  569. unset(_cmake_CUDAToolkit_implicit_frameworks)
  570. set(_cmake_CUDAToolkit_implicit_link_directories "${_cmake_CUDAToolkit_implicit_link_directories}" CACHE INTERNAL "CUDAToolkit internal list of implicit link directories")
  571. endif()
  572. unset(_CUDA_NVCC_OUT)
  573. set(CUDAToolkit_BIN_DIR "${CUDAToolkit_BIN_DIR}" CACHE PATH "" FORCE)
  574. mark_as_advanced(CUDAToolkit_BIN_DIR)
  575. endif()
  576. if(CUDAToolkit_SENTINEL_FILE)
  577. get_filename_component(CUDAToolkit_BIN_DIR ${CUDAToolkit_SENTINEL_FILE} DIRECTORY ABSOLUTE)
  578. set(CUDAToolkit_BIN_DIR "${CUDAToolkit_BIN_DIR}/bin")
  579. set(CUDAToolkit_BIN_DIR "${CUDAToolkit_BIN_DIR}" CACHE PATH "" FORCE)
  580. mark_as_advanced(CUDAToolkit_BIN_DIR)
  581. endif()
  582. endif()
  583. if(DEFINED _cmake_CUDAToolkit_include_directories)
  584. _CUDAToolkit_build_include_dirs(_cmake_CUDAToolkit_contents _cmake_CUDAToolkit_include_directories)
  585. set(CUDAToolkit_INCLUDE_DIRECTORIES "${_cmake_CUDAToolkit_contents}" PARENT_SCOPE)
  586. endif()
  587. if(DEFINED _cmake_CUDAToolkit_implicit_link_directories)
  588. _CUDAToolkit_build_library_dirs(_cmake_CUDAToolkit_contents _cmake_CUDAToolkit_implicit_link_directories)
  589. set(CUDAToolkit_IMPLICIT_LIBRARY_DIRECTORIES "${_cmake_CUDAToolkit_contents}" PARENT_SCOPE)
  590. endif()
  591. if(CUDAToolkit_BIN_DIR)
  592. get_filename_component(CUDAToolkit_ROOT_DIR ${CUDAToolkit_BIN_DIR} DIRECTORY ABSOLUTE)
  593. set(CUDAToolkit_ROOT_DIR "${CUDAToolkit_ROOT_DIR}" PARENT_SCOPE)
  594. endif()
  595. endfunction()
  596. function(_CUDAToolkit_guess_root_dir)
  597. # CUDAToolkit_ROOT cmake / env variable not specified, try platform defaults.
  598. #
  599. # - Linux: /usr/local/cuda-X.Y
  600. # - macOS: /Developer/NVIDIA/CUDA-X.Y
  601. # - Windows: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vX.Y
  602. #
  603. # We will also search the default symlink location /usr/local/cuda first since
  604. # if CUDAToolkit_ROOT is not specified, it is assumed that the symlinked
  605. # directory is the desired location.
  606. if(UNIX)
  607. if(NOT APPLE)
  608. set(platform_base "/usr/local/cuda-")
  609. else()
  610. set(platform_base "/Developer/NVIDIA/CUDA-")
  611. endif()
  612. else()
  613. set(platform_base "C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v")
  614. endif()
  615. # Build out a descending list of possible cuda installations, e.g.
  616. file(GLOB possible_paths "${platform_base}*")
  617. # Iterate the glob results and create a descending list.
  618. set(versions)
  619. foreach(p ${possible_paths})
  620. # Extract version number from end of string
  621. string(REGEX MATCH "[0-9][0-9]?\\.[0-9]$" p_version ${p})
  622. if(IS_DIRECTORY ${p} AND p_version)
  623. list(APPEND versions ${p_version})
  624. endif()
  625. endforeach()
  626. # Sort numerically in descending order, so we try the newest versions first.
  627. list(SORT versions COMPARE NATURAL ORDER DESCENDING)
  628. # With a descending list of versions, populate possible paths to search.
  629. set(search_paths)
  630. foreach(v ${versions})
  631. list(APPEND search_paths "${platform_base}${v}")
  632. endforeach()
  633. # Force the global default /usr/local/cuda to the front on Unix.
  634. if(UNIX)
  635. list(INSERT search_paths 0 "/usr/local/cuda")
  636. endif()
  637. # Now search for the toolkit again using the platform default search paths.
  638. _CUDAToolkit_find_root_dir(SEARCH_PATHS "${search_paths}" FIND_FLAGS PATH_SUFFIXES bin)
  639. if(CUDAToolkit_ROOT_DIR)
  640. set(CUDAToolkit_ROOT_DIR "${CUDAToolkit_ROOT_DIR}" PARENT_SCOPE)
  641. endif()
  642. # We are done with these variables now, cleanup for caller.
  643. unset(platform_base)
  644. unset(possible_paths)
  645. unset(versions)
  646. unset(search_paths)
  647. endfunction()
  648. function(_CUDAToolkit_find_version_file result_variable)
  649. # We first check for a non-scattered installation to prefer it over a scattered installation.
  650. set(version_files version.txt version.json)
  651. foreach(vf IN LISTS version_files)
  652. if(CUDAToolkit_ROOT AND EXISTS "${CUDAToolkit_ROOT}/${vf}")
  653. set(${result_variable} "${CUDAToolkit_ROOT}/${vf}" PARENT_SCOPE)
  654. break()
  655. elseif(CUDAToolkit_ROOT_DIR AND EXISTS "${CUDAToolkit_ROOT_DIR}/${vf}")
  656. set(${result_variable} "${CUDAToolkit_ROOT_DIR}/${vf}" PARENT_SCOPE)
  657. break()
  658. elseif(CMAKE_SYSROOT_LINK AND EXISTS "${CMAKE_SYSROOT_LINK}/usr/lib/cuda/${vf}")
  659. set(${result_variable} "${CMAKE_SYSROOT_LINK}/usr/lib/cuda/${vf}" PARENT_SCOPE)
  660. break()
  661. elseif(EXISTS "${CMAKE_SYSROOT}/usr/lib/cuda/${vf}")
  662. set(${result_variable} "${CMAKE_SYSROOT}/usr/lib/cuda/${vf}" PARENT_SCOPE)
  663. break()
  664. endif()
  665. endforeach()
  666. endfunction()
  667. function(_CUDAToolkit_parse_version_file version_file)
  668. if(version_file)
  669. file(READ "${version_file}" file_contents)
  670. cmake_path(GET version_file EXTENSION LAST_ONLY version_ext)
  671. if(version_ext STREQUAL ".json")
  672. string(JSON cuda_version_info GET "${file_contents}" "cuda" "version")
  673. set(cuda_version_match_regex [=[([0-9]+)\.([0-9]+)\.([0-9]+)]=])
  674. elseif(version_ext STREQUAL ".txt")
  675. set(cuda_version_info "${file_contents}")
  676. set(cuda_version_match_regex [=[CUDA Version ([0-9]+)\.([0-9]+)\.([0-9]+)]=])
  677. endif()
  678. if(cuda_version_info MATCHES "${cuda_version_match_regex}")
  679. set(CUDAToolkit_VERSION_MAJOR "${CMAKE_MATCH_1}" PARENT_SCOPE)
  680. set(CUDAToolkit_VERSION_MINOR "${CMAKE_MATCH_2}" PARENT_SCOPE)
  681. set(CUDAToolkit_VERSION_PATCH "${CMAKE_MATCH_3}" PARENT_SCOPE)
  682. set(CUDAToolkit_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}" PARENT_SCOPE)
  683. endif()
  684. endif()
  685. endfunction()
  686. macro(_CUDAToolkit_find_failure_message _CUDAToolkit_fail_mode)
  687. # Declare error messages now, print later depending on find_package args.
  688. if("${_CUDAToolkit_fail_mode}" STREQUAL "GUESS")
  689. set(_CUDAToolkit_fail_message "Could not find `nvcc` executable in any searched paths, please set CUDAToolkit_ROOT")
  690. elseif("${_CUDAToolkit_fail_mode}" STREQUAL "VARIABLE")
  691. set(_CUDAToolkit_fail_message "Could not find `nvcc` executable in path specified by variable CUDAToolkit_ROOT=${CUDAToolkit_ROOT}")
  692. else()
  693. set(_CUDAToolkit_fail_message "Could not find `nvcc` executable in path specified by environment variable CUDAToolkit_ROOT=$ENV{CUDAToolkit_ROOT}")
  694. endif()
  695. if(CUDAToolkit_FIND_REQUIRED)
  696. message(FATAL_ERROR ${_CUDAToolkit_fail_message})
  697. else()
  698. if(NOT CUDAToolkit_FIND_QUIETLY)
  699. message(STATUS ${_CUDAToolkit_fail_message})
  700. endif()
  701. set(CUDAToolkit_FOUND FALSE)
  702. unset(_CUDAToolkit_fail_message)
  703. return()
  704. endif()
  705. endmacro()
  706. # For NVCC we can easily deduce the SDK binary directory from the compiler path.
  707. if(CMAKE_CUDA_COMPILER_LOADED AND NOT CUDAToolkit_BIN_DIR AND CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
  708. get_filename_component(CUDAToolkit_BIN_DIR "${CMAKE_CUDA_COMPILER}" DIRECTORY)
  709. set(CUDAToolkit_BIN_DIR "${CUDAToolkit_BIN_DIR}" CACHE PATH "")
  710. # Try language provided path first.
  711. _CUDAToolkit_find_root_dir(SEARCH_PATHS "${CUDAToolkit_BIN_DIR}" FIND_FLAGS NO_DEFAULT_PATH)
  712. mark_as_advanced(CUDAToolkit_BIN_DIR)
  713. endif()
  714. # Try `CMAKE_CUDA_COMPILER` and `ENV{CUDACXX}`
  715. if(NOT CUDAToolkit_ROOT_DIR)
  716. _CUDAToolkit_find_root_dir(COMPILER_PATHS)
  717. endif()
  718. # Try user provided path
  719. if(NOT CUDAToolkit_ROOT_DIR AND DEFINED CUDAToolkit_ROOT)
  720. _CUDAToolkit_find_root_dir(SEARCH_PATHS "${CUDAToolkit_ROOT}" FIND_FLAGS PATH_SUFFIXES bin NO_DEFAULT_PATH)
  721. if(NOT CUDAToolkit_ROOT_DIR)
  722. # If the user specified CUDAToolkit_ROOT but the toolkit could not be found, this is an error.
  723. _CUDAToolkit_find_failure_message(VARIABLE)
  724. endif()
  725. endif()
  726. if(NOT CUDAToolkit_ROOT_DIR AND DEFINED ENV{CUDAToolkit_ROOT})
  727. _CUDAToolkit_find_root_dir(SEARCH_PATHS "$ENV{CUDAToolkit_ROOT}" FIND_FLAGS PATH_SUFFIXES bin NO_DEFAULT_PATH)
  728. if(NOT CUDAToolkit_ROOT_DIR)
  729. # If the user specified ENV{CUDAToolkit_ROOT} but the toolkit could not be found, this is an error.
  730. _CUDAToolkit_find_failure_message(ENV)
  731. endif()
  732. endif()
  733. # Try users PATH, and CUDA_PATH env variable
  734. if(NOT CUDAToolkit_ROOT_DIR)
  735. _CUDAToolkit_find_root_dir(FIND_FLAGS PATHS ENV CUDA_PATH PATH_SUFFIXES bin)
  736. endif()
  737. # Try guessing where CUDA is installed
  738. if(NOT CUDAToolkit_ROOT_DIR)
  739. _CUDAToolkit_guess_root_dir()
  740. if(NOT CUDAToolkit_ROOT_DIR)
  741. _CUDAToolkit_find_failure_message(GUESS)
  742. endif()
  743. endif()
  744. _CUDAToolkit_find_version_file( _CUDAToolkit_version_file )
  745. if(_CUDAToolkit_version_file)
  746. # CUDAToolkit_LIBRARY_ROOT contains the device library and version file.
  747. get_filename_component(CUDAToolkit_LIBRARY_ROOT "${_CUDAToolkit_version_file}" DIRECTORY ABSOLUTE)
  748. endif()
  749. unset(_CUDAToolkit_version_file)
  750. if(CUDAToolkit_NVCC_EXECUTABLE AND
  751. CMAKE_CUDA_COMPILER_VERSION AND
  752. CUDAToolkit_NVCC_EXECUTABLE STREQUAL CMAKE_CUDA_COMPILER)
  753. # Need to set these based off the already computed CMAKE_CUDA_COMPILER_VERSION value
  754. # This if statement will always match, but is used to provide variables for MATCH 1,2,3...
  755. if(CMAKE_CUDA_COMPILER_VERSION MATCHES [=[([0-9]+)\.([0-9]+)\.([0-9]+)]=])
  756. set(CUDAToolkit_VERSION_MAJOR "${CMAKE_MATCH_1}")
  757. set(CUDAToolkit_VERSION_MINOR "${CMAKE_MATCH_2}")
  758. set(CUDAToolkit_VERSION_PATCH "${CMAKE_MATCH_3}")
  759. set(CUDAToolkit_VERSION "${CMAKE_CUDA_COMPILER_VERSION}")
  760. endif()
  761. elseif(CUDAToolkit_NVCC_EXECUTABLE)
  762. # Compute the version by invoking nvcc
  763. execute_process(COMMAND ${CUDAToolkit_NVCC_EXECUTABLE} "--version" OUTPUT_VARIABLE NVCC_OUT)
  764. if(NVCC_OUT MATCHES [=[ V([0-9]+)\.([0-9]+)\.([0-9]+)]=])
  765. set(CUDAToolkit_VERSION_MAJOR "${CMAKE_MATCH_1}")
  766. set(CUDAToolkit_VERSION_MINOR "${CMAKE_MATCH_2}")
  767. set(CUDAToolkit_VERSION_PATCH "${CMAKE_MATCH_3}")
  768. set(CUDAToolkit_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}")
  769. endif()
  770. unset(NVCC_OUT)
  771. else()
  772. _CUDAToolkit_find_version_file(version_file)
  773. _CUDAToolkit_parse_version_file("${version_file}")
  774. endif()
  775. endif()
  776. # Figure out the target directory when either crosscompiling
  777. # or if we don't have `nvcc` and need to deduce the target arch
  778. if(CMAKE_CROSSCOMPILING OR NOT CUDAToolkit_NVCC_EXECUTABLE)
  779. # When a language is enabled we can use its compiler's target architecture.
  780. if(CMAKE_CUDA_COMPILER_LOADED AND CMAKE_CUDA_COMPILER_ARCHITECTURE_ID)
  781. set(_CUDA_TARGET_PROCESSOR "${CMAKE_CUDA_COMPILER_ARCHITECTURE_ID}")
  782. elseif(CMAKE_CXX_COMPILER_LOADED AND CMAKE_CXX_COMPILER_ARCHITECTURE_ID)
  783. set(_CUDA_TARGET_PROCESSOR "${CMAKE_CXX_COMPILER_ARCHITECTURE_ID}")
  784. elseif(CMAKE_C_COMPILER_LOADED AND CMAKE_C_COMPILER_ARCHITECTURE_ID)
  785. set(_CUDA_TARGET_PROCESSOR "${CMAKE_C_COMPILER_ARCHITECTURE_ID}")
  786. elseif(CMAKE_SYSTEM_PROCESSOR)
  787. set(_CUDA_TARGET_PROCESSOR "${CMAKE_SYSTEM_PROCESSOR}")
  788. elseif(CMAKE_CROSSCOMPILING)
  789. message(FATAL_ERROR "Cross-compiling with the CUDA toolkit requires CMAKE_SYSTEM_PROCESSOR to be set.")
  790. endif()
  791. # Keep in sync with equivalent table in CMakeDetermineCUDACompiler and FindCUDA!
  792. if(_CUDA_TARGET_PROCESSOR STREQUAL "armv7-a")
  793. # Support for NVPACK
  794. set(CUDAToolkit_TARGET_NAMES "armv7-linux-androideabi")
  795. elseif(_CUDA_TARGET_PROCESSOR MATCHES "arm")
  796. set(CUDAToolkit_TARGET_NAMES "armv7-linux-gnueabihf")
  797. elseif(_CUDA_TARGET_PROCESSOR MATCHES "aarch64")
  798. if(ANDROID_ARCH_NAME STREQUAL "arm64")
  799. set(CUDAToolkit_TARGET_NAMES "aarch64-linux-androideabi")
  800. elseif (CMAKE_SYSTEM_NAME STREQUAL "QNX")
  801. set(CUDAToolkit_TARGET_NAMES "aarch64-qnx")
  802. else()
  803. set(CUDAToolkit_TARGET_NAMES "aarch64-linux" "sbsa-linux")
  804. endif()
  805. elseif(_CUDA_TARGET_PROCESSOR STREQUAL "x86_64")
  806. set(CUDAToolkit_TARGET_NAMES "x86_64-linux")
  807. endif()
  808. unset(_CUDA_TARGET_PROCESSOR)
  809. foreach(CUDAToolkit_TARGET_NAME IN LISTS CUDAToolkit_TARGET_NAMES)
  810. if(EXISTS "${CUDAToolkit_ROOT_DIR}/targets/${CUDAToolkit_TARGET_NAME}")
  811. set(CUDAToolkit_TARGET_DIR "${CUDAToolkit_ROOT_DIR}/targets/${CUDAToolkit_TARGET_NAME}")
  812. # add known CUDA target root path to the set of directories we search for programs, libraries and headers
  813. list(PREPEND CMAKE_FIND_ROOT_PATH "${CUDAToolkit_TARGET_DIR}")
  814. # Mark that we need to pop the root search path changes after we have
  815. # found all cuda libraries so that searches for our cross-compilation
  816. # libraries work when another cuda sdk is in CMAKE_PREFIX_PATH or
  817. # PATH
  818. set(_CUDAToolkit_Pop_ROOT_PATH True)
  819. break()
  820. endif()
  821. endforeach()
  822. endif()
  823. #If not already set we simply use the toolkit root
  824. if(NOT CUDAToolkit_TARGET_DIR)
  825. set(CUDAToolkit_TARGET_DIR "${CUDAToolkit_ROOT_DIR}")
  826. endif()
  827. # Determine windows search path suffix for libraries
  828. if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
  829. if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "AMD64")
  830. set(_CUDAToolkit_win_search_dirs lib/x64)
  831. set(_CUDAToolkit_win_stub_search_dirs lib/x64/stubs)
  832. endif()
  833. endif()
  834. # We don't need to verify the cuda_runtime header when we are using `nvcc` include paths
  835. # as the compiler being enabled means the header was found
  836. if(NOT CUDAToolkit_INCLUDE_DIRECTORIES)
  837. # Otherwise use CUDAToolkit_TARGET_DIR to guess where the `cuda_runtime.h` is located
  838. # On a scattered installation /usr, on a non-scattered something like /usr/local/cuda or /usr/local/cuda-10.2/targets/aarch64-linux.
  839. if(EXISTS "${CUDAToolkit_TARGET_DIR}/include/cuda_runtime.h")
  840. set(CUDAToolkit_INCLUDE_DIRECTORIES "${CUDAToolkit_TARGET_DIR}/include")
  841. else()
  842. message(STATUS "Unable to find cuda_runtime.h in \"${CUDAToolkit_TARGET_DIR}/include\" for CUDAToolkit_INCLUDE_DIRECTORIES.")
  843. endif()
  844. endif()
  845. # The NVHPC layout moves math library headers and libraries to a sibling directory and it could be nested under
  846. # the version of the CUDA toolchain
  847. # Create a separate variable so this directory can be selectively added to math targets.
  848. find_path(CUDAToolkit_CUBLAS_INCLUDE_DIR cublas_v2.h PATHS
  849. ${CUDAToolkit_INCLUDE_DIRECTORIES}
  850. NO_DEFAULT_PATH)
  851. if(NOT CUDAToolkit_CUBLAS_INCLUDE_DIR)
  852. file(REAL_PATH "${CUDAToolkit_TARGET_DIR}" CUDAToolkit_MATH_INCLUDE_DIR)
  853. cmake_path(APPEND CUDAToolkit_MATH_INCLUDE_DIR "../../math_libs/")
  854. if(EXISTS "${CUDAToolkit_MATH_INCLUDE_DIR}/${CUDAToolkit_VERSION_MAJOR}.${CUDAToolkit_VERSION_MINOR}/")
  855. cmake_path(APPEND CUDAToolkit_MATH_INCLUDE_DIR "${CUDAToolkit_VERSION_MAJOR}.${CUDAToolkit_VERSION_MINOR}/")
  856. endif()
  857. cmake_path(APPEND CUDAToolkit_MATH_INCLUDE_DIR "include")
  858. cmake_path(NORMAL_PATH CUDAToolkit_MATH_INCLUDE_DIR)
  859. find_path(CUDAToolkit_CUBLAS_INCLUDE_DIR cublas_v2.h PATHS
  860. ${CUDAToolkit_MATH_INCLUDE_DIR}
  861. NO_DEFAULT_PATH
  862. )
  863. if(CUDAToolkit_CUBLAS_INCLUDE_DIR)
  864. list(APPEND CUDAToolkit_INCLUDE_DIRECTORIES "${CUDAToolkit_CUBLAS_INCLUDE_DIR}")
  865. endif()
  866. endif()
  867. unset(CUDAToolkit_CUBLAS_INCLUDE_DIR CACHE)
  868. unset(CUDAToolkit_CUBLAS_INCLUDE_DIR)
  869. # Find the CUDA Runtime Library libcudart
  870. find_library(CUDA_CUDART
  871. NAMES cudart
  872. PATHS ${CUDAToolkit_IMPLICIT_LIBRARY_DIRECTORIES} ${CUDAToolkit_TARGET_DIR}
  873. PATH_SUFFIXES lib64 ${_CUDAToolkit_win_search_dirs}
  874. )
  875. find_library(CUDA_CUDART
  876. NAMES cudart
  877. PATHS ${CUDAToolkit_IMPLICIT_LIBRARY_DIRECTORIES} ${CUDAToolkit_TARGET_DIR}
  878. PATH_SUFFIXES lib64/stubs ${_CUDAToolkit_win_stub_search_dirs} lib/stubs stubs
  879. )
  880. if(NOT CUDA_CUDART AND NOT CUDAToolkit_FIND_QUIETLY)
  881. message(STATUS "Unable to find cudart library.")
  882. endif()
  883. #-----------------------------------------------------------------------------
  884. # Perform version comparison and validate all required variables are set.
  885. include(FindPackageHandleStandardArgs)
  886. find_package_handle_standard_args(CUDAToolkit
  887. REQUIRED_VARS
  888. CUDAToolkit_INCLUDE_DIRECTORIES
  889. CUDA_CUDART
  890. CUDAToolkit_BIN_DIR
  891. VERSION_VAR
  892. CUDAToolkit_VERSION
  893. )
  894. unset(CUDAToolkit_ROOT_DIR)
  895. mark_as_advanced(CUDA_CUDART
  896. CUDAToolkit_NVCC_EXECUTABLE
  897. CUDAToolkit_SENTINEL_FILE
  898. )
  899. #-----------------------------------------------------------------------------
  900. # Construct result variables
  901. if(CUDAToolkit_FOUND)
  902. set(CUDAToolkit_INCLUDE_DIRS "${CUDAToolkit_INCLUDE_DIRECTORIES}")
  903. get_filename_component(CUDAToolkit_LIBRARY_DIR ${CUDA_CUDART} DIRECTORY ABSOLUTE)
  904. # Build search paths without any symlinks
  905. file(REAL_PATH "${CUDAToolkit_LIBRARY_DIR}" _cmake_search_dir)
  906. set(CUDAToolkit_LIBRARY_SEARCH_DIRS "${_cmake_search_dir}")
  907. # Detect we are in a splayed nvhpc toolkit layout and add extra
  908. # search paths without symlinks
  909. #
  910. # When the `nvcc` compiler output is parsed we have already resolved
  911. # symlinks so we have `cuda/12.X/targets/....` and not `cuda/12.X/lib64`.
  912. if(CUDAToolkit_LIBRARY_DIR MATCHES ".*/cuda/${CUDAToolkit_VERSION_MAJOR}.${CUDAToolkit_VERSION_MINOR}/(lib64$|targets/)")
  913. # Search location for math_libs/
  914. block(SCOPE_FOR POLICIES)
  915. cmake_policy(SET CMP0152 NEW)
  916. file(REAL_PATH "${CUDAToolkit_LIBRARY_DIR}/../../../../../" _cmake_search_dir)
  917. list(APPEND CUDAToolkit_LIBRARY_SEARCH_DIRS "${_cmake_search_dir}")
  918. # Search location for extras like cupti
  919. file(REAL_PATH "${CUDAToolkit_LIBRARY_DIR}/../../../" _cmake_search_dir)
  920. list(APPEND CUDAToolkit_LIBRARY_SEARCH_DIRS "${_cmake_search_dir}")
  921. endblock()
  922. endif()
  923. if(DEFINED CUDAToolkit_IMPLICIT_LIBRARY_DIRECTORIES)
  924. list(APPEND CUDAToolkit_LIBRARY_SEARCH_DIRS "${CUDAToolkit_IMPLICIT_LIBRARY_DIRECTORIES}")
  925. endif()
  926. # If no `CUDAToolkit_LIBRARY_ROOT` exists set it based on CUDAToolkit_LIBRARY_DIR
  927. if(NOT DEFINED CUDAToolkit_LIBRARY_ROOT)
  928. foreach(CUDAToolkit_search_loc IN LISTS CUDAToolkit_LIBRARY_DIR CUDAToolkit_BIN_DIR)
  929. get_filename_component(CUDAToolkit_possible_lib_root "${CUDAToolkit_search_loc}" DIRECTORY ABSOLUTE)
  930. if(EXISTS "${CUDAToolkit_possible_lib_root}/nvvm/")
  931. set(CUDAToolkit_LIBRARY_ROOT "${CUDAToolkit_possible_lib_root}")
  932. break()
  933. endif()
  934. endforeach()
  935. unset(CUDAToolkit_search_loc)
  936. unset(CUDAToolkit_possible_lib_root)
  937. endif()
  938. else()
  939. # clear cache results when we fail
  940. unset(_cmake_CUDAToolkit_implicit_link_directories CACHE)
  941. unset(_cmake_CUDAToolkit_include_directories CACHE)
  942. unset(CUDA_CUDART CACHE)
  943. unset(CUDAToolkit_BIN_DIR CACHE)
  944. unset(CUDAToolkit_NVCC_EXECUTABLE CACHE)
  945. unset(CUDAToolkit_SENTINEL_FILE CACHE)
  946. endif()
  947. unset(CUDAToolkit_IMPLICIT_LIBRARY_DIRECTORIES)
  948. unset(CUDAToolkit_INCLUDE_DIRECTORIES)
  949. #-----------------------------------------------------------------------------
  950. # Construct import targets
  951. if(CUDAToolkit_FOUND)
  952. function(_CUDAToolkit_find_and_add_import_lib lib_name)
  953. cmake_parse_arguments(arg "" "" "ALT;DEPS;EXTRA_PATH_SUFFIXES;EXTRA_INCLUDE_DIRS;ONLY_SEARCH_FOR" ${ARGN})
  954. if(arg_ONLY_SEARCH_FOR)
  955. set(search_names ${arg_ONLY_SEARCH_FOR})
  956. else()
  957. set(search_names ${lib_name} ${arg_ALT})
  958. endif()
  959. find_library(CUDA_${lib_name}_LIBRARY
  960. NAMES ${search_names}
  961. HINTS ${CUDAToolkit_LIBRARY_SEARCH_DIRS}
  962. ENV CUDA_PATH
  963. PATH_SUFFIXES nvidia/current lib64 ${_CUDAToolkit_win_search_dirs} lib
  964. # Support NVHPC splayed math library layout
  965. math_libs/${CUDAToolkit_VERSION_MAJOR}.${CUDAToolkit_VERSION_MINOR}/lib64
  966. math_libs/lib64
  967. ${arg_EXTRA_PATH_SUFFIXES}
  968. )
  969. # Don't try any stub directories until we have exhausted all other
  970. # search locations.
  971. set(CUDA_IMPORT_PROPERTY IMPORTED_LOCATION)
  972. set(CUDA_IMPORT_TYPE UNKNOWN)
  973. if(NOT CUDA_${lib_name}_LIBRARY)
  974. find_library(CUDA_${lib_name}_LIBRARY
  975. NAMES ${search_names}
  976. HINTS ${CUDAToolkit_LIBRARY_SEARCH_DIRS}
  977. ENV CUDA_PATH
  978. PATH_SUFFIXES lib64/stubs ${_CUDAToolkit_win_stub_search_dirs} lib/stubs stubs
  979. )
  980. endif()
  981. if(CUDA_${lib_name}_LIBRARY MATCHES "/stubs/" AND NOT CUDA_${lib_name}_LIBRARY MATCHES "\\.a$" AND NOT WIN32)
  982. # Use a SHARED library with IMPORTED_IMPLIB, but not IMPORTED_LOCATION,
  983. # to indicate that the stub is for linkers but not dynamic loaders.
  984. # It will not contribute any RPATH entry. When encountered as
  985. # a private transitive dependency of another shared library,
  986. # it will be passed explicitly to linkers so they can find it
  987. # even when the runtime library file does not exist on disk.
  988. set(CUDA_IMPORT_PROPERTY IMPORTED_IMPLIB)
  989. set(CUDA_IMPORT_TYPE SHARED)
  990. endif()
  991. mark_as_advanced(CUDA_${lib_name}_LIBRARY)
  992. if (NOT TARGET CUDA::${lib_name} AND CUDA_${lib_name}_LIBRARY)
  993. add_library(CUDA::${lib_name} ${CUDA_IMPORT_TYPE} IMPORTED)
  994. target_include_directories(CUDA::${lib_name} SYSTEM INTERFACE "${CUDAToolkit_INCLUDE_DIRS}")
  995. if(DEFINED CUDAToolkit_MATH_INCLUDE_DIR)
  996. string(FIND ${CUDA_${lib_name}_LIBRARY} "math_libs" math_libs)
  997. if(NOT ${math_libs} EQUAL -1)
  998. target_include_directories(CUDA::${lib_name} SYSTEM INTERFACE "${CUDAToolkit_MATH_INCLUDE_DIR}")
  999. endif()
  1000. endif()
  1001. set_property(TARGET CUDA::${lib_name} PROPERTY ${CUDA_IMPORT_PROPERTY} "${CUDA_${lib_name}_LIBRARY}")
  1002. foreach(dep ${arg_DEPS})
  1003. if(TARGET CUDA::${dep})
  1004. target_link_libraries(CUDA::${lib_name} INTERFACE CUDA::${dep})
  1005. endif()
  1006. endforeach()
  1007. if(arg_EXTRA_INCLUDE_DIRS)
  1008. target_include_directories(CUDA::${lib_name} SYSTEM INTERFACE "${arg_EXTRA_INCLUDE_DIRS}")
  1009. endif()
  1010. endif()
  1011. endfunction()
  1012. if(NOT TARGET CUDA::toolkit)
  1013. add_library(CUDA::toolkit IMPORTED INTERFACE)
  1014. target_include_directories(CUDA::toolkit SYSTEM INTERFACE "${CUDAToolkit_INCLUDE_DIRS}")
  1015. target_link_directories(CUDA::toolkit INTERFACE "${CUDAToolkit_LIBRARY_DIR}")
  1016. endif()
  1017. # setup dependencies that are required for cudart/cudart_static when building
  1018. # on linux. These are generally only required when using the CUDA toolkit
  1019. # when CUDA language is disabled
  1020. if(NOT TARGET CUDA::cudart_static_deps)
  1021. add_library(CUDA::cudart_static_deps IMPORTED INTERFACE)
  1022. if(UNIX AND (CMAKE_C_COMPILER_LOADED OR CMAKE_CXX_COMPILER_LOADED))
  1023. find_package(Threads REQUIRED)
  1024. target_link_libraries(CUDA::cudart_static_deps INTERFACE Threads::Threads ${CMAKE_DL_LIBS})
  1025. endif()
  1026. if(UNIX AND NOT APPLE AND NOT (CMAKE_SYSTEM_NAME STREQUAL "QNX"))
  1027. # On Linux, you must link against librt when using the static cuda runtime.
  1028. find_library(CUDAToolkit_rt_LIBRARY rt)
  1029. mark_as_advanced(CUDAToolkit_rt_LIBRARY)
  1030. if(NOT CUDAToolkit_rt_LIBRARY)
  1031. message(WARNING "Could not find librt library, needed by CUDA::cudart_static")
  1032. else()
  1033. target_link_libraries(CUDA::cudart_static_deps INTERFACE ${CUDAToolkit_rt_LIBRARY})
  1034. endif()
  1035. endif()
  1036. endif()
  1037. _CUDAToolkit_find_and_add_import_lib(cuda_driver ALT cuda DEPS cudart_static_deps)
  1038. _CUDAToolkit_find_and_add_import_lib(cudart DEPS cudart_static_deps)
  1039. _CUDAToolkit_find_and_add_import_lib(cudart_static DEPS cudart_static_deps)
  1040. if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 12.0.0)
  1041. _CUDAToolkit_find_and_add_import_lib(nvJitLink)
  1042. _CUDAToolkit_find_and_add_import_lib(nvJitLink_static DEPS cudart_static_deps)
  1043. endif()
  1044. if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 12.4.0)
  1045. _CUDAToolkit_find_and_add_import_lib(nvfatbin DEPS cudart_static_deps)
  1046. _CUDAToolkit_find_and_add_import_lib(nvfatbin_static DEPS cudart_static_deps)
  1047. endif()
  1048. _CUDAToolkit_find_and_add_import_lib(culibos) # it's a static library
  1049. foreach (cuda_lib cublasLt cufft nvjpeg)
  1050. _CUDAToolkit_find_and_add_import_lib(${cuda_lib})
  1051. _CUDAToolkit_find_and_add_import_lib(${cuda_lib}_static DEPS cudart_static_deps culibos)
  1052. endforeach()
  1053. foreach (cuda_lib curand nppc)
  1054. _CUDAToolkit_find_and_add_import_lib(${cuda_lib})
  1055. _CUDAToolkit_find_and_add_import_lib(${cuda_lib}_static DEPS culibos)
  1056. endforeach()
  1057. _CUDAToolkit_find_and_add_import_lib(cusparse DEPS nvJitLink)
  1058. _CUDAToolkit_find_and_add_import_lib(cusparse_static DEPS nvJitLink_static culibos)
  1059. if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 11.0.0)
  1060. # cublas depends on cublasLt
  1061. # https://docs.nvidia.com/cuda/archive/11.0/cublas#static-library
  1062. _CUDAToolkit_find_and_add_import_lib(cublas DEPS cublasLt culibos)
  1063. _CUDAToolkit_find_and_add_import_lib(cublas_static DEPS cublasLt_static culibos)
  1064. else()
  1065. _CUDAToolkit_find_and_add_import_lib(cublas DEPS culibos)
  1066. _CUDAToolkit_find_and_add_import_lib(cublas_static DEPS culibos)
  1067. endif()
  1068. if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 11.4)
  1069. _CUDAToolkit_find_and_add_import_lib(cuFile ALT cufile DEPS culibos)
  1070. _CUDAToolkit_find_and_add_import_lib(cuFile_static ALT cufile_static DEPS culibos)
  1071. _CUDAToolkit_find_and_add_import_lib(cuFile_rdma ALT cufile_rdma DEPS cuFile culibos)
  1072. _CUDAToolkit_find_and_add_import_lib(cuFile_rdma_static ALT cufile_rdma_static DEPS cuFile_static culibos)
  1073. endif()
  1074. if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 11.6)
  1075. _CUDAToolkit_find_and_add_import_lib(cudla)
  1076. endif()
  1077. # cuFFTW depends on cuFFT
  1078. _CUDAToolkit_find_and_add_import_lib(cufftw DEPS cufft)
  1079. _CUDAToolkit_find_and_add_import_lib(cufftw_static DEPS cufft_static)
  1080. if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 9.2)
  1081. _CUDAToolkit_find_and_add_import_lib(cufft_static_nocallback DEPS culibos)
  1082. endif()
  1083. # cuSOLVER depends on cuBLAS, and cuSPARSE
  1084. set(cusolver_deps cublas cusparse)
  1085. set(cusolver_static_deps cublas_static cusparse_static culibos)
  1086. if(CUDAToolkit_VERSION VERSION_GREATER 11.2.1)
  1087. # cusolver depends on libcusolver_metis and cublasLt
  1088. # https://docs.nvidia.com/cuda/archive/11.2.2/cusolver#link-dependency
  1089. list(APPEND cusolver_deps cublasLt)
  1090. _CUDAToolkit_find_and_add_import_lib(cusolver_metis_static ALT metis_static) # implementation detail static lib
  1091. list(APPEND cusolver_static_deps cusolver_metis_static cublasLt_static)
  1092. endif()
  1093. if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 10.1.2)
  1094. # cusolver depends on liblapack_static.a starting with CUDA 10.1 update 2,
  1095. # https://docs.nvidia.com/cuda/archive/11.5.0/cusolver#static-link-lapack
  1096. _CUDAToolkit_find_and_add_import_lib(cusolver_lapack_static ALT lapack_static) # implementation detail static lib
  1097. list(APPEND cusolver_static_deps cusolver_lapack_static)
  1098. endif()
  1099. _CUDAToolkit_find_and_add_import_lib(cusolver DEPS ${cusolver_deps})
  1100. _CUDAToolkit_find_and_add_import_lib(cusolver_static DEPS ${cusolver_static_deps})
  1101. unset(cusolver_deps)
  1102. unset(cusolver_static_deps)
  1103. # nvGRAPH depends on cuRAND, and cuSOLVER.
  1104. _CUDAToolkit_find_and_add_import_lib(nvgraph DEPS curand cusolver)
  1105. _CUDAToolkit_find_and_add_import_lib(nvgraph_static DEPS curand_static cusolver_static)
  1106. # Process the majority of the NPP libraries.
  1107. foreach (cuda_lib nppial nppicc nppidei nppif nppig nppim nppist nppitc npps nppicom nppisu)
  1108. _CUDAToolkit_find_and_add_import_lib(${cuda_lib} DEPS nppc)
  1109. _CUDAToolkit_find_and_add_import_lib(${cuda_lib}_static DEPS nppc_static)
  1110. endforeach()
  1111. find_path(CUDAToolkit_CUPTI_INCLUDE_DIR cupti.h PATHS
  1112. "${CUDAToolkit_ROOT_DIR}/extras/CUPTI/include"
  1113. ${CUDAToolkit_INCLUDE_DIRS}
  1114. PATH_SUFFIXES "../extras/CUPTI/include"
  1115. "../../../extras/CUPTI/include"
  1116. NO_DEFAULT_PATH)
  1117. mark_as_advanced(CUDAToolkit_CUPTI_INCLUDE_DIR)
  1118. if(CUDAToolkit_CUPTI_INCLUDE_DIR)
  1119. set(_cmake_cupti_extra_paths extras/CUPTI/lib64/
  1120. extras/CUPTI/lib/
  1121. ../extras/CUPTI/lib64/
  1122. ../extras/CUPTI/lib/
  1123. ../../../extras/CUPTI/lib64/
  1124. ../../../extras/CUPTI/lib/)
  1125. _CUDAToolkit_find_and_add_import_lib(cupti
  1126. EXTRA_PATH_SUFFIXES ${_cmake_cupti_extra_paths}
  1127. EXTRA_INCLUDE_DIRS "${CUDAToolkit_CUPTI_INCLUDE_DIR}")
  1128. _CUDAToolkit_find_and_add_import_lib(cupti_static
  1129. EXTRA_PATH_SUFFIXES ${_cmake_cupti_extra_paths}
  1130. EXTRA_INCLUDE_DIRS "${CUDAToolkit_CUPTI_INCLUDE_DIR}")
  1131. if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 10.2.0)
  1132. _CUDAToolkit_find_and_add_import_lib(nvperf_host
  1133. EXTRA_PATH_SUFFIXES ${_cmake_cupti_extra_paths}
  1134. EXTRA_INCLUDE_DIRS "${CUDAToolkit_CUPTI_INCLUDE_DIR}")
  1135. _CUDAToolkit_find_and_add_import_lib(nvperf_host_static
  1136. EXTRA_PATH_SUFFIXES ${_cmake_cupti_extra_paths}
  1137. EXTRA_INCLUDE_DIRS "${CUDAToolkit_CUPTI_INCLUDE_DIR}")
  1138. _CUDAToolkit_find_and_add_import_lib(nvperf_target
  1139. EXTRA_PATH_SUFFIXES ${_cmake_cupti_extra_paths}
  1140. EXTRA_INCLUDE_DIRS "${CUDAToolkit_CUPTI_INCLUDE_DIR}")
  1141. endif()
  1142. if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 11.3.0)
  1143. _CUDAToolkit_find_and_add_import_lib(pcsamplingutil
  1144. EXTRA_PATH_SUFFIXES ${_cmake_cupti_extra_paths}
  1145. EXTRA_INCLUDE_DIRS "${CUDAToolkit_CUPTI_INCLUDE_DIR}")
  1146. endif()
  1147. endif()
  1148. if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 11.1.0)
  1149. if(NOT TARGET CUDA::nvptxcompiler_static)
  1150. _CUDAToolkit_find_and_add_import_lib(nvptxcompiler_static)
  1151. if(TARGET CUDA::nvptxcompiler_static)
  1152. target_link_libraries(CUDA::nvptxcompiler_static INTERFACE CUDA::cudart_static_deps)
  1153. endif()
  1154. endif()
  1155. endif()
  1156. _CUDAToolkit_find_and_add_import_lib(nvrtc_builtins ALT nvrtc-builtins)
  1157. _CUDAToolkit_find_and_add_import_lib(nvrtc)
  1158. if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 11.5.0)
  1159. _CUDAToolkit_find_and_add_import_lib(nvrtc_builtins_static ALT nvrtc-builtins_static)
  1160. if(NOT TARGET CUDA::nvrtc_static)
  1161. _CUDAToolkit_find_and_add_import_lib(nvrtc_static DEPS nvrtc_builtins_static nvptxcompiler_static)
  1162. if(TARGET CUDA::nvrtc_static AND WIN32 AND NOT (BORLAND OR MINGW OR CYGWIN))
  1163. target_link_libraries(CUDA::nvrtc_static INTERFACE Ws2_32.lib)
  1164. endif()
  1165. endif()
  1166. endif()
  1167. _CUDAToolkit_find_and_add_import_lib(nvml ALT nvidia-ml nvml)
  1168. _CUDAToolkit_find_and_add_import_lib(nvml_static ONLY_SEARCH_FOR libnvidia-ml.a libnvml.a)
  1169. if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 10.0)
  1170. # Header-only variant. Uses dlopen().
  1171. if(NOT TARGET CUDA::nvtx3)
  1172. add_library(CUDA::nvtx3 INTERFACE IMPORTED)
  1173. target_include_directories(CUDA::nvtx3 SYSTEM INTERFACE "${CUDAToolkit_INCLUDE_DIRS}")
  1174. target_link_libraries(CUDA::nvtx3 INTERFACE ${CMAKE_DL_LIBS})
  1175. endif()
  1176. endif()
  1177. if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 12.9)
  1178. if(NOT TARGET CUDA::nvtx3_interop)
  1179. _CUDAToolkit_find_and_add_import_lib(nvtx3_interop ALT nvtx3interop)
  1180. endif()
  1181. endif()
  1182. # nvToolsExt is removed starting in 12.9
  1183. if(CUDAToolkit_VERSION VERSION_LESS 12.9)
  1184. if(WIN32)
  1185. # nvtools can be installed outside the CUDA toolkit directory
  1186. # so prefer the NVTOOLSEXT_PATH windows only environment variable
  1187. # In addition on windows the most common name is nvToolsExt64_1
  1188. find_library(CUDA_nvToolsExt_LIBRARY
  1189. NAMES nvToolsExt64_1 nvToolsExt64 nvToolsExt
  1190. PATHS ENV NVTOOLSEXT_PATH
  1191. ENV CUDA_PATH
  1192. PATH_SUFFIXES lib/x64 lib
  1193. )
  1194. endif()
  1195. _CUDAToolkit_find_and_add_import_lib(nvToolsExt ALT nvToolsExt64)
  1196. if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 10.0)
  1197. # nvToolsExt is deprecated since nvtx3 introduction.
  1198. # Warn only if the project requires a sufficiently new CMake to make migration possible.
  1199. if(TARGET CUDA::nvToolsExt AND CMAKE_MINIMUM_REQUIRED_VERSION VERSION_GREATER_EQUAL 3.25)
  1200. 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.")
  1201. endif()
  1202. endif()
  1203. endif()
  1204. _CUDAToolkit_find_and_add_import_lib(OpenCL)
  1205. find_program(CUDA_bin2c_EXECUTABLE
  1206. NAMES bin2c
  1207. HINTS ${CUDAToolkit_BIN_DIR}
  1208. NO_DEFAULT_PATH
  1209. )
  1210. if(NOT TARGET CUDA::bin2c AND CUDA_bin2c_EXECUTABLE)
  1211. add_executable(CUDA::bin2c IMPORTED)
  1212. set_property(TARGET CUDA::bin2c PROPERTY IMPORTED_LOCATION "${CUDA_bin2c_EXECUTABLE}")
  1213. endif()
  1214. endif()
  1215. if(_CUDAToolkit_Pop_ROOT_PATH)
  1216. list(REMOVE_AT CMAKE_FIND_ROOT_PATH 0)
  1217. unset(_CUDAToolkit_Pop_ROOT_PATH)
  1218. endif()
  1219. unset(_CUDAToolkit_win_search_dirs)
  1220. unset(_CUDAToolkit_win_stub_search_dirs)