FindCUDAToolkit.cmake 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindCUDAToolkit
  5. ---------------
  6. This script locates the NVIDIA CUDA toolkit and the associated libraries, but
  7. does not require the ``CUDA`` language be enabled for a given project. This
  8. module does not search for the NVIDIA CUDA Samples.
  9. Search Behavior
  10. ^^^^^^^^^^^^^^^
  11. Finding the CUDA Toolkit requires finding the ``nvcc`` executable, which is
  12. searched for in the following order:
  13. 1. If the ``CUDA`` language has been enabled we will use the directory
  14. containing the compiler as the first search location for ``nvcc``.
  15. 2. If the ``CUDAToolkit_ROOT`` cmake configuration variable (e.g.,
  16. ``-DCUDAToolkit_ROOT=/some/path``) *or* environment variable is defined, it
  17. will be searched. If both an environment variable **and** a
  18. configuration variable are specified, the *configuration* variable takes
  19. precedence.
  20. The directory specified here must be such that the executable ``nvcc`` can be
  21. found underneath the directory specified by ``CUDAToolkit_ROOT``. If
  22. ``CUDAToolkit_ROOT`` is specified, but no ``nvcc`` is found underneath, this
  23. package is marked as **not** found. No subsequent search attempts are
  24. performed.
  25. 3. If the CUDA_PATH environment variable is defined, it will be searched.
  26. 4. The user's path is searched for ``nvcc`` using :command:`find_program`. If
  27. this is found, no subsequent search attempts are performed. Users are
  28. responsible for ensuring that the first ``nvcc`` to show up in the path is
  29. the desired path in the event that multiple CUDA Toolkits are installed.
  30. 5. On Unix systems, if the symbolic link ``/usr/local/cuda`` exists, this is
  31. used. No subsequent search attempts are performed. No default symbolic link
  32. location exists for the Windows platform.
  33. 6. The platform specific default install locations are searched. If exactly one
  34. candidate is found, this is used. The default CUDA Toolkit install locations
  35. searched are:
  36. +-------------+-------------------------------------------------------------+
  37. | Platform | Search Pattern |
  38. +=============+=============================================================+
  39. | macOS | ``/Developer/NVIDIA/CUDA-X.Y`` |
  40. +-------------+-------------------------------------------------------------+
  41. | Other Unix | ``/usr/local/cuda-X.Y`` |
  42. +-------------+-------------------------------------------------------------+
  43. | Windows | ``C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vX.Y`` |
  44. +-------------+-------------------------------------------------------------+
  45. Where ``X.Y`` would be a specific version of the CUDA Toolkit, such as
  46. ``/usr/local/cuda-9.0`` or
  47. ``C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0``
  48. .. note::
  49. When multiple CUDA Toolkits are installed in the default location of a
  50. system (e.g., both ``/usr/local/cuda-9.0`` and ``/usr/local/cuda-10.0``
  51. exist but the ``/usr/local/cuda`` symbolic link does **not** exist), this
  52. package is marked as **not** found.
  53. There are too many factors involved in making an automatic decision in
  54. the presence of multiple CUDA Toolkits being installed. In this
  55. situation, users are encouraged to either (1) set ``CUDAToolkit_ROOT`` or
  56. (2) ensure that the correct ``nvcc`` executable shows up in ``$PATH`` for
  57. :command:`find_program` to find.
  58. Options
  59. ^^^^^^^
  60. ``VERSION``
  61. If specified, describes the version of the CUDA Toolkit to search for.
  62. ``REQUIRED``
  63. If specified, configuration will error if a suitable CUDA Toolkit is not
  64. found.
  65. ``QUIET``
  66. If specified, the search for a suitable CUDA Toolkit will not produce any
  67. messages.
  68. ``EXACT``
  69. If specified, the CUDA Toolkit is considered found only if the exact
  70. ``VERSION`` specified is recovered.
  71. Imported targets
  72. ^^^^^^^^^^^^^^^^
  73. An :ref:`imported target <Imported targets>` named ``CUDA::toolkit`` is provided.
  74. This module defines :prop_tgt:`IMPORTED` targets for each
  75. of the following libraries that are part of the CUDAToolkit:
  76. - :ref:`CUDA Runtime Library<cuda_toolkit_rt_lib>`
  77. - :ref:`CUDA Driver Library<cuda_toolkit_driver_lib>`
  78. - :ref:`cuBLAS<cuda_toolkit_cuBLAS>`
  79. - :ref:`cuFFT<cuda_toolkit_cuFFT>`
  80. - :ref:`cuRAND<cuda_toolkit_cuRAND>`
  81. - :ref:`cuSOLVER<cuda_toolkit_cuSOLVER>`
  82. - :ref:`cuSPARSE<cuda_toolkit_cuSPARSE>`
  83. - :ref:`cuPTI<cuda_toolkit_cupti>`
  84. - :ref:`NPP<cuda_toolkit_NPP>`
  85. - :ref:`nvBLAS<cuda_toolkit_nvBLAS>`
  86. - :ref:`nvGRAPH<cuda_toolkit_nvGRAPH>`
  87. - :ref:`nvJPEG<cuda_toolkit_nvJPEG>`
  88. - :ref:`nvidia-ML<cuda_toolkit_nvML>`
  89. - :ref:`nvRTC<cuda_toolkit_nvRTC>`
  90. - :ref:`nvToolsExt<cuda_toolkit_nvToolsExt>`
  91. - :ref:`OpenCL<cuda_toolkit_opencl>`
  92. - :ref:`cuLIBOS<cuda_toolkit_cuLIBOS>`
  93. .. _`cuda_toolkit_rt_lib`:
  94. CUDA Runtime Library
  95. """"""""""""""""""""
  96. The CUDA Runtime library (cudart) are what most applications will typically
  97. need to link against to make any calls such as `cudaMalloc`, and `cudaFree`.
  98. Targets Created:
  99. - ``CUDA::cudart``
  100. - ``CUDA::cudart_static``
  101. .. _`cuda_toolkit_driver_lib`:
  102. CUDA Driver Library
  103. """"""""""""""""""""
  104. The CUDA Driver library (cuda) are used by applications that use calls
  105. such as `cuMemAlloc`, and `cuMemFree`. This is generally used by advanced
  106. Targets Created:
  107. - ``CUDA::cuda_driver``
  108. - ``CUDA::cuda_driver``
  109. .. _`cuda_toolkit_cuBLAS`:
  110. cuBLAS
  111. """"""
  112. The `cuBLAS <https://docs.nvidia.com/cuda/cublas/index.html>`_ library.
  113. Targets Created:
  114. - ``CUDA::cublas``
  115. - ``CUDA::cublas_static``
  116. .. _`cuda_toolkit_cuFFT`:
  117. cuFFT
  118. """""
  119. The `cuFFT <https://docs.nvidia.com/cuda/cufft/index.html>`_ library.
  120. Targets Created:
  121. - ``CUDA::cufft``
  122. - ``CUDA::cufftw``
  123. - ``CUDA::cufft_static``
  124. - ``CUDA::cufftw_static``
  125. cuRAND
  126. """"""
  127. The `cuRAND <https://docs.nvidia.com/cuda/curand/index.html>`_ library.
  128. Targets Created:
  129. - ``CUDA::curand``
  130. - ``CUDA::curand_static``
  131. .. _`cuda_toolkit_cuSOLVER`:
  132. cuSOLVER
  133. """"""""
  134. The `cuSOLVER <https://docs.nvidia.com/cuda/cusolver/index.html>`_ library.
  135. Targets Created:
  136. - ``CUDA::cusolver``
  137. - ``CUDA::cusolver_static``
  138. .. _`cuda_toolkit_cuSPARSE`:
  139. cuSPARSE
  140. """"""""
  141. The `cuSPARSE <https://docs.nvidia.com/cuda/cusparse/index.html>`_ library.
  142. Targets Created:
  143. - ``CUDA::cusparse``
  144. - ``CUDA::cusparse_static``
  145. .. _`cuda_toolkit_cupti`:
  146. cupti
  147. """""
  148. The `NVIDIA CUDA Profiling Tools Interface <https://developer.nvidia.com/CUPTI>`_.
  149. Targets Created:
  150. - ``CUDA::cupti``
  151. - ``CUDA::cupti_static``
  152. .. _`cuda_toolkit_NPP`:
  153. NPP
  154. """
  155. The `NPP <https://docs.nvidia.com/cuda/npp/index.html>`_ libraries.
  156. Targets Created:
  157. - `nppc`:
  158. - ``CUDA::nppc``
  159. - ``CUDA::nppc_static``
  160. - `nppial`: Arithmetic and logical operation functions in `nppi_arithmetic_and_logical_operations.h`
  161. - ``CUDA::nppial``
  162. - ``CUDA::nppial_static``
  163. - `nppicc`: Color conversion and sampling functions in `nppi_color_conversion.h`
  164. - ``CUDA::nppicc``
  165. - ``CUDA::nppicc_static``
  166. - `nppicom`: JPEG compression and decompression functions in `nppi_compression_functions.h`
  167. - ``CUDA::nppicom``
  168. - ``CUDA::nppicom_static``
  169. - `nppidei`: Data exchange and initialization functions in `nppi_data_exchange_and_initialization.h`
  170. - ``CUDA::nppidei``
  171. - ``CUDA::nppidei_static``
  172. - `nppif`: Filtering and computer vision functions in `nppi_filter_functions.h`
  173. - ``CUDA::nppif``
  174. - ``CUDA::nppif_static``
  175. - `nppig`: Geometry transformation functions found in `nppi_geometry_transforms.h`
  176. - ``CUDA::nppig``
  177. - ``CUDA::nppig_static``
  178. - `nppim`: Morphological operation functions found in `nppi_morphological_operations.h`
  179. - ``CUDA::nppim``
  180. - ``CUDA::nppim_static``
  181. - `nppist`: Statistics and linear transform in `nppi_statistics_functions.h` and `nppi_linear_transforms.h`
  182. - ``CUDA::nppist``
  183. - ``CUDA::nppist_static``
  184. - `nppisu`: Memory support functions in `nppi_support_functions.h`
  185. - ``CUDA::nppisu``
  186. - ``CUDA::nppisu_static``
  187. - `nppitc`: Threshold and compare operation functions in `nppi_threshold_and_compare_operations.h`
  188. - ``CUDA::nppitc``
  189. - ``CUDA::nppitc_static``
  190. - `npps`:
  191. - ``CUDA::npps``
  192. - ``CUDA::npps_static``
  193. .. _`cuda_toolkit_nvBLAS`:
  194. nvBLAS
  195. """"""
  196. The `nvBLAS <https://docs.nvidia.com/cuda/nvblas/index.html>`_ libraries.
  197. This is a shared library only.
  198. Targets Created:
  199. - ``CUDA::nvblas``
  200. .. _`cuda_toolkit_nvGRAPH`:
  201. nvGRAPH
  202. """""""
  203. The `nvGRAPH <https://docs.nvidia.com/cuda/nvgraph/index.html>`_ library.
  204. Targets Created:
  205. - ``CUDA::nvgraph``
  206. - ``CUDA::nvgraph_static``
  207. .. _`cuda_toolkit_nvJPEG`:
  208. nvJPEG
  209. """"""
  210. The `nvJPEG <https://docs.nvidia.com/cuda/nvjpeg/index.html>`_ library.
  211. Introduced in CUDA 10.
  212. Targets Created:
  213. - ``CUDA::nvjpeg``
  214. - ``CUDA::nvjpeg_static``
  215. .. _`cuda_toolkit_nvRTC`:
  216. nvRTC
  217. """""
  218. The `nvRTC <https://docs.nvidia.com/cuda/nvrtc/index.html>`_ (Runtime Compilation) library.
  219. This is a shared library only.
  220. Targets Created:
  221. - ``CUDA::nvrtc``
  222. .. _`cuda_toolkit_nvml`:
  223. nvidia-ML
  224. """""""""
  225. The `NVIDIA Management Library <https://developer.nvidia.com/nvidia-management-library-nvml>`_.
  226. This is a shared library only.
  227. Targets Created:
  228. - ``CUDA::nvml``
  229. .. _`cuda_toolkit_nvToolsExt`:
  230. nvToolsExt
  231. """"""""""
  232. The `NVIDIA Tools Extension <https://docs.nvidia.com/gameworks/content/gameworkslibrary/nvtx/nvidia_tools_extension_library_nvtx.htm>`_.
  233. This is a shared library only.
  234. Targets Created:
  235. - ``CUDA::nvToolsExt``
  236. .. _`cuda_toolkit_opencl`:
  237. OpenCL
  238. """"""
  239. The `NVIDIA OpenCL Library <https://developer.nvidia.com/opencl>`_.
  240. This is a shared library only.
  241. Targets Created:
  242. - ``CUDA::OpenCL``
  243. .. _`cuda_toolkit_cuLIBOS`:
  244. cuLIBOS
  245. """""""
  246. The cuLIBOS library is a backend thread abstraction layer library which is
  247. static only. The ``CUDA::cublas_static``, ``CUDA::cusparse_static``,
  248. ``CUDA::cufft_static``, ``CUDA::curand_static``, and (when implemented) NPP
  249. libraries all automatically have this dependency linked.
  250. Target Created:
  251. - ``CUDA::culibos``
  252. **Note**: direct usage of this target by consumers should not be necessary.
  253. .. _`cuda_toolkit_cuRAND`:
  254. Result variables
  255. ^^^^^^^^^^^^^^^^
  256. ``CUDAToolkit_FOUND``
  257. A boolean specifying whether or not the CUDA Toolkit was found.
  258. ``CUDAToolkit_VERSION``
  259. The exact version of the CUDA Toolkit found (as reported by
  260. ``nvcc --version``).
  261. ``CUDAToolkit_VERSION_MAJOR``
  262. The major version of the CUDA Toolkit.
  263. ``CUDAToolkit_VERSION_MAJOR``
  264. The minor version of the CUDA Toolkit.
  265. ``CUDAToolkit_VERSION_PATCH``
  266. The patch version of the CUDA Toolkit.
  267. ``CUDAToolkit_BIN_DIR``
  268. The path to the CUDA Toolkit library directory that contains the CUDA
  269. executable ``nvcc``.
  270. ``CUDAToolkit_INCLUDE_DIRS``
  271. The path to the CUDA Toolkit ``include`` folder containing the header files
  272. required to compile a project linking against CUDA.
  273. ``CUDAToolkit_LIBRARY_DIR``
  274. The path to the CUDA Toolkit library directory that contains the CUDA
  275. Runtime library ``cudart``.
  276. ``CUDAToolkit_TARGET_DIR``
  277. The path to the CUDA Toolkit directory including the target architecture
  278. when cross-compiling. When not cross-compiling this will be equivalant to
  279. ``CUDAToolkit_ROOT_DIR``.
  280. ``CUDAToolkit_NVCC_EXECUTABLE``
  281. The path to the NVIDIA CUDA compiler ``nvcc``. Note that this path may
  282. **not** be the same as
  283. :variable:`CMAKE_CUDA_COMPILER <CMAKE_<LANG>_COMPILER>`. ``nvcc`` must be
  284. found to determine the CUDA Toolkit version as well as determining other
  285. features of the Toolkit. This variable is set for the convenience of
  286. modules that depend on this one.
  287. #]=======================================================================]
  288. # NOTE: much of this was simply extracted from FindCUDA.cmake.
  289. # James Bigler, NVIDIA Corp (nvidia.com - jbigler)
  290. # Abe Stephens, SCI Institute -- http://www.sci.utah.edu/~abe/FindCuda.html
  291. #
  292. # Copyright (c) 2008 - 2009 NVIDIA Corporation. All rights reserved.
  293. #
  294. # Copyright (c) 2007-2009
  295. # Scientific Computing and Imaging Institute, University of Utah
  296. #
  297. # This code is licensed under the MIT License. See the FindCUDA.cmake script
  298. # for the text of the license.
  299. # The MIT License
  300. #
  301. # License for the specific language governing rights and limitations under
  302. # Permission is hereby granted, free of charge, to any person obtaining a
  303. # copy of this software and associated documentation files (the "Software"),
  304. # to deal in the Software without restriction, including without limitation
  305. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  306. # and/or sell copies of the Software, and to permit persons to whom the
  307. # Software is furnished to do so, subject to the following conditions:
  308. #
  309. # The above copyright notice and this permission notice shall be included
  310. # in all copies or substantial portions of the Software.
  311. #
  312. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  313. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  314. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  315. # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  316. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  317. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  318. # DEALINGS IN THE SOFTWARE.
  319. #
  320. ###############################################################################
  321. # Include shared CUDA toolkit location code.
  322. include(Internal/CUDAToolkit)
  323. if(CUDAToolkit_NVCC_EXECUTABLE AND
  324. CUDAToolkit_NVCC_EXECUTABLE STREQUAL CMAKE_CUDA_COMPILER)
  325. # Need to set these based off the already computed CMAKE_CUDA_COMPILER_VERSION value
  326. # This if statement will always match, but is used to provide variables for MATCH 1,2,3...
  327. if(CMAKE_CUDA_COMPILER_VERSION MATCHES [=[([0-9]+)\.([0-9]+)\.([0-9]+)]=])
  328. set(CUDAToolkit_VERSION_MAJOR "${CMAKE_MATCH_1}")
  329. set(CUDAToolkit_VERSION_MINOR "${CMAKE_MATCH_2}")
  330. set(CUDAToolkit_VERSION_PATCH "${CMAKE_MATCH_3}")
  331. set(CUDAToolkit_VERSION "${CMAKE_CUDA_COMPILER_VERSION}")
  332. endif()
  333. else()
  334. # Compute the version by invoking nvcc
  335. execute_process (COMMAND ${CUDAToolkit_NVCC_EXECUTABLE} "--version" OUTPUT_VARIABLE NVCC_OUT)
  336. if(NVCC_OUT MATCHES [=[ V([0-9]+)\.([0-9]+)\.([0-9]+)]=])
  337. set(CUDAToolkit_VERSION_MAJOR "${CMAKE_MATCH_1}")
  338. set(CUDAToolkit_VERSION_MINOR "${CMAKE_MATCH_2}")
  339. set(CUDAToolkit_VERSION_PATCH "${CMAKE_MATCH_3}")
  340. set(CUDAToolkit_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}")
  341. endif()
  342. unset(NVCC_OUT)
  343. endif()
  344. if(NOT CUDA_CUDART AND NOT CUDAToolkit_FIND_QUIETLY)
  345. message(STATUS "Unable to find cudart library.")
  346. endif()
  347. # Find the CUDA Runtime Library libcudart
  348. find_library(CUDA_CUDART
  349. NAMES cudart
  350. PATH_SUFFIXES lib64 lib/x64
  351. )
  352. if(NOT CUDA_CUDART)
  353. find_library(CUDA_CUDART
  354. NAMES cudart
  355. PATH_SUFFIXES lib64/stubs lib/x64/stubs
  356. )
  357. endif()
  358. unset(CUDAToolkit_ROOT_DIR)
  359. if(_CUDAToolkit_Pop_Prefix)
  360. list(REMOVE_AT CMAKE_PREFIX_PATH -1)
  361. unset(_CUDAToolkit_Pop_Prefix)
  362. endif()
  363. #-----------------------------------------------------------------------------
  364. # Perform version comparison and validate all required variables are set.
  365. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  366. find_package_handle_standard_args(CUDAToolkit
  367. REQUIRED_VARS
  368. CUDAToolkit_INCLUDE_DIR
  369. CUDA_CUDART
  370. CUDAToolkit_NVCC_EXECUTABLE
  371. VERSION_VAR
  372. CUDAToolkit_VERSION
  373. )
  374. mark_as_advanced(CUDA_CUDART
  375. CUDAToolkit_INCLUDE_DIR
  376. CUDAToolkit_NVCC_EXECUTABLE
  377. )
  378. #-----------------------------------------------------------------------------
  379. # Construct result variables
  380. if(CUDAToolkit_FOUND)
  381. set(CUDAToolkit_INCLUDE_DIRS ${CUDAToolkit_INCLUDE_DIR})
  382. get_filename_component(CUDAToolkit_LIBRARY_DIR ${CUDA_CUDART} DIRECTORY ABSOLUTE)
  383. endif()
  384. #-----------------------------------------------------------------------------
  385. # Construct import targets
  386. if(CUDAToolkit_FOUND)
  387. function(_CUDAToolkit_find_and_add_import_lib lib_name)
  388. cmake_parse_arguments(arg "" "" "ALT;DEPS;EXTRA_PATH_SUFFIXES" ${ARGN})
  389. set(search_names ${lib_name} ${arg_ALT})
  390. find_library(CUDA_${lib_name}_LIBRARY
  391. NAMES ${search_names}
  392. HINTS ${CUDAToolkit_LIBRARY_DIR}
  393. ENV CUDA_PATH
  394. PATH_SUFFIXES nvidia/current lib64 lib/x64 lib
  395. ${arg_EXTRA_PATH_SUFFIXES}
  396. )
  397. # Don't try any stub directories intil we have exhausted all other
  398. # search locations.
  399. if(NOT CUDA_${lib_name}_LIBRARY)
  400. find_library(CUDA_${lib_name}_LIBRARY
  401. NAMES ${search_names}
  402. HINTS ${CUDAToolkit_LIBRARY_DIR}
  403. ENV CUDA_PATH
  404. PATH_SUFFIXES lib64/stubs lib/x64/stubs lib/stubs stubs
  405. )
  406. endif()
  407. mark_as_advanced(CUDA_${lib_name}_LIBRARY)
  408. if (NOT TARGET CUDA::${lib_name} AND CUDA_${lib_name}_LIBRARY)
  409. add_library(CUDA::${lib_name} IMPORTED INTERFACE)
  410. target_include_directories(CUDA::${lib_name} SYSTEM INTERFACE "${CUDAToolkit_INCLUDE_DIRS}")
  411. target_link_libraries(CUDA::${lib_name} INTERFACE "${CUDA_${lib_name}_LIBRARY}")
  412. foreach(dep ${arg_DEPS})
  413. if(TARGET CUDA::${dep})
  414. target_link_libraries(CUDA::${lib_name} INTERFACE CUDA::${dep})
  415. endif()
  416. endforeach()
  417. endif()
  418. endfunction()
  419. if(NOT TARGET CUDA::toolkit)
  420. add_library(CUDA::toolkit IMPORTED INTERFACE)
  421. target_include_directories(CUDA::toolkit SYSTEM INTERFACE "${CUDAToolkit_INCLUDE_DIRS}")
  422. target_link_directories(CUDA::toolkit INTERFACE "${CUDAToolkit_LIBRARY_DIR}")
  423. endif()
  424. _CUDAToolkit_find_and_add_import_lib(cuda_driver ALT cuda)
  425. _CUDAToolkit_find_and_add_import_lib(cudart)
  426. _CUDAToolkit_find_and_add_import_lib(cudart_static)
  427. # setup dependencies that are required for cudart_static when building
  428. # on linux. These are generally only required when using the CUDA toolkit
  429. # when CUDA language is disabled
  430. if(NOT TARGET CUDA::cudart_static_deps
  431. AND TARGET CUDA::cudart_static)
  432. add_library(CUDA::cudart_static_deps IMPORTED INTERFACE)
  433. target_link_libraries(CUDA::cudart_static INTERFACE CUDA::cudart_static_deps)
  434. if(UNIX AND (CMAKE_C_COMPILER OR CMAKE_CXX_COMPILER))
  435. find_package(Threads REQUIRED)
  436. target_link_libraries(CUDA::cudart_static_deps INTERFACE Threads::Threads ${CMAKE_DL_LIBS})
  437. endif()
  438. if(UNIX AND NOT APPLE)
  439. # On Linux, you must link against librt when using the static cuda runtime.
  440. find_library(CUDAToolkit_rt_LIBRARY rt)
  441. mark_as_advanced(CUDAToolkit_rt_LIBRARY)
  442. if(NOT CUDAToolkit_rt_LIBRARY)
  443. message(WARNING "Could not find librt library, needed by CUDA::cudart_static")
  444. else()
  445. target_link_libraries(CUDA::cudart_static_deps INTERFACE ${CUDAToolkit_rt_LIBRARY})
  446. endif()
  447. endif()
  448. endif()
  449. _CUDAToolkit_find_and_add_import_lib(culibos) # it's a static library
  450. foreach (cuda_lib cublas cufft curand cusparse nppc nvjpeg)
  451. _CUDAToolkit_find_and_add_import_lib(${cuda_lib})
  452. _CUDAToolkit_find_and_add_import_lib(${cuda_lib}_static DEPS culibos)
  453. endforeach()
  454. # cuFFTW depends on cuFFT
  455. _CUDAToolkit_find_and_add_import_lib(cufftw DEPS cufft)
  456. _CUDAToolkit_find_and_add_import_lib(cufftw DEPS cufft_static)
  457. # cuSOLVER depends on cuBLAS, and cuSPARSE
  458. _CUDAToolkit_find_and_add_import_lib(cusolver DEPS cublas cusparse)
  459. _CUDAToolkit_find_and_add_import_lib(cusolver_static DEPS cublas_static cusparse_static culibos)
  460. # nvGRAPH depends on cuRAND, and cuSOLVER.
  461. _CUDAToolkit_find_and_add_import_lib(nvgraph DEPS curand cusolver)
  462. _CUDAToolkit_find_and_add_import_lib(nvgraph_static DEPS curand_static cusolver_static)
  463. # Process the majority of the NPP libraries.
  464. foreach (cuda_lib nppial nppicc nppidei nppif nppig nppim nppist nppitc npps nppicom nppisu)
  465. _CUDAToolkit_find_and_add_import_lib(${cuda_lib} DEPS nppc)
  466. _CUDAToolkit_find_and_add_import_lib(${cuda_lib}_static DEPS nppc_static)
  467. endforeach()
  468. _CUDAToolkit_find_and_add_import_lib(cupti
  469. EXTRA_PATH_SUFFIXES ../extras/CUPTI/lib64/
  470. ../extras/CUPTI/lib/)
  471. _CUDAToolkit_find_and_add_import_lib(cupti_static
  472. EXTRA_PATH_SUFFIXES ../extras/CUPTI/lib64/
  473. ../extras/CUPTI/lib/)
  474. _CUDAToolkit_find_and_add_import_lib(nvrtc DEPS cuda_driver)
  475. _CUDAToolkit_find_and_add_import_lib(nvml ALT nvidia-ml nvml)
  476. if(WIN32)
  477. # nvtools can be installed outside the CUDA toolkit directory
  478. # so prefer the NVTOOLSEXT_PATH windows only environment variable
  479. # In addition on windows the most common name is nvToolsExt64_1
  480. find_library(CUDA_nvToolsExt_LIBRARY
  481. NAMES nvToolsExt64_1 nvToolsExt64 nvToolsExt
  482. PATHS ENV NVTOOLSEXT_PATH
  483. ENV CUDA_PATH
  484. PATH_SUFFIXES lib/x64 lib
  485. )
  486. endif()
  487. _CUDAToolkit_find_and_add_import_lib(nvToolsExt ALT nvToolsExt64)
  488. _CUDAToolkit_find_and_add_import_lib(OpenCL)
  489. endif()
  490. if(_CUDAToolkit_Pop_ROOT_PATH)
  491. list(REMOVE_AT CMAKE_FIND_ROOT_PATH 0)
  492. unset(_CUDAToolkit_Pop_ROOT_PATH)
  493. endif()