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