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