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