FindCUDAToolkit.cmake 52 KB

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