Imported Upstream version 2.8.10.2
[platform/upstream/cmake.git] / Modules / FindCUDA.cmake
1 # - Tools for building CUDA C files: libraries and build dependencies.
2 # This script locates the NVIDIA CUDA C tools. It should work on linux, windows,
3 # and mac and should be reasonably up to date with CUDA C releases.
4 #
5 # This script makes use of the standard find_package arguments of <VERSION>,
6 # REQUIRED and QUIET.  CUDA_FOUND will report if an acceptable version of CUDA
7 # was found.
8 #
9 # The script will prompt the user to specify CUDA_TOOLKIT_ROOT_DIR if the prefix
10 # cannot be determined by the location of nvcc in the system path and REQUIRED
11 # is specified to find_package(). To use a different installed version of the
12 # toolkit set the environment variable CUDA_BIN_PATH before running cmake
13 # (e.g. CUDA_BIN_PATH=/usr/local/cuda1.0 instead of the default /usr/local/cuda)
14 # or set CUDA_TOOLKIT_ROOT_DIR after configuring.  If you change the value of
15 # CUDA_TOOLKIT_ROOT_DIR, various components that depend on the path will be
16 # relocated.
17 #
18 # It might be necessary to set CUDA_TOOLKIT_ROOT_DIR manually on certain
19 # platforms, or to use a cuda runtime not installed in the default location. In
20 # newer versions of the toolkit the cuda library is included with the graphics
21 # driver- be sure that the driver version matches what is needed by the cuda
22 # runtime version.
23 #
24 # The following variables affect the behavior of the macros in the script (in
25 # alphebetical order).  Note that any of these flags can be changed multiple
26 # times in the same directory before calling CUDA_ADD_EXECUTABLE,
27 # CUDA_ADD_LIBRARY, CUDA_COMPILE, CUDA_COMPILE_PTX or CUDA_WRAP_SRCS.
28 #
29 #  CUDA_64_BIT_DEVICE_CODE (Default matches host bit size)
30 #  -- Set to ON to compile for 64 bit device code, OFF for 32 bit device code.
31 #     Note that making this different from the host code when generating object
32 #     or C files from CUDA code just won't work, because size_t gets defined by
33 #     nvcc in the generated source.  If you compile to PTX and then load the
34 #     file yourself, you can mix bit sizes between device and host.
35 #
36 #  CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE (Default ON)
37 #  -- Set to ON if you want the custom build rule to be attached to the source
38 #     file in Visual Studio.  Turn OFF if you add the same cuda file to multiple
39 #     targets.
40 #
41 #     This allows the user to build the target from the CUDA file; however, bad
42 #     things can happen if the CUDA source file is added to multiple targets.
43 #     When performing parallel builds it is possible for the custom build
44 #     command to be run more than once and in parallel causing cryptic build
45 #     errors.  VS runs the rules for every source file in the target, and a
46 #     source can have only one rule no matter how many projects it is added to.
47 #     When the rule is run from multiple targets race conditions can occur on
48 #     the generated file.  Eventually everything will get built, but if the user
49 #     is unaware of this behavior, there may be confusion.  It would be nice if
50 #     this script could detect the reuse of source files across multiple targets
51 #     and turn the option off for the user, but no good solution could be found.
52 #
53 #  CUDA_BUILD_CUBIN (Default OFF)
54 #  -- Set to ON to enable and extra compilation pass with the -cubin option in
55 #     Device mode. The output is parsed and register, shared memory usage is
56 #     printed during build.
57 #
58 #  CUDA_BUILD_EMULATION (Default OFF for device mode)
59 #  -- Set to ON for Emulation mode. -D_DEVICEEMU is defined for CUDA C files
60 #     when CUDA_BUILD_EMULATION is TRUE.
61 #
62 #  CUDA_GENERATED_OUTPUT_DIR (Default CMAKE_CURRENT_BINARY_DIR)
63 #  -- Set to the path you wish to have the generated files placed.  If it is
64 #     blank output files will be placed in CMAKE_CURRENT_BINARY_DIR.
65 #     Intermediate files will always be placed in
66 #     CMAKE_CURRENT_BINARY_DIR/CMakeFiles.
67 #
68 #  CUDA_HOST_COMPILATION_CPP (Default ON)
69 #  -- Set to OFF for C compilation of host code.
70 #
71 #  CUDA_HOST_COMPILER (Default CMAKE_C_COMPILER, $(VCInstallDir)/bin for VS)
72 #  -- Set the host compiler to be used by nvcc.  Ignored if -ccbin or
73 #     --compiler-bindir is already present in the CUDA_NVCC_FLAGS or
74 #     CUDA_NVCC_FLAGS_<CONFIG> variables.  For Visual Studio targets
75 #     $(VCInstallDir)/bin is a special value that expands out to the path when
76 #     the command is run from withing VS.
77 #
78 #  CUDA_NVCC_FLAGS
79 #  CUDA_NVCC_FLAGS_<CONFIG>
80 #  -- Additional NVCC command line arguments.  NOTE: multiple arguments must be
81 #     semi-colon delimited (e.g. --compiler-options;-Wall)
82 #
83 #  CUDA_PROPAGATE_HOST_FLAGS (Default ON)
84 #  -- Set to ON to propagate CMAKE_{C,CXX}_FLAGS and their configuration
85 #     dependent counterparts (e.g. CMAKE_C_FLAGS_DEBUG) automatically to the
86 #     host compiler through nvcc's -Xcompiler flag.  This helps make the
87 #     generated host code match the rest of the system better.  Sometimes
88 #     certain flags give nvcc problems, and this will help you turn the flag
89 #     propagation off.  This does not affect the flags supplied directly to nvcc
90 #     via CUDA_NVCC_FLAGS or through the OPTION flags specified through
91 #     CUDA_ADD_LIBRARY, CUDA_ADD_EXECUTABLE, or CUDA_WRAP_SRCS.  Flags used for
92 #     shared library compilation are not affected by this flag.
93 #
94 #  CUDA_VERBOSE_BUILD (Default OFF)
95 #  -- Set to ON to see all the commands used when building the CUDA file.  When
96 #     using a Makefile generator the value defaults to VERBOSE (run make
97 #     VERBOSE=1 to see output), although setting CUDA_VERBOSE_BUILD to ON will
98 #     always print the output.
99 #
100 # The script creates the following macros (in alphebetical order):
101 #
102 #  CUDA_ADD_CUFFT_TO_TARGET( cuda_target )
103 #  -- Adds the cufft library to the target (can be any target).  Handles whether
104 #     you are in emulation mode or not.
105 #
106 #  CUDA_ADD_CUBLAS_TO_TARGET( cuda_target )
107 #  -- Adds the cublas library to the target (can be any target).  Handles
108 #     whether you are in emulation mode or not.
109 #
110 #  CUDA_ADD_EXECUTABLE( cuda_target file0 file1 ...
111 #                       [WIN32] [MACOSX_BUNDLE] [EXCLUDE_FROM_ALL] [OPTIONS ...] )
112 #  -- Creates an executable "cuda_target" which is made up of the files
113 #     specified.  All of the non CUDA C files are compiled using the standard
114 #     build rules specified by CMAKE and the cuda files are compiled to object
115 #     files using nvcc and the host compiler.  In addition CUDA_INCLUDE_DIRS is
116 #     added automatically to include_directories().  Some standard CMake target
117 #     calls can be used on the target after calling this macro
118 #     (e.g. set_target_properties and target_link_libraries), but setting
119 #     properties that adjust compilation flags will not affect code compiled by
120 #     nvcc.  Such flags should be modified before calling CUDA_ADD_EXECUTABLE,
121 #     CUDA_ADD_LIBRARY or CUDA_WRAP_SRCS.
122 #
123 #  CUDA_ADD_LIBRARY( cuda_target file0 file1 ...
124 #                    [STATIC | SHARED | MODULE] [EXCLUDE_FROM_ALL] [OPTIONS ...] )
125 #  -- Same as CUDA_ADD_EXECUTABLE except that a library is created.
126 #
127 #  CUDA_BUILD_CLEAN_TARGET()
128 #  -- Creates a convience target that deletes all the dependency files
129 #     generated.  You should make clean after running this target to ensure the
130 #     dependency files get regenerated.
131 #
132 #  CUDA_COMPILE( generated_files file0 file1 ... [STATIC | SHARED | MODULE]
133 #                [OPTIONS ...] )
134 #  -- Returns a list of generated files from the input source files to be used
135 #     with ADD_LIBRARY or ADD_EXECUTABLE.
136 #
137 #  CUDA_COMPILE_PTX( generated_files file0 file1 ... [OPTIONS ...] )
138 #  -- Returns a list of PTX files generated from the input source files.
139 #
140 #  CUDA_INCLUDE_DIRECTORIES( path0 path1 ... )
141 #  -- Sets the directories that should be passed to nvcc
142 #     (e.g. nvcc -Ipath0 -Ipath1 ... ). These paths usually contain other .cu
143 #     files.
144 #
145 #  CUDA_WRAP_SRCS ( cuda_target format generated_files file0 file1 ...
146 #                   [STATIC | SHARED | MODULE] [OPTIONS ...] )
147 #  -- This is where all the magic happens.  CUDA_ADD_EXECUTABLE,
148 #     CUDA_ADD_LIBRARY, CUDA_COMPILE, and CUDA_COMPILE_PTX all call this
149 #     function under the hood.
150 #
151 #     Given the list of files (file0 file1 ... fileN) this macro generates
152 #     custom commands that generate either PTX or linkable objects (use "PTX" or
153 #     "OBJ" for the format argument to switch).  Files that don't end with .cu
154 #     or have the HEADER_FILE_ONLY property are ignored.
155 #
156 #     The arguments passed in after OPTIONS are extra command line options to
157 #     give to nvcc.  You can also specify per configuration options by
158 #     specifying the name of the configuration followed by the options.  General
159 #     options must preceed configuration specific options.  Not all
160 #     configurations need to be specified, only the ones provided will be used.
161 #
162 #        OPTIONS -DFLAG=2 "-DFLAG_OTHER=space in flag"
163 #        DEBUG -g
164 #        RELEASE --use_fast_math
165 #        RELWITHDEBINFO --use_fast_math;-g
166 #        MINSIZEREL --use_fast_math
167 #
168 #     For certain configurations (namely VS generating object files with
169 #     CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE set to ON), no generated file will
170 #     be produced for the given cuda file.  This is because when you add the
171 #     cuda file to Visual Studio it knows that this file produces an object file
172 #     and will link in the resulting object file automatically.
173 #
174 #     This script will also generate a separate cmake script that is used at
175 #     build time to invoke nvcc.  This is for several reasons.
176 #
177 #       1. nvcc can return negative numbers as return values which confuses
178 #       Visual Studio into thinking that the command succeeded.  The script now
179 #       checks the error codes and produces errors when there was a problem.
180 #
181 #       2. nvcc has been known to not delete incomplete results when it
182 #       encounters problems.  This confuses build systems into thinking the
183 #       target was generated when in fact an unusable file exists.  The script
184 #       now deletes the output files if there was an error.
185 #
186 #       3. By putting all the options that affect the build into a file and then
187 #       make the build rule dependent on the file, the output files will be
188 #       regenerated when the options change.
189 #
190 #     This script also looks at optional arguments STATIC, SHARED, or MODULE to
191 #     determine when to target the object compilation for a shared library.
192 #     BUILD_SHARED_LIBS is ignored in CUDA_WRAP_SRCS, but it is respected in
193 #     CUDA_ADD_LIBRARY.  On some systems special flags are added for building
194 #     objects intended for shared libraries.  A preprocessor macro,
195 #     <target_name>_EXPORTS is defined when a shared library compilation is
196 #     detected.
197 #
198 #     Flags passed into add_definitions with -D or /D are passed along to nvcc.
199 #
200 # The script defines the following variables:
201 #
202 #  CUDA_VERSION_MAJOR    -- The major version of cuda as reported by nvcc.
203 #  CUDA_VERSION_MINOR    -- The minor version.
204 #  CUDA_VERSION
205 #  CUDA_VERSION_STRING   -- CUDA_VERSION_MAJOR.CUDA_VERSION_MINOR
206 #
207 #  CUDA_TOOLKIT_ROOT_DIR -- Path to the CUDA Toolkit (defined if not set).
208 #  CUDA_SDK_ROOT_DIR     -- Path to the CUDA SDK.  Use this to find files in the
209 #                           SDK.  This script will not directly support finding
210 #                           specific libraries or headers, as that isn't
211 #                           supported by NVIDIA.  If you want to change
212 #                           libraries when the path changes see the
213 #                           FindCUDA.cmake script for an example of how to clear
214 #                           these variables.  There are also examples of how to
215 #                           use the CUDA_SDK_ROOT_DIR to locate headers or
216 #                           libraries, if you so choose (at your own risk).
217 #  CUDA_INCLUDE_DIRS     -- Include directory for cuda headers.  Added automatically
218 #                           for CUDA_ADD_EXECUTABLE and CUDA_ADD_LIBRARY.
219 #  CUDA_LIBRARIES        -- Cuda RT library.
220 #  CUDA_CUFFT_LIBRARIES  -- Device or emulation library for the Cuda FFT
221 #                           implementation (alternative to:
222 #                           CUDA_ADD_CUFFT_TO_TARGET macro)
223 #  CUDA_CUBLAS_LIBRARIES -- Device or emulation library for the Cuda BLAS
224 #                           implementation (alterative to:
225 #                           CUDA_ADD_CUBLAS_TO_TARGET macro).
226 #  CUDA_curand_LIBRARY   -- CUDA Random Number Generation library.
227 #                           Only available for CUDA version 3.2+.
228 #  CUDA_cusparse_LIBRARY -- CUDA Sparse Matrix library.
229 #                           Only available for CUDA version 3.2+.
230 #  CUDA_npp_LIBRARY      -- NVIDIA Performance Primitives library.
231 #                           Only available for CUDA version 4.0+.
232 #  CUDA_nvcuvenc_LIBRARY -- CUDA Video Encoder library.
233 #                           Only available for CUDA version 3.2+.
234 #                           Windows only.
235 #  CUDA_nvcuvid_LIBRARY  -- CUDA Video Decoder library.
236 #                           Only available for CUDA version 3.2+.
237 #                           Windows only.
238 #
239 #
240 #  James Bigler, NVIDIA Corp (nvidia.com - jbigler)
241 #  Abe Stephens, SCI Institute -- http://www.sci.utah.edu/~abe/FindCuda.html
242 #
243 #  Copyright (c) 2008 - 2009 NVIDIA Corporation.  All rights reserved.
244 #
245 #  Copyright (c) 2007-2009
246 #  Scientific Computing and Imaging Institute, University of Utah
247 #
248 #  This code is licensed under the MIT License.  See the FindCUDA.cmake script
249 #  for the text of the license.
250
251 # The MIT License
252 #
253 # License for the specific language governing rights and limitations under
254 # Permission is hereby granted, free of charge, to any person obtaining a
255 # copy of this software and associated documentation files (the "Software"),
256 # to deal in the Software without restriction, including without limitation
257 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
258 # and/or sell copies of the Software, and to permit persons to whom the
259 # Software is furnished to do so, subject to the following conditions:
260 #
261 # The above copyright notice and this permission notice shall be included
262 # in all copies or substantial portions of the Software.
263 #
264 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
265 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
266 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
267 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
268 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
269 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
270 # DEALINGS IN THE SOFTWARE.
271 #
272 ###############################################################################
273
274 # FindCUDA.cmake
275
276 # We need to have at least this version to support the VERSION_LESS argument to 'if' (2.6.2) and unset (2.6.3)
277 cmake_policy(PUSH)
278 cmake_minimum_required(VERSION 2.6.3)
279 cmake_policy(POP)
280
281 # This macro helps us find the location of helper files we will need the full path to
282 macro(CUDA_FIND_HELPER_FILE _name _extension)
283   set(_full_name "${_name}.${_extension}")
284   # CMAKE_CURRENT_LIST_FILE contains the full path to the file currently being
285   # processed.  Using this variable, we can pull out the current path, and
286   # provide a way to get access to the other files we need local to here.
287   get_filename_component(CMAKE_CURRENT_LIST_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
288   set(CUDA_${_name} "${CMAKE_CURRENT_LIST_DIR}/FindCUDA/${_full_name}")
289   if(NOT EXISTS "${CUDA_${_name}}")
290     set(error_message "${_full_name} not found in ${CMAKE_CURRENT_LIST_DIR}/FindCUDA")
291     if(CUDA_FIND_REQUIRED)
292       message(FATAL_ERROR "${error_message}")
293     else()
294       if(NOT CUDA_FIND_QUIETLY)
295         message(STATUS "${error_message}")
296       endif()
297     endif()
298   endif()
299   # Set this variable as internal, so the user isn't bugged with it.
300   set(CUDA_${_name} ${CUDA_${_name}} CACHE INTERNAL "Location of ${_full_name}" FORCE)
301 endmacro()
302
303 #####################################################################
304 ## CUDA_INCLUDE_NVCC_DEPENDENCIES
305 ##
306
307 # So we want to try and include the dependency file if it exists.  If
308 # it doesn't exist then we need to create an empty one, so we can
309 # include it.
310
311 # If it does exist, then we need to check to see if all the files it
312 # depends on exist.  If they don't then we should clear the dependency
313 # file and regenerate it later.  This covers the case where a header
314 # file has disappeared or moved.
315
316 macro(CUDA_INCLUDE_NVCC_DEPENDENCIES dependency_file)
317   set(CUDA_NVCC_DEPEND)
318   set(CUDA_NVCC_DEPEND_REGENERATE FALSE)
319
320
321   # Include the dependency file.  Create it first if it doesn't exist .  The
322   # INCLUDE puts a dependency that will force CMake to rerun and bring in the
323   # new info when it changes.  DO NOT REMOVE THIS (as I did and spent a few
324   # hours figuring out why it didn't work.
325   if(NOT EXISTS ${dependency_file})
326     file(WRITE ${dependency_file} "#FindCUDA.cmake generated file.  Do not edit.\n")
327   endif()
328   # Always include this file to force CMake to run again next
329   # invocation and rebuild the dependencies.
330   #message("including dependency_file = ${dependency_file}")
331   include(${dependency_file})
332
333   # Now we need to verify the existence of all the included files
334   # here.  If they aren't there we need to just blank this variable and
335   # make the file regenerate again.
336 #   if(DEFINED CUDA_NVCC_DEPEND)
337 #     message("CUDA_NVCC_DEPEND set")
338 #   else()
339 #     message("CUDA_NVCC_DEPEND NOT set")
340 #   endif()
341   if(CUDA_NVCC_DEPEND)
342     #message("CUDA_NVCC_DEPEND found")
343     foreach(f ${CUDA_NVCC_DEPEND})
344       # message("searching for ${f}")
345       if(NOT EXISTS ${f})
346         #message("file ${f} not found")
347         set(CUDA_NVCC_DEPEND_REGENERATE TRUE)
348       endif()
349     endforeach()
350   else()
351     #message("CUDA_NVCC_DEPEND false")
352     # No dependencies, so regenerate the file.
353     set(CUDA_NVCC_DEPEND_REGENERATE TRUE)
354   endif()
355
356   #message("CUDA_NVCC_DEPEND_REGENERATE = ${CUDA_NVCC_DEPEND_REGENERATE}")
357   # No incoming dependencies, so we need to generate them.  Make the
358   # output depend on the dependency file itself, which should cause the
359   # rule to re-run.
360   if(CUDA_NVCC_DEPEND_REGENERATE)
361     set(CUDA_NVCC_DEPEND ${dependency_file})
362     #message("Generating an empty dependency_file: ${dependency_file}")
363     file(WRITE ${dependency_file} "#FindCUDA.cmake generated file.  Do not edit.\n")
364   endif()
365
366 endmacro()
367
368 ###############################################################################
369 ###############################################################################
370 # Setup variables' defaults
371 ###############################################################################
372 ###############################################################################
373
374 # Allow the user to specify if the device code is supposed to be 32 or 64 bit.
375 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
376   set(CUDA_64_BIT_DEVICE_CODE_DEFAULT ON)
377 else()
378   set(CUDA_64_BIT_DEVICE_CODE_DEFAULT OFF)
379 endif()
380 option(CUDA_64_BIT_DEVICE_CODE "Compile device code in 64 bit mode" ${CUDA_64_BIT_DEVICE_CODE_DEFAULT})
381
382 # Attach the build rule to the source file in VS.  This option
383 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)
384
385 # Prints out extra information about the cuda file during compilation
386 option(CUDA_BUILD_CUBIN "Generate and parse .cubin files in Device mode." OFF)
387
388 # Set whether we are using emulation or device mode.
389 option(CUDA_BUILD_EMULATION "Build in Emulation mode" OFF)
390
391 # Where to put the generated output.
392 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")
393
394 # Parse HOST_COMPILATION mode.
395 option(CUDA_HOST_COMPILATION_CPP "Generated file extension" ON)
396
397 # Extra user settable flags
398 set(CUDA_NVCC_FLAGS "" CACHE STRING "Semi-colon delimit multiple arguments.")
399
400 if(CMAKE_GENERATOR MATCHES "Visual Studio")
401   set(CUDA_HOST_COMPILER "$(VCInstallDir)bin" CACHE FILEPATH "Host side compiler used by NVCC")
402 else()
403   set(CUDA_HOST_COMPILER "${CMAKE_C_COMPILER}" CACHE FILEPATH "Host side compiler used by NVCC")
404 endif()
405
406 # Propagate the host flags to the host compiler via -Xcompiler
407 option(CUDA_PROPAGATE_HOST_FLAGS "Propage C/CXX_FLAGS and friends to the host compiler via -Xcompile" ON)
408
409 # Specifies whether the commands used when compiling the .cu file will be printed out.
410 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)
411
412 mark_as_advanced(
413   CUDA_64_BIT_DEVICE_CODE
414   CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE
415   CUDA_GENERATED_OUTPUT_DIR
416   CUDA_HOST_COMPILATION_CPP
417   CUDA_NVCC_FLAGS
418   CUDA_PROPAGATE_HOST_FLAGS
419   )
420
421 # Makefile and similar generators don't define CMAKE_CONFIGURATION_TYPES, so we
422 # need to add another entry for the CMAKE_BUILD_TYPE.  We also need to add the
423 # standerd set of 4 build types (Debug, MinSizeRel, Release, and RelWithDebInfo)
424 # for completeness.  We need run this loop in order to accomodate the addition
425 # of extra configuration types.  Duplicate entries will be removed by
426 # REMOVE_DUPLICATES.
427 set(CUDA_configuration_types ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE} Debug MinSizeRel Release RelWithDebInfo)
428 list(REMOVE_DUPLICATES CUDA_configuration_types)
429 foreach(config ${CUDA_configuration_types})
430     string(TOUPPER ${config} config_upper)
431     set(CUDA_NVCC_FLAGS_${config_upper} "" CACHE STRING "Semi-colon delimit multiple arguments.")
432     mark_as_advanced(CUDA_NVCC_FLAGS_${config_upper})
433 endforeach()
434
435 ###############################################################################
436 ###############################################################################
437 # Locate CUDA, Set Build Type, etc.
438 ###############################################################################
439 ###############################################################################
440
441 # Check to see if the CUDA_TOOLKIT_ROOT_DIR and CUDA_SDK_ROOT_DIR have changed,
442 # if they have then clear the cache variables, so that will be detected again.
443 if(NOT "${CUDA_TOOLKIT_ROOT_DIR}" STREQUAL "${CUDA_TOOLKIT_ROOT_DIR_INTERNAL}")
444   unset(CUDA_NVCC_EXECUTABLE CACHE)
445   unset(CUDA_TOOLKIT_INCLUDE CACHE)
446   unset(CUDA_CUDART_LIBRARY CACHE)
447   # Make sure you run this before you unset CUDA_VERSION.
448   if(CUDA_VERSION VERSION_EQUAL "3.0")
449     # This only existed in the 3.0 version of the CUDA toolkit
450     unset(CUDA_CUDARTEMU_LIBRARY CACHE)
451   endif()
452   unset(CUDA_VERSION CACHE)
453   unset(CUDA_CUDA_LIBRARY CACHE)
454   unset(CUDA_cublas_LIBRARY CACHE)
455   unset(CUDA_cublasemu_LIBRARY CACHE)
456   unset(CUDA_cufft_LIBRARY CACHE)
457   unset(CUDA_cufftemu_LIBRARY CACHE)
458   unset(CUDA_curand_LIBRARY CACHE)
459   unset(CUDA_cusparse_LIBRARY CACHE)
460   unset(CUDA_npp_LIBRARY CACHE)
461   unset(CUDA_nvcuvenc_LIBRARY CACHE)
462   unset(CUDA_nvcuvid_LIBRARY CACHE)
463 endif()
464
465 if(NOT "${CUDA_SDK_ROOT_DIR}" STREQUAL "${CUDA_SDK_ROOT_DIR_INTERNAL}")
466   # No specific variables to catch.  Use this kind of code before calling
467   # find_package(CUDA) to clean up any variables that may depend on this path.
468
469   #   unset(MY_SPECIAL_CUDA_SDK_INCLUDE_DIR CACHE)
470   #   unset(MY_SPECIAL_CUDA_SDK_LIBRARY CACHE)
471 endif()
472
473 # Search for the cuda distribution.
474 if(NOT CUDA_TOOLKIT_ROOT_DIR)
475
476   # Search in the CUDA_BIN_PATH first.
477   find_path(CUDA_TOOLKIT_ROOT_DIR
478     NAMES nvcc nvcc.exe
479     PATHS
480       ENV CUDA_PATH
481       ENV CUDA_BIN_PATH
482     PATH_SUFFIXES bin bin64
483     DOC "Toolkit location."
484     NO_DEFAULT_PATH
485     )
486   # Now search default paths
487   find_path(CUDA_TOOLKIT_ROOT_DIR
488     NAMES nvcc nvcc.exe
489     PATHS /usr/local/bin
490           /usr/local/cuda/bin
491     DOC "Toolkit location."
492     )
493
494   if (CUDA_TOOLKIT_ROOT_DIR)
495     string(REGEX REPLACE "[/\\\\]?bin[64]*[/\\\\]?$" "" CUDA_TOOLKIT_ROOT_DIR ${CUDA_TOOLKIT_ROOT_DIR})
496     # We need to force this back into the cache.
497     set(CUDA_TOOLKIT_ROOT_DIR ${CUDA_TOOLKIT_ROOT_DIR} CACHE PATH "Toolkit location." FORCE)
498   endif()
499   if (NOT EXISTS ${CUDA_TOOLKIT_ROOT_DIR})
500     if(CUDA_FIND_REQUIRED)
501       message(FATAL_ERROR "Specify CUDA_TOOLKIT_ROOT_DIR")
502     elseif(NOT CUDA_FIND_QUIETLY)
503       message("CUDA_TOOLKIT_ROOT_DIR not found or specified")
504     endif()
505   endif ()
506 endif ()
507
508 # CUDA_NVCC_EXECUTABLE
509 find_program(CUDA_NVCC_EXECUTABLE
510   NAMES nvcc
511   PATHS "${CUDA_TOOLKIT_ROOT_DIR}"
512   ENV CUDA_PATH
513   ENV CUDA_BIN_PATH
514   PATH_SUFFIXES bin bin64
515   NO_DEFAULT_PATH
516   )
517 # Search default search paths, after we search our own set of paths.
518 find_program(CUDA_NVCC_EXECUTABLE nvcc)
519 mark_as_advanced(CUDA_NVCC_EXECUTABLE)
520
521 if(CUDA_NVCC_EXECUTABLE AND NOT CUDA_VERSION)
522   # Compute the version.
523   execute_process (COMMAND ${CUDA_NVCC_EXECUTABLE} "--version" OUTPUT_VARIABLE NVCC_OUT)
524   string(REGEX REPLACE ".*release ([0-9]+)\\.([0-9]+).*" "\\1" CUDA_VERSION_MAJOR ${NVCC_OUT})
525   string(REGEX REPLACE ".*release ([0-9]+)\\.([0-9]+).*" "\\2" CUDA_VERSION_MINOR ${NVCC_OUT})
526   set(CUDA_VERSION "${CUDA_VERSION_MAJOR}.${CUDA_VERSION_MINOR}" CACHE STRING "Version of CUDA as computed from nvcc.")
527   mark_as_advanced(CUDA_VERSION)
528 else()
529   # Need to set these based off of the cached value
530   string(REGEX REPLACE "([0-9]+)\\.([0-9]+).*" "\\1" CUDA_VERSION_MAJOR "${CUDA_VERSION}")
531   string(REGEX REPLACE "([0-9]+)\\.([0-9]+).*" "\\2" CUDA_VERSION_MINOR "${CUDA_VERSION}")
532 endif()
533
534 # Always set this convenience variable
535 set(CUDA_VERSION_STRING "${CUDA_VERSION}")
536
537 # CUDA_TOOLKIT_INCLUDE
538 find_path(CUDA_TOOLKIT_INCLUDE
539   device_functions.h # Header included in toolkit
540   PATHS "${CUDA_TOOLKIT_ROOT_DIR}"
541   ENV CUDA_PATH
542   ENV CUDA_INC_PATH
543   PATH_SUFFIXES include
544   NO_DEFAULT_PATH
545   )
546 # Search default search paths, after we search our own set of paths.
547 find_path(CUDA_TOOLKIT_INCLUDE device_functions.h)
548 mark_as_advanced(CUDA_TOOLKIT_INCLUDE)
549
550 # Set the user list of include dir to nothing to initialize it.
551 set (CUDA_NVCC_INCLUDE_ARGS_USER "")
552 set (CUDA_INCLUDE_DIRS ${CUDA_TOOLKIT_INCLUDE})
553
554 macro(FIND_LIBRARY_LOCAL_FIRST _var _names _doc)
555   if(CMAKE_SIZEOF_VOID_P EQUAL 8)
556     # CUDA 3.2+ on Windows moved the library directories, so we need the new
557     # and old paths.
558     set(_cuda_64bit_lib_dir "lib/x64" "lib64" )
559   endif()
560   # CUDA 3.2+ on Windows moved the library directories, so we need to new
561   # (lib/Win32) and the old path (lib).
562   find_library(${_var}
563     NAMES ${_names}
564     PATHS "${CUDA_TOOLKIT_ROOT_DIR}"
565     ENV CUDA_PATH
566     ENV CUDA_LIB_PATH
567     PATH_SUFFIXES ${_cuda_64bit_lib_dir} "lib/Win32" "lib"
568     DOC ${_doc}
569     NO_DEFAULT_PATH
570     )
571   # Search default search paths, after we search our own set of paths.
572   find_library(${_var} NAMES ${_names} DOC ${_doc})
573 endmacro()
574
575 # CUDA_LIBRARIES
576 find_library_local_first(CUDA_CUDART_LIBRARY cudart "\"cudart\" library")
577 if(CUDA_VERSION VERSION_EQUAL "3.0")
578   # The cudartemu library only existed for the 3.0 version of CUDA.
579   find_library_local_first(CUDA_CUDARTEMU_LIBRARY cudartemu "\"cudartemu\" library")
580   mark_as_advanced(
581     CUDA_CUDARTEMU_LIBRARY
582     )
583 endif()
584 # If we are using emulation mode and we found the cudartemu library then use
585 # that one instead of cudart.
586 if(CUDA_BUILD_EMULATION AND CUDA_CUDARTEMU_LIBRARY)
587   set(CUDA_LIBRARIES ${CUDA_CUDARTEMU_LIBRARY})
588 else()
589   set(CUDA_LIBRARIES ${CUDA_CUDART_LIBRARY})
590 endif()
591 if(APPLE)
592   # We need to add the path to cudart to the linker using rpath, since the
593   # library name for the cuda libraries is prepended with @rpath.
594   if(CUDA_BUILD_EMULATION AND CUDA_CUDARTEMU_LIBRARY)
595     get_filename_component(_cuda_path_to_cudart "${CUDA_CUDARTEMU_LIBRARY}" PATH)
596   else()
597     get_filename_component(_cuda_path_to_cudart "${CUDA_CUDART_LIBRARY}" PATH)
598   endif()
599   if(_cuda_path_to_cudart)
600     list(APPEND CUDA_LIBRARIES -Wl,-rpath "-Wl,${_cuda_path_to_cudart}")
601   endif()
602 endif()
603
604 # 1.1 toolkit on linux doesn't appear to have a separate library on
605 # some platforms.
606 find_library_local_first(CUDA_CUDA_LIBRARY cuda "\"cuda\" library (older versions only).")
607
608 # Add cuda library to the link line only if it is found.
609 if (CUDA_CUDA_LIBRARY)
610   set(CUDA_LIBRARIES ${CUDA_LIBRARIES} ${CUDA_CUDA_LIBRARY})
611 endif()
612
613 mark_as_advanced(
614   CUDA_CUDA_LIBRARY
615   CUDA_CUDART_LIBRARY
616   )
617
618 #######################
619 # Look for some of the toolkit helper libraries
620 macro(FIND_CUDA_HELPER_LIBS _name)
621   find_library_local_first(CUDA_${_name}_LIBRARY ${_name} "\"${_name}\" library")
622   mark_as_advanced(CUDA_${_name}_LIBRARY)
623 endmacro()
624
625 #######################
626 # Disable emulation for v3.1 onward
627 if(CUDA_VERSION VERSION_GREATER "3.0")
628   if(CUDA_BUILD_EMULATION)
629     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}.")
630   endif()
631 endif()
632
633 # Search for additional CUDA toolkit libraries.
634 if(CUDA_VERSION VERSION_LESS "3.1")
635   # Emulation libraries aren't available in version 3.1 onward.
636   find_cuda_helper_libs(cufftemu)
637   find_cuda_helper_libs(cublasemu)
638 endif()
639 find_cuda_helper_libs(cufft)
640 find_cuda_helper_libs(cublas)
641 if(NOT CUDA_VERSION VERSION_LESS "3.2")
642   # cusparse showed up in version 3.2
643   find_cuda_helper_libs(cusparse)
644   find_cuda_helper_libs(curand)
645   if (WIN32)
646     find_cuda_helper_libs(nvcuvenc)
647     find_cuda_helper_libs(nvcuvid)
648   endif()
649 endif()
650 if(NOT CUDA_VERSION VERSION_LESS "4.0")
651   find_cuda_helper_libs(npp)
652 endif()
653
654 if (CUDA_BUILD_EMULATION)
655   set(CUDA_CUFFT_LIBRARIES ${CUDA_cufftemu_LIBRARY})
656   set(CUDA_CUBLAS_LIBRARIES ${CUDA_cublasemu_LIBRARY})
657 else()
658   set(CUDA_CUFFT_LIBRARIES ${CUDA_cufft_LIBRARY})
659   set(CUDA_CUBLAS_LIBRARIES ${CUDA_cublas_LIBRARY})
660 endif()
661
662 ########################
663 # Look for the SDK stuff.  As of CUDA 3.0 NVSDKCUDA_ROOT has been replaced with
664 # NVSDKCOMPUTE_ROOT with the old CUDA C contents moved into the C subdirectory
665 find_path(CUDA_SDK_ROOT_DIR common/inc/cutil.h
666  HINTS
667   "$ENV{NVSDKCOMPUTE_ROOT}/C"
668   ENV NVSDKCUDA_ROOT
669   "[HKEY_LOCAL_MACHINE\\SOFTWARE\\NVIDIA Corporation\\Installed Products\\NVIDIA SDK 10\\Compute;InstallDir]"
670  PATHS
671   "/Developer/GPU\ Computing/C"
672   )
673
674 # Keep the CUDA_SDK_ROOT_DIR first in order to be able to override the
675 # environment variables.
676 set(CUDA_SDK_SEARCH_PATH
677   "${CUDA_SDK_ROOT_DIR}"
678   "${CUDA_TOOLKIT_ROOT_DIR}/local/NVSDK0.2"
679   "${CUDA_TOOLKIT_ROOT_DIR}/NVSDK0.2"
680   "${CUDA_TOOLKIT_ROOT_DIR}/NV_CUDA_SDK"
681   "$ENV{HOME}/NVIDIA_CUDA_SDK"
682   "$ENV{HOME}/NVIDIA_CUDA_SDK_MACOSX"
683   "/Developer/CUDA"
684   )
685
686 # Example of how to find an include file from the CUDA_SDK_ROOT_DIR
687
688 # find_path(CUDA_CUT_INCLUDE_DIR
689 #   cutil.h
690 #   PATHS ${CUDA_SDK_SEARCH_PATH}
691 #   PATH_SUFFIXES "common/inc"
692 #   DOC "Location of cutil.h"
693 #   NO_DEFAULT_PATH
694 #   )
695 # # Now search system paths
696 # find_path(CUDA_CUT_INCLUDE_DIR cutil.h DOC "Location of cutil.h")
697
698 # mark_as_advanced(CUDA_CUT_INCLUDE_DIR)
699
700
701 # Example of how to find a library in the CUDA_SDK_ROOT_DIR
702
703 # # cutil library is called cutil64 for 64 bit builds on windows.  We don't want
704 # # to get these confused, so we are setting the name based on the word size of
705 # # the build.
706
707 # if(CMAKE_SIZEOF_VOID_P EQUAL 8)
708 #   set(cuda_cutil_name cutil64)
709 # else()
710 #   set(cuda_cutil_name cutil32)
711 # endif()
712
713 # find_library(CUDA_CUT_LIBRARY
714 #   NAMES cutil ${cuda_cutil_name}
715 #   PATHS ${CUDA_SDK_SEARCH_PATH}
716 #   # The new version of the sdk shows up in common/lib, but the old one is in lib
717 #   PATH_SUFFIXES "common/lib" "lib"
718 #   DOC "Location of cutil library"
719 #   NO_DEFAULT_PATH
720 #   )
721 # # Now search system paths
722 # find_library(CUDA_CUT_LIBRARY NAMES cutil ${cuda_cutil_name} DOC "Location of cutil library")
723 # mark_as_advanced(CUDA_CUT_LIBRARY)
724 # set(CUDA_CUT_LIBRARIES ${CUDA_CUT_LIBRARY})
725
726
727
728 #############################
729 # Check for required components
730 set(CUDA_FOUND TRUE)
731
732 set(CUDA_TOOLKIT_ROOT_DIR_INTERNAL "${CUDA_TOOLKIT_ROOT_DIR}" CACHE INTERNAL
733   "This is the value of the last time CUDA_TOOLKIT_ROOT_DIR was set successfully." FORCE)
734 set(CUDA_SDK_ROOT_DIR_INTERNAL "${CUDA_SDK_ROOT_DIR}" CACHE INTERNAL
735   "This is the value of the last time CUDA_SDK_ROOT_DIR was set successfully." FORCE)
736
737 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
738 find_package_handle_standard_args(CUDA
739   REQUIRED_VARS
740     CUDA_TOOLKIT_ROOT_DIR
741     CUDA_NVCC_EXECUTABLE
742     CUDA_INCLUDE_DIRS
743     CUDA_CUDART_LIBRARY
744   VERSION_VAR
745     CUDA_VERSION
746   )
747
748
749
750 ###############################################################################
751 ###############################################################################
752 # Macros
753 ###############################################################################
754 ###############################################################################
755
756 ###############################################################################
757 # Add include directories to pass to the nvcc command.
758 macro(CUDA_INCLUDE_DIRECTORIES)
759   foreach(dir ${ARGN})
760     list(APPEND CUDA_NVCC_INCLUDE_ARGS_USER -I${dir})
761   endforeach()
762 endmacro()
763
764
765 ##############################################################################
766 cuda_find_helper_file(parse_cubin cmake)
767 cuda_find_helper_file(make2cmake cmake)
768 cuda_find_helper_file(run_nvcc cmake)
769
770 ##############################################################################
771 # Separate the OPTIONS out from the sources
772 #
773 macro(CUDA_GET_SOURCES_AND_OPTIONS _sources _cmake_options _options)
774   set( ${_sources} )
775   set( ${_cmake_options} )
776   set( ${_options} )
777   set( _found_options FALSE )
778   foreach(arg ${ARGN})
779     if(arg STREQUAL "OPTIONS")
780       set( _found_options TRUE )
781     elseif(
782         arg STREQUAL "WIN32" OR
783         arg STREQUAL "MACOSX_BUNDLE" OR
784         arg STREQUAL "EXCLUDE_FROM_ALL" OR
785         arg STREQUAL "STATIC" OR
786         arg STREQUAL "SHARED" OR
787         arg STREQUAL "MODULE"
788         )
789       list(APPEND ${_cmake_options} ${arg})
790     else()
791       if ( _found_options )
792         list(APPEND ${_options} ${arg})
793       else()
794         # Assume this is a file
795         list(APPEND ${_sources} ${arg})
796       endif()
797     endif()
798   endforeach()
799 endmacro()
800
801 ##############################################################################
802 # Parse the OPTIONS from ARGN and set the variables prefixed by _option_prefix
803 #
804 macro(CUDA_PARSE_NVCC_OPTIONS _option_prefix)
805   set( _found_config )
806   foreach(arg ${ARGN})
807     # Determine if we are dealing with a perconfiguration flag
808     foreach(config ${CUDA_configuration_types})
809       string(TOUPPER ${config} config_upper)
810       if (arg STREQUAL "${config_upper}")
811         set( _found_config _${arg})
812         # Set arg to nothing to keep it from being processed further
813         set( arg )
814       endif()
815     endforeach()
816
817     if ( arg )
818       list(APPEND ${_option_prefix}${_found_config} "${arg}")
819     endif()
820   endforeach()
821 endmacro()
822
823 ##############################################################################
824 # Helper to add the include directory for CUDA only once
825 function(CUDA_ADD_CUDA_INCLUDE_ONCE)
826   get_directory_property(_include_directories INCLUDE_DIRECTORIES)
827   set(_add TRUE)
828   if(_include_directories)
829     foreach(dir ${_include_directories})
830       if("${dir}" STREQUAL "${CUDA_INCLUDE_DIRS}")
831         set(_add FALSE)
832       endif()
833     endforeach()
834   endif()
835   if(_add)
836     include_directories(${CUDA_INCLUDE_DIRS})
837   endif()
838 endfunction()
839
840 function(CUDA_BUILD_SHARED_LIBRARY shared_flag)
841   set(cmake_args ${ARGN})
842   # If SHARED, MODULE, or STATIC aren't already in the list of arguments, then
843   # add SHARED or STATIC based on the value of BUILD_SHARED_LIBS.
844   list(FIND cmake_args SHARED _cuda_found_SHARED)
845   list(FIND cmake_args MODULE _cuda_found_MODULE)
846   list(FIND cmake_args STATIC _cuda_found_STATIC)
847   if( _cuda_found_SHARED GREATER -1 OR
848       _cuda_found_MODULE GREATER -1 OR
849       _cuda_found_STATIC GREATER -1)
850     set(_cuda_build_shared_libs)
851   else()
852     if (BUILD_SHARED_LIBS)
853       set(_cuda_build_shared_libs SHARED)
854     else()
855       set(_cuda_build_shared_libs STATIC)
856     endif()
857   endif()
858   set(${shared_flag} ${_cuda_build_shared_libs} PARENT_SCOPE)
859 endfunction()
860
861 ##############################################################################
862 # Helper to avoid clashes of files with the same basename but different paths.
863 # This doesn't attempt to do exactly what CMake internals do, which is to only
864 # add this path when there is a conflict, since by the time a second collision
865 # in names is detected it's already too late to fix the first one.  For
866 # consistency sake the relative path will be added to all files.
867 function(CUDA_COMPUTE_BUILD_PATH path build_path)
868   #message("CUDA_COMPUTE_BUILD_PATH([${path}] ${build_path})")
869   # Only deal with CMake style paths from here on out
870   file(TO_CMAKE_PATH "${path}" bpath)
871   if (IS_ABSOLUTE "${bpath}")
872     # Absolute paths are generally unnessary, especially if something like
873     # file(GLOB_RECURSE) is used to pick up the files.
874     file(RELATIVE_PATH bpath "${CMAKE_CURRENT_SOURCE_DIR}" "${bpath}")
875   endif()
876
877   # This recipie is from cmLocalGenerator::CreateSafeUniqueObjectFileName in the
878   # CMake source.
879
880   # Remove leading /
881   string(REGEX REPLACE "^[/]+" "" bpath "${bpath}")
882   # Avoid absolute paths by removing ':'
883   string(REPLACE ":" "_" bpath "${bpath}")
884   # Avoid relative paths that go up the tree
885   string(REPLACE "../" "__/" bpath "${bpath}")
886   # Avoid spaces
887   string(REPLACE " " "_" bpath "${bpath}")
888
889   # Strip off the filename.  I wait until here to do it, since removin the
890   # basename can make a path that looked like path/../basename turn into
891   # path/.. (notice the trailing slash).
892   get_filename_component(bpath "${bpath}" PATH)
893
894   set(${build_path} "${bpath}" PARENT_SCOPE)
895   #message("${build_path} = ${bpath}")
896 endfunction()
897
898 ##############################################################################
899 # This helper macro populates the following variables and setups up custom
900 # commands and targets to invoke the nvcc compiler to generate C or PTX source
901 # dependent upon the format parameter.  The compiler is invoked once with -M
902 # to generate a dependency file and a second time with -cuda or -ptx to generate
903 # a .cpp or .ptx file.
904 # INPUT:
905 #   cuda_target         - Target name
906 #   format              - PTX or OBJ
907 #   FILE1 .. FILEN      - The remaining arguments are the sources to be wrapped.
908 #   OPTIONS             - Extra options to NVCC
909 # OUTPUT:
910 #   generated_files     - List of generated files
911 ##############################################################################
912 ##############################################################################
913
914 macro(CUDA_WRAP_SRCS cuda_target format generated_files)
915
916   # Set up all the command line flags here, so that they can be overridden on a per target basis.
917
918   set(nvcc_flags "")
919
920   # Emulation if the card isn't present.
921   if (CUDA_BUILD_EMULATION)
922     # Emulation.
923     set(nvcc_flags ${nvcc_flags} --device-emulation -D_DEVICEEMU -g)
924   else()
925     # Device mode.  No flags necessary.
926   endif()
927
928   if(CUDA_HOST_COMPILATION_CPP)
929     set(CUDA_C_OR_CXX CXX)
930   else()
931     if(CUDA_VERSION VERSION_LESS "3.0")
932       set(nvcc_flags ${nvcc_flags} --host-compilation C)
933     else()
934       message(WARNING "--host-compilation flag is deprecated in CUDA version >= 3.0.  Removing --host-compilation C flag" )
935     endif()
936     set(CUDA_C_OR_CXX C)
937   endif()
938
939   set(generated_extension ${CMAKE_${CUDA_C_OR_CXX}_OUTPUT_EXTENSION})
940
941   if(CUDA_64_BIT_DEVICE_CODE)
942     set(nvcc_flags ${nvcc_flags} -m64)
943   else()
944     set(nvcc_flags ${nvcc_flags} -m32)
945   endif()
946
947   # This needs to be passed in at this stage, because VS needs to fill out the
948   # value of VCInstallDir from within VS.  Note that CCBIN is only used if
949   # -ccbin or --compiler-bindir isn't used and CUDA_HOST_COMPILER matches
950   # $(VCInstallDir)/bin.
951   if(CMAKE_GENERATOR MATCHES "Visual Studio")
952     set(ccbin_flags -D "\"CCBIN:PATH=$(VCInstallDir)bin\"" )
953   endif()
954
955   # Figure out which configure we will use and pass that in as an argument to
956   # the script.  We need to defer the decision until compilation time, because
957   # for VS projects we won't know if we are making a debug or release build
958   # until build time.
959   if(CMAKE_GENERATOR MATCHES "Visual Studio")
960     set( CUDA_build_configuration "$(ConfigurationName)" )
961   else()
962     set( CUDA_build_configuration "${CMAKE_BUILD_TYPE}")
963   endif()
964
965   # Initialize our list of includes with the user ones followed by the CUDA system ones.
966   set(CUDA_NVCC_INCLUDE_ARGS ${CUDA_NVCC_INCLUDE_ARGS_USER} "-I${CUDA_INCLUDE_DIRS}")
967   # Get the include directories for this directory and use them for our nvcc command.
968   get_directory_property(CUDA_NVCC_INCLUDE_DIRECTORIES INCLUDE_DIRECTORIES)
969   if(CUDA_NVCC_INCLUDE_DIRECTORIES)
970     foreach(dir ${CUDA_NVCC_INCLUDE_DIRECTORIES})
971       list(APPEND CUDA_NVCC_INCLUDE_ARGS -I${dir})
972     endforeach()
973   endif()
974
975   # Reset these variables
976   set(CUDA_WRAP_OPTION_NVCC_FLAGS)
977   foreach(config ${CUDA_configuration_types})
978     string(TOUPPER ${config} config_upper)
979     set(CUDA_WRAP_OPTION_NVCC_FLAGS_${config_upper})
980   endforeach()
981
982   CUDA_GET_SOURCES_AND_OPTIONS(_cuda_wrap_sources _cuda_wrap_cmake_options _cuda_wrap_options ${ARGN})
983   CUDA_PARSE_NVCC_OPTIONS(CUDA_WRAP_OPTION_NVCC_FLAGS ${_cuda_wrap_options})
984
985   # Figure out if we are building a shared library.  BUILD_SHARED_LIBS is
986   # respected in CUDA_ADD_LIBRARY.
987   set(_cuda_build_shared_libs FALSE)
988   # SHARED, MODULE
989   list(FIND _cuda_wrap_cmake_options SHARED _cuda_found_SHARED)
990   list(FIND _cuda_wrap_cmake_options MODULE _cuda_found_MODULE)
991   if(_cuda_found_SHARED GREATER -1 OR _cuda_found_MODULE GREATER -1)
992     set(_cuda_build_shared_libs TRUE)
993   endif()
994   # STATIC
995   list(FIND _cuda_wrap_cmake_options STATIC _cuda_found_STATIC)
996   if(_cuda_found_STATIC GREATER -1)
997     set(_cuda_build_shared_libs FALSE)
998   endif()
999
1000   # CUDA_HOST_FLAGS
1001   if(_cuda_build_shared_libs)
1002     # If we are setting up code for a shared library, then we need to add extra flags for
1003     # compiling objects for shared libraries.
1004     set(CUDA_HOST_SHARED_FLAGS ${CMAKE_SHARED_LIBRARY_${CUDA_C_OR_CXX}_FLAGS})
1005   else()
1006     set(CUDA_HOST_SHARED_FLAGS)
1007   endif()
1008   # Only add the CMAKE_{C,CXX}_FLAGS if we are propagating host flags.  We
1009   # always need to set the SHARED_FLAGS, though.
1010   if(CUDA_PROPAGATE_HOST_FLAGS)
1011     set(_cuda_host_flags "set(CMAKE_HOST_FLAGS ${CMAKE_${CUDA_C_OR_CXX}_FLAGS} ${CUDA_HOST_SHARED_FLAGS})")
1012   else()
1013     set(_cuda_host_flags "set(CMAKE_HOST_FLAGS ${CUDA_HOST_SHARED_FLAGS})")
1014   endif()
1015
1016   set(_cuda_nvcc_flags_config "# Build specific configuration flags")
1017   # Loop over all the configuration types to generate appropriate flags for run_nvcc.cmake
1018   foreach(config ${CUDA_configuration_types})
1019     string(TOUPPER ${config} config_upper)
1020     # CMAKE_FLAGS are strings and not lists.  By not putting quotes around CMAKE_FLAGS
1021     # we convert the strings to lists (like we want).
1022
1023     if(CUDA_PROPAGATE_HOST_FLAGS)
1024       # nvcc chokes on -g3 in versions previous to 3.0, so replace it with -g
1025       set(_cuda_fix_g3 FALSE)
1026
1027       if(CMAKE_COMPILER_IS_GNUCC)
1028         if (CUDA_VERSION VERSION_LESS  "3.0" OR
1029             CUDA_VERSION VERSION_EQUAL "4.1" OR
1030             CUDA_VERSION VERSION_EQUAL "4.2"
1031             )
1032           set(_cuda_fix_g3 TRUE)
1033         endif()
1034       endif()
1035       if(_cuda_fix_g3)
1036         string(REPLACE "-g3" "-g" _cuda_C_FLAGS "${CMAKE_${CUDA_C_OR_CXX}_FLAGS_${config_upper}}")
1037       else()
1038         set(_cuda_C_FLAGS "${CMAKE_${CUDA_C_OR_CXX}_FLAGS_${config_upper}}")
1039       endif()
1040
1041       set(_cuda_host_flags "${_cuda_host_flags}\nset(CMAKE_HOST_FLAGS_${config_upper} ${_cuda_C_FLAGS})")
1042     endif()
1043
1044     # Note that if we ever want CUDA_NVCC_FLAGS_<CONFIG> to be string (instead of a list
1045     # like it is currently), we can remove the quotes around the
1046     # ${CUDA_NVCC_FLAGS_${config_upper}} variable like the CMAKE_HOST_FLAGS_<CONFIG> variable.
1047     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}})")
1048   endforeach()
1049
1050   # Get the list of definitions from the directory property
1051   get_directory_property(CUDA_NVCC_DEFINITIONS COMPILE_DEFINITIONS)
1052   if(CUDA_NVCC_DEFINITIONS)
1053     foreach(_definition ${CUDA_NVCC_DEFINITIONS})
1054       list(APPEND nvcc_flags "-D${_definition}")
1055     endforeach()
1056   endif()
1057
1058   if(_cuda_build_shared_libs)
1059     list(APPEND nvcc_flags "-D${cuda_target}_EXPORTS")
1060   endif()
1061
1062   # Reset the output variable
1063   set(_cuda_wrap_generated_files "")
1064
1065   # Iterate over the macro arguments and create custom
1066   # commands for all the .cu files.
1067   foreach(file ${ARGN})
1068     # Ignore any file marked as a HEADER_FILE_ONLY
1069     get_source_file_property(_is_header ${file} HEADER_FILE_ONLY)
1070     if(${file} MATCHES ".*\\.cu$" AND NOT _is_header)
1071
1072       # Allow per source file overrides of the format.
1073       get_source_file_property(_cuda_source_format ${file} CUDA_SOURCE_PROPERTY_FORMAT)
1074       if(NOT _cuda_source_format)
1075         set(_cuda_source_format ${format})
1076       endif()
1077
1078       if( ${_cuda_source_format} MATCHES "PTX" )
1079         set( compile_to_ptx ON )
1080       elseif( ${_cuda_source_format} MATCHES "OBJ")
1081         set( compile_to_ptx OFF )
1082       else()
1083         message( FATAL_ERROR "Invalid format flag passed to CUDA_WRAP_SRCS for file '${file}': '${_cuda_source_format}'.  Use OBJ or PTX.")
1084       endif()
1085
1086
1087       if(compile_to_ptx)
1088         # Don't use any of the host compilation flags for PTX targets.
1089         set(CUDA_HOST_FLAGS)
1090         set(CUDA_NVCC_FLAGS_CONFIG)
1091       else()
1092         set(CUDA_HOST_FLAGS ${_cuda_host_flags})
1093         set(CUDA_NVCC_FLAGS_CONFIG ${_cuda_nvcc_flags_config})
1094       endif()
1095
1096       # Determine output directory
1097       cuda_compute_build_path("${file}" cuda_build_path)
1098       set(cuda_compile_intermediate_directory "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${cuda_target}.dir/${cuda_build_path}")
1099       if(CUDA_GENERATED_OUTPUT_DIR)
1100         set(cuda_compile_output_dir "${CUDA_GENERATED_OUTPUT_DIR}")
1101       else()
1102         if ( compile_to_ptx )
1103           set(cuda_compile_output_dir "${CMAKE_CURRENT_BINARY_DIR}")
1104         else()
1105           set(cuda_compile_output_dir "${cuda_compile_intermediate_directory}")
1106         endif()
1107       endif()
1108
1109       # Add a custom target to generate a c or ptx file. ######################
1110
1111       get_filename_component( basename ${file} NAME )
1112       if( compile_to_ptx )
1113         set(generated_file_path "${cuda_compile_output_dir}")
1114         set(generated_file_basename "${cuda_target}_generated_${basename}.ptx")
1115         set(format_flag "-ptx")
1116         file(MAKE_DIRECTORY "${cuda_compile_output_dir}")
1117       else()
1118         set(generated_file_path "${cuda_compile_output_dir}/${CMAKE_CFG_INTDIR}")
1119         set(generated_file_basename "${cuda_target}_generated_${basename}${generated_extension}")
1120         set(format_flag "-c")
1121       endif()
1122
1123       # Set all of our file names.  Make sure that whatever filenames that have
1124       # generated_file_path in them get passed in through as a command line
1125       # argument, so that the ${CMAKE_CFG_INTDIR} gets expanded at run time
1126       # instead of configure time.
1127       set(generated_file "${generated_file_path}/${generated_file_basename}")
1128       set(cmake_dependency_file "${cuda_compile_intermediate_directory}/${generated_file_basename}.depend")
1129       set(NVCC_generated_dependency_file "${cuda_compile_intermediate_directory}/${generated_file_basename}.NVCC-depend")
1130       set(generated_cubin_file "${generated_file_path}/${generated_file_basename}.cubin.txt")
1131       set(custom_target_script "${cuda_compile_intermediate_directory}/${generated_file_basename}.cmake")
1132
1133       # Setup properties for obj files:
1134       if( NOT compile_to_ptx )
1135         set_source_files_properties("${generated_file}"
1136           PROPERTIES
1137           EXTERNAL_OBJECT true # This is an object file not to be compiled, but only be linked.
1138           )
1139       endif()
1140
1141       # Don't add CMAKE_CURRENT_SOURCE_DIR if the path is already an absolute path.
1142       get_filename_component(file_path "${file}" PATH)
1143       if(IS_ABSOLUTE "${file_path}")
1144         set(source_file "${file}")
1145       else()
1146         set(source_file "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
1147       endif()
1148
1149       # Bring in the dependencies.  Creates a variable CUDA_NVCC_DEPEND #######
1150       cuda_include_nvcc_dependencies(${cmake_dependency_file})
1151
1152       # Convience string for output ###########################################
1153       if(CUDA_BUILD_EMULATION)
1154         set(cuda_build_type "Emulation")
1155       else()
1156         set(cuda_build_type "Device")
1157       endif()
1158
1159       # Build the NVCC made dependency file ###################################
1160       set(build_cubin OFF)
1161       if ( NOT CUDA_BUILD_EMULATION AND CUDA_BUILD_CUBIN )
1162          if ( NOT compile_to_ptx )
1163            set ( build_cubin ON )
1164          endif()
1165       endif()
1166
1167       # Configure the build script
1168       configure_file("${CUDA_run_nvcc}" "${custom_target_script}" @ONLY)
1169
1170       # So if a user specifies the same cuda file as input more than once, you
1171       # can have bad things happen with dependencies.  Here we check an option
1172       # to see if this is the behavior they want.
1173       if(CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE)
1174         set(main_dep MAIN_DEPENDENCY ${source_file})
1175       else()
1176         set(main_dep DEPENDS ${source_file})
1177       endif()
1178
1179       if(CUDA_VERBOSE_BUILD)
1180         set(verbose_output ON)
1181       elseif(CMAKE_GENERATOR MATCHES "Makefiles")
1182         set(verbose_output "$(VERBOSE)")
1183       else()
1184         set(verbose_output OFF)
1185       endif()
1186
1187       # Create up the comment string
1188       file(RELATIVE_PATH generated_file_relative_path "${CMAKE_BINARY_DIR}" "${generated_file}")
1189       if(compile_to_ptx)
1190         set(cuda_build_comment_string "Building NVCC ptx file ${generated_file_relative_path}")
1191       else()
1192         set(cuda_build_comment_string "Building NVCC (${cuda_build_type}) object ${generated_file_relative_path}")
1193       endif()
1194
1195       # Build the generated file and dependency file ##########################
1196       add_custom_command(
1197         OUTPUT ${generated_file}
1198         # These output files depend on the source_file and the contents of cmake_dependency_file
1199         ${main_dep}
1200         DEPENDS ${CUDA_NVCC_DEPEND}
1201         DEPENDS ${custom_target_script}
1202         # Make sure the output directory exists before trying to write to it.
1203         COMMAND ${CMAKE_COMMAND} -E make_directory "${generated_file_path}"
1204         COMMAND ${CMAKE_COMMAND} ARGS
1205           -D verbose:BOOL=${verbose_output}
1206           ${ccbin_flags}
1207           -D build_configuration:STRING=${CUDA_build_configuration}
1208           -D "generated_file:STRING=${generated_file}"
1209           -D "generated_cubin_file:STRING=${generated_cubin_file}"
1210           -P "${custom_target_script}"
1211         WORKING_DIRECTORY "${cuda_compile_intermediate_directory}"
1212         COMMENT "${cuda_build_comment_string}"
1213         )
1214
1215       # Make sure the build system knows the file is generated.
1216       set_source_files_properties(${generated_file} PROPERTIES GENERATED TRUE)
1217
1218       # Don't add the object file to the list of generated files if we are using
1219       # visual studio and we are attaching the build rule to the cuda file.  VS
1220       # will add our object file to the linker automatically for us.
1221       set(cuda_add_generated_file TRUE)
1222
1223       if(NOT compile_to_ptx AND CMAKE_GENERATOR MATCHES "Visual Studio" AND CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE)
1224         # Visual Studio 8 crashes when you close the solution when you don't add the object file.
1225         if(NOT CMAKE_GENERATOR MATCHES "Visual Studio 8")
1226           #message("Not adding ${generated_file}")
1227           set(cuda_add_generated_file FALSE)
1228         endif()
1229       endif()
1230
1231       if(cuda_add_generated_file)
1232         list(APPEND _cuda_wrap_generated_files ${generated_file})
1233       endif()
1234
1235       # Add the other files that we want cmake to clean on a cleanup ##########
1236       list(APPEND CUDA_ADDITIONAL_CLEAN_FILES "${cmake_dependency_file}")
1237       list(REMOVE_DUPLICATES CUDA_ADDITIONAL_CLEAN_FILES)
1238       set(CUDA_ADDITIONAL_CLEAN_FILES ${CUDA_ADDITIONAL_CLEAN_FILES} CACHE INTERNAL "List of intermediate files that are part of the cuda dependency scanning.")
1239
1240     endif()
1241   endforeach()
1242
1243   # Set the return parameter
1244   set(${generated_files} ${_cuda_wrap_generated_files})
1245 endmacro()
1246
1247
1248 ###############################################################################
1249 ###############################################################################
1250 # ADD LIBRARY
1251 ###############################################################################
1252 ###############################################################################
1253 macro(CUDA_ADD_LIBRARY cuda_target)
1254
1255   CUDA_ADD_CUDA_INCLUDE_ONCE()
1256
1257   # Separate the sources from the options
1258   CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
1259   CUDA_BUILD_SHARED_LIBRARY(_cuda_shared_flag ${ARGN})
1260   # Create custom commands and targets for each file.
1261   CUDA_WRAP_SRCS( ${cuda_target} OBJ _generated_files ${_sources}
1262     ${_cmake_options} ${_cuda_shared_flag}
1263     OPTIONS ${_options} )
1264
1265   # Add the library.
1266   add_library(${cuda_target} ${_cmake_options}
1267     ${_generated_files}
1268     ${_sources}
1269     )
1270
1271   target_link_libraries(${cuda_target}
1272     ${CUDA_LIBRARIES}
1273     )
1274
1275   # We need to set the linker language based on what the expected generated file
1276   # would be. CUDA_C_OR_CXX is computed based on CUDA_HOST_COMPILATION_CPP.
1277   set_target_properties(${cuda_target}
1278     PROPERTIES
1279     LINKER_LANGUAGE ${CUDA_C_OR_CXX}
1280     )
1281
1282 endmacro()
1283
1284
1285 ###############################################################################
1286 ###############################################################################
1287 # ADD EXECUTABLE
1288 ###############################################################################
1289 ###############################################################################
1290 macro(CUDA_ADD_EXECUTABLE cuda_target)
1291
1292   CUDA_ADD_CUDA_INCLUDE_ONCE()
1293
1294   # Separate the sources from the options
1295   CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
1296   # Create custom commands and targets for each file.
1297   CUDA_WRAP_SRCS( ${cuda_target} OBJ _generated_files ${_sources} OPTIONS ${_options} )
1298
1299   # Add the library.
1300   add_executable(${cuda_target} ${_cmake_options}
1301     ${_generated_files}
1302     ${_sources}
1303     )
1304
1305   target_link_libraries(${cuda_target}
1306     ${CUDA_LIBRARIES}
1307     )
1308
1309   # We need to set the linker language based on what the expected generated file
1310   # would be. CUDA_C_OR_CXX is computed based on CUDA_HOST_COMPILATION_CPP.
1311   set_target_properties(${cuda_target}
1312     PROPERTIES
1313     LINKER_LANGUAGE ${CUDA_C_OR_CXX}
1314     )
1315
1316 endmacro()
1317
1318
1319 ###############################################################################
1320 ###############################################################################
1321 # CUDA COMPILE
1322 ###############################################################################
1323 ###############################################################################
1324 macro(CUDA_COMPILE generated_files)
1325
1326   # Separate the sources from the options
1327   CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
1328   # Create custom commands and targets for each file.
1329   CUDA_WRAP_SRCS( cuda_compile OBJ _generated_files ${_sources} ${_cmake_options}
1330     OPTIONS ${_options} )
1331
1332   set( ${generated_files} ${_generated_files})
1333
1334 endmacro()
1335
1336
1337 ###############################################################################
1338 ###############################################################################
1339 # CUDA COMPILE PTX
1340 ###############################################################################
1341 ###############################################################################
1342 macro(CUDA_COMPILE_PTX generated_files)
1343
1344   # Separate the sources from the options
1345   CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
1346   # Create custom commands and targets for each file.
1347   CUDA_WRAP_SRCS( cuda_compile_ptx PTX _generated_files ${_sources} ${_cmake_options}
1348     OPTIONS ${_options} )
1349
1350   set( ${generated_files} ${_generated_files})
1351
1352 endmacro()
1353
1354 ###############################################################################
1355 ###############################################################################
1356 # CUDA ADD CUFFT TO TARGET
1357 ###############################################################################
1358 ###############################################################################
1359 macro(CUDA_ADD_CUFFT_TO_TARGET target)
1360   if (CUDA_BUILD_EMULATION)
1361     target_link_libraries(${target} ${CUDA_cufftemu_LIBRARY})
1362   else()
1363     target_link_libraries(${target} ${CUDA_cufft_LIBRARY})
1364   endif()
1365 endmacro()
1366
1367 ###############################################################################
1368 ###############################################################################
1369 # CUDA ADD CUBLAS TO TARGET
1370 ###############################################################################
1371 ###############################################################################
1372 macro(CUDA_ADD_CUBLAS_TO_TARGET target)
1373   if (CUDA_BUILD_EMULATION)
1374     target_link_libraries(${target} ${CUDA_cublasemu_LIBRARY})
1375   else()
1376     target_link_libraries(${target} ${CUDA_cublas_LIBRARY})
1377   endif()
1378 endmacro()
1379
1380 ###############################################################################
1381 ###############################################################################
1382 # CUDA BUILD CLEAN TARGET
1383 ###############################################################################
1384 ###############################################################################
1385 macro(CUDA_BUILD_CLEAN_TARGET)
1386   # Call this after you add all your CUDA targets, and you will get a convience
1387   # target.  You should also make clean after running this target to get the
1388   # build system to generate all the code again.
1389
1390   set(cuda_clean_target_name clean_cuda_depends)
1391   if (CMAKE_GENERATOR MATCHES "Visual Studio")
1392     string(TOUPPER ${cuda_clean_target_name} cuda_clean_target_name)
1393   endif()
1394   add_custom_target(${cuda_clean_target_name}
1395     COMMAND ${CMAKE_COMMAND} -E remove ${CUDA_ADDITIONAL_CLEAN_FILES})
1396
1397   # Clear out the variable, so the next time we configure it will be empty.
1398   # This is useful so that the files won't persist in the list after targets
1399   # have been removed.
1400   set(CUDA_ADDITIONAL_CLEAN_FILES "" CACHE INTERNAL "List of intermediate files that are part of the cuda dependency scanning.")
1401 endmacro()