FindCUDAToolkit.cmake 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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. Search Behavior
  11. ^^^^^^^^^^^^^^^
  12. Finding the CUDA Toolkit requires finding the ``nvcc`` executable, which is
  13. searched for in the following order:
  14. 1. If the ``CUDA`` language has been enabled we will use the directory
  15. containing the compiler as the first search location for ``nvcc``.
  16. 2. If the ``CUDAToolkit_ROOT`` cmake configuration variable (e.g.,
  17. ``-DCUDAToolkit_ROOT=/some/path``) *or* environment variable is defined, it
  18. will be searched. If both an environment variable **and** a
  19. configuration variable are specified, the *configuration* variable takes
  20. precedence.
  21. The directory specified here must be such that the executable ``nvcc`` can be
  22. found underneath the directory specified by ``CUDAToolkit_ROOT``. If
  23. ``CUDAToolkit_ROOT`` is specified, but no ``nvcc`` is found underneath, this
  24. package is marked as **not** found. No subsequent search attempts are
  25. performed.
  26. 3. If the CUDA_PATH environment variable is defined, it will be searched.
  27. 4. The user's path is searched for ``nvcc`` using :command:`find_program`. If
  28. this is found, no subsequent search attempts are performed. Users are
  29. responsible for ensuring that the first ``nvcc`` to show up in the path is
  30. the desired path in the event that multiple CUDA Toolkits are installed.
  31. 5. On Unix systems, if the symbolic link ``/usr/local/cuda`` exists, this is
  32. used. No subsequent search attempts are performed. No default symbolic link
  33. location exists for the Windows platform.
  34. 6. The platform specific default install locations are searched. If exactly one
  35. candidate is found, this is used. The default CUDA Toolkit install locations
  36. searched are:
  37. +-------------+-------------------------------------------------------------+
  38. | Platform | Search Pattern |
  39. +=============+=============================================================+
  40. | macOS | ``/Developer/NVIDIA/CUDA-X.Y`` |
  41. +-------------+-------------------------------------------------------------+
  42. | Other Unix | ``/usr/local/cuda-X.Y`` |
  43. +-------------+-------------------------------------------------------------+
  44. | Windows | ``C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vX.Y`` |
  45. +-------------+-------------------------------------------------------------+
  46. Where ``X.Y`` would be a specific version of the CUDA Toolkit, such as
  47. ``/usr/local/cuda-9.0`` or
  48. ``C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0``
  49. .. note::
  50. When multiple CUDA Toolkits are installed in the default location of a
  51. system (e.g., both ``/usr/local/cuda-9.0`` and ``/usr/local/cuda-10.0``
  52. exist but the ``/usr/local/cuda`` symbolic link does **not** exist), this
  53. package is marked as **not** found.
  54. There are too many factors involved in making an automatic decision in
  55. the presence of multiple CUDA Toolkits being installed. In this
  56. situation, users are encouraged to either (1) set ``CUDAToolkit_ROOT`` or
  57. (2) ensure that the correct ``nvcc`` executable shows up in ``$PATH`` for
  58. :command:`find_program` to find.
  59. Options
  60. ^^^^^^^
  61. ``VERSION``
  62. If specified, describes the version of the CUDA Toolkit to search for.
  63. ``REQUIRED``
  64. If specified, configuration will error if a suitable CUDA Toolkit is not
  65. found.
  66. ``QUIET``
  67. If specified, the search for a suitable CUDA Toolkit will not produce any
  68. messages.
  69. ``EXACT``
  70. If specified, the CUDA Toolkit is considered found only if the exact
  71. ``VERSION`` specified is recovered.
  72. Imported targets
  73. ^^^^^^^^^^^^^^^^
  74. An :ref:`imported target <Imported targets>` named ``CUDA::toolkit`` is provided.
  75. This module defines :prop_tgt:`IMPORTED` targets for each
  76. of the following libraries that are part of the CUDAToolkit:
  77. - :ref:`CUDA Runtime Library<cuda_toolkit_rt_lib>`
  78. - :ref:`CUDA Driver Library<cuda_toolkit_driver_lib>`
  79. - :ref:`cuBLAS<cuda_toolkit_cuBLAS>`
  80. - :ref:`cuFFT<cuda_toolkit_cuFFT>`
  81. - :ref:`cuRAND<cuda_toolkit_cuRAND>`
  82. - :ref:`cuSOLVER<cuda_toolkit_cuSOLVER>`
  83. - :ref:`cuSPARSE<cuda_toolkit_cuSPARSE>`
  84. - :ref:`cuPTI<cuda_toolkit_cupti>`
  85. - :ref:`NPP<cuda_toolkit_NPP>`
  86. - :ref:`nvBLAS<cuda_toolkit_nvBLAS>`
  87. - :ref:`nvGRAPH<cuda_toolkit_nvGRAPH>`
  88. - :ref:`nvJPEG<cuda_toolkit_nvJPEG>`
  89. - :ref:`nvidia-ML<cuda_toolkit_nvML>`
  90. - :ref:`nvRTC<cuda_toolkit_nvRTC>`
  91. - :ref:`nvToolsExt<cuda_toolkit_nvToolsExt>`
  92. - :ref:`OpenCL<cuda_toolkit_opencl>`
  93. - :ref:`cuLIBOS<cuda_toolkit_cuLIBOS>`
  94. .. _`cuda_toolkit_rt_lib`:
  95. CUDA Runtime Library
  96. """"""""""""""""""""
  97. The CUDA Runtime library (cudart) are what most applications will typically
  98. need to link against to make any calls such as `cudaMalloc`, and `cudaFree`.
  99. Targets Created:
  100. - ``CUDA::cudart``
  101. - ``CUDA::cudart_static``
  102. .. _`cuda_toolkit_driver_lib`:
  103. CUDA Driver Library
  104. """"""""""""""""""""
  105. The CUDA Driver library (cuda) are used by applications that use calls
  106. such as `cuMemAlloc`, and `cuMemFree`. This is generally used by advanced
  107. Targets Created:
  108. - ``CUDA::cuda_driver``
  109. - ``CUDA::cuda_driver``
  110. .. _`cuda_toolkit_cuBLAS`:
  111. cuBLAS
  112. """"""
  113. The `cuBLAS <https://docs.nvidia.com/cuda/cublas/index.html>`_ library.
  114. Targets Created:
  115. - ``CUDA::cublas``
  116. - ``CUDA::cublas_static``
  117. - ``CUDA::cublasLt`` starting in CUDA 10.1
  118. - ``CUDA::cublasLt_static`` starting in CUDA 10.1
  119. .. _`cuda_toolkit_cuFFT`:
  120. cuFFT
  121. """""
  122. The `cuFFT <https://docs.nvidia.com/cuda/cufft/index.html>`_ library.
  123. Targets Created:
  124. - ``CUDA::cufft``
  125. - ``CUDA::cufftw``
  126. - ``CUDA::cufft_static``
  127. - ``CUDA::cufftw_static``
  128. cuRAND
  129. """"""
  130. The `cuRAND <https://docs.nvidia.com/cuda/curand/index.html>`_ library.
  131. Targets Created:
  132. - ``CUDA::curand``
  133. - ``CUDA::curand_static``
  134. .. _`cuda_toolkit_cuSOLVER`:
  135. cuSOLVER
  136. """"""""
  137. The `cuSOLVER <https://docs.nvidia.com/cuda/cusolver/index.html>`_ library.
  138. Targets Created:
  139. - ``CUDA::cusolver``
  140. - ``CUDA::cusolver_static``
  141. .. _`cuda_toolkit_cuSPARSE`:
  142. cuSPARSE
  143. """"""""
  144. The `cuSPARSE <https://docs.nvidia.com/cuda/cusparse/index.html>`_ library.
  145. Targets Created:
  146. - ``CUDA::cusparse``
  147. - ``CUDA::cusparse_static``
  148. .. _`cuda_toolkit_cupti`:
  149. cupti
  150. """""
  151. The `NVIDIA CUDA Profiling Tools Interface <https://developer.nvidia.com/CUPTI>`_.
  152. Targets Created:
  153. - ``CUDA::cupti``
  154. - ``CUDA::cupti_static``
  155. .. _`cuda_toolkit_NPP`:
  156. NPP
  157. """
  158. The `NPP <https://docs.nvidia.com/cuda/npp/index.html>`_ libraries.
  159. Targets Created:
  160. - `nppc`:
  161. - ``CUDA::nppc``
  162. - ``CUDA::nppc_static``
  163. - `nppial`: Arithmetic and logical operation functions in `nppi_arithmetic_and_logical_operations.h`
  164. - ``CUDA::nppial``
  165. - ``CUDA::nppial_static``
  166. - `nppicc`: Color conversion and sampling functions in `nppi_color_conversion.h`
  167. - ``CUDA::nppicc``
  168. - ``CUDA::nppicc_static``
  169. - `nppicom`: JPEG compression and decompression functions in `nppi_compression_functions.h`
  170. Removed starting in CUDA 11.0, use :ref:`nvJPEG<cuda_toolkit_nvJPEG>` instead.
  171. - ``CUDA::nppicom``
  172. - ``CUDA::nppicom_static``
  173. - `nppidei`: Data exchange and initialization functions in `nppi_data_exchange_and_initialization.h`
  174. - ``CUDA::nppidei``
  175. - ``CUDA::nppidei_static``
  176. - `nppif`: Filtering and computer vision functions in `nppi_filter_functions.h`
  177. - ``CUDA::nppif``
  178. - ``CUDA::nppif_static``
  179. - `nppig`: Geometry transformation functions found in `nppi_geometry_transforms.h`
  180. - ``CUDA::nppig``
  181. - ``CUDA::nppig_static``
  182. - `nppim`: Morphological operation functions found in `nppi_morphological_operations.h`
  183. - ``CUDA::nppim``
  184. - ``CUDA::nppim_static``
  185. - `nppist`: Statistics and linear transform in `nppi_statistics_functions.h` and `nppi_linear_transforms.h`
  186. - ``CUDA::nppist``
  187. - ``CUDA::nppist_static``
  188. - `nppisu`: Memory support functions in `nppi_support_functions.h`
  189. - ``CUDA::nppisu``
  190. - ``CUDA::nppisu_static``
  191. - `nppitc`: Threshold and compare operation functions in `nppi_threshold_and_compare_operations.h`
  192. - ``CUDA::nppitc``
  193. - ``CUDA::nppitc_static``
  194. - `npps`:
  195. - ``CUDA::npps``
  196. - ``CUDA::npps_static``
  197. .. _`cuda_toolkit_nvBLAS`:
  198. nvBLAS
  199. """"""
  200. The `nvBLAS <https://docs.nvidia.com/cuda/nvblas/index.html>`_ libraries.
  201. This is a shared library only.
  202. Targets Created:
  203. - ``CUDA::nvblas``
  204. .. _`cuda_toolkit_nvGRAPH`:
  205. nvGRAPH
  206. """""""
  207. The `nvGRAPH <https://docs.nvidia.com/cuda/nvgraph/index.html>`_ library.
  208. Removed starting in CUDA 11.0
  209. Targets Created:
  210. - ``CUDA::nvgraph``
  211. - ``CUDA::nvgraph_static``
  212. .. _`cuda_toolkit_nvJPEG`:
  213. nvJPEG
  214. """"""
  215. The `nvJPEG <https://docs.nvidia.com/cuda/nvjpeg/index.html>`_ library.
  216. Introduced in CUDA 10.
  217. Targets Created:
  218. - ``CUDA::nvjpeg``
  219. - ``CUDA::nvjpeg_static``
  220. .. _`cuda_toolkit_nvRTC`:
  221. nvRTC
  222. """""
  223. The `nvRTC <https://docs.nvidia.com/cuda/nvrtc/index.html>`_ (Runtime Compilation) library.
  224. This is a shared library only.
  225. Targets Created:
  226. - ``CUDA::nvrtc``
  227. .. _`cuda_toolkit_nvml`:
  228. nvidia-ML
  229. """""""""
  230. The `NVIDIA Management Library <https://developer.nvidia.com/nvidia-management-library-nvml>`_.
  231. This is a shared library only.
  232. Targets Created:
  233. - ``CUDA::nvml``
  234. .. _`cuda_toolkit_nvToolsExt`:
  235. nvToolsExt
  236. """"""""""
  237. The `NVIDIA Tools Extension <https://docs.nvidia.com/gameworks/content/gameworkslibrary/nvtx/nvidia_tools_extension_library_nvtx.htm>`_.
  238. This is a shared library only.
  239. Targets Created:
  240. - ``CUDA::nvToolsExt``
  241. .. _`cuda_toolkit_opencl`:
  242. OpenCL
  243. """"""
  244. The `NVIDIA OpenCL Library <https://developer.nvidia.com/opencl>`_.
  245. This is a shared library only.
  246. Targets Created:
  247. - ``CUDA::OpenCL``
  248. .. _`cuda_toolkit_cuLIBOS`:
  249. cuLIBOS
  250. """""""
  251. The cuLIBOS library is a backend thread abstraction layer library which is
  252. static only. The ``CUDA::cublas_static``, ``CUDA::cusparse_static``,
  253. ``CUDA::cufft_static``, ``CUDA::curand_static``, and (when implemented) NPP
  254. libraries all automatically have this dependency linked.
  255. Target Created:
  256. - ``CUDA::culibos``
  257. **Note**: direct usage of this target by consumers should not be necessary.
  258. .. _`cuda_toolkit_cuRAND`:
  259. Result variables
  260. ^^^^^^^^^^^^^^^^
  261. ``CUDAToolkit_FOUND``
  262. A boolean specifying whether or not the CUDA Toolkit was found.
  263. ``CUDAToolkit_VERSION``
  264. The exact version of the CUDA Toolkit found (as reported by
  265. ``nvcc --version``).
  266. ``CUDAToolkit_VERSION_MAJOR``
  267. The major version of the CUDA Toolkit.
  268. ``CUDAToolkit_VERSION_MAJOR``
  269. The minor version of the CUDA Toolkit.
  270. ``CUDAToolkit_VERSION_PATCH``
  271. The patch version of the CUDA Toolkit.
  272. ``CUDAToolkit_BIN_DIR``
  273. The path to the CUDA Toolkit library directory that contains the CUDA
  274. executable ``nvcc``.
  275. ``CUDAToolkit_INCLUDE_DIRS``
  276. The path to the CUDA Toolkit ``include`` folder containing the header files
  277. required to compile a project linking against CUDA.
  278. ``CUDAToolkit_LIBRARY_DIR``
  279. The path to the CUDA Toolkit library directory that contains the CUDA
  280. Runtime library ``cudart``.
  281. ``CUDAToolkit_LIBRARY_ROOT``
  282. The path to the CUDA Toolkit directory containing the nvvm directory and
  283. version.txt.
  284. ``CUDAToolkit_TARGET_DIR``
  285. The path to the CUDA Toolkit directory including the target architecture
  286. when cross-compiling. When not cross-compiling this will be equivalent to
  287. ``CUDAToolkit_ROOT_DIR``.
  288. ``CUDAToolkit_NVCC_EXECUTABLE``
  289. The path to the NVIDIA CUDA compiler ``nvcc``. Note that this path may
  290. **not** be the same as
  291. :variable:`CMAKE_CUDA_COMPILER <CMAKE_<LANG>_COMPILER>`. ``nvcc`` must be
  292. found to determine the CUDA Toolkit version as well as determining other
  293. features of the Toolkit. This variable is set for the convenience of
  294. modules that depend on this one.
  295. #]=======================================================================]
  296. # NOTE: much of this was simply extracted from FindCUDA.cmake.
  297. # James Bigler, NVIDIA Corp (nvidia.com - jbigler)
  298. # Abe Stephens, SCI Institute -- http://www.sci.utah.edu/~abe/FindCuda.html
  299. #
  300. # Copyright (c) 2008 - 2009 NVIDIA Corporation. All rights reserved.
  301. #
  302. # Copyright (c) 2007-2009
  303. # Scientific Computing and Imaging Institute, University of Utah
  304. #
  305. # This code is licensed under the MIT License. See the FindCUDA.cmake script
  306. # for the text of the license.
  307. # The MIT License
  308. #
  309. # License for the specific language governing rights and limitations under
  310. # Permission is hereby granted, free of charge, to any person obtaining a
  311. # copy of this software and associated documentation files (the "Software"),
  312. # to deal in the Software without restriction, including without limitation
  313. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  314. # and/or sell copies of the Software, and to permit persons to whom the
  315. # Software is furnished to do so, subject to the following conditions:
  316. #
  317. # The above copyright notice and this permission notice shall be included
  318. # in all copies or substantial portions of the Software.
  319. #
  320. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  321. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  322. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  323. # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  324. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  325. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  326. # DEALINGS IN THE SOFTWARE.
  327. #
  328. ###############################################################################
  329. # The toolkit is located during compiler detection for CUDA and stored in CMakeCUDACompiler.cmake as
  330. # CMAKE_CUDA_COMPILER_TOOLKIT_ROOT and CMAKE_CUDA_COMPILER_LIBRARY_ROOT.
  331. # We compute the rest based on those here to avoid re-searching and to avoid finding a possibly
  332. # different installation.
  333. if(CMAKE_CUDA_COMPILER_TOOLKIT_ROOT)
  334. set(CUDAToolkit_ROOT_DIR "${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}")
  335. set(CUDAToolkit_LIBRARY_ROOT "${CMAKE_CUDA_COMPILER_LIBRARY_ROOT}")
  336. set(CUDAToolkit_BIN_DIR "${CUDAToolkit_ROOT_DIR}/bin")
  337. set(CUDAToolkit_NVCC_EXECUTABLE "${CUDAToolkit_BIN_DIR}/nvcc${CMAKE_EXECUTABLE_SUFFIX}")
  338. else()
  339. # For NVCC we can easily deduce the SDK binary directory from the compiler path.
  340. if(CMAKE_CUDA_COMPILER_LOADED AND NOT CUDAToolkit_BIN_DIR AND CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
  341. get_filename_component(CUDAToolkit_BIN_DIR "${CMAKE_CUDA_COMPILER}" DIRECTORY)
  342. set(CUDAToolkit_BIN_DIR "${CUDAToolkit_BIN_DIR}" CACHE PATH "")
  343. mark_as_advanced(CUDAToolkit_BIN_DIR)
  344. endif()
  345. # Try language- or user-provided path first.
  346. if(CUDAToolkit_BIN_DIR)
  347. find_program(CUDAToolkit_NVCC_EXECUTABLE
  348. NAMES nvcc nvcc.exe
  349. PATHS ${CUDAToolkit_BIN_DIR}
  350. NO_DEFAULT_PATH
  351. )
  352. endif()
  353. # Search using CUDAToolkit_ROOT
  354. find_program(CUDAToolkit_NVCC_EXECUTABLE
  355. NAMES nvcc nvcc.exe
  356. PATHS ENV CUDA_PATH
  357. PATH_SUFFIXES bin
  358. )
  359. # If the user specified CUDAToolkit_ROOT but nvcc could not be found, this is an error.
  360. if(NOT CUDAToolkit_NVCC_EXECUTABLE AND (DEFINED CUDAToolkit_ROOT OR DEFINED ENV{CUDAToolkit_ROOT}))
  361. # Declare error messages now, print later depending on find_package args.
  362. set(fail_base "Could not find nvcc executable in path specified by")
  363. set(cuda_root_fail "${fail_base} CUDAToolkit_ROOT=${CUDAToolkit_ROOT}")
  364. set(env_cuda_root_fail "${fail_base} environment variable CUDAToolkit_ROOT=$ENV{CUDAToolkit_ROOT}")
  365. if(CUDAToolkit_FIND_REQUIRED)
  366. if(DEFINED CUDAToolkit_ROOT)
  367. message(FATAL_ERROR ${cuda_root_fail})
  368. elseif(DEFINED ENV{CUDAToolkit_ROOT})
  369. message(FATAL_ERROR ${env_cuda_root_fail})
  370. endif()
  371. else()
  372. if(NOT CUDAToolkit_FIND_QUIETLY)
  373. if(DEFINED CUDAToolkit_ROOT)
  374. message(STATUS ${cuda_root_fail})
  375. elseif(DEFINED ENV{CUDAToolkit_ROOT})
  376. message(STATUS ${env_cuda_root_fail})
  377. endif()
  378. endif()
  379. set(CUDAToolkit_FOUND FALSE)
  380. unset(fail_base)
  381. unset(cuda_root_fail)
  382. unset(env_cuda_root_fail)
  383. return()
  384. endif()
  385. endif()
  386. # CUDAToolkit_ROOT cmake / env variable not specified, try platform defaults.
  387. #
  388. # - Linux: /usr/local/cuda-X.Y
  389. # - macOS: /Developer/NVIDIA/CUDA-X.Y
  390. # - Windows: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vX.Y
  391. #
  392. # We will also search the default symlink location /usr/local/cuda first since
  393. # if CUDAToolkit_ROOT is not specified, it is assumed that the symlinked
  394. # directory is the desired location.
  395. if(NOT CUDAToolkit_NVCC_EXECUTABLE)
  396. if(UNIX)
  397. if(NOT APPLE)
  398. set(platform_base "/usr/local/cuda-")
  399. else()
  400. set(platform_base "/Developer/NVIDIA/CUDA-")
  401. endif()
  402. else()
  403. set(platform_base "C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v")
  404. endif()
  405. # Build out a descending list of possible cuda installations, e.g.
  406. file(GLOB possible_paths "${platform_base}*")
  407. # Iterate the glob results and create a descending list.
  408. set(versions)
  409. foreach(p ${possible_paths})
  410. # Extract version number from end of string
  411. string(REGEX MATCH "[0-9][0-9]?\\.[0-9]$" p_version ${p})
  412. if(IS_DIRECTORY ${p} AND p_version)
  413. list(APPEND versions ${p_version})
  414. endif()
  415. endforeach()
  416. # Sort numerically in descending order, so we try the newest versions first.
  417. list(SORT versions COMPARE NATURAL ORDER DESCENDING)
  418. # With a descending list of versions, populate possible paths to search.
  419. set(search_paths)
  420. foreach(v ${versions})
  421. list(APPEND search_paths "${platform_base}${v}")
  422. endforeach()
  423. # Force the global default /usr/local/cuda to the front on Unix.
  424. if(UNIX)
  425. list(INSERT search_paths 0 "/usr/local/cuda")
  426. endif()
  427. # Now search for nvcc again using the platform default search paths.
  428. find_program(CUDAToolkit_NVCC_EXECUTABLE
  429. NAMES nvcc nvcc.exe
  430. PATHS ${search_paths}
  431. PATH_SUFFIXES bin
  432. )
  433. # We are done with these variables now, cleanup for caller.
  434. unset(platform_base)
  435. unset(possible_paths)
  436. unset(versions)
  437. unset(search_paths)
  438. if(NOT CUDAToolkit_NVCC_EXECUTABLE)
  439. if(CUDAToolkit_FIND_REQUIRED)
  440. message(FATAL_ERROR "Could not find nvcc, please set CUDAToolkit_ROOT.")
  441. elseif(NOT CUDAToolkit_FIND_QUIETLY)
  442. message(STATUS "Could not find nvcc, please set CUDAToolkit_ROOT.")
  443. endif()
  444. set(CUDAToolkit_FOUND FALSE)
  445. return()
  446. endif()
  447. endif()
  448. if(NOT CUDAToolkit_BIN_DIR AND CUDAToolkit_NVCC_EXECUTABLE)
  449. get_filename_component(CUDAToolkit_BIN_DIR "${CUDAToolkit_NVCC_EXECUTABLE}" DIRECTORY)
  450. set(CUDAToolkit_BIN_DIR "${CUDAToolkit_BIN_DIR}" CACHE PATH "" FORCE)
  451. mark_as_advanced(CUDAToolkit_BIN_DIR)
  452. endif()
  453. get_filename_component(CUDAToolkit_ROOT_DIR ${CUDAToolkit_BIN_DIR} DIRECTORY ABSOLUTE)
  454. # CUDAToolkit_LIBRARY_ROOT contains the device library and version file.
  455. # In a non-scattered installation this is equivalent to CUDAToolkit_ROOT_DIR.
  456. # We first check for a non-scattered installation to prefer it over a scattered installation.
  457. if(EXISTS "${CUDAToolkit_ROOT_DIR}/version.txt")
  458. set(CUDAToolkit_LIBRARY_ROOT "${CUDAToolkit_ROOT_DIR}")
  459. elseif(CMAKE_SYSROOT_LINK AND EXISTS "${CMAKE_SYSROOT_LINK}/usr/lib/cuda/version.txt")
  460. set(CUDAToolkit_LIBRARY_ROOT "${CMAKE_SYSROOT_LINK}/usr/lib/cuda")
  461. elseif(EXISTS "${CMAKE_SYSROOT}/usr/lib/cuda/version.txt")
  462. set(CUDAToolkit_LIBRARY_ROOT "${CMAKE_SYSROOT}/usr/lib/cuda")
  463. endif()
  464. endif()
  465. # Handle cross compilation
  466. if(CMAKE_CROSSCOMPILING)
  467. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "armv7-a")
  468. # Support for NVPACK
  469. set(CUDAToolkit_TARGET_NAME "armv7-linux-androideabi")
  470. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm")
  471. # Support for arm cross compilation
  472. set(CUDAToolkit_TARGET_NAME "armv7-linux-gnueabihf")
  473. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
  474. # Support for aarch64 cross compilation
  475. if(ANDROID_ARCH_NAME STREQUAL "arm64")
  476. set(CUDAToolkit_TARGET_NAME "aarch64-linux-androideabi")
  477. else()
  478. set(CUDAToolkit_TARGET_NAME "aarch64-linux")
  479. endif(ANDROID_ARCH_NAME STREQUAL "arm64")
  480. elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
  481. set(CUDAToolkit_TARGET_NAME "x86_64-linux")
  482. endif()
  483. if(EXISTS "${CUDAToolkit_ROOT_DIR}/targets/${CUDAToolkit_TARGET_NAME}")
  484. set(CUDAToolkit_TARGET_DIR "${CUDAToolkit_ROOT_DIR}/targets/${CUDAToolkit_TARGET_NAME}")
  485. # add known CUDA target root path to the set of directories we search for programs, libraries and headers
  486. list(PREPEND CMAKE_FIND_ROOT_PATH "${CUDAToolkit_TARGET_DIR}")
  487. # Mark that we need to pop the root search path changes after we have
  488. # found all cuda libraries so that searches for our cross-compilation
  489. # libraries work when another cuda sdk is in CMAKE_PREFIX_PATH or
  490. # PATh
  491. set(_CUDAToolkit_Pop_ROOT_PATH True)
  492. endif()
  493. else()
  494. # Not cross compiling
  495. set(CUDAToolkit_TARGET_DIR "${CUDAToolkit_ROOT_DIR}")
  496. # Now that we have the real ROOT_DIR, find components inside it.
  497. list(APPEND CMAKE_PREFIX_PATH ${CUDAToolkit_ROOT_DIR})
  498. # Mark that we need to pop the prefix path changes after we have
  499. # found the cudart library.
  500. set(_CUDAToolkit_Pop_Prefix True)
  501. endif()
  502. # CUDAToolkit_TARGET_DIR always points to the directory containing the include directory.
  503. # On a scattered installation /usr, on a non-scattered something like /usr/local/cuda or /usr/local/cuda-10.2/targets/aarch64-linux.
  504. if(EXISTS "${CUDAToolkit_TARGET_DIR}/include/cuda_runtime.h")
  505. set(CUDAToolkit_INCLUDE_DIR "${CUDAToolkit_TARGET_DIR}/include")
  506. elseif(NOT CUDAToolkit_FIND_QUIETLY)
  507. message(STATUS "Unable to find cuda_runtime.h in \"${CUDAToolkit_TARGET_DIR}/include\" for CUDAToolkit_INCLUDE_DIR.")
  508. endif()
  509. if(CUDAToolkit_NVCC_EXECUTABLE AND
  510. CUDAToolkit_NVCC_EXECUTABLE STREQUAL CMAKE_CUDA_COMPILER)
  511. # Need to set these based off the already computed CMAKE_CUDA_COMPILER_VERSION value
  512. # This if statement will always match, but is used to provide variables for MATCH 1,2,3...
  513. if(CMAKE_CUDA_COMPILER_VERSION MATCHES [=[([0-9]+)\.([0-9]+)\.([0-9]+)]=])
  514. set(CUDAToolkit_VERSION_MAJOR "${CMAKE_MATCH_1}")
  515. set(CUDAToolkit_VERSION_MINOR "${CMAKE_MATCH_2}")
  516. set(CUDAToolkit_VERSION_PATCH "${CMAKE_MATCH_3}")
  517. set(CUDAToolkit_VERSION "${CMAKE_CUDA_COMPILER_VERSION}")
  518. endif()
  519. else()
  520. # Compute the version by invoking nvcc
  521. execute_process(COMMAND ${CUDAToolkit_NVCC_EXECUTABLE} "--version" OUTPUT_VARIABLE NVCC_OUT)
  522. if(NVCC_OUT MATCHES [=[ V([0-9]+)\.([0-9]+)\.([0-9]+)]=])
  523. set(CUDAToolkit_VERSION_MAJOR "${CMAKE_MATCH_1}")
  524. set(CUDAToolkit_VERSION_MINOR "${CMAKE_MATCH_2}")
  525. set(CUDAToolkit_VERSION_PATCH "${CMAKE_MATCH_3}")
  526. set(CUDAToolkit_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}")
  527. endif()
  528. unset(NVCC_OUT)
  529. endif()
  530. # Find the CUDA Runtime Library libcudart
  531. find_library(CUDA_CUDART
  532. NAMES cudart
  533. PATH_SUFFIXES lib64 lib/x64
  534. )
  535. find_library(CUDA_CUDART
  536. NAMES cudart
  537. PATH_SUFFIXES lib64/stubs lib/x64/stubs
  538. )
  539. if(NOT CUDA_CUDART AND NOT CUDAToolkit_FIND_QUIETLY)
  540. message(STATUS "Unable to find cudart library.")
  541. endif()
  542. unset(CUDAToolkit_ROOT_DIR)
  543. if(_CUDAToolkit_Pop_Prefix)
  544. list(REMOVE_AT CMAKE_PREFIX_PATH -1)
  545. unset(_CUDAToolkit_Pop_Prefix)
  546. endif()
  547. #-----------------------------------------------------------------------------
  548. # Perform version comparison and validate all required variables are set.
  549. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  550. find_package_handle_standard_args(CUDAToolkit
  551. REQUIRED_VARS
  552. CUDAToolkit_INCLUDE_DIR
  553. CUDA_CUDART
  554. CUDAToolkit_NVCC_EXECUTABLE
  555. VERSION_VAR
  556. CUDAToolkit_VERSION
  557. )
  558. mark_as_advanced(CUDA_CUDART
  559. CUDAToolkit_INCLUDE_DIR
  560. CUDAToolkit_NVCC_EXECUTABLE
  561. )
  562. #-----------------------------------------------------------------------------
  563. # Construct result variables
  564. if(CUDAToolkit_FOUND)
  565. set(CUDAToolkit_INCLUDE_DIRS ${CUDAToolkit_INCLUDE_DIR})
  566. get_filename_component(CUDAToolkit_LIBRARY_DIR ${CUDA_CUDART} DIRECTORY ABSOLUTE)
  567. endif()
  568. #-----------------------------------------------------------------------------
  569. # Construct import targets
  570. if(CUDAToolkit_FOUND)
  571. function(_CUDAToolkit_find_and_add_import_lib lib_name)
  572. cmake_parse_arguments(arg "" "" "ALT;DEPS;EXTRA_PATH_SUFFIXES" ${ARGN})
  573. set(search_names ${lib_name} ${arg_ALT})
  574. find_library(CUDA_${lib_name}_LIBRARY
  575. NAMES ${search_names}
  576. HINTS ${CUDAToolkit_LIBRARY_DIR}
  577. ENV CUDA_PATH
  578. PATH_SUFFIXES nvidia/current lib64 lib/x64 lib
  579. ${arg_EXTRA_PATH_SUFFIXES}
  580. )
  581. # Don't try any stub directories until we have exhausted all other
  582. # search locations.
  583. find_library(CUDA_${lib_name}_LIBRARY
  584. NAMES ${search_names}
  585. HINTS ${CUDAToolkit_LIBRARY_DIR}
  586. ENV CUDA_PATH
  587. PATH_SUFFIXES lib64/stubs lib/x64/stubs lib/stubs stubs
  588. )
  589. mark_as_advanced(CUDA_${lib_name}_LIBRARY)
  590. if (NOT TARGET CUDA::${lib_name} AND CUDA_${lib_name}_LIBRARY)
  591. add_library(CUDA::${lib_name} IMPORTED INTERFACE)
  592. target_include_directories(CUDA::${lib_name} SYSTEM INTERFACE "${CUDAToolkit_INCLUDE_DIRS}")
  593. target_link_libraries(CUDA::${lib_name} INTERFACE "${CUDA_${lib_name}_LIBRARY}")
  594. foreach(dep ${arg_DEPS})
  595. if(TARGET CUDA::${dep})
  596. target_link_libraries(CUDA::${lib_name} INTERFACE CUDA::${dep})
  597. endif()
  598. endforeach()
  599. endif()
  600. endfunction()
  601. if(NOT TARGET CUDA::toolkit)
  602. add_library(CUDA::toolkit IMPORTED INTERFACE)
  603. target_include_directories(CUDA::toolkit SYSTEM INTERFACE "${CUDAToolkit_INCLUDE_DIRS}")
  604. target_link_directories(CUDA::toolkit INTERFACE "${CUDAToolkit_LIBRARY_DIR}")
  605. endif()
  606. _CUDAToolkit_find_and_add_import_lib(cuda_driver ALT cuda)
  607. _CUDAToolkit_find_and_add_import_lib(cudart)
  608. _CUDAToolkit_find_and_add_import_lib(cudart_static)
  609. # setup dependencies that are required for cudart_static when building
  610. # on linux. These are generally only required when using the CUDA toolkit
  611. # when CUDA language is disabled
  612. if(NOT TARGET CUDA::cudart_static_deps
  613. AND TARGET CUDA::cudart_static)
  614. add_library(CUDA::cudart_static_deps IMPORTED INTERFACE)
  615. target_link_libraries(CUDA::cudart_static INTERFACE CUDA::cudart_static_deps)
  616. if(UNIX AND (CMAKE_C_COMPILER OR CMAKE_CXX_COMPILER))
  617. find_package(Threads REQUIRED)
  618. target_link_libraries(CUDA::cudart_static_deps INTERFACE Threads::Threads ${CMAKE_DL_LIBS})
  619. endif()
  620. if(UNIX AND NOT APPLE)
  621. # On Linux, you must link against librt when using the static cuda runtime.
  622. find_library(CUDAToolkit_rt_LIBRARY rt)
  623. mark_as_advanced(CUDAToolkit_rt_LIBRARY)
  624. if(NOT CUDAToolkit_rt_LIBRARY)
  625. message(WARNING "Could not find librt library, needed by CUDA::cudart_static")
  626. else()
  627. target_link_libraries(CUDA::cudart_static_deps INTERFACE ${CUDAToolkit_rt_LIBRARY})
  628. endif()
  629. endif()
  630. endif()
  631. _CUDAToolkit_find_and_add_import_lib(culibos) # it's a static library
  632. foreach (cuda_lib cublasLt cublas cufft curand cusparse nppc nvjpeg)
  633. _CUDAToolkit_find_and_add_import_lib(${cuda_lib})
  634. _CUDAToolkit_find_and_add_import_lib(${cuda_lib}_static DEPS culibos)
  635. endforeach()
  636. # cuFFTW depends on cuFFT
  637. _CUDAToolkit_find_and_add_import_lib(cufftw DEPS cufft)
  638. _CUDAToolkit_find_and_add_import_lib(cufftw DEPS cufft_static)
  639. # cuSOLVER depends on cuBLAS, and cuSPARSE
  640. _CUDAToolkit_find_and_add_import_lib(cusolver DEPS cublas cusparse)
  641. _CUDAToolkit_find_and_add_import_lib(cusolver_static DEPS cublas_static cusparse_static culibos)
  642. # nvGRAPH depends on cuRAND, and cuSOLVER.
  643. _CUDAToolkit_find_and_add_import_lib(nvgraph DEPS curand cusolver)
  644. _CUDAToolkit_find_and_add_import_lib(nvgraph_static DEPS curand_static cusolver_static)
  645. # Process the majority of the NPP libraries.
  646. foreach (cuda_lib nppial nppicc nppidei nppif nppig nppim nppist nppitc npps nppicom nppisu)
  647. _CUDAToolkit_find_and_add_import_lib(${cuda_lib} DEPS nppc)
  648. _CUDAToolkit_find_and_add_import_lib(${cuda_lib}_static DEPS nppc_static)
  649. endforeach()
  650. _CUDAToolkit_find_and_add_import_lib(cupti
  651. EXTRA_PATH_SUFFIXES ../extras/CUPTI/lib64/
  652. ../extras/CUPTI/lib/)
  653. _CUDAToolkit_find_and_add_import_lib(cupti_static
  654. EXTRA_PATH_SUFFIXES ../extras/CUPTI/lib64/
  655. ../extras/CUPTI/lib/)
  656. _CUDAToolkit_find_and_add_import_lib(nvrtc DEPS cuda_driver)
  657. _CUDAToolkit_find_and_add_import_lib(nvml ALT nvidia-ml nvml)
  658. if(WIN32)
  659. # nvtools can be installed outside the CUDA toolkit directory
  660. # so prefer the NVTOOLSEXT_PATH windows only environment variable
  661. # In addition on windows the most common name is nvToolsExt64_1
  662. find_library(CUDA_nvToolsExt_LIBRARY
  663. NAMES nvToolsExt64_1 nvToolsExt64 nvToolsExt
  664. PATHS ENV NVTOOLSEXT_PATH
  665. ENV CUDA_PATH
  666. PATH_SUFFIXES lib/x64 lib
  667. )
  668. endif()
  669. _CUDAToolkit_find_and_add_import_lib(nvToolsExt ALT nvToolsExt64)
  670. _CUDAToolkit_find_and_add_import_lib(OpenCL)
  671. endif()
  672. if(_CUDAToolkit_Pop_ROOT_PATH)
  673. list(REMOVE_AT CMAKE_FIND_ROOT_PATH 0)
  674. unset(_CUDAToolkit_Pop_ROOT_PATH)
  675. endif()