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