FindCUDA.cmake 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191
  1. #[=======================================================================[.rst:
  2. FindCUDA
  3. --------
  4. .. versionchanged:: 3.27
  5. This module is available only if policy :policy:`CMP0146` is not set to ``NEW``.
  6. Port projects to CMake's first-class ``CUDA`` language support.
  7. .. deprecated:: 3.10
  8. Do not use this module in new code.
  9. It is no longer necessary to use this module or call ``find_package(CUDA)``
  10. for compiling CUDA code. Instead, list ``CUDA`` among the languages named
  11. in the top-level call to the :command:`project` command, or call the
  12. :command:`enable_language` command with ``CUDA``.
  13. Then one can add CUDA (``.cu``) sources directly to targets similar to other
  14. languages.
  15. .. versionadded:: 3.17
  16. To find and use the CUDA toolkit libraries manually, use the
  17. :module:`FindCUDAToolkit` module instead. It works regardless of the
  18. ``CUDA`` language being enabled.
  19. Documentation of Deprecated Usage
  20. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  21. Tools for building CUDA C files: libraries and build dependencies.
  22. This script locates the NVIDIA CUDA C tools. It should work on Linux,
  23. Windows, and macOS and should be reasonably up to date with CUDA C
  24. releases.
  25. .. versionadded:: 3.19
  26. QNX support.
  27. This script makes use of the standard :command:`find_package` arguments of
  28. ``<VERSION>``, ``REQUIRED`` and ``QUIET``. ``CUDA_FOUND`` will report if an
  29. acceptable version of CUDA was found.
  30. The script will prompt the user to specify ``CUDA_TOOLKIT_ROOT_DIR`` if
  31. the prefix cannot be determined by the location of nvcc in the system
  32. path and ``REQUIRED`` is specified to :command:`find_package`. To use
  33. a different installed version of the toolkit set the environment variable
  34. ``CUDA_BIN_PATH`` before running cmake (e.g.
  35. ``CUDA_BIN_PATH=/usr/local/cuda1.0`` instead of the default
  36. ``/usr/local/cuda``) or set ``CUDA_TOOLKIT_ROOT_DIR`` after configuring. If
  37. you change the value of ``CUDA_TOOLKIT_ROOT_DIR``, various components that
  38. depend on the path will be relocated.
  39. It might be necessary to set ``CUDA_TOOLKIT_ROOT_DIR`` manually on certain
  40. platforms, or to use a CUDA runtime not installed in the default
  41. location. In newer versions of the toolkit the CUDA library is
  42. included with the graphics driver -- be sure that the driver version
  43. matches what is needed by the CUDA runtime version.
  44. Input Variables
  45. """""""""""""""
  46. The following variables affect the behavior of the macros in the
  47. script (in alphabetical order). Note that any of these flags can be
  48. changed multiple times in the same directory before calling
  49. ``cuda_add_executable()``, ``cuda_add_library()``, ``cuda_compile()``,
  50. ``cuda_compile_ptx()``, ``cuda_compile_fatbin()``, ``cuda_compile_cubin()``
  51. or ``cuda_wrap_srcs()``:
  52. ``CUDA_64_BIT_DEVICE_CODE`` (Default: host bit size)
  53. Set to ``ON`` to compile for 64 bit device code, OFF for 32 bit device code.
  54. Note that making this different from the host code when generating object
  55. or C files from CUDA code just won't work, because size_t gets defined by
  56. nvcc in the generated source. If you compile to PTX and then load the
  57. file yourself, you can mix bit sizes between device and host.
  58. ``CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE`` (Default: ``ON``)
  59. Set to ``ON`` if you want the custom build rule to be attached to the source
  60. file in Visual Studio. Turn OFF if you add the same cuda file to multiple
  61. targets.
  62. This allows the user to build the target from the CUDA file; however, bad
  63. things can happen if the CUDA source file is added to multiple targets.
  64. When performing parallel builds it is possible for the custom build
  65. command to be run more than once and in parallel causing cryptic build
  66. errors. VS runs the rules for every source file in the target, and a
  67. source can have only one rule no matter how many projects it is added to.
  68. When the rule is run from multiple targets race conditions can occur on
  69. the generated file. Eventually everything will get built, but if the user
  70. is unaware of this behavior, there may be confusion. It would be nice if
  71. this script could detect the reuse of source files across multiple targets
  72. and turn the option off for the user, but no good solution could be found.
  73. ``CUDA_BUILD_CUBIN`` (Default: ``OFF``)
  74. Set to ``ON`` to enable and extra compilation pass with the ``-cubin`` option in
  75. Device mode. The output is parsed and register, shared memory usage is
  76. printed during build.
  77. ``CUDA_BUILD_EMULATION`` (Default: ``OFF`` for device mode)
  78. Set to ``ON`` for Emulation mode. ``-D_DEVICEEMU`` is defined for CUDA C files
  79. when ``CUDA_BUILD_EMULATION`` is ``TRUE``.
  80. ``CUDA_LINK_LIBRARIES_KEYWORD`` (Default: ``""``)
  81. .. versionadded:: 3.9
  82. The ``<PRIVATE|PUBLIC|INTERFACE>`` keyword to use for internal
  83. :command:`target_link_libraries` calls. The default is to use no keyword which
  84. uses the old "plain" form of :command:`target_link_libraries`. Note that is matters
  85. because whatever is used inside the ``FindCUDA`` module must also be used
  86. outside - the two forms of :command:`target_link_libraries` cannot be mixed.
  87. ``CUDA_GENERATED_OUTPUT_DIR`` (Default: :variable:`CMAKE_CURRENT_BINARY_DIR`)
  88. Set to the path you wish to have the generated files placed. If it is
  89. blank output files will be placed in :variable:`CMAKE_CURRENT_BINARY_DIR`.
  90. Intermediate files will always be placed in
  91. ``CMAKE_CURRENT_BINARY_DIR/CMakeFiles``.
  92. ``CUDA_HOST_COMPILATION_CPP`` (Default: ``ON``)
  93. Set to ``OFF`` for C compilation of host code.
  94. ``CUDA_HOST_COMPILER`` (Default: ``CMAKE_C_COMPILER``)
  95. Set the host compiler to be used by nvcc. Ignored if ``-ccbin`` or
  96. ``--compiler-bindir`` is already present in the ``CUDA_NVCC_FLAGS`` or
  97. ``CUDA_NVCC_FLAGS_<CONFIG>`` variables. For Visual Studio targets,
  98. the host compiler is constructed with one or more visual studio macros
  99. such as ``$(VCInstallDir)``, that expands out to the path when
  100. the command is run from within VS.
  101. .. versionadded:: 3.13
  102. If the :envvar:`CUDAHOSTCXX` environment variable is set it will
  103. be used as the default.
  104. ``CUDA_NVCC_FLAGS``, ``CUDA_NVCC_FLAGS_<CONFIG>``
  105. Additional NVCC command line arguments. NOTE: multiple arguments must be
  106. semi-colon delimited (e.g. ``--compiler-options;-Wall``)
  107. .. versionadded:: 3.6
  108. Contents of these variables may use
  109. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  110. ``CUDA_PROPAGATE_HOST_FLAGS`` (Default: ``ON``)
  111. Set to ``ON`` to propagate :variable:`CMAKE_{C,CXX}_FLAGS <CMAKE_<LANG>_FLAGS>` and their configuration
  112. dependent counterparts (e.g. ``CMAKE_C_FLAGS_DEBUG``) automatically to the
  113. host compiler through nvcc's ``-Xcompiler`` flag. This helps make the
  114. generated host code match the rest of the system better. Sometimes
  115. certain flags give nvcc problems, and this will help you turn the flag
  116. propagation off. This does not affect the flags supplied directly to nvcc
  117. via ``CUDA_NVCC_FLAGS`` or through the ``OPTION`` flags specified through
  118. ``cuda_add_library()``, ``cuda_add_executable()``, or ``cuda_wrap_srcs()``. Flags used for
  119. shared library compilation are not affected by this flag.
  120. ``CUDA_SEPARABLE_COMPILATION`` (Default: ``OFF``)
  121. If set this will enable separable compilation for all CUDA runtime object
  122. files. If used outside of ``cuda_add_executable()`` and ``cuda_add_library()``
  123. (e.g. calling ``cuda_wrap_srcs()`` directly),
  124. ``cuda_compute_separable_compilation_object_file_name()`` and
  125. ``cuda_link_separable_compilation_objects()`` should be called.
  126. ``CUDA_SOURCE_PROPERTY_FORMAT``
  127. .. versionadded:: 3.3
  128. If this source file property is set, it can override the format specified
  129. to ``cuda_wrap_srcs()`` (``OBJ``, ``PTX``, ``CUBIN``, or ``FATBIN``). If an input source file
  130. is not a ``.cu`` file, setting this file will cause it to be treated as a ``.cu``
  131. file. See documentation for set_source_files_properties on how to set
  132. this property.
  133. ``CUDA_USE_STATIC_CUDA_RUNTIME`` (Default: ``ON``)
  134. .. versionadded:: 3.3
  135. When enabled the static version of the CUDA runtime library will be used
  136. in ``CUDA_LIBRARIES``. If the version of CUDA configured doesn't support
  137. this option, then it will be silently disabled.
  138. ``CUDA_VERBOSE_BUILD`` (Default: ``OFF``)
  139. Set to ``ON`` to see all the commands used when building the CUDA file. When
  140. using a Makefile generator the value defaults to ``VERBOSE`` (run
  141. ``make VERBOSE=1`` to see output), although setting ``CUDA_VERBOSE_BUILD`` to ``ON`` will
  142. always print the output.
  143. Commands
  144. """"""""
  145. The script creates the following functions and macros (in alphabetical order):
  146. .. code-block:: cmake
  147. cuda_add_cufft_to_target(<cuda_target>)
  148. Adds the cufft library to the target (can be any target). Handles whether
  149. you are in emulation mode or not.
  150. .. code-block:: cmake
  151. cuda_add_cublas_to_target(<cuda_target>)
  152. Adds the cublas library to the target (can be any target). Handles
  153. whether you are in emulation mode or not.
  154. .. code-block:: cmake
  155. cuda_add_executable(<cuda_target> <file>...
  156. [WIN32] [MACOSX_BUNDLE] [EXCLUDE_FROM_ALL] [OPTIONS ...])
  157. Creates an executable ``<cuda_target>`` which is made up of the files
  158. specified. All of the non CUDA C files are compiled using the standard
  159. build rules specified by CMake and the CUDA files are compiled to object
  160. files using nvcc and the host compiler. In addition ``CUDA_INCLUDE_DIRS`` is
  161. added automatically to :command:`include_directories`. Some standard CMake target
  162. calls can be used on the target after calling this macro
  163. (e.g. :command:`set_target_properties` and :command:`target_link_libraries`), but setting
  164. properties that adjust compilation flags will not affect code compiled by
  165. nvcc. Such flags should be modified before calling ``cuda_add_executable()``,
  166. ``cuda_add_library()`` or ``cuda_wrap_srcs()``.
  167. .. code-block:: cmake
  168. cuda_add_library(<cuda_target> <file>...
  169. [STATIC | SHARED | MODULE] [EXCLUDE_FROM_ALL] [OPTIONS ...])
  170. Same as ``cuda_add_executable()`` except that a library is created.
  171. .. code-block:: cmake
  172. cuda_build_clean_target()
  173. Creates a convenience target that deletes all the dependency files
  174. generated. You should make clean after running this target to ensure the
  175. dependency files get regenerated.
  176. .. code-block:: cmake
  177. cuda_compile(<generated_files> <file>... [STATIC | SHARED | MODULE]
  178. [OPTIONS ...])
  179. Returns a list of generated files from the input source files to be used
  180. with :command:`add_library` or :command:`add_executable`.
  181. .. code-block:: cmake
  182. cuda_compile_ptx(<generated_files> <file>... [OPTIONS ...])
  183. Returns a list of ``PTX`` files generated from the input source files.
  184. .. code-block:: cmake
  185. cuda_compile_fatbin(<generated_files> <file>... [OPTIONS ...])
  186. .. versionadded:: 3.1
  187. Returns a list of ``FATBIN`` files generated from the input source files.
  188. .. code-block:: cmake
  189. cuda_compile_cubin(<generated_files> <file>... [OPTIONS ...])
  190. .. versionadded:: 3.1
  191. Returns a list of ``CUBIN`` files generated from the input source files.
  192. .. code-block:: cmake
  193. cuda_compute_separable_compilation_object_file_name(<output_file_var>
  194. <cuda_target>
  195. <object_files>)
  196. Compute the name of the intermediate link file used for separable
  197. compilation. This file name is typically passed into
  198. ``CUDA_LINK_SEPARABLE_COMPILATION_OBJECTS``. output_file_var is produced
  199. based on cuda_target the list of objects files that need separable
  200. compilation as specified by ``<object_files>``. If the ``<object_files>`` list is
  201. empty, then ``<output_file_var>`` will be empty. This function is called
  202. automatically for ``cuda_add_library()`` and ``cuda_add_executable()``. Note that
  203. this is a function and not a macro.
  204. .. code-block:: cmake
  205. cuda_include_directories(path0 path1 ...)
  206. Sets the directories that should be passed to nvcc
  207. (e.g. ``nvcc -Ipath0 -Ipath1 ...``). These paths usually contain other ``.cu``
  208. files.
  209. .. code-block:: cmake
  210. cuda_link_separable_compilation_objects(<output_file_var> <cuda_target>
  211. <nvcc_flags> <object_files>)
  212. Generates the link object required by separable compilation from the given
  213. object files. This is called automatically for ``cuda_add_executable()`` and
  214. ``cuda_add_library()``, but can be called manually when using ``cuda_wrap_srcs()``
  215. directly. When called from ``cuda_add_library()`` or ``cuda_add_executable()`` the
  216. ``<nvcc_flags>`` passed in are the same as the flags passed in via the ``OPTIONS``
  217. argument. The only nvcc flag added automatically is the bitness flag as
  218. specified by ``CUDA_64_BIT_DEVICE_CODE``. Note that this is a function
  219. instead of a macro.
  220. .. code-block:: cmake
  221. cuda_select_nvcc_arch_flags(<out_variable> [<target_CUDA_architecture> ...])
  222. Selects GPU arch flags for nvcc based on ``target_CUDA_architecture``.
  223. Values for ``target_CUDA_architecture``:
  224. * ``Auto``: detects local machine GPU compute arch at runtime.
  225. * ``Common`` and ``All``: cover common and entire subsets of architectures.
  226. * ``<name>``: one of ``Fermi``, ``Kepler``, ``Maxwell``, ``Kepler+Tegra``, ``Kepler+Tesla``, ``Maxwell+Tegra``, ``Pascal``.
  227. * ``<ver>``, ``<ver>(<ver>)``, ``<ver>+PTX``, where ``<ver>`` is one of
  228. ``2.0``, ``2.1``, ``3.0``, ``3.2``, ``3.5``, ``3.7``, ``5.0``, ``5.2``, ``5.3``, ``6.0``, ``6.2``.
  229. Returns list of flags to be added to ``CUDA_NVCC_FLAGS`` in ``<out_variable>``.
  230. Additionally, sets ``<out_variable>_readable`` to the resulting numeric list.
  231. Example::
  232. cuda_select_nvcc_arch_flags(ARCH_FLAGS 3.0 3.5+PTX 5.2(5.0) Maxwell)
  233. list(APPEND CUDA_NVCC_FLAGS ${ARCH_FLAGS})
  234. More info on CUDA architectures: https://en.wikipedia.org/wiki/CUDA.
  235. Note that this is a function instead of a macro.
  236. .. code-block:: cmake
  237. cuda_wrap_srcs(<cuda_target> <format> <generated_files> <file>...
  238. [STATIC | SHARED | MODULE] [OPTIONS ...])
  239. This is where all the magic happens. ``cuda_add_executable()``,
  240. ``cuda_add_library()``, ``cuda_compile()``, and ``cuda_compile_ptx()`` all call this
  241. function under the hood.
  242. Given the list of files ``<file>...`` this macro generates
  243. custom commands that generate either PTX or linkable objects (use ``PTX`` or
  244. ``OBJ`` for the ``<format>`` argument to switch). Files that don't end with ``.cu``
  245. or have the ``HEADER_FILE_ONLY`` property are ignored.
  246. The arguments passed in after ``OPTIONS`` are extra command line options to
  247. give to nvcc. You can also specify per configuration options by
  248. specifying the name of the configuration followed by the options. General
  249. options must precede configuration specific options. Not all
  250. configurations need to be specified, only the ones provided will be used.
  251. For example:
  252. .. code-block:: cmake
  253. cuda_add_executable(...
  254. OPTIONS -DFLAG=2 "-DFLAG_OTHER=space in flag"
  255. DEBUG -g
  256. RELEASE --use_fast_math
  257. RELWITHDEBINFO --use_fast_math;-g
  258. MINSIZEREL --use_fast_math)
  259. For certain configurations (namely VS generating object files with
  260. ``CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE`` set to ``ON``), no generated file will
  261. be produced for the given cuda file. This is because when you add the
  262. cuda file to Visual Studio it knows that this file produces an object file
  263. and will link in the resulting object file automatically.
  264. This script will also generate a separate cmake script that is used at
  265. build time to invoke nvcc. This is for several reasons:
  266. * nvcc can return negative numbers as return values which confuses
  267. Visual Studio into thinking that the command succeeded. The script now
  268. checks the error codes and produces errors when there was a problem.
  269. * nvcc has been known to not delete incomplete results when it
  270. encounters problems. This confuses build systems into thinking the
  271. target was generated when in fact an unusable file exists. The script
  272. now deletes the output files if there was an error.
  273. * By putting all the options that affect the build into a file and then
  274. make the build rule dependent on the file, the output files will be
  275. regenerated when the options change.
  276. This script also looks at optional arguments ``STATIC``, ``SHARED``, or ``MODULE`` to
  277. determine when to target the object compilation for a shared library.
  278. :variable:`BUILD_SHARED_LIBS` is ignored in ``cuda_wrap_srcs()``, but it is respected in
  279. ``cuda_add_library()``. On some systems special flags are added for building
  280. objects intended for shared libraries. A preprocessor macro,
  281. ``<target_name>_EXPORTS`` is defined when a shared library compilation is
  282. detected.
  283. Flags passed into add_definitions with ``-D`` or ``/D`` are passed along to nvcc.
  284. Result Variables
  285. """"""""""""""""
  286. The script defines the following variables:
  287. ``CUDA_VERSION_MAJOR``
  288. The major version of cuda as reported by nvcc.
  289. ``CUDA_VERSION_MINOR``
  290. The minor version.
  291. ``CUDA_VERSION``, ``CUDA_VERSION_STRING``
  292. Full version in the ``X.Y`` format.
  293. ``CUDA_HAS_FP16``
  294. .. versionadded:: 3.6
  295. Whether a short float (``float16``, ``fp16``) is supported.
  296. ``CUDA_TOOLKIT_ROOT_DIR``
  297. Path to the CUDA Toolkit (defined if not set).
  298. ``CUDA_SDK_ROOT_DIR``
  299. Path to the CUDA SDK. Use this to find files in the SDK. This script will
  300. not directly support finding specific libraries or headers, as that isn't
  301. supported by NVIDIA. If you want to change libraries when the path changes
  302. see the ``FindCUDA.cmake`` script for an example of how to clear these
  303. variables. There are also examples of how to use the ``CUDA_SDK_ROOT_DIR``
  304. to locate headers or libraries, if you so choose (at your own risk).
  305. ``CUDA_INCLUDE_DIRS``
  306. Include directory for cuda headers. Added automatically
  307. for ``cuda_add_executable()`` and ``cuda_add_library()``.
  308. ``CUDA_LIBRARIES``
  309. Cuda RT library.
  310. ``CUDA_CUFFT_LIBRARIES``
  311. Device or emulation library for the Cuda FFT implementation (alternative to
  312. ``cuda_add_cufft_to_target()`` macro)
  313. ``CUDA_CUBLAS_LIBRARIES``
  314. Device or emulation library for the Cuda BLAS implementation (alternative to
  315. ``cuda_add_cublas_to_target()`` macro).
  316. ``CUDA_cudart_static_LIBRARY``
  317. Statically linkable cuda runtime library.
  318. Only available for CUDA version 5.5+.
  319. ``CUDA_cudadevrt_LIBRARY``
  320. .. versionadded:: 3.7
  321. Device runtime library. Required for separable compilation.
  322. ``CUDA_cupti_LIBRARY``
  323. CUDA Profiling Tools Interface library.
  324. Only available for CUDA version 4.0+.
  325. ``CUDA_curand_LIBRARY``
  326. CUDA Random Number Generation library.
  327. Only available for CUDA version 3.2+.
  328. ``CUDA_cusolver_LIBRARY``
  329. .. versionadded:: 3.2
  330. CUDA Direct Solver library.
  331. Only available for CUDA version 7.0+.
  332. ``CUDA_cusparse_LIBRARY``
  333. CUDA Sparse Matrix library.
  334. Only available for CUDA version 3.2+.
  335. ``CUDA_npp_LIBRARY``
  336. NVIDIA Performance Primitives lib.
  337. Only available for CUDA version 4.0+.
  338. ``CUDA_nppc_LIBRARY``
  339. NVIDIA Performance Primitives lib (core).
  340. Only available for CUDA version 5.5+.
  341. ``CUDA_nppi_LIBRARY``
  342. NVIDIA Performance Primitives lib (image processing).
  343. Only available for CUDA version 5.5 - 8.0.
  344. ``CUDA_nppial_LIBRARY``
  345. NVIDIA Performance Primitives lib (image processing).
  346. Only available for CUDA version 9.0.
  347. ``CUDA_nppicc_LIBRARY``
  348. NVIDIA Performance Primitives lib (image processing).
  349. Only available for CUDA version 9.0.
  350. ``CUDA_nppicom_LIBRARY``
  351. NVIDIA Performance Primitives lib (image processing).
  352. Only available for CUDA version 9.0 - 10.2.
  353. Replaced by nvjpeg.
  354. ``CUDA_nppidei_LIBRARY``
  355. NVIDIA Performance Primitives lib (image processing).
  356. Only available for CUDA version 9.0.
  357. ``CUDA_nppif_LIBRARY``
  358. NVIDIA Performance Primitives lib (image processing).
  359. Only available for CUDA version 9.0.
  360. ``CUDA_nppig_LIBRARY``
  361. NVIDIA Performance Primitives lib (image processing).
  362. Only available for CUDA version 9.0.
  363. ``CUDA_nppim_LIBRARY``
  364. NVIDIA Performance Primitives lib (image processing).
  365. Only available for CUDA version 9.0.
  366. ``CUDA_nppist_LIBRARY``
  367. NVIDIA Performance Primitives lib (image processing).
  368. Only available for CUDA version 9.0.
  369. ``CUDA_nppisu_LIBRARY``
  370. NVIDIA Performance Primitives lib (image processing).
  371. Only available for CUDA version 9.0.
  372. ``CUDA_nppitc_LIBRARY``
  373. NVIDIA Performance Primitives lib (image processing).
  374. Only available for CUDA version 9.0.
  375. ``CUDA_npps_LIBRARY``
  376. NVIDIA Performance Primitives lib (signal processing).
  377. Only available for CUDA version 5.5+.
  378. ``CUDA_nvcuvenc_LIBRARY``
  379. CUDA Video Encoder library.
  380. Only available for CUDA version 3.2+.
  381. Windows only.
  382. ``CUDA_nvcuvid_LIBRARY``
  383. CUDA Video Decoder library.
  384. Only available for CUDA version 3.2+.
  385. Windows only.
  386. ``CUDA_nvToolsExt_LIBRARY``
  387. .. versionadded:: 3.16
  388. NVIDA CUDA Tools Extension library.
  389. Available for CUDA version 5+.
  390. ``CUDA_OpenCL_LIBRARY``
  391. .. versionadded:: 3.16
  392. NVIDA CUDA OpenCL library.
  393. Available for CUDA version 5+.
  394. #]=======================================================================]
  395. # James Bigler, NVIDIA Corp (nvidia.com - jbigler)
  396. # Abe Stephens, SCI Institute -- http://www.sci.utah.edu/~abe/FindCuda.html
  397. #
  398. # Copyright (c) 2008 - 2009 NVIDIA Corporation. All rights reserved.
  399. #
  400. # Copyright (c) 2007-2009
  401. # Scientific Computing and Imaging Institute, University of Utah
  402. #
  403. # This code is licensed under the MIT License. See the FindCUDA.cmake script
  404. # for the text of the license.
  405. # The MIT License
  406. #
  407. # License for the specific language governing rights and limitations under
  408. # Permission is hereby granted, free of charge, to any person obtaining a
  409. # copy of this software and associated documentation files (the "Software"),
  410. # to deal in the Software without restriction, including without limitation
  411. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  412. # and/or sell copies of the Software, and to permit persons to whom the
  413. # Software is furnished to do so, subject to the following conditions:
  414. #
  415. # The above copyright notice and this permission notice shall be included
  416. # in all copies or substantial portions of the Software.
  417. #
  418. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  419. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  420. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  421. # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  422. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  423. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  424. # DEALINGS IN THE SOFTWARE.
  425. #
  426. ###############################################################################
  427. cmake_policy(GET CMP0146 _FindCUDA_CMP0146)
  428. if(_FindCUDA_CMP0146 STREQUAL "NEW")
  429. message(FATAL_ERROR "The FindCUDA module has been removed by policy CMP0146.")
  430. endif()
  431. if(_FindCUDA_testing)
  432. set(_FindCUDA_included TRUE)
  433. return()
  434. endif()
  435. # FindCUDA.cmake
  436. # This macro helps us find the location of helper files we will need the full path to
  437. macro(CUDA_FIND_HELPER_FILE _name _extension)
  438. set(_full_name "${_name}.${_extension}")
  439. # CMAKE_CURRENT_LIST_FILE contains the full path to the file currently being
  440. # processed. Using this variable, we can pull out the current path, and
  441. # provide a way to get access to the other files we need local to here.
  442. get_filename_component(CMAKE_CURRENT_LIST_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
  443. set(CUDA_${_name} "${CMAKE_CURRENT_LIST_DIR}/FindCUDA/${_full_name}")
  444. if(NOT EXISTS "${CUDA_${_name}}")
  445. set(error_message "${_full_name} not found in ${CMAKE_CURRENT_LIST_DIR}/FindCUDA")
  446. if(CUDA_FIND_REQUIRED)
  447. message(FATAL_ERROR "${error_message}")
  448. else()
  449. if(NOT CUDA_FIND_QUIETLY)
  450. message(STATUS "${error_message}")
  451. endif()
  452. endif()
  453. endif()
  454. # Set this variable as internal, so the user isn't bugged with it.
  455. set(CUDA_${_name} ${CUDA_${_name}} CACHE INTERNAL "Location of ${_full_name}" FORCE)
  456. endmacro()
  457. #####################################################################
  458. ## CUDA_INCLUDE_NVCC_DEPENDENCIES
  459. ##
  460. # So we want to try and include the dependency file if it exists. If
  461. # it doesn't exist then we need to create an empty one, so we can
  462. # include it.
  463. # If it does exist, then we need to check to see if all the files it
  464. # depends on exist. If they don't then we should clear the dependency
  465. # file and regenerate it later. This covers the case where a header
  466. # file has disappeared or moved.
  467. macro(CUDA_INCLUDE_NVCC_DEPENDENCIES dependency_file)
  468. set(CUDA_NVCC_DEPEND)
  469. set(CUDA_NVCC_DEPEND_REGENERATE FALSE)
  470. # Include the dependency file. Create it first if it doesn't exist . The
  471. # INCLUDE puts a dependency that will force CMake to rerun and bring in the
  472. # new info when it changes. DO NOT REMOVE THIS (as I did and spent a few
  473. # hours figuring out why it didn't work.
  474. if(NOT EXISTS ${dependency_file})
  475. file(WRITE ${dependency_file} "#FindCUDA.cmake generated file. Do not edit.\n")
  476. endif()
  477. # Always include this file to force CMake to run again next
  478. # invocation and rebuild the dependencies.
  479. #message("including dependency_file = ${dependency_file}")
  480. include(${dependency_file})
  481. # Now we need to verify the existence of all the included files
  482. # here. If they aren't there we need to just blank this variable and
  483. # make the file regenerate again.
  484. # if(DEFINED CUDA_NVCC_DEPEND)
  485. # message("CUDA_NVCC_DEPEND set")
  486. # else()
  487. # message("CUDA_NVCC_DEPEND NOT set")
  488. # endif()
  489. if(CUDA_NVCC_DEPEND)
  490. #message("CUDA_NVCC_DEPEND found")
  491. foreach(f ${CUDA_NVCC_DEPEND})
  492. # message("searching for ${f}")
  493. if(NOT EXISTS ${f})
  494. #message("file ${f} not found")
  495. set(CUDA_NVCC_DEPEND_REGENERATE TRUE)
  496. endif()
  497. endforeach()
  498. else()
  499. #message("CUDA_NVCC_DEPEND false")
  500. # No dependencies, so regenerate the file.
  501. set(CUDA_NVCC_DEPEND_REGENERATE TRUE)
  502. endif()
  503. #message("CUDA_NVCC_DEPEND_REGENERATE = ${CUDA_NVCC_DEPEND_REGENERATE}")
  504. # No incoming dependencies, so we need to generate them. Make the
  505. # output depend on the dependency file itself, which should cause the
  506. # rule to re-run.
  507. if(CUDA_NVCC_DEPEND_REGENERATE)
  508. set(CUDA_NVCC_DEPEND ${dependency_file})
  509. #message("Generating an empty dependency_file: ${dependency_file}")
  510. file(WRITE ${dependency_file} "#FindCUDA.cmake generated file. Do not edit.\n")
  511. endif()
  512. endmacro()
  513. ###############################################################################
  514. ###############################################################################
  515. # Setup variables' defaults
  516. ###############################################################################
  517. ###############################################################################
  518. # Allow the user to specify if the device code is supposed to be 32 or 64 bit.
  519. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  520. set(CUDA_64_BIT_DEVICE_CODE_DEFAULT ON)
  521. else()
  522. set(CUDA_64_BIT_DEVICE_CODE_DEFAULT OFF)
  523. endif()
  524. option(CUDA_64_BIT_DEVICE_CODE "Compile device code in 64 bit mode" ${CUDA_64_BIT_DEVICE_CODE_DEFAULT})
  525. # Attach the build rule to the source file in VS. This option
  526. option(CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE "Attach the build rule to the CUDA source file. Enable only when the CUDA source file is added to at most one target." ON)
  527. # Prints out extra information about the cuda file during compilation
  528. option(CUDA_BUILD_CUBIN "Generate and parse .cubin files in Device mode." OFF)
  529. # Set whether we are using emulation or device mode.
  530. option(CUDA_BUILD_EMULATION "Build in Emulation mode" OFF)
  531. # Where to put the generated output.
  532. set(CUDA_GENERATED_OUTPUT_DIR "" CACHE PATH "Directory to put all the output files. If blank it will default to the CMAKE_CURRENT_BINARY_DIR")
  533. # Parse HOST_COMPILATION mode.
  534. option(CUDA_HOST_COMPILATION_CPP "Generated file extension" ON)
  535. # Extra user settable flags
  536. cmake_initialize_per_config_variable(CUDA_NVCC_FLAGS "Semi-colon delimit multiple arguments.")
  537. if(DEFINED ENV{CUDAHOSTCXX})
  538. set(CUDA_HOST_COMPILER "$ENV{CUDAHOSTCXX}" CACHE FILEPATH "Host side compiler used by NVCC")
  539. elseif(CMAKE_GENERATOR MATCHES "Visual Studio")
  540. set(_CUDA_MSVC_HOST_COMPILER "$(VCInstallDir)Tools/MSVC/$(VCToolsVersion)/bin/Host$(Platform)/$(PlatformTarget)")
  541. if(MSVC_VERSION LESS 1910)
  542. set(_CUDA_MSVC_HOST_COMPILER "$(VCInstallDir)bin")
  543. endif()
  544. set(CUDA_HOST_COMPILER "${_CUDA_MSVC_HOST_COMPILER}" CACHE FILEPATH "Host side compiler used by NVCC")
  545. else()
  546. if(APPLE
  547. AND "${CMAKE_C_COMPILER_ID}" MATCHES "Clang"
  548. AND "${CMAKE_C_COMPILER}" MATCHES "/cc$")
  549. # Using cc which is symlink to clang may let NVCC think it is GCC and issue
  550. # unhandled -dumpspecs option to clang. Also in case neither
  551. # CMAKE_C_COMPILER is defined (project does not use C language) nor
  552. # CUDA_HOST_COMPILER is specified manually we should skip -ccbin and let
  553. # nvcc use its own default C compiler.
  554. # Only care about this on APPLE with clang to avoid
  555. # following symlinks to things like ccache
  556. if(DEFINED CMAKE_C_COMPILER AND NOT DEFINED CUDA_HOST_COMPILER)
  557. get_filename_component(c_compiler_realpath "${CMAKE_C_COMPILER}" REALPATH)
  558. # if the real path does not end up being clang then
  559. # go back to using CMAKE_C_COMPILER
  560. if(NOT "${c_compiler_realpath}" MATCHES "/clang$")
  561. set(c_compiler_realpath "${CMAKE_C_COMPILER}")
  562. endif()
  563. else()
  564. set(c_compiler_realpath "")
  565. endif()
  566. set(CUDA_HOST_COMPILER "${c_compiler_realpath}" CACHE FILEPATH "Host side compiler used by NVCC")
  567. elseif(MSVC AND "${CMAKE_C_COMPILER}" MATCHES "clcache|sccache")
  568. # NVCC does not think it will work if it is passed clcache.exe or sccache.exe
  569. # as the host compiler, which means that builds with CC=cl.exe won't work.
  570. # Best to just feed it whatever the actual cl.exe is as the host compiler.
  571. set(CUDA_HOST_COMPILER "cl.exe" CACHE FILEPATH "Host side compiler used by NVCC")
  572. else()
  573. set(CUDA_HOST_COMPILER "${CMAKE_C_COMPILER}"
  574. CACHE FILEPATH "Host side compiler used by NVCC")
  575. endif()
  576. endif()
  577. # Propagate the host flags to the host compiler via -Xcompiler
  578. option(CUDA_PROPAGATE_HOST_FLAGS "Propagate C/CXX_FLAGS and friends to the host compiler via -Xcompile" ON)
  579. # Enable CUDA_SEPARABLE_COMPILATION
  580. option(CUDA_SEPARABLE_COMPILATION "Compile CUDA objects with separable compilation enabled. Requires CUDA 5.0+" OFF)
  581. # Specifies whether the commands used when compiling the .cu file will be printed out.
  582. option(CUDA_VERBOSE_BUILD "Print out the commands run while compiling the CUDA source file. With the Makefile generator this defaults to VERBOSE variable specified on the command line, but can be forced on with this option." OFF)
  583. mark_as_advanced(
  584. CUDA_64_BIT_DEVICE_CODE
  585. CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE
  586. CUDA_GENERATED_OUTPUT_DIR
  587. CUDA_HOST_COMPILATION_CPP
  588. CUDA_NVCC_FLAGS
  589. CUDA_PROPAGATE_HOST_FLAGS
  590. CUDA_BUILD_CUBIN
  591. CUDA_BUILD_EMULATION
  592. CUDA_VERBOSE_BUILD
  593. CUDA_SEPARABLE_COMPILATION
  594. )
  595. # Single config generators like Makefiles or Ninja don't usually have
  596. # CMAKE_CONFIGURATION_TYPES defined (but note that it can be defined if set by
  597. # projects or developers). Even CMAKE_BUILD_TYPE might not be defined for
  598. # single config generators (and should not be defined for multi-config
  599. # generators). To ensure we get a complete superset of all possible
  600. # configurations, we combine CMAKE_CONFIGURATION_TYPES, CMAKE_BUILD_TYPE and
  601. # all of the standard configurations, then weed out duplicates with
  602. # list(REMOVE_DUPLICATES). Looping over the unique set then ensures we have
  603. # each configuration-specific set of nvcc flags defined and marked as advanced.
  604. set(CUDA_configuration_types ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE} Debug MinSizeRel Release RelWithDebInfo)
  605. list(REMOVE_DUPLICATES CUDA_configuration_types)
  606. ###############################################################################
  607. ###############################################################################
  608. # Locate CUDA, Set Build Type, etc.
  609. ###############################################################################
  610. ###############################################################################
  611. macro(cuda_unset_include_and_libraries)
  612. unset(CUDA_TOOLKIT_INCLUDE CACHE)
  613. unset(CUDA_CUDART_LIBRARY CACHE)
  614. unset(CUDA_CUDA_LIBRARY CACHE)
  615. # Make sure you run this before you unset CUDA_VERSION.
  616. if(CUDA_VERSION VERSION_EQUAL "3.0")
  617. # This only existed in the 3.0 version of the CUDA toolkit
  618. unset(CUDA_CUDARTEMU_LIBRARY CACHE)
  619. endif()
  620. unset(CUDA_cudart_static_LIBRARY CACHE)
  621. unset(CUDA_cudadevrt_LIBRARY CACHE)
  622. unset(CUDA_cublas_LIBRARY CACHE)
  623. unset(CUDA_cublas_device_LIBRARY CACHE)
  624. unset(CUDA_cublasemu_LIBRARY CACHE)
  625. unset(CUDA_cufft_LIBRARY CACHE)
  626. unset(CUDA_cufftemu_LIBRARY CACHE)
  627. unset(CUDA_cupti_LIBRARY CACHE)
  628. unset(CUDA_curand_LIBRARY CACHE)
  629. unset(CUDA_cusolver_LIBRARY CACHE)
  630. unset(CUDA_cusparse_LIBRARY CACHE)
  631. unset(CUDA_npp_LIBRARY CACHE)
  632. unset(CUDA_nppc_LIBRARY CACHE)
  633. unset(CUDA_nppi_LIBRARY CACHE)
  634. unset(CUDA_npps_LIBRARY CACHE)
  635. unset(CUDA_nvcuvenc_LIBRARY CACHE)
  636. unset(CUDA_nvcuvid_LIBRARY CACHE)
  637. unset(CUDA_nvToolsExt_LIBRARY CACHE)
  638. unset(CUDA_OpenCL_LIBRARY CACHE)
  639. unset(CUDA_GPU_DETECT_OUTPUT CACHE)
  640. endmacro()
  641. # Check to see if the CUDA_TOOLKIT_ROOT_DIR and CUDA_SDK_ROOT_DIR have changed,
  642. # if they have then clear the cache variables, so that will be detected again.
  643. if(NOT "${CUDA_TOOLKIT_ROOT_DIR}" STREQUAL "${CUDA_TOOLKIT_ROOT_DIR_INTERNAL}")
  644. unset(CUDA_TOOLKIT_TARGET_DIR CACHE)
  645. unset(CUDA_NVCC_EXECUTABLE CACHE)
  646. cuda_unset_include_and_libraries()
  647. unset(CUDA_VERSION CACHE)
  648. endif()
  649. # If CUDA_TOOLKIT_TARGET_DIR exists, check if it has changed.
  650. if(DEFINED CUDA_TOOLKIT_TARGET_DIR
  651. AND NOT "${CUDA_TOOLKIT_TARGET_DIR}" STREQUAL "${CUDA_TOOLKIT_TARGET_DIR_INTERNAL}")
  652. cuda_unset_include_and_libraries()
  653. endif()
  654. #
  655. # End of unset()
  656. #
  657. #
  658. # Start looking for things
  659. #
  660. # Search for the cuda distribution.
  661. if(NOT CUDA_TOOLKIT_ROOT_DIR AND NOT CMAKE_CROSSCOMPILING)
  662. # Search in the CUDA_BIN_PATH first.
  663. find_program(CUDA_TOOLKIT_ROOT_DIR_NVCC
  664. NAMES nvcc nvcc.exe
  665. PATHS
  666. ENV CUDA_TOOLKIT_ROOT
  667. ENV CUDA_PATH
  668. ENV CUDA_BIN_PATH
  669. PATH_SUFFIXES bin bin64
  670. DOC "Toolkit location."
  671. NO_DEFAULT_PATH
  672. )
  673. # Now search default paths
  674. find_program(CUDA_TOOLKIT_ROOT_DIR_NVCC
  675. NAMES nvcc nvcc.exe
  676. PATHS /opt/cuda/bin
  677. PATH_SUFFIXES cuda/bin
  678. DOC "Toolkit location."
  679. )
  680. if (CUDA_TOOLKIT_ROOT_DIR_NVCC)
  681. # Given that NVCC can be provided by multiple different sources (NVIDIA HPC SDK, CUDA Toolkit, distro)
  682. # each of which has a different layout, we need to extract the CUDA toolkit root from the compiler
  683. # itself, allowing us to support numerous different scattered toolkit layouts
  684. execute_process(COMMAND ${CUDA_TOOLKIT_ROOT_DIR_NVCC} "-v" "__cmake_determine_cuda"
  685. OUTPUT_VARIABLE _CUDA_NVCC_OUT ERROR_VARIABLE _CUDA_NVCC_OUT)
  686. if(_CUDA_NVCC_OUT MATCHES "\\#\\$ TOP=([^\r\n]*)")
  687. get_filename_component(CUDA_TOOLKIT_ROOT_DIR "${CMAKE_MATCH_1}" ABSOLUTE CACHE)
  688. else()
  689. get_filename_component(CUDA_TOOLKIT_ROOT_DIR "${CUDA_TOOLKIT_ROOT_DIR_NVCC}" DIRECTORY)
  690. get_filename_component(CUDA_TOOLKIT_ROOT_DIR "${CUDA_TOOLKIT_ROOT_DIR}" DIRECTORY CACHE)
  691. endif()
  692. unset(_CUDA_NVCC_OUT)
  693. string(REGEX REPLACE "[/\\\\]?bin[64]*[/\\\\]?$" "" CUDA_TOOLKIT_ROOT_DIR ${CUDA_TOOLKIT_ROOT_DIR})
  694. # We need to force this back into the cache.
  695. set(CUDA_TOOLKIT_ROOT_DIR ${CUDA_TOOLKIT_ROOT_DIR} CACHE PATH "Toolkit location." FORCE)
  696. set(CUDA_TOOLKIT_TARGET_DIR ${CUDA_TOOLKIT_ROOT_DIR})
  697. endif()
  698. unset(CUDA_TOOLKIT_ROOT_DIR_NVCC CACHE)
  699. if (NOT EXISTS ${CUDA_TOOLKIT_ROOT_DIR})
  700. if(CUDA_FIND_REQUIRED)
  701. message(FATAL_ERROR "Specify CUDA_TOOLKIT_ROOT_DIR")
  702. elseif(NOT CUDA_FIND_QUIETLY)
  703. message("CUDA_TOOLKIT_ROOT_DIR not found or specified")
  704. endif()
  705. endif ()
  706. endif ()
  707. if(CMAKE_CROSSCOMPILING)
  708. SET (CUDA_TOOLKIT_ROOT $ENV{CUDA_TOOLKIT_ROOT})
  709. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "armv7-a")
  710. # Support for NVPACK
  711. set (CUDA_TOOLKIT_TARGET_NAME "armv7-linux-androideabi")
  712. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm")
  713. # Support for arm cross compilation
  714. set(CUDA_TOOLKIT_TARGET_NAME "armv7-linux-gnueabihf")
  715. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
  716. # Support for aarch64 cross compilation
  717. if (ANDROID_ARCH_NAME STREQUAL "arm64")
  718. set(CUDA_TOOLKIT_TARGET_NAME "aarch64-linux-androideabi")
  719. elseif (CMAKE_SYSTEM_NAME STREQUAL "QNX")
  720. set(CUDA_TOOLKIT_TARGET_NAME "aarch64-qnx")
  721. else()
  722. set(CUDA_TOOLKIT_TARGET_NAME "aarch64-linux")
  723. endif (ANDROID_ARCH_NAME STREQUAL "arm64")
  724. endif()
  725. if (EXISTS "${CUDA_TOOLKIT_ROOT}/targets/${CUDA_TOOLKIT_TARGET_NAME}")
  726. set(CUDA_TOOLKIT_TARGET_DIR "${CUDA_TOOLKIT_ROOT}/targets/${CUDA_TOOLKIT_TARGET_NAME}" CACHE PATH "CUDA Toolkit target location.")
  727. SET (CUDA_TOOLKIT_ROOT_DIR ${CUDA_TOOLKIT_ROOT})
  728. mark_as_advanced(CUDA_TOOLKIT_TARGET_DIR)
  729. endif()
  730. # add known CUDA targetr root path to the set of directories we search for programs, libraries and headers
  731. set( CMAKE_FIND_ROOT_PATH "${CUDA_TOOLKIT_TARGET_DIR};${CMAKE_FIND_ROOT_PATH}")
  732. macro( cuda_find_host_program )
  733. if (COMMAND find_host_program)
  734. find_host_program( ${ARGN} )
  735. else()
  736. find_program( ${ARGN} )
  737. endif()
  738. endmacro()
  739. else()
  740. # for non-cross-compile, find_host_program == find_program and CUDA_TOOLKIT_TARGET_DIR == CUDA_TOOLKIT_ROOT_DIR
  741. macro( cuda_find_host_program )
  742. find_program( ${ARGN} )
  743. endmacro()
  744. SET (CUDA_TOOLKIT_TARGET_DIR ${CUDA_TOOLKIT_ROOT_DIR})
  745. endif()
  746. # CUDA_NVCC_EXECUTABLE
  747. if(DEFINED ENV{CUDA_NVCC_EXECUTABLE})
  748. set(CUDA_NVCC_EXECUTABLE "$ENV{CUDA_NVCC_EXECUTABLE}" CACHE FILEPATH "The CUDA compiler")
  749. else()
  750. cuda_find_host_program(CUDA_NVCC_EXECUTABLE
  751. NAMES nvcc
  752. PATHS "${CUDA_TOOLKIT_ROOT_DIR}"
  753. ENV CUDA_PATH
  754. ENV CUDA_BIN_PATH
  755. PATH_SUFFIXES bin bin64
  756. NO_DEFAULT_PATH
  757. )
  758. # Search default search paths, after we search our own set of paths.
  759. cuda_find_host_program(CUDA_NVCC_EXECUTABLE nvcc)
  760. endif()
  761. mark_as_advanced(CUDA_NVCC_EXECUTABLE)
  762. if(CUDA_NVCC_EXECUTABLE AND NOT CUDA_VERSION)
  763. # Compute the version.
  764. execute_process (COMMAND ${CUDA_NVCC_EXECUTABLE} "--version" OUTPUT_VARIABLE NVCC_OUT)
  765. string(REGEX REPLACE ".*release ([0-9]+)\\.([0-9]+).*" "\\1" CUDA_VERSION_MAJOR "${NVCC_OUT}")
  766. string(REGEX REPLACE ".*release ([0-9]+)\\.([0-9]+).*" "\\2" CUDA_VERSION_MINOR "${NVCC_OUT}")
  767. set(CUDA_VERSION "${CUDA_VERSION_MAJOR}.${CUDA_VERSION_MINOR}" CACHE STRING "Version of CUDA as computed from nvcc.")
  768. mark_as_advanced(CUDA_VERSION)
  769. else()
  770. # Need to set these based off of the cached value
  771. string(REGEX REPLACE "([0-9]+)\\.([0-9]+).*" "\\1" CUDA_VERSION_MAJOR "${CUDA_VERSION}")
  772. string(REGEX REPLACE "([0-9]+)\\.([0-9]+).*" "\\2" CUDA_VERSION_MINOR "${CUDA_VERSION}")
  773. endif()
  774. # Always set this convenience variable
  775. set(CUDA_VERSION_STRING "${CUDA_VERSION}")
  776. # CUDA_TOOLKIT_INCLUDE
  777. find_path(CUDA_TOOLKIT_INCLUDE
  778. device_functions.h # Header included in toolkit
  779. PATHS ${CUDA_TOOLKIT_TARGET_DIR}
  780. ENV CUDA_PATH
  781. ENV CUDA_INC_PATH
  782. PATH_SUFFIXES include
  783. NO_DEFAULT_PATH
  784. )
  785. # Search default search paths, after we search our own set of paths.
  786. find_path(CUDA_TOOLKIT_INCLUDE device_functions.h)
  787. mark_as_advanced(CUDA_TOOLKIT_INCLUDE)
  788. if (CUDA_VERSION VERSION_GREATER "7.0" OR EXISTS "${CUDA_TOOLKIT_INCLUDE}/cuda_fp16.h")
  789. set(CUDA_HAS_FP16 TRUE)
  790. else()
  791. set(CUDA_HAS_FP16 FALSE)
  792. endif()
  793. # Set the user list of include dir to nothing to initialize it.
  794. set (CUDA_NVCC_INCLUDE_DIRS_USER "")
  795. set (CUDA_INCLUDE_DIRS ${CUDA_TOOLKIT_INCLUDE})
  796. macro(cuda_find_library_local_first_with_path_ext _var _names _doc _path_ext )
  797. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  798. # CUDA 3.2+ on Windows moved the library directories, so we need the new
  799. # and old paths.
  800. set(_cuda_64bit_lib_dir "${_path_ext}lib/x64" "${_path_ext}lib64" "${_path_ext}libx64" )
  801. endif()
  802. # CUDA 3.2+ on Windows moved the library directories, so we need to new
  803. # (lib/Win32) and the old path (lib).
  804. find_library(${_var}
  805. NAMES ${_names}
  806. PATHS "${CUDA_TOOLKIT_TARGET_DIR}"
  807. ENV CUDA_PATH
  808. ENV CUDA_LIB_PATH
  809. PATH_SUFFIXES ${_cuda_64bit_lib_dir} "${_path_ext}lib/Win32" "${_path_ext}lib" "${_path_ext}libWin32"
  810. DOC ${_doc}
  811. NO_DEFAULT_PATH
  812. )
  813. if (NOT CMAKE_CROSSCOMPILING)
  814. # Search default search paths, after we search our own set of paths.
  815. find_library(${_var}
  816. NAMES ${_names}
  817. PATHS "/usr/lib/nvidia-current"
  818. DOC ${_doc}
  819. )
  820. endif()
  821. endmacro()
  822. macro(cuda_find_library_local_first _var _names _doc)
  823. cuda_find_library_local_first_with_path_ext( "${_var}" "${_names}" "${_doc}" "" )
  824. endmacro()
  825. macro(find_library_local_first _var _names _doc )
  826. cuda_find_library_local_first( "${_var}" "${_names}" "${_doc}" "" )
  827. endmacro()
  828. # CUDA_LIBRARIES
  829. cuda_find_library_local_first(CUDA_CUDART_LIBRARY cudart "\"cudart\" library")
  830. if(CUDA_VERSION VERSION_EQUAL "3.0")
  831. # The cudartemu library only existed for the 3.0 version of CUDA.
  832. cuda_find_library_local_first(CUDA_CUDARTEMU_LIBRARY cudartemu "\"cudartemu\" library")
  833. mark_as_advanced(
  834. CUDA_CUDARTEMU_LIBRARY
  835. )
  836. endif()
  837. if(NOT CUDA_VERSION VERSION_LESS "5.5")
  838. cuda_find_library_local_first(CUDA_cudart_static_LIBRARY cudart_static "static CUDA runtime library")
  839. mark_as_advanced(CUDA_cudart_static_LIBRARY)
  840. endif()
  841. if(CUDA_cudart_static_LIBRARY)
  842. # If static cudart available, use it by default, but provide a user-visible option to disable it.
  843. option(CUDA_USE_STATIC_CUDA_RUNTIME "Use the static version of the CUDA runtime library if available" ON)
  844. else()
  845. # If not available, silently disable the option.
  846. set(CUDA_USE_STATIC_CUDA_RUNTIME OFF CACHE INTERNAL "")
  847. endif()
  848. if(CUDA_USE_STATIC_CUDA_RUNTIME)
  849. set(CUDA_CUDART_LIBRARY_VAR CUDA_cudart_static_LIBRARY)
  850. else()
  851. set(CUDA_CUDART_LIBRARY_VAR CUDA_CUDART_LIBRARY)
  852. endif()
  853. if(NOT CUDA_VERSION VERSION_LESS "5.0")
  854. cuda_find_library_local_first(CUDA_cudadevrt_LIBRARY cudadevrt "\"cudadevrt\" library")
  855. mark_as_advanced(CUDA_cudadevrt_LIBRARY)
  856. endif()
  857. if(CUDA_USE_STATIC_CUDA_RUNTIME)
  858. if(UNIX)
  859. # Check for the dependent libraries.
  860. # Many of the FindXYZ CMake comes with makes use of try_compile with int main(){return 0;}
  861. # as the source file. Unfortunately this causes a warning with -Wstrict-prototypes and
  862. # -Werror causes the try_compile to fail. We will just temporarily disable other flags
  863. # when doing the find_package command here.
  864. set(_cuda_cmake_c_flags ${CMAKE_C_FLAGS})
  865. set(CMAKE_C_FLAGS "-fPIC")
  866. find_package(Threads REQUIRED)
  867. set(CMAKE_C_FLAGS ${_cuda_cmake_c_flags})
  868. if(NOT APPLE AND NOT (CMAKE_SYSTEM_NAME STREQUAL "QNX"))
  869. #On Linux, you must link against librt when using the static cuda runtime.
  870. find_library(CUDA_rt_LIBRARY rt)
  871. if (NOT CUDA_rt_LIBRARY)
  872. message(WARNING "Expecting to find librt for libcudart_static, but didn't find it.")
  873. endif()
  874. endif()
  875. endif()
  876. endif()
  877. # CUPTI library showed up in cuda toolkit 4.0
  878. if(NOT CUDA_VERSION VERSION_LESS "4.0")
  879. cuda_find_library_local_first_with_path_ext(CUDA_cupti_LIBRARY cupti "\"cupti\" library" "extras/CUPTI/")
  880. mark_as_advanced(CUDA_cupti_LIBRARY)
  881. endif()
  882. # Set the CUDA_LIBRARIES variable. This is the set of stuff to link against if you are
  883. # using the CUDA runtime. For the dynamic version of the runtime, most of the
  884. # dependencies are brough in, but for the static version there are additional libraries
  885. # and linker commands needed.
  886. # Initialize to empty
  887. set(CUDA_LIBRARIES)
  888. # If we are using emulation mode and we found the cudartemu library then use
  889. # that one instead of cudart.
  890. if(CUDA_BUILD_EMULATION AND CUDA_CUDARTEMU_LIBRARY)
  891. list(APPEND CUDA_LIBRARIES ${CUDA_CUDARTEMU_LIBRARY})
  892. elseif(CUDA_USE_STATIC_CUDA_RUNTIME AND CUDA_cudart_static_LIBRARY)
  893. list(APPEND CUDA_LIBRARIES ${CUDA_cudart_static_LIBRARY})
  894. if (TARGET Threads::Threads)
  895. list(APPEND CUDA_LIBRARIES Threads::Threads)
  896. endif()
  897. list(APPEND CUDA_LIBRARIES ${CMAKE_DL_LIBS})
  898. if (CUDA_rt_LIBRARY)
  899. list(APPEND CUDA_LIBRARIES ${CUDA_rt_LIBRARY})
  900. endif()
  901. if(APPLE)
  902. # We need to add the default path to the driver (libcuda.dylib) as an rpath, so that
  903. # the static cuda runtime can find it at runtime.
  904. list(APPEND CUDA_LIBRARIES -Wl,-rpath,/usr/local/cuda/lib)
  905. endif()
  906. else()
  907. list(APPEND CUDA_LIBRARIES ${CUDA_CUDART_LIBRARY})
  908. endif()
  909. # 1.1 toolkit on linux doesn't appear to have a separate library on
  910. # some platforms.
  911. cuda_find_library_local_first(CUDA_CUDA_LIBRARY cuda "\"cuda\" library (older versions only).")
  912. mark_as_advanced(
  913. CUDA_CUDA_LIBRARY
  914. CUDA_CUDART_LIBRARY
  915. )
  916. #######################
  917. # Look for some of the toolkit helper libraries
  918. macro(FIND_CUDA_HELPER_LIBS _name)
  919. cuda_find_library_local_first(CUDA_${_name}_LIBRARY ${_name} "\"${_name}\" library")
  920. mark_as_advanced(CUDA_${_name}_LIBRARY)
  921. endmacro()
  922. #######################
  923. # Disable emulation for v3.1 onward
  924. if(CUDA_VERSION VERSION_GREATER "3.0")
  925. if(CUDA_BUILD_EMULATION)
  926. message(FATAL_ERROR "CUDA_BUILD_EMULATION is not supported in version 3.1 and onwards. You must disable it to proceed. You have version ${CUDA_VERSION}.")
  927. endif()
  928. endif()
  929. # Search for additional CUDA toolkit libraries.
  930. if(CUDA_VERSION VERSION_LESS "3.1")
  931. # Emulation libraries aren't available in version 3.1 onward.
  932. find_cuda_helper_libs(cufftemu)
  933. find_cuda_helper_libs(cublasemu)
  934. endif()
  935. find_cuda_helper_libs(cufft)
  936. find_cuda_helper_libs(cublas)
  937. if(NOT CUDA_VERSION VERSION_LESS "3.2")
  938. # cusparse showed up in version 3.2
  939. find_cuda_helper_libs(cusparse)
  940. find_cuda_helper_libs(curand)
  941. if (WIN32)
  942. find_cuda_helper_libs(nvcuvenc)
  943. find_cuda_helper_libs(nvcuvid)
  944. endif()
  945. endif()
  946. if(CUDA_VERSION VERSION_GREATER "5.0" AND CUDA_VERSION VERSION_LESS "9.2")
  947. # In CUDA 9.2 cublas_device was deprecated
  948. find_cuda_helper_libs(cublas_device)
  949. endif()
  950. if(NOT CUDA_VERSION VERSION_LESS "5.0")
  951. find_cuda_helper_libs(nvToolsExt)
  952. find_cuda_helper_libs(OpenCL)
  953. endif()
  954. if(NOT CUDA_VERSION VERSION_LESS "9.0")
  955. # In CUDA 9.0 NPP was nppi was removed
  956. find_cuda_helper_libs(nppc)
  957. find_cuda_helper_libs(nppial)
  958. find_cuda_helper_libs(nppicc)
  959. set(CUDA_npp_LIBRARY ${CUDA_nppc_LIBRARY} ${CUDA_nppial_LIBRARY} ${CUDA_nppicc_LIBRARY})
  960. if(CUDA_VERSION VERSION_LESS "11.0")
  961. find_cuda_helper_libs(nppicom)
  962. list(APPEND CUDA_npp_LIBRARY ${CUDA_nppicom_LIBRARY})
  963. endif()
  964. find_cuda_helper_libs(nppidei)
  965. find_cuda_helper_libs(nppif)
  966. find_cuda_helper_libs(nppig)
  967. find_cuda_helper_libs(nppim)
  968. find_cuda_helper_libs(nppist)
  969. find_cuda_helper_libs(nppisu)
  970. find_cuda_helper_libs(nppitc)
  971. find_cuda_helper_libs(npps)
  972. list(APPEND CUDA_npp_LIBRARY ${CUDA_nppidei_LIBRARY} ${CUDA_nppif_LIBRARY} ${CUDA_nppig_LIBRARY} ${CUDA_nppim_LIBRARY} ${CUDA_nppist_LIBRARY} ${CUDA_nppisu_LIBRARY} ${CUDA_nppitc_LIBRARY} ${CUDA_npps_LIBRARY})
  973. elseif(CUDA_VERSION VERSION_GREATER "5.0")
  974. # In CUDA 5.5 NPP was split into 3 separate libraries.
  975. find_cuda_helper_libs(nppc)
  976. find_cuda_helper_libs(nppi)
  977. find_cuda_helper_libs(npps)
  978. set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppi_LIBRARY};${CUDA_npps_LIBRARY}")
  979. elseif(NOT CUDA_VERSION VERSION_LESS "4.0")
  980. find_cuda_helper_libs(npp)
  981. endif()
  982. if(NOT CUDA_VERSION VERSION_LESS "7.0")
  983. # cusolver showed up in version 7.0
  984. find_cuda_helper_libs(cusolver)
  985. endif()
  986. if (CUDA_BUILD_EMULATION)
  987. set(CUDA_CUFFT_LIBRARIES ${CUDA_cufftemu_LIBRARY})
  988. set(CUDA_CUBLAS_LIBRARIES ${CUDA_cublasemu_LIBRARY})
  989. else()
  990. set(CUDA_CUFFT_LIBRARIES ${CUDA_cufft_LIBRARY})
  991. set(CUDA_CUBLAS_LIBRARIES ${CUDA_cublas_LIBRARY} ${CUDA_cublas_device_LIBRARY})
  992. endif()
  993. ########################
  994. # Look for the SDK stuff. As of CUDA 3.0 NVSDKCUDA_ROOT has been replaced with
  995. # NVSDKCOMPUTE_ROOT with the old CUDA C contents moved into the C subdirectory
  996. find_path(CUDA_SDK_ROOT_DIR common/inc/cutil.h
  997. HINTS
  998. "$ENV{NVSDKCOMPUTE_ROOT}/C"
  999. ENV NVSDKCUDA_ROOT
  1000. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\NVIDIA Corporation\\Installed Products\\NVIDIA SDK 10\\Compute;InstallDir]"
  1001. PATHS
  1002. "/Developer/GPU\ Computing/C"
  1003. )
  1004. # Keep the CUDA_SDK_ROOT_DIR first in order to be able to override the
  1005. # environment variables.
  1006. set(CUDA_SDK_SEARCH_PATH
  1007. "${CUDA_SDK_ROOT_DIR}"
  1008. "${CUDA_TOOLKIT_ROOT_DIR}/local/NVSDK0.2"
  1009. "${CUDA_TOOLKIT_ROOT_DIR}/NVSDK0.2"
  1010. "${CUDA_TOOLKIT_ROOT_DIR}/NV_CUDA_SDK"
  1011. "$ENV{HOME}/NVIDIA_CUDA_SDK"
  1012. "$ENV{HOME}/NVIDIA_CUDA_SDK_MACOSX"
  1013. "/Developer/CUDA"
  1014. )
  1015. # Example of how to find an include file from the CUDA_SDK_ROOT_DIR
  1016. # find_path(CUDA_CUT_INCLUDE_DIR
  1017. # cutil.h
  1018. # PATHS ${CUDA_SDK_SEARCH_PATH}
  1019. # PATH_SUFFIXES "common/inc"
  1020. # DOC "Location of cutil.h"
  1021. # NO_DEFAULT_PATH
  1022. # )
  1023. # # Now search system paths
  1024. # find_path(CUDA_CUT_INCLUDE_DIR cutil.h DOC "Location of cutil.h")
  1025. # mark_as_advanced(CUDA_CUT_INCLUDE_DIR)
  1026. # Example of how to find a library in the CUDA_SDK_ROOT_DIR
  1027. # # cutil library is called cutil64 for 64 bit builds on windows. We don't want
  1028. # # to get these confused, so we are setting the name based on the word size of
  1029. # # the build.
  1030. # if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  1031. # set(cuda_cutil_name cutil64)
  1032. # else()
  1033. # set(cuda_cutil_name cutil32)
  1034. # endif()
  1035. # find_library(CUDA_CUT_LIBRARY
  1036. # NAMES cutil ${cuda_cutil_name}
  1037. # PATHS ${CUDA_SDK_SEARCH_PATH}
  1038. # # The new version of the sdk shows up in common/lib, but the old one is in lib
  1039. # PATH_SUFFIXES "common/lib" "lib"
  1040. # DOC "Location of cutil library"
  1041. # NO_DEFAULT_PATH
  1042. # )
  1043. # # Now search system paths
  1044. # find_library(CUDA_CUT_LIBRARY NAMES cutil ${cuda_cutil_name} DOC "Location of cutil library")
  1045. # mark_as_advanced(CUDA_CUT_LIBRARY)
  1046. # set(CUDA_CUT_LIBRARIES ${CUDA_CUT_LIBRARY})
  1047. #############################
  1048. # Check for required components
  1049. set(CUDA_FOUND TRUE)
  1050. set(CUDA_TOOLKIT_ROOT_DIR_INTERNAL "${CUDA_TOOLKIT_ROOT_DIR}" CACHE INTERNAL
  1051. "This is the value of the last time CUDA_TOOLKIT_ROOT_DIR was set successfully." FORCE)
  1052. set(CUDA_TOOLKIT_TARGET_DIR_INTERNAL "${CUDA_TOOLKIT_TARGET_DIR}" CACHE INTERNAL
  1053. "This is the value of the last time CUDA_TOOLKIT_TARGET_DIR was set successfully." FORCE)
  1054. set(CUDA_SDK_ROOT_DIR_INTERNAL "${CUDA_SDK_ROOT_DIR}" CACHE INTERNAL
  1055. "This is the value of the last time CUDA_SDK_ROOT_DIR was set successfully." FORCE)
  1056. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  1057. find_package_handle_standard_args(CUDA
  1058. REQUIRED_VARS
  1059. CUDA_TOOLKIT_ROOT_DIR
  1060. CUDA_NVCC_EXECUTABLE
  1061. CUDA_INCLUDE_DIRS
  1062. ${CUDA_CUDART_LIBRARY_VAR}
  1063. VERSION_VAR
  1064. CUDA_VERSION
  1065. )
  1066. ###############################################################################
  1067. ###############################################################################
  1068. # Macros
  1069. ###############################################################################
  1070. ###############################################################################
  1071. ###############################################################################
  1072. # Add include directories to pass to the nvcc command.
  1073. macro(CUDA_INCLUDE_DIRECTORIES)
  1074. foreach(dir ${ARGN})
  1075. list(APPEND CUDA_NVCC_INCLUDE_DIRS_USER ${dir})
  1076. endforeach()
  1077. endmacro()
  1078. ##############################################################################
  1079. cuda_find_helper_file(parse_cubin cmake)
  1080. cuda_find_helper_file(make2cmake cmake)
  1081. cuda_find_helper_file(run_nvcc cmake)
  1082. include("${CMAKE_CURRENT_LIST_DIR}/FindCUDA/select_compute_arch.cmake")
  1083. ##############################################################################
  1084. # Separate the OPTIONS out from the sources
  1085. #
  1086. macro(CUDA_GET_SOURCES_AND_OPTIONS _sources _cmake_options _options)
  1087. set( ${_sources} )
  1088. set( ${_cmake_options} )
  1089. set( ${_options} )
  1090. set( _found_options FALSE )
  1091. foreach(arg ${ARGN})
  1092. if("x${arg}" STREQUAL "xOPTIONS")
  1093. set( _found_options TRUE )
  1094. elseif(
  1095. "x${arg}" STREQUAL "xWIN32" OR
  1096. "x${arg}" STREQUAL "xMACOSX_BUNDLE" OR
  1097. "x${arg}" STREQUAL "xEXCLUDE_FROM_ALL" OR
  1098. "x${arg}" STREQUAL "xSTATIC" OR
  1099. "x${arg}" STREQUAL "xSHARED" OR
  1100. "x${arg}" STREQUAL "xMODULE"
  1101. )
  1102. list(APPEND ${_cmake_options} ${arg})
  1103. else()
  1104. if ( _found_options )
  1105. list(APPEND ${_options} ${arg})
  1106. else()
  1107. # Assume this is a file
  1108. list(APPEND ${_sources} ${arg})
  1109. endif()
  1110. endif()
  1111. endforeach()
  1112. endmacro()
  1113. ##############################################################################
  1114. # Parse the OPTIONS from ARGN and set the variables prefixed by _option_prefix
  1115. #
  1116. macro(CUDA_PARSE_NVCC_OPTIONS _option_prefix)
  1117. set( _found_config )
  1118. foreach(arg ${ARGN})
  1119. # Determine if we are dealing with a perconfiguration flag
  1120. foreach(config ${CUDA_configuration_types})
  1121. string(TOUPPER ${config} config_upper)
  1122. if (arg STREQUAL "${config_upper}")
  1123. set( _found_config _${arg})
  1124. # Set arg to nothing to keep it from being processed further
  1125. set( arg )
  1126. endif()
  1127. endforeach()
  1128. if ( arg )
  1129. list(APPEND ${_option_prefix}${_found_config} "${arg}")
  1130. endif()
  1131. endforeach()
  1132. endmacro()
  1133. ##############################################################################
  1134. # Helper to add the include directory for CUDA only once
  1135. function(CUDA_ADD_CUDA_INCLUDE_ONCE)
  1136. get_directory_property(_include_directories INCLUDE_DIRECTORIES)
  1137. set(_add TRUE)
  1138. if(_include_directories)
  1139. foreach(dir ${_include_directories})
  1140. if("${dir}" STREQUAL "${CUDA_INCLUDE_DIRS}")
  1141. set(_add FALSE)
  1142. endif()
  1143. endforeach()
  1144. endif()
  1145. if(_add)
  1146. include_directories(${CUDA_INCLUDE_DIRS})
  1147. endif()
  1148. endfunction()
  1149. function(CUDA_BUILD_SHARED_LIBRARY shared_flag)
  1150. set(cmake_args ${ARGN})
  1151. # If SHARED, MODULE, or STATIC aren't already in the list of arguments, then
  1152. # add SHARED or STATIC based on the value of BUILD_SHARED_LIBS.
  1153. list(FIND cmake_args SHARED _cuda_found_SHARED)
  1154. list(FIND cmake_args MODULE _cuda_found_MODULE)
  1155. list(FIND cmake_args STATIC _cuda_found_STATIC)
  1156. if( _cuda_found_SHARED GREATER -1 OR
  1157. _cuda_found_MODULE GREATER -1 OR
  1158. _cuda_found_STATIC GREATER -1)
  1159. set(_cuda_build_shared_libs)
  1160. else()
  1161. if (BUILD_SHARED_LIBS)
  1162. set(_cuda_build_shared_libs SHARED)
  1163. else()
  1164. set(_cuda_build_shared_libs STATIC)
  1165. endif()
  1166. endif()
  1167. set(${shared_flag} ${_cuda_build_shared_libs} PARENT_SCOPE)
  1168. endfunction()
  1169. ##############################################################################
  1170. # Helper to avoid clashes of files with the same basename but different paths.
  1171. # This doesn't attempt to do exactly what CMake internals do, which is to only
  1172. # add this path when there is a conflict, since by the time a second collision
  1173. # in names is detected it's already too late to fix the first one. For
  1174. # consistency sake the relative path will be added to all files.
  1175. function(CUDA_COMPUTE_BUILD_PATH path build_path)
  1176. #message("CUDA_COMPUTE_BUILD_PATH([${path}] ${build_path})")
  1177. # Only deal with CMake style paths from here on out
  1178. file(TO_CMAKE_PATH "${path}" bpath)
  1179. if (IS_ABSOLUTE "${bpath}")
  1180. # Absolute paths are generally unnessary, especially if something like
  1181. # file(GLOB_RECURSE) is used to pick up the files.
  1182. string(FIND "${bpath}" "${CMAKE_CURRENT_BINARY_DIR}" _binary_dir_pos)
  1183. if (_binary_dir_pos EQUAL 0)
  1184. file(RELATIVE_PATH bpath "${CMAKE_CURRENT_BINARY_DIR}" "${bpath}")
  1185. else()
  1186. file(RELATIVE_PATH bpath "${CMAKE_CURRENT_SOURCE_DIR}" "${bpath}")
  1187. endif()
  1188. endif()
  1189. # This recipe is from cmLocalGenerator::CreateSafeUniqueObjectFileName in the
  1190. # CMake source.
  1191. # Remove leading /
  1192. string(REGEX REPLACE "^[/]+" "" bpath "${bpath}")
  1193. # Avoid absolute paths by removing ':'
  1194. string(REPLACE ":" "_" bpath "${bpath}")
  1195. # Avoid relative paths that go up the tree
  1196. string(REPLACE "../" "__/" bpath "${bpath}")
  1197. # Avoid spaces
  1198. string(REPLACE " " "_" bpath "${bpath}")
  1199. # Strip off the filename. I wait until here to do it, since removin the
  1200. # basename can make a path that looked like path/../basename turn into
  1201. # path/.. (notice the trailing slash).
  1202. get_filename_component(bpath "${bpath}" PATH)
  1203. set(${build_path} "${bpath}" PARENT_SCOPE)
  1204. #message("${build_path} = ${bpath}")
  1205. endfunction()
  1206. ##############################################################################
  1207. # This helper macro populates the following variables and setups up custom
  1208. # commands and targets to invoke the nvcc compiler to generate C or PTX source
  1209. # dependent upon the format parameter. The compiler is invoked once with -M
  1210. # to generate a dependency file and a second time with -cuda or -ptx to generate
  1211. # a .cpp or .ptx file.
  1212. # INPUT:
  1213. # cuda_target - Target name
  1214. # format - PTX, CUBIN, FATBIN or OBJ
  1215. # FILE1 .. FILEN - The remaining arguments are the sources to be wrapped.
  1216. # OPTIONS - Extra options to NVCC
  1217. # OUTPUT:
  1218. # generated_files - List of generated files
  1219. ##############################################################################
  1220. ##############################################################################
  1221. macro(CUDA_WRAP_SRCS cuda_target format generated_files)
  1222. # Put optional arguments in list.
  1223. set(_argn_list "${ARGN}")
  1224. # If one of the given optional arguments is "PHONY", make a note of it, then
  1225. # remove it from the list.
  1226. list(FIND _argn_list "PHONY" _phony_idx)
  1227. if("${_phony_idx}" GREATER "-1")
  1228. set(_target_is_phony true)
  1229. list(REMOVE_AT _argn_list ${_phony_idx})
  1230. else()
  1231. set(_target_is_phony false)
  1232. endif()
  1233. # If CMake doesn't support separable compilation, complain
  1234. if(CUDA_SEPARABLE_COMPILATION AND CMAKE_VERSION VERSION_LESS "2.8.10.1")
  1235. message(SEND_ERROR "CUDA_SEPARABLE_COMPILATION isn't supported for CMake versions less than 2.8.10.1")
  1236. endif()
  1237. # Set up all the command line flags here, so that they can be overridden on a per target basis.
  1238. set(nvcc_flags "")
  1239. # Emulation if the card isn't present.
  1240. if (CUDA_BUILD_EMULATION)
  1241. # Emulation.
  1242. set(nvcc_flags ${nvcc_flags} --device-emulation -D_DEVICEEMU -g)
  1243. else()
  1244. # Device mode. No flags necessary.
  1245. endif()
  1246. if(CUDA_HOST_COMPILATION_CPP)
  1247. set(CUDA_C_OR_CXX CXX)
  1248. else()
  1249. if(CUDA_VERSION VERSION_LESS "3.0")
  1250. set(nvcc_flags ${nvcc_flags} --host-compilation C)
  1251. else()
  1252. message(WARNING "--host-compilation flag is deprecated in CUDA version >= 3.0. Removing --host-compilation C flag" )
  1253. endif()
  1254. set(CUDA_C_OR_CXX C)
  1255. endif()
  1256. set(generated_extension ${CMAKE_${CUDA_C_OR_CXX}_OUTPUT_EXTENSION})
  1257. if(CUDA_64_BIT_DEVICE_CODE)
  1258. set(nvcc_flags ${nvcc_flags} -m64)
  1259. else()
  1260. set(nvcc_flags ${nvcc_flags} -m32)
  1261. endif()
  1262. if(CUDA_TARGET_CPU_ARCH)
  1263. set(nvcc_flags ${nvcc_flags} "--target-cpu-architecture=${CUDA_TARGET_CPU_ARCH}")
  1264. endif()
  1265. # This needs to be passed in at this stage, because VS needs to fill out the
  1266. # various macros from within VS. Note that CCBIN is only used if
  1267. # -ccbin or --compiler-bindir isn't used and CUDA_HOST_COMPILER matches
  1268. # _CUDA_MSVC_HOST_COMPILER
  1269. if(CMAKE_GENERATOR MATCHES "Visual Studio")
  1270. set(ccbin_flags -D "\"CCBIN:PATH=${_CUDA_MSVC_HOST_COMPILER}\"" )
  1271. else()
  1272. set(ccbin_flags)
  1273. endif()
  1274. # Figure out which configure we will use and pass that in as an argument to
  1275. # the script. We need to defer the decision until compilation time, because
  1276. # for VS projects we won't know if we are making a debug or release build
  1277. # until build time.
  1278. if(CMAKE_GENERATOR MATCHES "Visual Studio")
  1279. set( CUDA_build_configuration "$(ConfigurationName)" )
  1280. else()
  1281. set( CUDA_build_configuration "${CMAKE_BUILD_TYPE}")
  1282. endif()
  1283. # Initialize our list of includes with the user ones followed by the CUDA system ones.
  1284. set(CUDA_NVCC_INCLUDE_DIRS ${CUDA_NVCC_INCLUDE_DIRS_USER} "${CUDA_INCLUDE_DIRS}")
  1285. if(_target_is_phony)
  1286. # If the passed in target name isn't a real target (i.e., this is from a call to one of the
  1287. # cuda_compile_* functions), need to query directory properties to get include directories
  1288. # and compile definitions.
  1289. get_directory_property(_dir_include_dirs INCLUDE_DIRECTORIES)
  1290. get_directory_property(_dir_compile_defs COMPILE_DEFINITIONS)
  1291. list(APPEND CUDA_NVCC_INCLUDE_DIRS "${_dir_include_dirs}")
  1292. set(CUDA_NVCC_COMPILE_DEFINITIONS "${_dir_compile_defs}")
  1293. else()
  1294. # Append the include directories for this target via generator expression, which is
  1295. # expanded by the FILE(GENERATE) call below. This generator expression captures all
  1296. # include dirs set by the user, whether via directory properties or target properties
  1297. list(APPEND CUDA_NVCC_INCLUDE_DIRS "$<TARGET_PROPERTY:${cuda_target},INCLUDE_DIRECTORIES>")
  1298. # Do the same thing with compile definitions
  1299. set(CUDA_NVCC_COMPILE_DEFINITIONS "$<TARGET_PROPERTY:${cuda_target},COMPILE_DEFINITIONS>")
  1300. endif()
  1301. # Reset these variables
  1302. set(CUDA_WRAP_OPTION_NVCC_FLAGS)
  1303. foreach(config ${CUDA_configuration_types})
  1304. string(TOUPPER ${config} config_upper)
  1305. set(CUDA_WRAP_OPTION_NVCC_FLAGS_${config_upper})
  1306. endforeach()
  1307. CUDA_GET_SOURCES_AND_OPTIONS(_cuda_wrap_sources _cuda_wrap_cmake_options _cuda_wrap_options ${_argn_list})
  1308. CUDA_PARSE_NVCC_OPTIONS(CUDA_WRAP_OPTION_NVCC_FLAGS ${_cuda_wrap_options})
  1309. # Figure out if we are building a shared library. BUILD_SHARED_LIBS is
  1310. # respected in CUDA_ADD_LIBRARY.
  1311. set(_cuda_build_shared_libs FALSE)
  1312. # SHARED, MODULE
  1313. list(FIND _cuda_wrap_cmake_options SHARED _cuda_found_SHARED)
  1314. list(FIND _cuda_wrap_cmake_options MODULE _cuda_found_MODULE)
  1315. if(_cuda_found_SHARED GREATER -1 OR _cuda_found_MODULE GREATER -1)
  1316. set(_cuda_build_shared_libs TRUE)
  1317. endif()
  1318. # STATIC
  1319. list(FIND _cuda_wrap_cmake_options STATIC _cuda_found_STATIC)
  1320. if(_cuda_found_STATIC GREATER -1)
  1321. set(_cuda_build_shared_libs FALSE)
  1322. endif()
  1323. # CUDA_HOST_FLAGS
  1324. if(_cuda_build_shared_libs)
  1325. # If we are setting up code for a shared library, then we need to add extra flags for
  1326. # compiling objects for shared libraries.
  1327. set(CUDA_HOST_SHARED_FLAGS ${CMAKE_SHARED_LIBRARY_${CUDA_C_OR_CXX}_FLAGS})
  1328. else()
  1329. set(CUDA_HOST_SHARED_FLAGS)
  1330. endif()
  1331. # Only add the CMAKE_{C,CXX}_FLAGS if we are propagating host flags. We
  1332. # always need to set the SHARED_FLAGS, though.
  1333. if(CUDA_PROPAGATE_HOST_FLAGS)
  1334. set(_cuda_host_flags "set(CMAKE_HOST_FLAGS ${CMAKE_${CUDA_C_OR_CXX}_FLAGS} ${CUDA_HOST_SHARED_FLAGS})")
  1335. else()
  1336. set(_cuda_host_flags "set(CMAKE_HOST_FLAGS ${CUDA_HOST_SHARED_FLAGS})")
  1337. endif()
  1338. set(_cuda_nvcc_flags_config "# Build specific configuration flags")
  1339. # Loop over all the configuration types to generate appropriate flags for run_nvcc.cmake
  1340. foreach(config ${CUDA_configuration_types})
  1341. string(TOUPPER ${config} config_upper)
  1342. # CMAKE_FLAGS are strings and not lists. By not putting quotes around CMAKE_FLAGS
  1343. # we convert the strings to lists (like we want).
  1344. if(CUDA_PROPAGATE_HOST_FLAGS)
  1345. # nvcc chokes on -g3 in versions previous to 3.0, so replace it with -g
  1346. set(_cuda_fix_g3 FALSE)
  1347. if(CMAKE_COMPILER_IS_GNUCC)
  1348. if (CUDA_VERSION VERSION_LESS "3.0" OR
  1349. CUDA_VERSION VERSION_EQUAL "4.1" OR
  1350. CUDA_VERSION VERSION_EQUAL "4.2"
  1351. )
  1352. set(_cuda_fix_g3 TRUE)
  1353. endif()
  1354. endif()
  1355. if(_cuda_fix_g3)
  1356. string(REPLACE "-g3" "-g" _cuda_C_FLAGS "${CMAKE_${CUDA_C_OR_CXX}_FLAGS_${config_upper}}")
  1357. else()
  1358. set(_cuda_C_FLAGS "${CMAKE_${CUDA_C_OR_CXX}_FLAGS_${config_upper}}")
  1359. endif()
  1360. string(APPEND _cuda_host_flags "\nset(CMAKE_HOST_FLAGS_${config_upper} ${_cuda_C_FLAGS})")
  1361. endif()
  1362. # Note that if we ever want CUDA_NVCC_FLAGS_<CONFIG> to be string (instead of a list
  1363. # like it is currently), we can remove the quotes around the
  1364. # ${CUDA_NVCC_FLAGS_${config_upper}} variable like the CMAKE_HOST_FLAGS_<CONFIG> variable.
  1365. string(APPEND _cuda_nvcc_flags_config "\nset(CUDA_NVCC_FLAGS_${config_upper} ${CUDA_NVCC_FLAGS_${config_upper}} ;; ${CUDA_WRAP_OPTION_NVCC_FLAGS_${config_upper}})")
  1366. endforeach()
  1367. # Process the C++11 flag. If the host sets the flag, we need to add it to nvcc and
  1368. # remove it from the host. This is because -Xcompile -std=c++ will choke nvcc (it uses
  1369. # the C preprocessor). In order to get this to work correctly, we need to use nvcc's
  1370. # specific c++11 flag.
  1371. if( "${_cuda_host_flags}" MATCHES "-std=c\\+\\+11")
  1372. # Add the c++11 flag to nvcc if it isn't already present. Note that we only look at
  1373. # the main flag instead of the configuration specific flags.
  1374. if( NOT "${CUDA_NVCC_FLAGS}" MATCHES "-std=c\\+\\+11" )
  1375. list(APPEND nvcc_flags --std c++11)
  1376. endif()
  1377. string(REGEX REPLACE "[-]+std=c\\+\\+11" "" _cuda_host_flags "${_cuda_host_flags}")
  1378. endif()
  1379. if(_cuda_build_shared_libs)
  1380. list(APPEND nvcc_flags "-D${cuda_target}_EXPORTS")
  1381. endif()
  1382. # Reset the output variable
  1383. set(_cuda_wrap_generated_files "")
  1384. # Iterate over the macro arguments and create custom
  1385. # commands for all the .cu files.
  1386. foreach(file ${_argn_list})
  1387. # Ignore any file marked as a HEADER_FILE_ONLY
  1388. get_source_file_property(_is_header ${file} HEADER_FILE_ONLY)
  1389. # Allow per source file overrides of the format. Also allows compiling non-.cu files.
  1390. get_source_file_property(_cuda_source_format ${file} CUDA_SOURCE_PROPERTY_FORMAT)
  1391. if((${file} MATCHES "\\.cu$" OR _cuda_source_format) AND NOT _is_header)
  1392. if(NOT _cuda_source_format)
  1393. set(_cuda_source_format ${format})
  1394. endif()
  1395. # If file isn't a .cu file, we need to tell nvcc to treat it as such.
  1396. if(NOT ${file} MATCHES "\\.cu$")
  1397. set(cuda_language_flag -x=cu)
  1398. else()
  1399. set(cuda_language_flag)
  1400. endif()
  1401. if( ${_cuda_source_format} MATCHES "OBJ")
  1402. set( cuda_compile_to_external_module OFF )
  1403. else()
  1404. set( cuda_compile_to_external_module ON )
  1405. if( ${_cuda_source_format} MATCHES "PTX" )
  1406. set( cuda_compile_to_external_module_type "ptx" )
  1407. elseif( ${_cuda_source_format} MATCHES "CUBIN")
  1408. set( cuda_compile_to_external_module_type "cubin" )
  1409. elseif( ${_cuda_source_format} MATCHES "FATBIN")
  1410. set( cuda_compile_to_external_module_type "fatbin" )
  1411. else()
  1412. message( FATAL_ERROR "Invalid format flag passed to CUDA_WRAP_SRCS or set with CUDA_SOURCE_PROPERTY_FORMAT file property for file '${file}': '${_cuda_source_format}'. Use OBJ, PTX, CUBIN or FATBIN.")
  1413. endif()
  1414. endif()
  1415. if(cuda_compile_to_external_module)
  1416. # Don't use any of the host compilation flags for PTX targets.
  1417. set(CUDA_HOST_FLAGS)
  1418. set(CUDA_NVCC_FLAGS_CONFIG)
  1419. else()
  1420. set(CUDA_HOST_FLAGS ${_cuda_host_flags})
  1421. set(CUDA_NVCC_FLAGS_CONFIG ${_cuda_nvcc_flags_config})
  1422. endif()
  1423. # Determine output directory
  1424. cuda_compute_build_path("${file}" cuda_build_path)
  1425. set(cuda_compile_intermediate_directory "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${cuda_target}.dir/${cuda_build_path}")
  1426. if(CUDA_GENERATED_OUTPUT_DIR)
  1427. set(cuda_compile_output_dir "${CUDA_GENERATED_OUTPUT_DIR}")
  1428. else()
  1429. if ( cuda_compile_to_external_module )
  1430. set(cuda_compile_output_dir "${CMAKE_CURRENT_BINARY_DIR}")
  1431. else()
  1432. set(cuda_compile_output_dir "${cuda_compile_intermediate_directory}")
  1433. endif()
  1434. endif()
  1435. # Add a custom target to generate a c or ptx file. ######################
  1436. get_filename_component( basename ${file} NAME )
  1437. if( cuda_compile_to_external_module )
  1438. set(generated_file_path "${cuda_compile_output_dir}")
  1439. set(generated_file_basename "${cuda_target}_generated_${basename}.${cuda_compile_to_external_module_type}")
  1440. set(format_flag "-${cuda_compile_to_external_module_type}")
  1441. file(MAKE_DIRECTORY "${cuda_compile_output_dir}")
  1442. else()
  1443. set(generated_file_path "${cuda_compile_output_dir}/${CMAKE_CFG_INTDIR}")
  1444. set(generated_file_basename "${cuda_target}_generated_${basename}${generated_extension}")
  1445. if(CUDA_SEPARABLE_COMPILATION)
  1446. set(format_flag "-dc")
  1447. else()
  1448. set(format_flag "-c")
  1449. endif()
  1450. endif()
  1451. # Set all of our file names. Make sure that whatever filenames that have
  1452. # generated_file_path in them get passed in through as a command line
  1453. # argument, so that the ${CMAKE_CFG_INTDIR} gets expanded at run time
  1454. # instead of configure time.
  1455. set(generated_file "${generated_file_path}/${generated_file_basename}")
  1456. set(cmake_dependency_file "${cuda_compile_intermediate_directory}/${generated_file_basename}.depend")
  1457. set(NVCC_generated_dependency_file "${cuda_compile_intermediate_directory}/${generated_file_basename}.NVCC-depend")
  1458. set(generated_cubin_file "${generated_file_path}/${generated_file_basename}.cubin.txt")
  1459. set(custom_target_script_pregen "${cuda_compile_intermediate_directory}/${generated_file_basename}.cmake.pre-gen")
  1460. set(custom_target_script "${cuda_compile_intermediate_directory}/${generated_file_basename}$<$<BOOL:$<CONFIG>>:.$<CONFIG>>.cmake")
  1461. # Setup properties for obj files:
  1462. if( NOT cuda_compile_to_external_module )
  1463. set_source_files_properties("${generated_file}"
  1464. PROPERTIES
  1465. EXTERNAL_OBJECT true # This is an object file not to be compiled, but only be linked.
  1466. )
  1467. endif()
  1468. # Don't add CMAKE_CURRENT_SOURCE_DIR if the path is already an absolute path.
  1469. get_filename_component(file_path "${file}" PATH)
  1470. if(IS_ABSOLUTE "${file_path}")
  1471. set(source_file "${file}")
  1472. else()
  1473. set(source_file "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
  1474. endif()
  1475. if( NOT cuda_compile_to_external_module AND CUDA_SEPARABLE_COMPILATION)
  1476. list(APPEND ${cuda_target}_SEPARABLE_COMPILATION_OBJECTS "${generated_file}")
  1477. endif()
  1478. # Bring in the dependencies. Creates a variable CUDA_NVCC_DEPEND #######
  1479. cuda_include_nvcc_dependencies(${cmake_dependency_file})
  1480. # Convenience string for output #########################################
  1481. if(CUDA_BUILD_EMULATION)
  1482. set(cuda_build_type "Emulation")
  1483. else()
  1484. set(cuda_build_type "Device")
  1485. endif()
  1486. # Build the NVCC made dependency file ###################################
  1487. set(build_cubin OFF)
  1488. if ( NOT CUDA_BUILD_EMULATION AND CUDA_BUILD_CUBIN )
  1489. if ( NOT cuda_compile_to_external_module )
  1490. set ( build_cubin ON )
  1491. endif()
  1492. endif()
  1493. # Configure the build script
  1494. configure_file("${CUDA_run_nvcc}" "${custom_target_script_pregen}" @ONLY)
  1495. file(GENERATE
  1496. OUTPUT "${custom_target_script}"
  1497. INPUT "${custom_target_script_pregen}"
  1498. )
  1499. # So if a user specifies the same cuda file as input more than once, you
  1500. # can have bad things happen with dependencies. Here we check an option
  1501. # to see if this is the behavior they want.
  1502. if(CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE)
  1503. set(main_dep MAIN_DEPENDENCY ${source_file})
  1504. else()
  1505. set(main_dep DEPENDS ${source_file})
  1506. endif()
  1507. if(CUDA_VERBOSE_BUILD)
  1508. set(verbose_output ON)
  1509. elseif(CMAKE_GENERATOR MATCHES "Makefiles")
  1510. set(verbose_output "$(VERBOSE)")
  1511. else()
  1512. set(verbose_output OFF)
  1513. endif()
  1514. # Create up the comment string
  1515. file(RELATIVE_PATH generated_file_relative_path "${CMAKE_BINARY_DIR}" "${generated_file}")
  1516. if(cuda_compile_to_external_module)
  1517. set(cuda_build_comment_string "Building NVCC ${cuda_compile_to_external_module_type} file ${generated_file_relative_path}")
  1518. else()
  1519. set(cuda_build_comment_string "Building NVCC (${cuda_build_type}) object ${generated_file_relative_path}")
  1520. endif()
  1521. set(_verbatim VERBATIM)
  1522. if(ccbin_flags MATCHES "\\$\\(VCInstallDir\\)")
  1523. set(_verbatim "")
  1524. endif()
  1525. # Build the generated file and dependency file ##########################
  1526. add_custom_command(
  1527. OUTPUT ${generated_file}
  1528. # These output files depend on the source_file and the contents of cmake_dependency_file
  1529. ${main_dep}
  1530. DEPENDS ${CUDA_NVCC_DEPEND}
  1531. DEPENDS ${custom_target_script}
  1532. # Make sure the output directory exists before trying to write to it.
  1533. COMMAND ${CMAKE_COMMAND} -E make_directory "${generated_file_path}"
  1534. COMMAND ${CMAKE_COMMAND} ARGS
  1535. -D verbose:BOOL=${verbose_output}
  1536. ${ccbin_flags}
  1537. -D build_configuration:STRING=${CUDA_build_configuration}
  1538. -D "generated_file:STRING=${generated_file}"
  1539. -D "generated_cubin_file:STRING=${generated_cubin_file}"
  1540. -P "${custom_target_script}"
  1541. WORKING_DIRECTORY "${cuda_compile_intermediate_directory}"
  1542. COMMENT "${cuda_build_comment_string}"
  1543. ${_verbatim}
  1544. )
  1545. # Make sure the build system knows the file is generated.
  1546. set_source_files_properties(${generated_file} PROPERTIES GENERATED TRUE)
  1547. list(APPEND _cuda_wrap_generated_files ${generated_file})
  1548. # Add the other files that we want cmake to clean on a cleanup ##########
  1549. list(APPEND CUDA_ADDITIONAL_CLEAN_FILES "${cmake_dependency_file}")
  1550. list(REMOVE_DUPLICATES CUDA_ADDITIONAL_CLEAN_FILES)
  1551. set(CUDA_ADDITIONAL_CLEAN_FILES ${CUDA_ADDITIONAL_CLEAN_FILES} CACHE INTERNAL "List of intermediate files that are part of the cuda dependency scanning.")
  1552. endif()
  1553. endforeach()
  1554. # Set the return parameter
  1555. set(${generated_files} ${_cuda_wrap_generated_files})
  1556. endmacro()
  1557. function(_cuda_get_important_host_flags important_flags flag_string)
  1558. if(CMAKE_GENERATOR MATCHES "Visual Studio")
  1559. string(REGEX MATCHALL "/M[DT][d]?" flags "${flag_string}")
  1560. list(APPEND ${important_flags} ${flags})
  1561. else()
  1562. string(REGEX MATCHALL "-fPIC" flags "${flag_string}")
  1563. list(APPEND ${important_flags} ${flags})
  1564. endif()
  1565. set(${important_flags} ${${important_flags}} PARENT_SCOPE)
  1566. endfunction()
  1567. ###############################################################################
  1568. ###############################################################################
  1569. # Separable Compilation Link
  1570. ###############################################################################
  1571. ###############################################################################
  1572. # Compute the filename to be used by CUDA_LINK_SEPARABLE_COMPILATION_OBJECTS
  1573. function(CUDA_COMPUTE_SEPARABLE_COMPILATION_OBJECT_FILE_NAME output_file_var cuda_target object_files)
  1574. if (object_files)
  1575. set(generated_extension ${CMAKE_${CUDA_C_OR_CXX}_OUTPUT_EXTENSION})
  1576. set(output_file "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${cuda_target}.dir/${CMAKE_CFG_INTDIR}/${cuda_target}_intermediate_link${generated_extension}")
  1577. else()
  1578. set(output_file)
  1579. endif()
  1580. set(${output_file_var} "${output_file}" PARENT_SCOPE)
  1581. endfunction()
  1582. # Setup the build rule for the separable compilation intermediate link file.
  1583. function(CUDA_LINK_SEPARABLE_COMPILATION_OBJECTS output_file cuda_target options object_files)
  1584. if (object_files)
  1585. set_source_files_properties("${output_file}"
  1586. PROPERTIES
  1587. EXTERNAL_OBJECT TRUE # This is an object file not to be compiled, but only
  1588. # be linked.
  1589. GENERATED TRUE # This file is generated during the build
  1590. )
  1591. # For now we are ignoring all the configuration specific flags.
  1592. set(nvcc_flags)
  1593. CUDA_PARSE_NVCC_OPTIONS(nvcc_flags ${options})
  1594. if(CUDA_64_BIT_DEVICE_CODE)
  1595. list(APPEND nvcc_flags -m64)
  1596. else()
  1597. list(APPEND nvcc_flags -m32)
  1598. endif()
  1599. # If -ccbin, --compiler-bindir has been specified, don't do anything. Otherwise add it here.
  1600. list( FIND nvcc_flags "-ccbin" ccbin_found0 )
  1601. list( FIND nvcc_flags "--compiler-bindir" ccbin_found1 )
  1602. if( ccbin_found0 LESS 0 AND ccbin_found1 LESS 0 AND CUDA_HOST_COMPILER )
  1603. # Match VERBATIM check below.
  1604. if(CUDA_HOST_COMPILER MATCHES "\\$\\(VCInstallDir\\)")
  1605. list(APPEND nvcc_flags -ccbin "\"${CUDA_HOST_COMPILER}\"")
  1606. else()
  1607. list(APPEND nvcc_flags -ccbin "${CUDA_HOST_COMPILER}")
  1608. endif()
  1609. endif()
  1610. # Create a list of flags specified by CUDA_NVCC_FLAGS_${CONFIG} and CMAKE_${CUDA_C_OR_CXX}_FLAGS*
  1611. set(config_specific_flags)
  1612. set(flags)
  1613. foreach(config ${CUDA_configuration_types})
  1614. string(TOUPPER ${config} config_upper)
  1615. # Add config specific flags
  1616. foreach(f ${CUDA_NVCC_FLAGS_${config_upper}})
  1617. list(APPEND config_specific_flags $<$<CONFIG:${config}>:${f}>)
  1618. endforeach()
  1619. set(important_host_flags)
  1620. _cuda_get_important_host_flags(important_host_flags "${CMAKE_${CUDA_C_OR_CXX}_FLAGS_${config_upper}}")
  1621. foreach(f ${important_host_flags})
  1622. list(APPEND flags $<$<CONFIG:${config}>:-Xcompiler> $<$<CONFIG:${config}>:${f}>)
  1623. endforeach()
  1624. endforeach()
  1625. # Add CMAKE_${CUDA_C_OR_CXX}_FLAGS
  1626. set(important_host_flags)
  1627. _cuda_get_important_host_flags(important_host_flags "${CMAKE_${CUDA_C_OR_CXX}_FLAGS}")
  1628. foreach(f ${important_host_flags})
  1629. list(APPEND flags -Xcompiler ${f})
  1630. endforeach()
  1631. # Add our general CUDA_NVCC_FLAGS with the configuration specifig flags
  1632. set(nvcc_flags ${CUDA_NVCC_FLAGS} ${config_specific_flags} ${nvcc_flags})
  1633. file(RELATIVE_PATH output_file_relative_path "${CMAKE_BINARY_DIR}" "${output_file}")
  1634. # Some generators don't handle the multiple levels of custom command
  1635. # dependencies correctly (obj1 depends on file1, obj2 depends on obj1), so
  1636. # we work around that issue by compiling the intermediate link object as a
  1637. # pre-link custom command in that situation.
  1638. set(do_obj_build_rule TRUE)
  1639. if (MSVC_VERSION GREATER 1599 AND MSVC_VERSION LESS 1800)
  1640. # VS 2010 and 2012 have this problem.
  1641. set(do_obj_build_rule FALSE)
  1642. endif()
  1643. set(_verbatim VERBATIM)
  1644. if(nvcc_flags MATCHES "\\$\\(VCInstallDir\\)")
  1645. set(_verbatim "")
  1646. endif()
  1647. if (do_obj_build_rule)
  1648. add_custom_command(
  1649. OUTPUT ${output_file}
  1650. DEPENDS ${object_files}
  1651. COMMAND ${CUDA_NVCC_EXECUTABLE} ${nvcc_flags} -dlink ${object_files} -o ${output_file}
  1652. ${flags}
  1653. COMMENT "Building NVCC intermediate link file ${output_file_relative_path}"
  1654. COMMAND_EXPAND_LISTS
  1655. ${_verbatim}
  1656. )
  1657. else()
  1658. get_filename_component(output_file_dir "${output_file}" DIRECTORY)
  1659. add_custom_command(
  1660. TARGET ${cuda_target}
  1661. PRE_LINK
  1662. COMMAND ${CMAKE_COMMAND} -E echo "Building NVCC intermediate link file ${output_file_relative_path}"
  1663. COMMAND ${CMAKE_COMMAND} -E make_directory "${output_file_dir}"
  1664. COMMAND ${CUDA_NVCC_EXECUTABLE} ${nvcc_flags} ${flags} -dlink ${object_files} -o "${output_file}"
  1665. COMMAND_EXPAND_LISTS
  1666. ${_verbatim}
  1667. )
  1668. endif()
  1669. endif()
  1670. endfunction()
  1671. ###############################################################################
  1672. ###############################################################################
  1673. # ADD LIBRARY
  1674. ###############################################################################
  1675. ###############################################################################
  1676. macro(CUDA_ADD_LIBRARY cuda_target)
  1677. CUDA_ADD_CUDA_INCLUDE_ONCE()
  1678. # Separate the sources from the options
  1679. CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
  1680. CUDA_BUILD_SHARED_LIBRARY(_cuda_shared_flag ${ARGN})
  1681. # Create custom commands and targets for each file.
  1682. CUDA_WRAP_SRCS( ${cuda_target} OBJ _generated_files ${_sources}
  1683. ${_cmake_options} ${_cuda_shared_flag}
  1684. OPTIONS ${_options} )
  1685. # Compute the file name of the intermedate link file used for separable
  1686. # compilation.
  1687. CUDA_COMPUTE_SEPARABLE_COMPILATION_OBJECT_FILE_NAME(link_file ${cuda_target} "${${cuda_target}_SEPARABLE_COMPILATION_OBJECTS}")
  1688. # Add the library.
  1689. add_library(${cuda_target} ${_cmake_options}
  1690. ${_generated_files}
  1691. ${_sources}
  1692. ${link_file}
  1693. )
  1694. # Add a link phase for the separable compilation if it has been enabled. If
  1695. # it has been enabled then the ${cuda_target}_SEPARABLE_COMPILATION_OBJECTS
  1696. # variable will have been defined.
  1697. CUDA_LINK_SEPARABLE_COMPILATION_OBJECTS("${link_file}" ${cuda_target} "${_options}" "${${cuda_target}_SEPARABLE_COMPILATION_OBJECTS}")
  1698. target_link_libraries(${cuda_target} ${CUDA_LINK_LIBRARIES_KEYWORD}
  1699. ${CUDA_LIBRARIES}
  1700. )
  1701. if(CUDA_SEPARABLE_COMPILATION)
  1702. target_link_libraries(${cuda_target} ${CUDA_LINK_LIBRARIES_KEYWORD}
  1703. ${CUDA_cudadevrt_LIBRARY}
  1704. )
  1705. endif()
  1706. # We need to set the linker language based on what the expected generated file
  1707. # would be. CUDA_C_OR_CXX is computed based on CUDA_HOST_COMPILATION_CPP.
  1708. set_target_properties(${cuda_target}
  1709. PROPERTIES
  1710. LINKER_LANGUAGE ${CUDA_C_OR_CXX}
  1711. )
  1712. endmacro()
  1713. ###############################################################################
  1714. ###############################################################################
  1715. # ADD EXECUTABLE
  1716. ###############################################################################
  1717. ###############################################################################
  1718. macro(CUDA_ADD_EXECUTABLE cuda_target)
  1719. CUDA_ADD_CUDA_INCLUDE_ONCE()
  1720. # Separate the sources from the options
  1721. CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
  1722. # Create custom commands and targets for each file.
  1723. CUDA_WRAP_SRCS( ${cuda_target} OBJ _generated_files ${_sources} OPTIONS ${_options} )
  1724. # Compute the file name of the intermedate link file used for separable
  1725. # compilation.
  1726. CUDA_COMPUTE_SEPARABLE_COMPILATION_OBJECT_FILE_NAME(link_file ${cuda_target} "${${cuda_target}_SEPARABLE_COMPILATION_OBJECTS}")
  1727. # Add the library.
  1728. add_executable(${cuda_target} ${_cmake_options}
  1729. ${_generated_files}
  1730. ${_sources}
  1731. ${link_file}
  1732. )
  1733. # Add a link phase for the separable compilation if it has been enabled. If
  1734. # it has been enabled then the ${cuda_target}_SEPARABLE_COMPILATION_OBJECTS
  1735. # variable will have been defined.
  1736. CUDA_LINK_SEPARABLE_COMPILATION_OBJECTS("${link_file}" ${cuda_target} "${_options}" "${${cuda_target}_SEPARABLE_COMPILATION_OBJECTS}")
  1737. target_link_libraries(${cuda_target} ${CUDA_LINK_LIBRARIES_KEYWORD}
  1738. ${CUDA_LIBRARIES}
  1739. )
  1740. # We need to set the linker language based on what the expected generated file
  1741. # would be. CUDA_C_OR_CXX is computed based on CUDA_HOST_COMPILATION_CPP.
  1742. set_target_properties(${cuda_target}
  1743. PROPERTIES
  1744. LINKER_LANGUAGE ${CUDA_C_OR_CXX}
  1745. )
  1746. endmacro()
  1747. ###############################################################################
  1748. ###############################################################################
  1749. # (Internal) helper for manually added cuda source files with specific targets
  1750. ###############################################################################
  1751. ###############################################################################
  1752. macro(cuda_compile_base cuda_target format generated_files)
  1753. # Update a counter in this directory, to keep phony target names unique.
  1754. set(_cuda_target "${cuda_target}")
  1755. get_property(_counter DIRECTORY PROPERTY _cuda_internal_phony_counter)
  1756. if(_counter)
  1757. math(EXPR _counter "${_counter} + 1")
  1758. else()
  1759. set(_counter 1)
  1760. endif()
  1761. string(APPEND _cuda_target "_${_counter}")
  1762. set_property(DIRECTORY PROPERTY _cuda_internal_phony_counter ${_counter})
  1763. # Separate the sources from the options
  1764. CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
  1765. # Create custom commands and targets for each file.
  1766. CUDA_WRAP_SRCS( ${_cuda_target} ${format} _generated_files ${_sources}
  1767. ${_cmake_options} OPTIONS ${_options} PHONY)
  1768. set( ${generated_files} ${_generated_files})
  1769. endmacro()
  1770. ###############################################################################
  1771. ###############################################################################
  1772. # CUDA COMPILE
  1773. ###############################################################################
  1774. ###############################################################################
  1775. macro(CUDA_COMPILE generated_files)
  1776. cuda_compile_base(cuda_compile OBJ ${generated_files} ${ARGN})
  1777. endmacro()
  1778. ###############################################################################
  1779. ###############################################################################
  1780. # CUDA COMPILE PTX
  1781. ###############################################################################
  1782. ###############################################################################
  1783. macro(CUDA_COMPILE_PTX generated_files)
  1784. cuda_compile_base(cuda_compile_ptx PTX ${generated_files} ${ARGN})
  1785. endmacro()
  1786. ###############################################################################
  1787. ###############################################################################
  1788. # CUDA COMPILE FATBIN
  1789. ###############################################################################
  1790. ###############################################################################
  1791. macro(CUDA_COMPILE_FATBIN generated_files)
  1792. cuda_compile_base(cuda_compile_fatbin FATBIN ${generated_files} ${ARGN})
  1793. endmacro()
  1794. ###############################################################################
  1795. ###############################################################################
  1796. # CUDA COMPILE CUBIN
  1797. ###############################################################################
  1798. ###############################################################################
  1799. macro(CUDA_COMPILE_CUBIN generated_files)
  1800. cuda_compile_base(cuda_compile_cubin CUBIN ${generated_files} ${ARGN})
  1801. endmacro()
  1802. ###############################################################################
  1803. ###############################################################################
  1804. # CUDA ADD CUFFT TO TARGET
  1805. ###############################################################################
  1806. ###############################################################################
  1807. macro(CUDA_ADD_CUFFT_TO_TARGET target)
  1808. if (CUDA_BUILD_EMULATION)
  1809. target_link_libraries(${target} ${CUDA_LINK_LIBRARIES_KEYWORD} ${CUDA_cufftemu_LIBRARY})
  1810. else()
  1811. target_link_libraries(${target} ${CUDA_LINK_LIBRARIES_KEYWORD} ${CUDA_cufft_LIBRARY})
  1812. endif()
  1813. endmacro()
  1814. ###############################################################################
  1815. ###############################################################################
  1816. # CUDA ADD CUBLAS TO TARGET
  1817. ###############################################################################
  1818. ###############################################################################
  1819. macro(CUDA_ADD_CUBLAS_TO_TARGET target)
  1820. if (CUDA_BUILD_EMULATION)
  1821. target_link_libraries(${target} ${CUDA_LINK_LIBRARIES_KEYWORD} ${CUDA_cublasemu_LIBRARY})
  1822. else()
  1823. target_link_libraries(${target} ${CUDA_LINK_LIBRARIES_KEYWORD} ${CUDA_cublas_LIBRARY} ${CUDA_cublas_device_LIBRARY})
  1824. endif()
  1825. endmacro()
  1826. ###############################################################################
  1827. ###############################################################################
  1828. # CUDA BUILD CLEAN TARGET
  1829. ###############################################################################
  1830. ###############################################################################
  1831. macro(CUDA_BUILD_CLEAN_TARGET)
  1832. # Call this after you add all your CUDA targets, and you will get a
  1833. # convenience target. You should also make clean after running this target
  1834. # to get the build system to generate all the code again.
  1835. set(cuda_clean_target_name clean_cuda_depends)
  1836. if (CMAKE_GENERATOR MATCHES "Visual Studio")
  1837. string(TOUPPER ${cuda_clean_target_name} cuda_clean_target_name)
  1838. endif()
  1839. add_custom_target(${cuda_clean_target_name}
  1840. COMMAND ${CMAKE_COMMAND} -E rm -f ${CUDA_ADDITIONAL_CLEAN_FILES})
  1841. # Clear out the variable, so the next time we configure it will be empty.
  1842. # This is useful so that the files won't persist in the list after targets
  1843. # have been removed.
  1844. set(CUDA_ADDITIONAL_CLEAN_FILES "" CACHE INTERNAL "List of intermediate files that are part of the cuda dependency scanning.")
  1845. endmacro()