Merge branch 'master' of git@git.gromacs.org:gromacs
[gromacs/rigid-bodies.git] / cmake / FindCUDA.cmake
blob7c28498d65ab90e67b0c7afb07956687a509af5a
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
68 #  CUDA_HOST_COMPILATION_CPP (Default ON)
69 #  -- Set to OFF for C compilation of host code.
71 #  CUDA_NVCC_FLAGS
72 #  CUDA_NVCC_FLAGS_<CONFIG>
73 #  -- Additional NVCC command line arguments.  NOTE: multiple arguments must be
74 #     semi-colon delimited (e.g. --compiler-options;-Wall)
76 #  CUDA_PROPAGATE_HOST_FLAGS (Default ON)
77 #  -- Set to ON to propagate CMAKE_{C,CXX}_FLAGS and their configuration
78 #     dependent counterparts (e.g. CMAKE_C_FLAGS_DEBUG) automatically to the
79 #     host compiler through nvcc's -Xcompiler flag.  This helps make the
80 #     generated host code match the rest of the system better.  Sometimes
81 #     certain flags give nvcc problems, and this will help you turn the flag
82 #     propagation off.  This does not affect the flags supplied directly to nvcc
83 #     via CUDA_NVCC_FLAGS or through the OPTION flags specified through
84 #     CUDA_ADD_LIBRARY, CUDA_ADD_EXECUTABLE, or CUDA_WRAP_SRCS.  Flags used for
85 #     shared library compilation are not affected by this flag.
87 #  CUDA_VERBOSE_BUILD (Default OFF)
88 #  -- Set to ON to see all the commands used when building the CUDA file.  When
89 #     using a Makefile generator the value defaults to VERBOSE (run make
90 #     VERBOSE=1 to see output), although setting CUDA_VERBOSE_BUILD to ON will
91 #     always print the output.
93 # The script creates the following macros (in alphebetical order):
95 #  CUDA_ADD_CUFFT_TO_TARGET( cuda_target )
96 #  -- Adds the cufft library to the target (can be any target).  Handles whether
97 #     you are in emulation mode or not.
99 #  CUDA_ADD_CUBLAS_TO_TARGET( cuda_target )
100 #  -- Adds the cublas library to the target (can be any target).  Handles
101 #     whether you are in emulation mode or not.
103 #  CUDA_ADD_EXECUTABLE( cuda_target file0 file1 ...
104 #                       [WIN32] [MACOSX_BUNDLE] [EXCLUDE_FROM_ALL] [OPTIONS ...] )
105 #  -- Creates an executable "cuda_target" which is made up of the files
106 #     specified.  All of the non CUDA C files are compiled using the standard
107 #     build rules specified by CMAKE and the cuda files are compiled to object
108 #     files using nvcc and the host compiler.  In addition CUDA_INCLUDE_DIRS is
109 #     added automatically to include_directories().  Standard CMake target calls
110 #     can be used on the target after calling this macro
111 #     (e.g. set_target_properties and target_link_libraries).
113 #  CUDA_ADD_LIBRARY( cuda_target file0 file1 ...
114 #                    [STATIC | SHARED | MODULE] [EXCLUDE_FROM_ALL] [OPTIONS ...] )
115 #  -- Same as CUDA_ADD_EXECUTABLE except that a library is created.
117 #  CUDA_BUILD_CLEAN_TARGET()
118 #  -- Creates a convience target that deletes all the dependency files
119 #     generated.  You should make clean after running this target to ensure the
120 #     dependency files get regenerated.
122 #  CUDA_COMPILE( generated_files file0 file1 ... [STATIC | SHARED | MODULE]
123 #                [OPTIONS ...] )
124 #  -- Returns a list of generated files from the input source files to be used
125 #     with ADD_LIBRARY or ADD_EXECUTABLE.
127 #  CUDA_COMPILE_PTX( generated_files file0 file1 ... [OPTIONS ...] )
128 #  -- Returns a list of PTX files generated from the input source files.
130 #  CUDA_INCLUDE_DIRECTORIES( path0 path1 ... )
131 #  -- Sets the directories that should be passed to nvcc
132 #     (e.g. nvcc -Ipath0 -Ipath1 ... ). These paths usually contain other .cu
133 #     files.
135 #  CUDA_WRAP_SRCS ( cuda_target format generated_files file0 file1 ...
136 #                   [STATIC | SHARED | MODULE] [OPTIONS ...] )
137 #  -- This is where all the magic happens.  CUDA_ADD_EXECUTABLE,
138 #     CUDA_ADD_LIBRARY, CUDA_COMPILE, and CUDA_COMPILE_PTX all call this
139 #     function under the hood.
141 #     Given the list of files (file0 file1 ... fileN) this macro generates
142 #     custom commands that generate either PTX or linkable objects (use "PTX" or
143 #     "OBJ" for the format argument to switch).  Files that don't end with .cu
144 #     or have the HEADER_FILE_ONLY property are ignored.
146 #     The arguments passed in after OPTIONS are extra command line options to
147 #     give to nvcc.  You can also specify per configuration options by
148 #     specifying the name of the configuration followed by the options.  General
149 #     options must preceed configuration specific options.  Not all
150 #     configurations need to be specified, only the ones provided will be used.
152 #        OPTIONS -DFLAG=2 "-DFLAG_OTHER=space in flag"
153 #        DEBUG -g
154 #        RELEASE --use_fast_math
155 #        RELWITHDEBINFO --use_fast_math;-g
156 #        MINSIZEREL --use_fast_math
158 #     For certain configurations (namely VS generating object files with
159 #     CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE set to ON), no generated file will
160 #     be produced for the given cuda file.  This is because when you add the
161 #     cuda file to Visual Studio it knows that this file produces an object file
162 #     and will link in the resulting object file automatically.
164 #     This script will also generate a separate cmake script that is used at
165 #     build time to invoke nvcc.  This is for serveral reasons.
167 #       1. nvcc can return negative numbers as return values which confuses
168 #       Visual Studio into thinking that the command succeeded.  The script now
169 #       checks the error codes and produces errors when there was a problem.
171 #       2. nvcc has been known to not delete incomplete results when it
172 #       encounters problems.  This confuses build systems into thinking the
173 #       target was generated when in fact an unusable file exists.  The script
174 #       now deletes the output files if there was an error.
176 #       3. By putting all the options that affect the build into a file and then
177 #       make the build rule dependent on the file, the output files will be
178 #       regenerated when the options change.
180 #     This script also looks at optional arguments STATIC, SHARED, or MODULE to
181 #     determine when to target the object compilation for a shared library.
182 #     BUILD_SHARED_LIBS is ignored in CUDA_WRAP_SRCS, but it is respected in
183 #     CUDA_ADD_LIBRARY.  On some systems special flags are added for building
184 #     objects intended for shared libraries.  A preprocessor macro,
185 #     <target_name>_EXPORTS is defined when a shared library compilation is
186 #     detected.
188 #     Flags passed into add_definitions with -D or /D are passed along to nvcc.
190 # The script defines the following variables:
192 #  CUDA_VERSION_MAJOR    -- The major version of cuda as reported by nvcc.
193 #  CUDA_VERSION_MINOR    -- The minor version.
194 #  CUDA_VERSION
195 #  CUDA_VERSION_STRING   -- CUDA_VERSION_MAJOR.CUDA_VERSION_MINOR
197 #  CUDA_TOOLKIT_ROOT_DIR -- Path to the CUDA Toolkit (defined if not set).
198 #  CUDA_SDK_ROOT_DIR     -- Path to the CUDA SDK.  Use this to find files in the
199 #                           SDK.  This script will not directly support finding
200 #                           specific libraries or headers, as that isn't
201 #                           supported by NVIDIA.  If you want to change
202 #                           libraries when the path changes see the
203 #                           FindCUDA.cmake script for an example of how to clear
204 #                           these variables.  There are also examples of how to
205 #                           use the CUDA_SDK_ROOT_DIR to locate headers or
206 #                           libraries, if you so choose (at your own risk).
207 #  CUDA_INCLUDE_DIRS     -- Include directory for cuda headers.  Added automatically
208 #                           for CUDA_ADD_EXECUTABLE and CUDA_ADD_LIBRARY.
209 #  CUDA_LIBRARIES        -- Cuda RT library.
210 #  CUDA_CUFFT_LIBRARIES  -- Device or emulation library for the Cuda FFT
211 #                           implementation (alternative to:
212 #                           CUDA_ADD_CUFFT_TO_TARGET macro)
213 #  CUDA_CUBLAS_LIBRARIES -- Device or emulation library for the Cuda BLAS
214 #                           implementation (alterative to:
215 #                           CUDA_ADD_CUBLAS_TO_TARGET macro).
218 #  James Bigler, NVIDIA Corp (nvidia.com - jbigler)
219 #  Abe Stephens, SCI Institute -- http://www.sci.utah.edu/~abe/FindCuda.html
221 #  Copyright (c) 2008 - 2009 NVIDIA Corporation.  All rights reserved.
223 #  Copyright (c) 2007-2009
224 #  Scientific Computing and Imaging Institute, University of Utah
226 #  This code is licensed under the MIT License.  See the FindCUDA.cmake script
227 #  for the text of the license.
229 # The MIT License
231 # License for the specific language governing rights and limitations under
232 # Permission is hereby granted, free of charge, to any person obtaining a
233 # copy of this software and associated documentation files (the "Software"),
234 # to deal in the Software without restriction, including without limitation
235 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
236 # and/or sell copies of the Software, and to permit persons to whom the
237 # Software is furnished to do so, subject to the following conditions:
239 # The above copyright notice and this permission notice shall be included
240 # in all copies or substantial portions of the Software.
242 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
243 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
244 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
245 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
246 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
247 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
248 # DEALINGS IN THE SOFTWARE.
250 ###############################################################################
252 # FindCUDA.cmake
254 # We need to have at least this version to support the VERSION_LESS argument to 'if' (2.6.2) and unset (2.6.3)
255 cmake_policy(PUSH)
256 cmake_minimum_required(VERSION 2.6.3)
257 cmake_policy(POP)
259 # This macro helps us find the location of helper files we will need the full path to
260 macro(CUDA_FIND_HELPER_FILE _name _extension)
261   set(_full_name "${_name}.${_extension}")
262   # CMAKE_CURRENT_LIST_FILE contains the full path to the file currently being
263   # processed.  Using this variable, we can pull out the current path, and
264   # provide a way to get access to the other files we need local to here.
265   get_filename_component(CMAKE_CURRENT_LIST_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
266   find_file(CUDA_${_name} ${_full_name} PATHS ${CMAKE_CURRENT_LIST_DIR}/FindCUDA NO_DEFAULT_PATH)
267   if(NOT CUDA_${_name})
268     set(error_message "${_full_name} not found in CMAKE_MODULE_PATH")
269     if(CUDA_FIND_REQUIRED)
270       message(FATAL_ERROR "${error_message}")
271     else(CUDA_FIND_REQUIRED)
272       if(NOT CUDA_FIND_QUIETLY)
273         message(STATUS "${error_message}")
274       endif(NOT CUDA_FIND_QUIETLY)
275     endif(CUDA_FIND_REQUIRED)
276   endif(NOT CUDA_${_name})
277   # Set this variable as internal, so the user isn't bugged with it.
278   set(CUDA_${_name} ${CUDA_${_name}} CACHE INTERNAL "Location of ${_full_name}" FORCE)
279 endmacro(CUDA_FIND_HELPER_FILE)
281 #####################################################################
282 ## CUDA_INCLUDE_NVCC_DEPENDENCIES
285 # So we want to try and include the dependency file if it exists.  If
286 # it doesn't exist then we need to create an empty one, so we can
287 # include it.
289 # If it does exist, then we need to check to see if all the files it
290 # depends on exist.  If they don't then we should clear the dependency
291 # file and regenerate it later.  This covers the case where a header
292 # file has disappeared or moved.
294 macro(CUDA_INCLUDE_NVCC_DEPENDENCIES dependency_file)
295   set(CUDA_NVCC_DEPEND)
296   set(CUDA_NVCC_DEPEND_REGENERATE FALSE)
299   # Include the dependency file.  Create it first if it doesn't exist .  The
300   # INCLUDE puts a dependency that will force CMake to rerun and bring in the
301   # new info when it changes.  DO NOT REMOVE THIS (as I did and spent a few
302   # hours figuring out why it didn't work.
303   if(NOT EXISTS ${dependency_file})
304     file(WRITE ${dependency_file} "#FindCUDA.cmake generated file.  Do not edit.\n")
305   endif()
306   # Always include this file to force CMake to run again next
307   # invocation and rebuild the dependencies.
308   #message("including dependency_file = ${dependency_file}")
309   include(${dependency_file})
311   # Now we need to verify the existence of all the included files
312   # here.  If they aren't there we need to just blank this variable and
313   # make the file regenerate again.
314 #   if(DEFINED CUDA_NVCC_DEPEND)
315 #     message("CUDA_NVCC_DEPEND set")
316 #   else()
317 #     message("CUDA_NVCC_DEPEND NOT set")
318 #   endif()
319   if(CUDA_NVCC_DEPEND)
320     #message("CUDA_NVCC_DEPEND true")
321     foreach(f ${CUDA_NVCC_DEPEND})
322       #message("searching for ${f}")
323       if(NOT EXISTS ${f})
324         #message("file ${f} not found")
325         set(CUDA_NVCC_DEPEND_REGENERATE TRUE)
326       endif()
327     endforeach(f)
328   else(CUDA_NVCC_DEPEND)
329     #message("CUDA_NVCC_DEPEND false")
330     # No dependencies, so regenerate the file.
331     set(CUDA_NVCC_DEPEND_REGENERATE TRUE)
332   endif(CUDA_NVCC_DEPEND)
334   #message("CUDA_NVCC_DEPEND_REGENERATE = ${CUDA_NVCC_DEPEND_REGENERATE}")
335   # No incoming dependencies, so we need to generate them.  Make the
336   # output depend on the dependency file itself, which should cause the
337   # rule to re-run.
338   if(CUDA_NVCC_DEPEND_REGENERATE)
339     file(WRITE ${dependency_file} "#FindCUDA.cmake generated file.  Do not edit.\n")
340   endif(CUDA_NVCC_DEPEND_REGENERATE)
342 endmacro(CUDA_INCLUDE_NVCC_DEPENDENCIES)
344 ###############################################################################
345 ###############################################################################
346 # Setup variables' defaults
347 ###############################################################################
348 ###############################################################################
350 # Allow the user to specify if the device code is supposed to be 32 or 64 bit.
351 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
352   set(CUDA_64_BIT_DEVICE_CODE_DEFAULT ON)
353 else()
354   set(CUDA_64_BIT_DEVICE_CODE_DEFAULT OFF)
355 endif()
356 option(CUDA_64_BIT_DEVICE_CODE "Compile device code in 64 bit mode" ${CUDA_64_BIT_DEVICE_CODE_DEFAULT})
358 # Attach the build rule to the source file in VS.  This option
359 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)
361 # Prints out extra information about the cuda file during compilation
362 option(CUDA_BUILD_CUBIN "Generate and parse .cubin files in Device mode." OFF)
364 # Set whether we are using emulation or device mode.
365 option(CUDA_BUILD_EMULATION "Build in Emulation mode" OFF)
367 # Where to put the generated output.
368 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")
370 # Parse HOST_COMPILATION mode.
371 option(CUDA_HOST_COMPILATION_CPP "Generated file extension" ON)
373 # Extra user settable flags
374 set(CUDA_NVCC_FLAGS "" CACHE STRING "Semi-colon delimit multiple arguments.")
376 # Propagate the host flags to the host compiler via -Xcompiler
377 option(CUDA_PROPAGATE_HOST_FLAGS "Propage C/CXX_FLAGS and friends to the host compiler via -Xcompile" ON)
379 # Specifies whether the commands used when compiling the .cu file will be printed out.
380 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)
382 mark_as_advanced(
383   CUDA_64_BIT_DEVICE_CODE
384   CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE
385   CUDA_GENERATED_OUTPUT_DIR
386   CUDA_HOST_COMPILATION_CPP
387   CUDA_NVCC_FLAGS
388   CUDA_PROPAGATE_HOST_FLAGS
389   )
391 # Makefile and similar generators don't define CMAKE_CONFIGURATION_TYPES, so we
392 # need to add another entry for the CMAKE_BUILD_TYPE.  We also need to add the
393 # standerd set of 4 build types (Debug, MinSizeRel, Release, and RelWithDebInfo)
394 # for completeness.  We need run this loop in order to accomodate the addition
395 # of extra configuration types.  Duplicate entries will be removed by
396 # REMOVE_DUPLICATES.
397 set(CUDA_configuration_types ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE} Debug MinSizeRel Release RelWithDebInfo)
398 list(REMOVE_DUPLICATES CUDA_configuration_types)
399 foreach(config ${CUDA_configuration_types})
400     string(TOUPPER ${config} config_upper)
401     set(CUDA_NVCC_FLAGS_${config_upper} "" CACHE STRING "Semi-colon delimit multiple arguments.")
402     mark_as_advanced(CUDA_NVCC_FLAGS_${config_upper})
403 endforeach()
405 ###############################################################################
406 ###############################################################################
407 # Locate CUDA, Set Build Type, etc.
408 ###############################################################################
409 ###############################################################################
411 # Check to see if the CUDA_TOOLKIT_ROOT_DIR and CUDA_SDK_ROOT_DIR have changed,
412 # if they have then clear the cache variables, so that will be detected again.
413 if(NOT "${CUDA_TOOLKIT_ROOT_DIR}" STREQUAL "${CUDA_TOOLKIT_ROOT_DIR_INTERNAL}")
414   unset(CUDA_NVCC_EXECUTABLE CACHE)
415   unset(CUDA_VERSION CACHE)
416   unset(CUDA_TOOLKIT_INCLUDE CACHE)
417   unset(CUDA_CUDART_LIBRARY CACHE)
418   unset(CUDA_CUDA_LIBRARY CACHE)
419   unset(CUDA_cublas_LIBRARY CACHE)
420   unset(CUDA_cublasemu_LIBRARY CACHE)
421   unset(CUDA_cufft_LIBRARY CACHE)
422   unset(CUDA_cufftemu_LIBRARY CACHE)
423 endif()
425 if(NOT "${CUDA_SDK_ROOT_DIR}" STREQUAL "${CUDA_SDK_ROOT_DIR_INTERNAL}")
426   # No specific variables to catch.  Use this kind of code before calling
427   # find_package(CUDA) to clean up any variables that may depend on this path.
429   #   unset(MY_SPECIAL_CUDA_SDK_INCLUDE_DIR CACHE)
430   #   unset(MY_SPECIAL_CUDA_SDK_LIBRARY CACHE)
431 endif()
433 # Search for the cuda distribution.
434 if(NOT CUDA_TOOLKIT_ROOT_DIR)
436   # Search in the CUDA_BIN_PATH first.
437   find_path(CUDA_TOOLKIT_ROOT_DIR
438     NAMES nvcc nvcc.exe
439     PATHS ENV CUDA_BIN_PATH
440     DOC "Toolkit location."
441     NO_DEFAULT_PATH
442     )
443   # Now search default paths
444   find_path(CUDA_TOOLKIT_ROOT_DIR
445     NAMES nvcc nvcc.exe
446     PATHS /usr/local/bin
447           /usr/local/cuda/bin
448     DOC "Toolkit location."
449     )
451   if (CUDA_TOOLKIT_ROOT_DIR)
452     string(REGEX REPLACE "[/\\\\]?bin[64]*[/\\\\]?$" "" CUDA_TOOLKIT_ROOT_DIR ${CUDA_TOOLKIT_ROOT_DIR})
453     # We need to force this back into the cache.
454     set(CUDA_TOOLKIT_ROOT_DIR ${CUDA_TOOLKIT_ROOT_DIR} CACHE PATH "Toolkit location." FORCE)
455   endif(CUDA_TOOLKIT_ROOT_DIR)
456   if (NOT EXISTS ${CUDA_TOOLKIT_ROOT_DIR})
457     if(CUDA_FIND_REQUIRED)
458       message(FATAL_ERROR "Specify CUDA_TOOLKIT_ROOT_DIR")
459     elseif(NOT CUDA_FIND_QUIETLY)
460       message("CUDA_TOOLKIT_ROOT_DIR not found or specified")
461     endif()
462   endif (NOT EXISTS ${CUDA_TOOLKIT_ROOT_DIR})
463 endif (NOT CUDA_TOOLKIT_ROOT_DIR)
465 # CUDA_NVCC_EXECUTABLE
466 find_program(CUDA_NVCC_EXECUTABLE
467   NAMES nvcc
468   PATHS "${CUDA_TOOLKIT_ROOT_DIR}/bin"
469         "${CUDA_TOOLKIT_ROOT_DIR}/bin64"
470   ENV CUDA_BIN_PATH
471   NO_DEFAULT_PATH
472   )
473 # Search default search paths, after we search our own set of paths.
474 find_program(CUDA_NVCC_EXECUTABLE nvcc)
475 mark_as_advanced(CUDA_NVCC_EXECUTABLE)
477 if(CUDA_NVCC_EXECUTABLE AND NOT CUDA_VERSION)
478   # Compute the version.
479   exec_program(${CUDA_NVCC_EXECUTABLE} ARGS "--version" OUTPUT_VARIABLE NVCC_OUT)
480   string(REGEX REPLACE ".*release ([0-9]+)\\.([0-9]+).*" "\\1" CUDA_VERSION_MAJOR ${NVCC_OUT})
481   string(REGEX REPLACE ".*release ([0-9]+)\\.([0-9]+).*" "\\2" CUDA_VERSION_MINOR ${NVCC_OUT})
482   set(CUDA_VERSION "${CUDA_VERSION_MAJOR}.${CUDA_VERSION_MINOR}" CACHE STRING "Version of CUDA as computed from nvcc.")
483   mark_as_advanced(CUDA_VERSION)
484 endif()
486 # Always set this convenience variable
487 set(CUDA_VERSION_STRING "${CUDA_VERSION}")
489 # Here we need to determine if the version we found is acceptable.  We will
490 # assume that is unless CUDA_FIND_VERSION_EXACT or CUDA_FIND_VERSION is
491 # specified.  The presence of either of these options checks the version
492 # string and signals if the version is acceptable or not.
493 set(_cuda_version_acceptable TRUE)
495 if(CUDA_FIND_VERSION_EXACT AND NOT CUDA_VERSION VERSION_EQUAL CUDA_FIND_VERSION)
496   set(_cuda_version_acceptable FALSE)
497 endif()
499 if(CUDA_FIND_VERSION       AND     CUDA_VERSION VERSION_LESS  CUDA_FIND_VERSION)
500   set(_cuda_version_acceptable FALSE)
501 endif()
503 if(NOT _cuda_version_acceptable)
504   set(_cuda_error_message "Requested CUDA version ${CUDA_FIND_VERSION}, but found unacceptable version ${CUDA_VERSION}")
505   if(CUDA_FIND_REQUIRED)
506     message("${_cuda_error_message}")
507   elseif(NOT CUDA_FIND_QUIETLY)
508     message("${_cuda_error_message}")
509   endif()
510 endif()
512 # CUDA_TOOLKIT_INCLUDE
513 find_path(CUDA_TOOLKIT_INCLUDE
514   device_functions.h # Header included in toolkit
515   PATHS "${CUDA_TOOLKIT_ROOT_DIR}/include"
516   ENV CUDA_INC_PATH
517   NO_DEFAULT_PATH
518   )
519 # Search default search paths, after we search our own set of paths.
520 find_path(CUDA_TOOLKIT_INCLUDE device_functions.h)
521 mark_as_advanced(CUDA_TOOLKIT_INCLUDE)
523 # Set the user list of include dir to nothing to initialize it.
524 set (CUDA_NVCC_INCLUDE_ARGS_USER "")
525 set (CUDA_INCLUDE_DIRS ${CUDA_TOOLKIT_INCLUDE})
527 macro(FIND_LIBRARY_LOCAL_FIRST _var _names _doc)
528   if(CMAKE_SIZEOF_VOID_P EQUAL 8)
529     set(_cuda_64bit_lib_dir "${CUDA_TOOLKIT_ROOT_DIR}/lib64")
530   endif()
531   find_library(${_var}
532     NAMES ${_names}
533     PATHS ${_cuda_64bit_lib_dir}
534           "${CUDA_TOOLKIT_ROOT_DIR}/lib"
535     ENV CUDA_LIB_PATH
536     DOC ${_doc}
537     NO_DEFAULT_PATH
538     )
539   # Search default search paths, after we search our own set of paths.
540   find_library(${_var} NAMES ${_names} DOC ${_doc})
541 endmacro()
543 # CUDA_LIBRARIES
544 find_library_local_first(CUDA_CUDART_LIBRARY cudart "\"cudart\" library")
545 set(CUDA_LIBRARIES ${CUDA_CUDART_LIBRARY})
547 # 1.1 toolkit on linux doesn't appear to have a separate library on
548 # some platforms.
549 find_library_local_first(CUDA_CUDA_LIBRARY cuda "\"cuda\" library (older versions only).")
551 # Add cuda library to the link line only if it is found.
552 if (CUDA_CUDA_LIBRARY)
553   set(CUDA_LIBRARIES ${CUDA_LIBRARIES} ${CUDA_CUDA_LIBRARY})
554 endif(CUDA_CUDA_LIBRARY)
556 mark_as_advanced(
557   CUDA_CUDA_LIBRARY
558   CUDA_CUDART_LIBRARY
559   )
561 #######################
562 # Look for some of the toolkit helper libraries
563 macro(FIND_CUDA_HELPER_LIBS _name)
564   find_library_local_first(CUDA_${_name}_LIBRARY ${_name} "\"${_name}\" library")
565   mark_as_advanced(CUDA_${_name}_LIBRARY)
566 endmacro(FIND_CUDA_HELPER_LIBS)
568 # Search for cufft and cublas libraries.
569 find_cuda_helper_libs(cufftemu)
570 find_cuda_helper_libs(cublasemu)
571 find_cuda_helper_libs(cufft)
572 find_cuda_helper_libs(cublas)
574 if (CUDA_BUILD_EMULATION)
575   set(CUDA_CUFFT_LIBRARIES ${CUDA_cufftemu_LIBRARY})
576   set(CUDA_CUBLAS_LIBRARIES ${CUDA_cublasemu_LIBRARY})
577 else()
578   set(CUDA_CUFFT_LIBRARIES ${CUDA_cufft_LIBRARY})
579   set(CUDA_CUBLAS_LIBRARIES ${CUDA_cublas_LIBRARY})
580 endif()
582 ########################
583 # Look for the SDK stuff
584 find_path(CUDA_SDK_ROOT_DIR common/inc/cutil.h
585   "$ENV{NVSDKCUDA_ROOT}"
586   "[HKEY_LOCAL_MACHINE\\SOFTWARE\\NVIDIA Corporation\\Installed Products\\NVIDIA SDK 10\\Compute;InstallDir]"
587   "/Developer/GPU\ Computing/C"
588   )
590 # Keep the CUDA_SDK_ROOT_DIR first in order to be able to override the
591 # environment variables.
592 set(CUDA_SDK_SEARCH_PATH
593   "${CUDA_SDK_ROOT_DIR}"
594   "${CUDA_TOOLKIT_ROOT_DIR}/local/NVSDK0.2"
595   "${CUDA_TOOLKIT_ROOT_DIR}/NVSDK0.2"
596   "${CUDA_TOOLKIT_ROOT_DIR}/NV_CUDA_SDK"
597   "$ENV{HOME}/NVIDIA_CUDA_SDK"
598   "$ENV{HOME}/NVIDIA_CUDA_SDK_MACOSX"
599   "/Developer/CUDA"
600   )
602 # Example of how to find an include file from the CUDA_SDK_ROOT_DIR
604 # find_path(CUDA_CUT_INCLUDE_DIR
605 #   cutil.h
606 #   PATHS ${CUDA_SDK_SEARCH_PATH}
607 #   PATH_SUFFIXES "common/inc"
608 #   DOC "Location of cutil.h"
609 #   NO_DEFAULT_PATH
610 #   )
611 # # Now search system paths
612 # find_path(CUDA_CUT_INCLUDE_DIR cutil.h DOC "Location of cutil.h")
614 # mark_as_advanced(CUDA_CUT_INCLUDE_DIR)
617 # Example of how to find a library in the CUDA_SDK_ROOT_DIR
619 # # cutil library is called cutil64 for 64 bit builds on windows.  We don't want
620 # # to get these confused, so we are setting the name based on the word size of
621 # # the build.
623 # if(CMAKE_SIZEOF_VOID_P EQUAL 8)
624 #   set(cuda_cutil_name cutil64)
625 # else(CMAKE_SIZEOF_VOID_P EQUAL 8)
626 #   set(cuda_cutil_name cutil32)
627 # endif(CMAKE_SIZEOF_VOID_P EQUAL 8)
629 # find_library(CUDA_CUT_LIBRARY
630 #   NAMES cutil ${cuda_cutil_name}
631 #   PATHS ${CUDA_SDK_SEARCH_PATH}
632 #   # The new version of the sdk shows up in common/lib, but the old one is in lib
633 #   PATH_SUFFIXES "common/lib" "lib"
634 #   DOC "Location of cutil library"
635 #   NO_DEFAULT_PATH
636 #   )
637 # # Now search system paths
638 # find_library(CUDA_CUT_LIBRARY NAMES cutil ${cuda_cutil_name} DOC "Location of cutil library")
639 # mark_as_advanced(CUDA_CUT_LIBRARY)
640 # set(CUDA_CUT_LIBRARIES ${CUDA_CUT_LIBRARY})
644 #############################
645 # Check for required components
646 set(CUDA_FOUND TRUE)
648 set(CUDA_TOOLKIT_ROOT_DIR_INTERNAL "${CUDA_TOOLKIT_ROOT_DIR}" CACHE INTERNAL
649   "This is the value of the last time CUDA_TOOLKIT_ROOT_DIR was set successfully." FORCE)
650 set(CUDA_SDK_ROOT_DIR_INTERNAL "${CUDA_SDK_ROOT_DIR}" CACHE INTERNAL
651   "This is the value of the last time CUDA_SDK_ROOT_DIR was set successfully." FORCE)
653 include(FindPackageHandleStandardArgs)
654 find_package_handle_standard_args(CUDA DEFAULT_MSG
655   CUDA_TOOLKIT_ROOT_DIR
656   CUDA_NVCC_EXECUTABLE
657   CUDA_INCLUDE_DIRS
658   CUDA_CUDART_LIBRARY
659   _cuda_version_acceptable
660   )
664 ###############################################################################
665 ###############################################################################
666 # Macros
667 ###############################################################################
668 ###############################################################################
670 ###############################################################################
671 # Add include directories to pass to the nvcc command.
672 macro(CUDA_INCLUDE_DIRECTORIES)
673   foreach(dir ${ARGN})
674     list(APPEND CUDA_NVCC_INCLUDE_ARGS_USER "-I${dir}")
675   endforeach(dir ${ARGN})
676 endmacro(CUDA_INCLUDE_DIRECTORIES)
679 ##############################################################################
680 cuda_find_helper_file(parse_cubin cmake)
681 cuda_find_helper_file(make2cmake cmake)
682 cuda_find_helper_file(run_nvcc cmake)
684 ##############################################################################
685 # Separate the OPTIONS out from the sources
687 macro(CUDA_GET_SOURCES_AND_OPTIONS _sources _cmake_options _options)
688   set( ${_sources} )
689   set( ${_cmake_options} )
690   set( ${_options} )
691   set( _found_options FALSE )
692   foreach(arg ${ARGN})
693     if(arg STREQUAL "OPTIONS")
694       set( _found_options TRUE )
695     elseif(
696         arg STREQUAL "WIN32" OR
697         arg STREQUAL "MACOSX_BUNDLE" OR
698         arg STREQUAL "EXCLUDE_FROM_ALL" OR
699         arg STREQUAL "STATIC" OR
700         arg STREQUAL "SHARED" OR
701         arg STREQUAL "MODULE"
702         )
703       list(APPEND ${_cmake_options} "${arg}")
704     else()
705       if ( _found_options )
706         list(APPEND ${_options} "${arg}")
707       else()
708         # Assume this is a file
709         list(APPEND ${_sources} "${arg}")
710       endif()
711     endif()
712   endforeach()
713 endmacro()
715 ##############################################################################
716 # Parse the OPTIONS from ARGN and set the variables prefixed by _option_prefix
718 macro(CUDA_PARSE_NVCC_OPTIONS _option_prefix)
719   set( _found_config )
720   foreach(arg ${ARGN})
721     # Determine if we are dealing with a perconfiguration flag
722     foreach(config ${CUDA_configuration_types})
723       string(TOUPPER ${config} config_upper)
724       if (arg STREQUAL "${config_upper}")
725         set( _found_config _${arg})
726         # Set arg to nothing to keep it from being processed further
727         set( arg )
728       endif()
729     endforeach()
731     if ( arg )
732       list(APPEND ${_option_prefix}${_found_config} "${arg}")
733     endif()
734   endforeach()
735 endmacro()
737 ##############################################################################
738 # Helper to add the include directory for CUDA only once
739 function(CUDA_ADD_CUDA_INCLUDE_ONCE)
740   get_directory_property(_include_directories INCLUDE_DIRECTORIES)
741   set(_add TRUE)
742   if(_include_directories)
743     foreach(dir ${_include_directories})
744       if("${dir}" STREQUAL "${CUDA_INCLUDE_DIRS}")
745         set(_add FALSE)
746       endif()
747     endforeach()
748   endif()
749   if(_add)
750     include_directories(${CUDA_INCLUDE_DIRS})
751   endif()
752 endfunction()
754 function(CUDA_BUILD_SHARED_LIBRARY shared_flag)
755   set(cmake_args ${ARGN})
756   # If SHARED, MODULE, or STATIC aren't already in the list of arguments, then
757   # add SHARED or STATIC based on the value of BUILD_SHARED_LIBS.
758   list(FIND cmake_args SHARED _cuda_found_SHARED)
759   list(FIND cmake_args MODULE _cuda_found_MODULE)
760   list(FIND cmake_args STATIC _cuda_found_STATIC)
761   if( _cuda_found_SHARED GREATER -1 OR
762       _cuda_found_MODULE GREATER -1 OR
763       _cuda_found_STATIC GREATER -1)
764     set(_cuda_build_shared_libs)
765   else()
766     if (BUILD_SHARED_LIBS)
767       set(_cuda_build_shared_libs SHARED)
768     else()
769       set(_cuda_build_shared_libs STATIC)
770     endif()
771   endif()
772   set(${shared_flag} ${_cuda_build_shared_libs} PARENT_SCOPE)
773 endfunction()
775 ##############################################################################
776 # This helper macro populates the following variables and setups up custom
777 # commands and targets to invoke the nvcc compiler to generate C or PTX source
778 # dependant upon the format parameter.  The compiler is invoked once with -M
779 # to generate a dependency file and a second time with -cuda or -ptx to generate
780 # a .cpp or .ptx file.
781 # INPUT:
782 #   cuda_target         - Target name
783 #   format              - PTX or OBJ
784 #   FILE1 .. FILEN      - The remaining arguments are the sources to be wrapped.
785 #   OPTIONS             - Extra options to NVCC
786 # OUTPUT:
787 #   generated_files     - List of generated files
788 ##############################################################################
789 ##############################################################################
791 macro(CUDA_WRAP_SRCS cuda_target format generated_files)
793   if( ${format} MATCHES "PTX" )
794     set( compile_to_ptx ON )
795   elseif( ${format} MATCHES "OBJ")
796     set( compile_to_ptx OFF )
797   else()
798     message( FATAL_ERROR "Invalid format flag passed to CUDA_WRAP_SRCS: '${format}'.  Use OBJ or PTX.")
799   endif()
801   # Set up all the command line flags here, so that they can be overriden on a per target basis.
803   set(nvcc_flags "")
805   # Emulation if the card isn't present.
806   if (CUDA_BUILD_EMULATION)
807     # Emulation.
808     set(nvcc_flags ${nvcc_flags} --device-emulation -D_DEVICEEMU -g)
809   else(CUDA_BUILD_EMULATION)
810     # Device mode.  No flags necessary.
811   endif(CUDA_BUILD_EMULATION)
813   if(CUDA_HOST_COMPILATION_CPP)
814     set(CUDA_C_OR_CXX CXX)
815   else(CUDA_HOST_COMPILATION_CPP)
816     set(nvcc_flags ${nvcc_flags} --host-compilation C)
817     set(CUDA_C_OR_CXX C)
818   endif(CUDA_HOST_COMPILATION_CPP)
820   set(generated_extension ${CMAKE_${CUDA_C_OR_CXX}_OUTPUT_EXTENSION})
822   if(CUDA_64_BIT_DEVICE_CODE)
823     set(nvcc_flags ${nvcc_flags} -m64)
824   else()
825     set(nvcc_flags ${nvcc_flags} -m32)
826   endif()
828   # This needs to be passed in at this stage, because VS needs to fill out the
829   # value of VCInstallDir from within VS.
830   if(CMAKE_GENERATOR MATCHES "Visual Studio")
831     if( CMAKE_SIZEOF_VOID_P EQUAL 8 )
832       # Add nvcc flag for 64b Windows
833       set(ccbin_flags -D "\"CCBIN:PATH=$(VCInstallDir)bin\"" )
834     endif()
835   endif()
837   # Figure out which configure we will use and pass that in as an argument to
838   # the script.  We need to defer the decision until compilation time, because
839   # for VS projects we won't know if we are making a debug or release build
840   # until build time.
841   if(CMAKE_GENERATOR MATCHES "Visual Studio")
842     set( CUDA_build_configuration "$(ConfigurationName)" )
843   else()
844     set( CUDA_build_configuration "${CMAKE_BUILD_TYPE}")
845   endif()
847   # Initialize our list of includes with the user ones followed by the CUDA system ones.
848   set(CUDA_NVCC_INCLUDE_ARGS ${CUDA_NVCC_INCLUDE_ARGS_USER} "-I${CUDA_INCLUDE_DIRS}")
849   # Get the include directories for this directory and use them for our nvcc command.
850   get_directory_property(CUDA_NVCC_INCLUDE_DIRECTORIES INCLUDE_DIRECTORIES)
851   if(CUDA_NVCC_INCLUDE_DIRECTORIES)
852     foreach(dir ${CUDA_NVCC_INCLUDE_DIRECTORIES})
853       list(APPEND CUDA_NVCC_INCLUDE_ARGS "-I${dir}")
854     endforeach()
855   endif()
857   # Reset these variables
858   set(CUDA_WRAP_OPTION_NVCC_FLAGS)
859   foreach(config ${CUDA_configuration_types})
860     string(TOUPPER ${config} config_upper)
861     set(CUDA_WRAP_OPTION_NVCC_FLAGS_${config_upper})
862   endforeach()
864   CUDA_GET_SOURCES_AND_OPTIONS(_cuda_wrap_sources _cuda_wrap_cmake_options _cuda_wrap_options ${ARGN})
865   CUDA_PARSE_NVCC_OPTIONS(CUDA_WRAP_OPTION_NVCC_FLAGS ${_cuda_wrap_options})
867   # Figure out if we are building a shared library.  BUILD_SHARED_LIBS is
868   # respected in CUDA_ADD_LIBRARY.
869   set(_cuda_build_shared_libs FALSE)
870   # SHARED, MODULE
871   list(FIND _cuda_wrap_cmake_options SHARED _cuda_found_SHARED)
872   list(FIND _cuda_wrap_cmake_options MODULE _cuda_found_MODULE)
873   if(_cuda_found_SHARED GREATER -1 OR _cuda_found_MODULE GREATER -1)
874     set(_cuda_build_shared_libs TRUE)
875   endif()
876   # STATIC
877   list(FIND _cuda_wrap_cmake_options STATIC _cuda_found_STATIC)
878   if(_cuda_found_STATIC GREATER -1)
879     set(_cuda_build_shared_libs FALSE)
880   endif()
882   # CUDA_HOST_FLAGS
883   if(_cuda_build_shared_libs)
884     # If we are setting up code for a shared library, then we need to add extra flags for
885     # compiling objects for shared libraries.
886     set(CUDA_HOST_SHARED_FLAGS ${CMAKE_SHARED_LIBRARY_${CUDA_C_OR_CXX}_FLAGS})
887   else()
888     set(CUDA_HOST_SHARED_FLAGS)
889   endif()
890   # Only add the CMAKE_{C,CXX}_FLAGS if we are propagating host flags.  We
891   # always need to set the SHARED_FLAGS, though.
892   if(CUDA_PROPAGATE_HOST_FLAGS)
893     set(CUDA_HOST_FLAGS "set(CMAKE_HOST_FLAGS ${CMAKE_${CUDA_C_OR_CXX}_FLAGS} ${CUDA_HOST_SHARED_FLAGS})")
894   else()
895     set(CUDA_HOST_FLAGS "set(CMAKE_HOST_FLAGS ${CUDA_HOST_SHARED_FLAGS})")
896   endif()
898   set(CUDA_NVCC_FLAGS_CONFIG "# Build specific configuration flags")
899   # Loop over all the configuration types to generate appropriate flags for run_nvcc.cmake
900   foreach(config ${CUDA_configuration_types})
901     string(TOUPPER ${config} config_upper)
902     # CMAKE_FLAGS are strings and not lists.  By not putting quotes around CMAKE_FLAGS
903     # we convert the strings to lists (like we want).
905     if(CUDA_PROPAGATE_HOST_FLAGS)
906       # nvcc chokes on -g3, so replace it with -g
907       if(CMAKE_COMPILER_IS_GNUCC)
908         string(REPLACE "-g3" "-g" _cuda_C_FLAGS "${CMAKE_${CUDA_C_OR_CXX}_FLAGS_${config_upper}}")
909       else()
910         set(_cuda_C_FLAGS "${CMAKE_${CUDA_C_OR_CXX}_FLAGS_${config_upper}}")
911       endif()
913       set(CUDA_HOST_FLAGS "${CUDA_HOST_FLAGS}\nset(CMAKE_HOST_FLAGS_${config_upper} ${_cuda_C_FLAGS})")
914     endif()
916     # Note that if we ever want CUDA_NVCC_FLAGS_<CONFIG> to be string (instead of a list
917     # like it is currently), we can remove the quotes around the
918     # ${CUDA_NVCC_FLAGS_${config_upper}} variable like the CMAKE_HOST_FLAGS_<CONFIG> variable.
919     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}}\")")
920   endforeach()
922   if(compile_to_ptx)
923     # Don't use any of the host compilation flags for PTX targets.
924     set(CUDA_HOST_FLAGS)
925     set(CUDA_NVCC_FLAGS_CONFIG)
926   endif()
928   # Get the list of definitions from the directory property
929   get_directory_property(CUDA_NVCC_DEFINITIONS COMPILE_DEFINITIONS)
930   if(CUDA_NVCC_DEFINITIONS)
931     foreach(_definition ${CUDA_NVCC_DEFINITIONS})
932       list(APPEND nvcc_flags "-D${_definition}")
933     endforeach()
934   endif()
936   if(_cuda_build_shared_libs)
937     list(APPEND nvcc_flags "-D${cuda_target}_EXPORTS")
938   endif()
940   # Determine output directory
941   if(CUDA_GENERATED_OUTPUT_DIR)
942     set(cuda_compile_output_dir "${CUDA_GENERATED_OUTPUT_DIR}")
943   else()
944     set(cuda_compile_output_dir "${CMAKE_CURRENT_BINARY_DIR}")
945   endif()
947   # Reset the output variable
948   set(_cuda_wrap_generated_files "")
950   # Iterate over the macro arguments and create custom
951   # commands for all the .cu files.
952   foreach(file ${ARGN})
953     # Ignore any file marked as a HEADER_FILE_ONLY
954     get_source_file_property(_is_header ${file} HEADER_FILE_ONLY)
955     if(${file} MATCHES ".*\\.cu$" AND NOT _is_header)
957       # Add a custom target to generate a c or ptx file. ######################
959       get_filename_component( basename ${file} NAME )
960       if( compile_to_ptx )
961         set(generated_file_path "${cuda_compile_output_dir}")
962         set(generated_file_basename "${cuda_target}_generated_${basename}.ptx")
963         set(format_flag "-ptx")
964         file(MAKE_DIRECTORY "${cuda_compile_output_dir}")
965       else( compile_to_ptx )
966         set(generated_file_path "${cuda_compile_output_dir}/${CMAKE_CFG_INTDIR}")
967         set(generated_file_basename "${cuda_target}_generated_${basename}${generated_extension}")
968         set(format_flag "-c")
969       endif( compile_to_ptx )
971       # Set all of our file names.  Make sure that whatever filenames that have
972       # generated_file_path in them get passed in through as a command line
973       # argument, so that the ${CMAKE_CFG_INTDIR} gets expanded at run time
974       # instead of configure time.
975       set(generated_file "${generated_file_path}/${generated_file_basename}")
976       set(cmake_dependency_file "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${generated_file_basename}.depend")
977       set(NVCC_generated_dependency_file "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${generated_file_basename}.NVCC-depend")
978       set(generated_cubin_file "${generated_file_path}/${generated_file_basename}.cubin.txt")
979       set(custom_target_script "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${generated_file_basename}.cmake")
981       # Setup properties for obj files:
982       if( NOT compile_to_ptx )
983         set_source_files_properties("${generated_file}"
984           PROPERTIES
985           EXTERNAL_OBJECT true # This is an object file not to be compiled, but only be linked.
986           )
987       endif()
989       # Don't add CMAKE_CURRENT_SOURCE_DIR if the path is already an absolute path.
990       get_filename_component(file_path "${file}" PATH)
991       if(IS_ABSOLUTE "${file_path}")
992         set(source_file "${file}")
993       else()
994         set(source_file "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
995       endif()
997       # Bring in the dependencies.  Creates a variable CUDA_NVCC_DEPEND #######
998       cuda_include_nvcc_dependencies(${cmake_dependency_file})
1000       # Convience string for output ###########################################
1001       if(CUDA_BUILD_EMULATION)
1002         set(cuda_build_type "Emulation")
1003       else(CUDA_BUILD_EMULATION)
1004         set(cuda_build_type "Device")
1005       endif(CUDA_BUILD_EMULATION)
1007       # Build the NVCC made dependency file ###################################
1008       set(build_cubin OFF)
1009       if ( NOT CUDA_BUILD_EMULATION AND CUDA_BUILD_CUBIN )
1010          if ( NOT compile_to_ptx )
1011            set ( build_cubin ON )
1012          endif( NOT compile_to_ptx )
1013       endif( NOT CUDA_BUILD_EMULATION AND CUDA_BUILD_CUBIN )
1015       # Configure the build script
1016       configure_file("${CUDA_run_nvcc}" "${custom_target_script}" @ONLY)
1018       # So if a user specifies the same cuda file as input more than once, you
1019       # can have bad things happen with dependencies.  Here we check an option
1020       # to see if this is the behavior they want.
1021       if(CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE)
1022         set(main_dep MAIN_DEPENDENCY ${source_file})
1023       else()
1024         set(main_dep DEPENDS ${source_file})
1025       endif()
1027       if(CUDA_VERBOSE_BUILD)
1028         set(verbose_output ON)
1029       elseif(CMAKE_GENERATOR MATCHES "Makefiles")
1030         set(verbose_output "$(VERBOSE)")
1031       else()
1032         set(verbose_output OFF)
1033       endif()
1035       # Create up the comment string
1036       file(RELATIVE_PATH generated_file_relative_path "${CMAKE_BINARY_DIR}" "${generated_file}")
1037       if(compile_to_ptx)
1038         set(cuda_build_comment_string "Building NVCC ptx file ${generated_file_relative_path}")
1039       else()
1040         set(cuda_build_comment_string "Building NVCC (${cuda_build_type}) object ${generated_file_relative_path}")
1041       endif()
1043       # Build the generated file and dependency file ##########################
1044       add_custom_command(
1045         OUTPUT ${generated_file}
1046         # These output files depend on the source_file and the contents of cmake_dependency_file
1047         ${main_dep}
1048         DEPENDS ${CUDA_NVCC_DEPEND}
1049         DEPENDS ${custom_target_script}
1050         # Make sure the output directory exists before trying to write to it.
1051         COMMAND ${CMAKE_COMMAND} -E make_directory "${generated_file_path}"
1052         COMMAND ${CMAKE_COMMAND} ARGS
1053           -D verbose:BOOL=${verbose_output}
1054           ${ccbin_flags}
1055           -D build_configuration:STRING=${CUDA_build_configuration}
1056           -D "generated_file:STRING=${generated_file}"
1057           -D "generated_cubin_file:STRING=${generated_cubin_file}"
1058           -P "${custom_target_script}"
1059         COMMENT "${cuda_build_comment_string}"
1060         )
1062       # Make sure the build system knows the file is generated.
1063       set_source_files_properties(${generated_file} PROPERTIES GENERATED TRUE)
1065       # Don't add the object file to the list of generated files if we are using
1066       # visual studio and we are attaching the build rule to the cuda file.  VS
1067       # will add our object file to the linker automatically for us.
1068       set(cuda_add_generated_file TRUE)
1070       if(NOT compile_to_ptx AND CMAKE_GENERATOR MATCHES "Visual Studio" AND CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE)
1071         # Visual Studio 8 crashes when you close the solution when you don't add the object file.
1072         if(NOT CMAKE_GENERATOR MATCHES "Visual Studio 8")
1073           #message("Not adding ${generated_file}")
1074           set(cuda_add_generated_file FALSE)
1075         endif()
1076       endif()
1078       if(cuda_add_generated_file)
1079         list(APPEND _cuda_wrap_generated_files ${generated_file})
1080       endif()
1082       # Add the other files that we want cmake to clean on a cleanup ##########
1083       list(APPEND CUDA_ADDITIONAL_CLEAN_FILES "${cmake_dependency_file}")
1084       list(REMOVE_DUPLICATES CUDA_ADDITIONAL_CLEAN_FILES)
1085       set(CUDA_ADDITIONAL_CLEAN_FILES ${CUDA_ADDITIONAL_CLEAN_FILES} CACHE INTERNAL "List of intermediate files that are part of the cuda dependency scanning.")
1087     endif(${file} MATCHES ".*\\.cu$" AND NOT _is_header)
1088   endforeach(file)
1090   # Set the return parameter
1091   set(${generated_files} ${_cuda_wrap_generated_files})
1092 endmacro(CUDA_WRAP_SRCS)
1095 ###############################################################################
1096 ###############################################################################
1097 # ADD LIBRARY
1098 ###############################################################################
1099 ###############################################################################
1100 macro(CUDA_ADD_LIBRARY cuda_target)
1102   CUDA_ADD_CUDA_INCLUDE_ONCE()
1104   # Separate the sources from the options
1105   CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
1106   CUDA_BUILD_SHARED_LIBRARY(_cuda_shared_flag ${ARGN})
1107   # Create custom commands and targets for each file.
1108   CUDA_WRAP_SRCS( ${cuda_target} OBJ _generated_files ${_sources}
1109     ${_cmake_options} ${_cuda_shared_flag}
1110     OPTIONS ${_options} )
1112   # Add the library.
1113   add_library(${cuda_target} ${_cmake_options}
1114     ${_generated_files}
1115     ${_sources}
1116     )
1118   target_link_libraries(${cuda_target}
1119     ${CUDA_LIBRARIES}
1120     )
1122   # We need to set the linker language based on what the expected generated file
1123   # would be. CUDA_C_OR_CXX is computed based on CUDA_HOST_COMPILATION_CPP.
1124   set_target_properties(${cuda_target}
1125     PROPERTIES
1126     LINKER_LANGUAGE ${CUDA_C_OR_CXX}
1127     )
1129 endmacro(CUDA_ADD_LIBRARY cuda_target)
1132 ###############################################################################
1133 ###############################################################################
1134 # ADD EXECUTABLE
1135 ###############################################################################
1136 ###############################################################################
1137 macro(CUDA_ADD_EXECUTABLE cuda_target)
1139   CUDA_ADD_CUDA_INCLUDE_ONCE()
1141   # Separate the sources from the options
1142   CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
1143   # Create custom commands and targets for each file.
1144   CUDA_WRAP_SRCS( ${cuda_target} OBJ _generated_files ${_sources} OPTIONS ${_options} )
1146   # Add the library.
1147   add_executable(${cuda_target} ${_cmake_options}
1148     ${_generated_files}
1149     ${_sources}
1150     )
1152   target_link_libraries(${cuda_target}
1153     ${CUDA_LIBRARIES}
1154     )
1156   # We need to set the linker language based on what the expected generated file
1157   # would be. CUDA_C_OR_CXX is computed based on CUDA_HOST_COMPILATION_CPP.
1158   set_target_properties(${cuda_target}
1159     PROPERTIES
1160     LINKER_LANGUAGE ${CUDA_C_OR_CXX}
1161     )
1163 endmacro(CUDA_ADD_EXECUTABLE cuda_target)
1166 ###############################################################################
1167 ###############################################################################
1168 # CUDA COMPILE
1169 ###############################################################################
1170 ###############################################################################
1171 macro(CUDA_COMPILE generated_files)
1173   # Separate the sources from the options
1174   CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
1175   # Create custom commands and targets for each file.
1176   CUDA_WRAP_SRCS( cuda_compile OBJ _generated_files ${_sources} ${_cmake_options}
1177     OPTIONS ${_options} )
1179   set( ${generated_files} ${_generated_files})
1181 endmacro(CUDA_COMPILE)
1184 ###############################################################################
1185 ###############################################################################
1186 # CUDA COMPILE PTX
1187 ###############################################################################
1188 ###############################################################################
1189 macro(CUDA_COMPILE_PTX generated_files)
1191   # Separate the sources from the options
1192   CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
1193   # Create custom commands and targets for each file.
1194   CUDA_WRAP_SRCS( cuda_compile_ptx PTX _generated_files ${_sources} ${_cmake_options}
1195     OPTIONS ${_options} )
1197   set( ${generated_files} ${_generated_files})
1199 endmacro(CUDA_COMPILE_PTX)
1201 ###############################################################################
1202 ###############################################################################
1203 # CUDA ADD CUFFT TO TARGET
1204 ###############################################################################
1205 ###############################################################################
1206 macro(CUDA_ADD_CUFFT_TO_TARGET target)
1207   if (CUDA_BUILD_EMULATION)
1208     target_link_libraries(${target} ${CUDA_cufftemu_LIBRARY})
1209   else()
1210     target_link_libraries(${target} ${CUDA_cufft_LIBRARY})
1211   endif()
1212 endmacro()
1214 ###############################################################################
1215 ###############################################################################
1216 # CUDA ADD CUBLAS TO TARGET
1217 ###############################################################################
1218 ###############################################################################
1219 macro(CUDA_ADD_CUBLAS_TO_TARGET target)
1220   if (CUDA_BUILD_EMULATION)
1221     target_link_libraries(${target} ${CUDA_cublasemu_LIBRARY})
1222   else()
1223     target_link_libraries(${target} ${CUDA_cublas_LIBRARY})
1224   endif()
1225 endmacro()
1227 ###############################################################################
1228 ###############################################################################
1229 # CUDA BUILD CLEAN TARGET
1230 ###############################################################################
1231 ###############################################################################
1232 macro(CUDA_BUILD_CLEAN_TARGET)
1233   # Call this after you add all your CUDA targets, and you will get a convience
1234   # target.  You should also make clean after running this target to get the
1235   # build system to generate all the code again.
1237   set(cuda_clean_target_name clean_cuda_depends)
1238   if (CMAKE_GENERATOR MATCHES "Visual Studio")
1239     string(TOUPPER ${cuda_clean_target_name} cuda_clean_target_name)
1240   endif()
1241   add_custom_target(${cuda_clean_target_name}
1242     COMMAND ${CMAKE_COMMAND} -E remove ${CUDA_ADDITIONAL_CLEAN_FILES})
1244   # Clear out the variable, so the next time we configure it will be empty.
1245   # This is useful so that the files won't persist in the list after targets
1246   # have been removed.
1247   set(CUDA_ADDITIONAL_CLEAN_FILES "" CACHE INTERNAL "List of intermediate files that are part of the cuda dependency scanning.")
1248 endmacro(CUDA_BUILD_CLEAN_TARGET)