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
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
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
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
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.
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]
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
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"
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
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.
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.
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 ###############################################################################
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)
256 cmake_minimum_required(VERSION 2.6.3)
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
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")
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")
317 # message("CUDA_NVCC_DEPEND NOT set")
320 #message("CUDA_NVCC_DEPEND true")
321 foreach(f ${CUDA_NVCC_DEPEND})
322 #message("searching for ${f}")
324 #message("file ${f} not found")
325 set(CUDA_NVCC_DEPEND_REGENERATE TRUE)
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
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)
354 set(CUDA_64_BIT_DEVICE_CODE_DEFAULT OFF)
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)
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
388 CUDA_PROPAGATE_HOST_FLAGS
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
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})
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)
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)
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
439 PATHS ENV CUDA_BIN_PATH
440 DOC "Toolkit location."
443 # Now search default paths
444 find_path(CUDA_TOOLKIT_ROOT_DIR
448 DOC "Toolkit location."
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")
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
468 PATHS "${CUDA_TOOLKIT_ROOT_DIR}/bin"
469 "${CUDA_TOOLKIT_ROOT_DIR}/bin64"
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)
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)
499 if(CUDA_FIND_VERSION AND CUDA_VERSION VERSION_LESS CUDA_FIND_VERSION)
500 set(_cuda_version_acceptable FALSE)
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}")
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"
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")
533 PATHS ${_cuda_64bit_lib_dir}
534 "${CUDA_TOOLKIT_ROOT_DIR}/lib"
539 # Search default search paths, after we search our own set of paths.
540 find_library(${_var} NAMES ${_names} DOC ${_doc})
544 find_library_local_first(CUDA_CUDART_LIBRARY cudart "\"cudart\" library")
545 find_library_local_first(CUDA_CUDART_EMU_LIBRARY cudartemu "\"cudartemu\" library")
547 if (CUDA_VERSION VERSION_GREATER 2.9.9)
548 if (CUDA_BUILD_EMULATION)
549 set(CUDA_LIBRARIES ${CUDA_CUDART_EMU_LIBRARY})
550 else (CUDA_BUILD_EMULATION)
551 set(CUDA_LIBRARIES ${CUDA_CUDART_LIBRARY})
552 endif(CUDA_BUILD_EMULATION)
553 else (CUDA_VERSION VERSION_GREATER 2.9.9)
554 set(CUDA_LIBRARIES ${CUDA_CUDART_LIBRARY})
555 endif (CUDA_VERSION VERSION_GREATER 2.9.9)
557 # 1.1 toolkit on linux doesn't appear to have a separate library on
559 find_library_local_first(CUDA_CUDA_LIBRARY cuda "\"cuda\" library (older versions only).")
561 # Add cuda library to the link line only if it is found.
562 if (CUDA_VERSION VERSION_LESS 2.0)
563 if (CUDA_CUDA_LIBRARY)
564 set(CUDA_LIBRARIES ${CUDA_LIBRARIES} ${CUDA_CUDA_LIBRARY})
565 endif(CUDA_CUDA_LIBRARY)
566 endif (CUDA_VERSION VERSION_LESS 2.0)
571 CUDA_CUDART_EMU_LIBRARY
574 #######################
575 # Look for some of the toolkit helper libraries
576 macro(FIND_CUDA_HELPER_LIBS _name)
577 find_library_local_first(CUDA_${_name}_LIBRARY ${_name} "\"${_name}\" library")
578 mark_as_advanced(CUDA_${_name}_LIBRARY)
579 endmacro(FIND_CUDA_HELPER_LIBS)
581 # Search for cufft and cublas libraries.
582 find_cuda_helper_libs(cufftemu)
583 find_cuda_helper_libs(cublasemu)
584 find_cuda_helper_libs(cufft)
585 find_cuda_helper_libs(cublas)
587 if (CUDA_BUILD_EMULATION)
588 set(CUDA_CUFFT_LIBRARIES ${CUDA_cufftemu_LIBRARY})
589 set(CUDA_CUBLAS_LIBRARIES ${CUDA_cublasemu_LIBRARY})
591 set(CUDA_CUFFT_LIBRARIES ${CUDA_cufft_LIBRARY})
592 set(CUDA_CUBLAS_LIBRARIES ${CUDA_cublas_LIBRARY})
595 ########################
596 # Look for the SDK stuff
597 find_path(CUDA_SDK_ROOT_DIR common/inc/cutil.h
598 "$ENV{NVSDKCUDA_ROOT}"
599 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\NVIDIA Corporation\\Installed Products\\NVIDIA SDK 10\\Compute;InstallDir]"
600 "/Developer/GPU\ Computing/C"
603 # Keep the CUDA_SDK_ROOT_DIR first in order to be able to override the
604 # environment variables.
605 set(CUDA_SDK_SEARCH_PATH
606 "${CUDA_SDK_ROOT_DIR}"
607 "${CUDA_TOOLKIT_ROOT_DIR}/local/NVSDK0.2"
608 "${CUDA_TOOLKIT_ROOT_DIR}/NVSDK0.2"
609 "${CUDA_TOOLKIT_ROOT_DIR}/NV_CUDA_SDK"
610 "$ENV{HOME}/NVIDIA_CUDA_SDK"
611 "$ENV{HOME}/NVIDIA_CUDA_SDK_MACOSX"
615 # Example of how to find an include file from the CUDA_SDK_ROOT_DIR
617 # find_path(CUDA_CUT_INCLUDE_DIR
619 # PATHS ${CUDA_SDK_SEARCH_PATH}
620 # PATH_SUFFIXES "common/inc"
621 # DOC "Location of cutil.h"
624 # # Now search system paths
625 # find_path(CUDA_CUT_INCLUDE_DIR cutil.h DOC "Location of cutil.h")
627 # mark_as_advanced(CUDA_CUT_INCLUDE_DIR)
630 # Example of how to find a library in the CUDA_SDK_ROOT_DIR
632 # # cutil library is called cutil64 for 64 bit builds on windows. We don't want
633 # # to get these confused, so we are setting the name based on the word size of
636 # if(CMAKE_SIZEOF_VOID_P EQUAL 8)
637 # set(cuda_cutil_name cutil64)
638 # else(CMAKE_SIZEOF_VOID_P EQUAL 8)
639 # set(cuda_cutil_name cutil32)
640 # endif(CMAKE_SIZEOF_VOID_P EQUAL 8)
642 # find_library(CUDA_CUT_LIBRARY
643 # NAMES cutil ${cuda_cutil_name}
644 # PATHS ${CUDA_SDK_SEARCH_PATH}
645 # # The new version of the sdk shows up in common/lib, but the old one is in lib
646 # PATH_SUFFIXES "common/lib" "lib"
647 # DOC "Location of cutil library"
650 # # Now search system paths
651 # find_library(CUDA_CUT_LIBRARY NAMES cutil ${cuda_cutil_name} DOC "Location of cutil library")
652 # mark_as_advanced(CUDA_CUT_LIBRARY)
653 # set(CUDA_CUT_LIBRARIES ${CUDA_CUT_LIBRARY})
657 #############################
658 # Check for required components
661 set(CUDA_TOOLKIT_ROOT_DIR_INTERNAL "${CUDA_TOOLKIT_ROOT_DIR}" CACHE INTERNAL
662 "This is the value of the last time CUDA_TOOLKIT_ROOT_DIR was set successfully." FORCE)
663 set(CUDA_SDK_ROOT_DIR_INTERNAL "${CUDA_SDK_ROOT_DIR}" CACHE INTERNAL
664 "This is the value of the last time CUDA_SDK_ROOT_DIR was set successfully." FORCE)
666 include(FindPackageHandleStandardArgs)
667 find_package_handle_standard_args(CUDA DEFAULT_MSG
668 CUDA_TOOLKIT_ROOT_DIR
672 _cuda_version_acceptable
677 ###############################################################################
678 ###############################################################################
680 ###############################################################################
681 ###############################################################################
683 ###############################################################################
684 # Add include directories to pass to the nvcc command.
685 macro(CUDA_INCLUDE_DIRECTORIES)
687 list(APPEND CUDA_NVCC_INCLUDE_ARGS_USER "-I${dir}")
688 endforeach(dir ${ARGN})
689 endmacro(CUDA_INCLUDE_DIRECTORIES)
692 ##############################################################################
693 cuda_find_helper_file(parse_cubin cmake)
694 cuda_find_helper_file(make2cmake cmake)
695 cuda_find_helper_file(run_nvcc cmake)
697 ##############################################################################
698 # Separate the OPTIONS out from the sources
700 macro(CUDA_GET_SOURCES_AND_OPTIONS _sources _cmake_options _options)
702 set( ${_cmake_options} )
704 set( _found_options FALSE )
706 if(arg STREQUAL "OPTIONS")
707 set( _found_options TRUE )
709 arg STREQUAL "WIN32" OR
710 arg STREQUAL "MACOSX_BUNDLE" OR
711 arg STREQUAL "EXCLUDE_FROM_ALL" OR
712 arg STREQUAL "STATIC" OR
713 arg STREQUAL "SHARED" OR
714 arg STREQUAL "MODULE"
716 list(APPEND ${_cmake_options} "${arg}")
718 if ( _found_options )
719 list(APPEND ${_options} "${arg}")
721 # Assume this is a file
722 list(APPEND ${_sources} "${arg}")
728 ##############################################################################
729 # Parse the OPTIONS from ARGN and set the variables prefixed by _option_prefix
731 macro(CUDA_PARSE_NVCC_OPTIONS _option_prefix)
734 # Determine if we are dealing with a perconfiguration flag
735 foreach(config ${CUDA_configuration_types})
736 string(TOUPPER ${config} config_upper)
737 if (arg STREQUAL "${config_upper}")
738 set( _found_config _${arg})
739 # Set arg to nothing to keep it from being processed further
745 list(APPEND ${_option_prefix}${_found_config} "${arg}")
750 ##############################################################################
751 # Helper to add the include directory for CUDA only once
752 function(CUDA_ADD_CUDA_INCLUDE_ONCE)
753 get_directory_property(_include_directories INCLUDE_DIRECTORIES)
755 if(_include_directories)
756 foreach(dir ${_include_directories})
757 if("${dir}" STREQUAL "${CUDA_INCLUDE_DIRS}")
763 include_directories(${CUDA_INCLUDE_DIRS})
767 function(CUDA_BUILD_SHARED_LIBRARY shared_flag)
768 set(cmake_args ${ARGN})
769 # If SHARED, MODULE, or STATIC aren't already in the list of arguments, then
770 # add SHARED or STATIC based on the value of BUILD_SHARED_LIBS.
771 list(FIND cmake_args SHARED _cuda_found_SHARED)
772 list(FIND cmake_args MODULE _cuda_found_MODULE)
773 list(FIND cmake_args STATIC _cuda_found_STATIC)
774 if( _cuda_found_SHARED GREATER -1 OR
775 _cuda_found_MODULE GREATER -1 OR
776 _cuda_found_STATIC GREATER -1)
777 set(_cuda_build_shared_libs)
779 if (BUILD_SHARED_LIBS)
780 set(_cuda_build_shared_libs SHARED)
782 set(_cuda_build_shared_libs STATIC)
785 set(${shared_flag} ${_cuda_build_shared_libs} PARENT_SCOPE)
788 ##############################################################################
789 # This helper macro populates the following variables and setups up custom
790 # commands and targets to invoke the nvcc compiler to generate C or PTX source
791 # dependant upon the format parameter. The compiler is invoked once with -M
792 # to generate a dependency file and a second time with -cuda or -ptx to generate
793 # a .cpp or .ptx file.
795 # cuda_target - Target name
796 # format - PTX or OBJ
797 # FILE1 .. FILEN - The remaining arguments are the sources to be wrapped.
798 # OPTIONS - Extra options to NVCC
800 # generated_files - List of generated files
801 ##############################################################################
802 ##############################################################################
804 macro(CUDA_WRAP_SRCS cuda_target format generated_files)
806 if( ${format} MATCHES "PTX" )
807 set( compile_to_ptx ON )
808 elseif( ${format} MATCHES "OBJ")
809 set( compile_to_ptx OFF )
811 message( FATAL_ERROR "Invalid format flag passed to CUDA_WRAP_SRCS: '${format}'. Use OBJ or PTX.")
814 # Set up all the command line flags here, so that they can be overriden on a per target basis.
818 # Emulation if the card isn't present.
819 if (CUDA_BUILD_EMULATION)
821 set(nvcc_flags ${nvcc_flags} --device-emulation -D_DEVICEEMU -g)
822 else(CUDA_BUILD_EMULATION)
823 # Device mode. No flags necessary.
824 endif(CUDA_BUILD_EMULATION)
826 if(CUDA_HOST_COMPILATION_CPP)
827 set(CUDA_C_OR_CXX CXX)
828 else(CUDA_HOST_COMPILATION_CPP)
829 set(nvcc_flags ${nvcc_flags} --host-compilation C)
831 endif(CUDA_HOST_COMPILATION_CPP)
833 set(generated_extension ${CMAKE_${CUDA_C_OR_CXX}_OUTPUT_EXTENSION})
835 if(CUDA_64_BIT_DEVICE_CODE)
836 set(nvcc_flags ${nvcc_flags} -m64)
838 set(nvcc_flags ${nvcc_flags} -m32)
841 # This needs to be passed in at this stage, because VS needs to fill out the
842 # value of VCInstallDir from within VS.
843 if(CMAKE_GENERATOR MATCHES "Visual Studio")
844 if( CMAKE_SIZEOF_VOID_P EQUAL 8 )
845 # Add nvcc flag for 64b Windows
846 set(ccbin_flags -D "\"CCBIN:PATH=$(VCInstallDir)bin\"" )
850 # Figure out which configure we will use and pass that in as an argument to
851 # the script. We need to defer the decision until compilation time, because
852 # for VS projects we won't know if we are making a debug or release build
854 if(CMAKE_GENERATOR MATCHES "Visual Studio")
855 set( CUDA_build_configuration "$(ConfigurationName)" )
857 set( CUDA_build_configuration "${CMAKE_BUILD_TYPE}")
860 # Initialize our list of includes with the user ones followed by the CUDA system ones.
861 set(CUDA_NVCC_INCLUDE_ARGS ${CUDA_NVCC_INCLUDE_ARGS_USER})
862 # Get the include directories for this directory and use them for our nvcc command.
863 get_directory_property(CUDA_NVCC_INCLUDE_DIRECTORIES INCLUDE_DIRECTORIES)
864 if(CUDA_NVCC_INCLUDE_DIRECTORIES)
865 foreach(dir ${CUDA_NVCC_INCLUDE_DIRECTORIES})
866 list(APPEND CUDA_NVCC_INCLUDE_ARGS "-I${dir}")
870 # Reset these variables
871 set(CUDA_WRAP_OPTION_NVCC_FLAGS)
872 foreach(config ${CUDA_configuration_types})
873 string(TOUPPER ${config} config_upper)
874 set(CUDA_WRAP_OPTION_NVCC_FLAGS_${config_upper})
877 CUDA_GET_SOURCES_AND_OPTIONS(_cuda_wrap_sources _cuda_wrap_cmake_options _cuda_wrap_options ${ARGN})
878 CUDA_PARSE_NVCC_OPTIONS(CUDA_WRAP_OPTION_NVCC_FLAGS ${_cuda_wrap_options})
880 # Figure out if we are building a shared library. BUILD_SHARED_LIBS is
881 # respected in CUDA_ADD_LIBRARY.
882 set(_cuda_build_shared_libs FALSE)
884 list(FIND _cuda_wrap_cmake_options SHARED _cuda_found_SHARED)
885 list(FIND _cuda_wrap_cmake_options MODULE _cuda_found_MODULE)
886 if(_cuda_found_SHARED GREATER -1 OR _cuda_found_MODULE GREATER -1)
887 set(_cuda_build_shared_libs TRUE)
890 list(FIND _cuda_wrap_cmake_options STATIC _cuda_found_STATIC)
891 if(_cuda_found_STATIC GREATER -1)
892 set(_cuda_build_shared_libs FALSE)
896 if(_cuda_build_shared_libs)
897 # If we are setting up code for a shared library, then we need to add extra flags for
898 # compiling objects for shared libraries.
899 set(CUDA_HOST_SHARED_FLAGS ${CMAKE_SHARED_LIBRARY_${CUDA_C_OR_CXX}_FLAGS})
901 set(CUDA_HOST_SHARED_FLAGS)
903 # Only add the CMAKE_{C,CXX}_FLAGS if we are propagating host flags. We
904 # always need to set the SHARED_FLAGS, though.
905 if(CUDA_PROPAGATE_HOST_FLAGS)
906 set(CUDA_HOST_FLAGS "set(CMAKE_HOST_FLAGS ${CMAKE_${CUDA_C_OR_CXX}_FLAGS} ${CUDA_HOST_SHARED_FLAGS})")
908 set(CUDA_HOST_FLAGS "set(CMAKE_HOST_FLAGS ${CUDA_HOST_SHARED_FLAGS})")
911 set(CUDA_NVCC_FLAGS_CONFIG "# Build specific configuration flags")
912 # Loop over all the configuration types to generate appropriate flags for run_nvcc.cmake
913 foreach(config ${CUDA_configuration_types})
914 string(TOUPPER ${config} config_upper)
915 # CMAKE_FLAGS are strings and not lists. By not putting quotes around CMAKE_FLAGS
916 # we convert the strings to lists (like we want).
918 if(CUDA_PROPAGATE_HOST_FLAGS)
919 # nvcc chokes on -g3, so replace it with -g
920 if(CMAKE_COMPILER_IS_GNUCC)
921 string(REPLACE "-g3" "-g" _cuda_C_FLAGS "${CMAKE_${CUDA_C_OR_CXX}_FLAGS_${config_upper}}")
923 set(_cuda_C_FLAGS "${CMAKE_${CUDA_C_OR_CXX}_FLAGS_${config_upper}}")
926 set(CUDA_HOST_FLAGS "${CUDA_HOST_FLAGS}\nset(CMAKE_HOST_FLAGS_${config_upper} ${_cuda_C_FLAGS})")
929 # Note that if we ever want CUDA_NVCC_FLAGS_<CONFIG> to be string (instead of a list
930 # like it is currently), we can remove the quotes around the
931 # ${CUDA_NVCC_FLAGS_${config_upper}} variable like the CMAKE_HOST_FLAGS_<CONFIG> variable.
932 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}}\")")
936 # Don't use any of the host compilation flags for PTX targets.
938 set(CUDA_NVCC_FLAGS_CONFIG)
941 # Get the list of definitions from the directory property
942 get_directory_property(CUDA_NVCC_DEFINITIONS COMPILE_DEFINITIONS)
943 if(CUDA_NVCC_DEFINITIONS)
944 foreach(_definition ${CUDA_NVCC_DEFINITIONS})
945 list(APPEND nvcc_flags "-D${_definition}")
949 if(_cuda_build_shared_libs)
950 list(APPEND nvcc_flags "-D${cuda_target}_EXPORTS")
953 # Determine output directory
954 if(CUDA_GENERATED_OUTPUT_DIR)
955 set(cuda_compile_output_dir "${CUDA_GENERATED_OUTPUT_DIR}")
957 set(cuda_compile_output_dir "${CMAKE_CURRENT_BINARY_DIR}")
960 # Reset the output variable
961 set(_cuda_wrap_generated_files "")
963 # Iterate over the macro arguments and create custom
964 # commands for all the .cu files.
965 foreach(file ${ARGN})
966 # Ignore any file marked as a HEADER_FILE_ONLY
967 get_source_file_property(_is_header ${file} HEADER_FILE_ONLY)
968 if(${file} MATCHES ".*\\.cu$" AND NOT _is_header)
970 # Add a custom target to generate a c or ptx file. ######################
972 get_filename_component( basename ${file} NAME )
974 set(generated_file_path "${cuda_compile_output_dir}")
975 set(generated_file_basename "${cuda_target}_generated_${basename}.ptx")
976 set(format_flag "-ptx")
977 file(MAKE_DIRECTORY "${cuda_compile_output_dir}")
978 else( compile_to_ptx )
979 set(generated_file_path "${cuda_compile_output_dir}/${CMAKE_CFG_INTDIR}")
980 set(generated_file_basename "${cuda_target}_generated_${basename}${generated_extension}")
981 set(format_flag "-c")
982 endif( compile_to_ptx )
984 # Set all of our file names. Make sure that whatever filenames that have
985 # generated_file_path in them get passed in through as a command line
986 # argument, so that the ${CMAKE_CFG_INTDIR} gets expanded at run time
987 # instead of configure time.
988 set(generated_file "${generated_file_path}/${generated_file_basename}")
989 set(cmake_dependency_file "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${generated_file_basename}.depend")
990 set(NVCC_generated_dependency_file "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${generated_file_basename}.NVCC-depend")
991 set(generated_cubin_file "${generated_file_path}/${generated_file_basename}.cubin.txt")
992 set(custom_target_script "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${generated_file_basename}.cmake")
994 # Setup properties for obj files:
995 if( NOT compile_to_ptx )
996 set_source_files_properties("${generated_file}"
998 EXTERNAL_OBJECT true # This is an object file not to be compiled, but only be linked.
1002 # Don't add CMAKE_CURRENT_SOURCE_DIR if the path is already an absolute path.
1003 get_filename_component(file_path "${file}" PATH)
1004 if(IS_ABSOLUTE "${file_path}")
1005 set(source_file "${file}")
1007 set(source_file "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
1010 # Bring in the dependencies. Creates a variable CUDA_NVCC_DEPEND #######
1011 cuda_include_nvcc_dependencies(${cmake_dependency_file})
1013 # Convience string for output ###########################################
1014 if(CUDA_BUILD_EMULATION)
1015 set(cuda_build_type "Emulation")
1016 else(CUDA_BUILD_EMULATION)
1017 set(cuda_build_type "Device")
1018 endif(CUDA_BUILD_EMULATION)
1020 # Build the NVCC made dependency file ###################################
1021 set(build_cubin OFF)
1022 if ( NOT CUDA_BUILD_EMULATION AND CUDA_BUILD_CUBIN )
1023 if ( NOT compile_to_ptx )
1024 set ( build_cubin ON )
1025 endif( NOT compile_to_ptx )
1026 endif( NOT CUDA_BUILD_EMULATION AND CUDA_BUILD_CUBIN )
1028 # Configure the build script
1029 configure_file("${CUDA_run_nvcc}" "${custom_target_script}" @ONLY)
1031 # So if a user specifies the same cuda file as input more than once, you
1032 # can have bad things happen with dependencies. Here we check an option
1033 # to see if this is the behavior they want.
1034 if(CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE)
1035 set(main_dep MAIN_DEPENDENCY ${source_file})
1037 set(main_dep DEPENDS ${source_file})
1040 if(CUDA_VERBOSE_BUILD)
1041 set(verbose_output ON)
1042 elseif(CMAKE_GENERATOR MATCHES "Makefiles")
1043 set(verbose_output "$(VERBOSE)")
1045 set(verbose_output OFF)
1048 # Create up the comment string
1049 file(RELATIVE_PATH generated_file_relative_path "${CMAKE_BINARY_DIR}" "${generated_file}")
1051 set(cuda_build_comment_string "Building NVCC ptx file ${generated_file_relative_path}")
1053 set(cuda_build_comment_string "Building NVCC (${cuda_build_type}) object ${generated_file_relative_path}")
1056 # Build the generated file and dependency file ##########################
1058 OUTPUT ${generated_file}
1059 # These output files depend on the source_file and the contents of cmake_dependency_file
1061 DEPENDS ${CUDA_NVCC_DEPEND}
1062 DEPENDS ${custom_target_script}
1063 # Make sure the output directory exists before trying to write to it.
1064 COMMAND ${CMAKE_COMMAND} -E make_directory "${generated_file_path}"
1065 COMMAND ${CMAKE_COMMAND} ARGS
1066 -D verbose:BOOL=${verbose_output}
1068 -D build_configuration:STRING=${CUDA_build_configuration}
1069 -D "generated_file:STRING=${generated_file}"
1070 -D "generated_cubin_file:STRING=${generated_cubin_file}"
1071 -P "${custom_target_script}"
1072 COMMENT "${cuda_build_comment_string}"
1075 # Make sure the build system knows the file is generated.
1076 set_source_files_properties(${generated_file} PROPERTIES GENERATED TRUE)
1078 # Don't add the object file to the list of generated files if we are using
1079 # visual studio and we are attaching the build rule to the cuda file. VS
1080 # will add our object file to the linker automatically for us.
1081 set(cuda_add_generated_file TRUE)
1083 if(NOT compile_to_ptx AND CMAKE_GENERATOR MATCHES "Visual Studio" AND CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE)
1084 # Visual Studio 8 crashes when you close the solution when you don't add the object file.
1085 if(NOT CMAKE_GENERATOR MATCHES "Visual Studio 8")
1086 #message("Not adding ${generated_file}")
1087 set(cuda_add_generated_file FALSE)
1091 if(cuda_add_generated_file)
1092 list(APPEND _cuda_wrap_generated_files ${generated_file})
1095 # Add the other files that we want cmake to clean on a cleanup ##########
1096 list(APPEND CUDA_ADDITIONAL_CLEAN_FILES "${cmake_dependency_file}")
1097 list(REMOVE_DUPLICATES CUDA_ADDITIONAL_CLEAN_FILES)
1098 set(CUDA_ADDITIONAL_CLEAN_FILES ${CUDA_ADDITIONAL_CLEAN_FILES} CACHE INTERNAL "List of intermediate files that are part of the cuda dependency scanning.")
1100 endif(${file} MATCHES ".*\\.cu$" AND NOT _is_header)
1103 # Set the return parameter
1104 set(${generated_files} ${_cuda_wrap_generated_files})
1105 endmacro(CUDA_WRAP_SRCS)
1108 ###############################################################################
1109 ###############################################################################
1111 ###############################################################################
1112 ###############################################################################
1113 macro(CUDA_ADD_LIBRARY cuda_target)
1115 CUDA_ADD_CUDA_INCLUDE_ONCE()
1117 # Separate the sources from the options
1118 CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
1119 CUDA_BUILD_SHARED_LIBRARY(_cuda_shared_flag ${ARGN})
1120 # Create custom commands and targets for each file.
1121 CUDA_WRAP_SRCS( ${cuda_target} OBJ _generated_files ${_sources}
1122 ${_cmake_options} ${_cuda_shared_flag}
1123 OPTIONS ${_options} )
1126 add_library(${cuda_target} ${_cmake_options}
1131 target_link_libraries(${cuda_target}
1135 # We need to set the linker language based on what the expected generated file
1136 # would be. CUDA_C_OR_CXX is computed based on CUDA_HOST_COMPILATION_CPP.
1137 set_target_properties(${cuda_target}
1139 LINKER_LANGUAGE ${CUDA_C_OR_CXX}
1142 endmacro(CUDA_ADD_LIBRARY cuda_target)
1145 ###############################################################################
1146 ###############################################################################
1148 ###############################################################################
1149 ###############################################################################
1150 macro(CUDA_ADD_EXECUTABLE cuda_target)
1152 CUDA_ADD_CUDA_INCLUDE_ONCE()
1154 # Separate the sources from the options
1155 CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
1156 # Create custom commands and targets for each file.
1157 CUDA_WRAP_SRCS( ${cuda_target} OBJ _generated_files ${_sources} OPTIONS ${_options} )
1160 add_executable(${cuda_target} ${_cmake_options}
1165 target_link_libraries(${cuda_target}
1169 # We need to set the linker language based on what the expected generated file
1170 # would be. CUDA_C_OR_CXX is computed based on CUDA_HOST_COMPILATION_CPP.
1171 set_target_properties(${cuda_target}
1173 LINKER_LANGUAGE ${CUDA_C_OR_CXX}
1176 endmacro(CUDA_ADD_EXECUTABLE cuda_target)
1179 ###############################################################################
1180 ###############################################################################
1182 ###############################################################################
1183 ###############################################################################
1184 macro(CUDA_COMPILE generated_files)
1186 # Separate the sources from the options
1187 CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
1188 # Create custom commands and targets for each file.
1189 CUDA_WRAP_SRCS( cuda_compile OBJ _generated_files ${_sources} ${_cmake_options}
1190 OPTIONS ${_options} )
1192 set( ${generated_files} ${_generated_files})
1194 endmacro(CUDA_COMPILE)
1197 ###############################################################################
1198 ###############################################################################
1200 ###############################################################################
1201 ###############################################################################
1202 macro(CUDA_COMPILE_PTX generated_files)
1204 # Separate the sources from the options
1205 CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
1206 # Create custom commands and targets for each file.
1207 CUDA_WRAP_SRCS( cuda_compile_ptx PTX _generated_files ${_sources} ${_cmake_options}
1208 OPTIONS ${_options} )
1210 set( ${generated_files} ${_generated_files})
1212 endmacro(CUDA_COMPILE_PTX)
1214 ###############################################################################
1215 ###############################################################################
1216 # CUDA ADD CUFFT TO TARGET
1217 ###############################################################################
1218 ###############################################################################
1219 macro(CUDA_ADD_CUFFT_TO_TARGET target)
1220 if (CUDA_BUILD_EMULATION)
1221 target_link_libraries(${target} ${CUDA_cufftemu_LIBRARY})
1223 target_link_libraries(${target} ${CUDA_cufft_LIBRARY})
1227 ###############################################################################
1228 ###############################################################################
1229 # CUDA ADD CUBLAS TO TARGET
1230 ###############################################################################
1231 ###############################################################################
1232 macro(CUDA_ADD_CUBLAS_TO_TARGET target)
1233 if (CUDA_BUILD_EMULATION)
1234 target_link_libraries(${target} ${CUDA_cublasemu_LIBRARY})
1236 target_link_libraries(${target} ${CUDA_cublas_LIBRARY})
1240 ###############################################################################
1241 ###############################################################################
1242 # CUDA BUILD CLEAN TARGET
1243 ###############################################################################
1244 ###############################################################################
1245 macro(CUDA_BUILD_CLEAN_TARGET)
1246 # Call this after you add all your CUDA targets, and you will get a convience
1247 # target. You should also make clean after running this target to get the
1248 # build system to generate all the code again.
1250 set(cuda_clean_target_name clean_cuda_depends)
1251 if (CMAKE_GENERATOR MATCHES "Visual Studio")
1252 string(TOUPPER ${cuda_clean_target_name} cuda_clean_target_name)
1254 add_custom_target(${cuda_clean_target_name}
1255 COMMAND ${CMAKE_COMMAND} -E remove ${CUDA_ADDITIONAL_CLEAN_FILES})
1257 # Clear out the variable, so the next time we configure it will be empty.
1258 # This is useful so that the files won't persist in the list after targets
1259 # have been removed.
1260 set(CUDA_ADDITIONAL_CLEAN_FILES "" CACHE INTERNAL "List of intermediate files that are part of the cuda dependency scanning.")
1261 endmacro(CUDA_BUILD_CLEAN_TARGET)