2 # This file is part of the GROMACS molecular simulation package.
4 # Copyright (c) 2017, by the GROMACS development team, led by
5 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 # and including many others, as listed in the AUTHORS file in the
7 # top-level source directory and at http://www.gromacs.org.
9 # GROMACS is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public License
11 # as published by the Free Software Foundation; either version 2.1
12 # of the License, or (at your option) any later version.
14 # GROMACS is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # Lesser General Public License for more details.
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with GROMACS; if not, see
21 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 # If you want to redistribute modifications to GROMACS, please
25 # consider that scientific software is very special. Version
26 # control is crucial - bugs must be traceable. We will be happy to
27 # consider code for inclusion in the official distribution, but
28 # derived work must not be called official GROMACS. Details are found
29 # in the README & COPYING files - if they are missing, get the
30 # official version at http://www.gromacs.org.
32 # To help us fund GROMACS development, we humbly ask that you cite
33 # the research papers on the package. Check out http://www.gromacs.org.
39 # GROMACS developers README
40 # -------------------------
42 # This file is taken from CMake git tag v3.4.3
43 # Modules/FindCUDA.cmake. Several other supporting cmake files from
44 # Modules/FindCUDA/*cmake are also required, and these are also copied
45 # from the CMake repo, unmodified.
47 # The main file is modified only to make FindPackageHandleStandardArgs
48 # able to find those required supporting cmake files.
50 # Once CMake 3.7.0 is required for GROMACS, then the fix for CMake
51 # issue #14201 contained therein may remove the need for GROMACS to
52 # import these files. Or CMake 3.8 supports CUDA natively (but 3.9
53 # required for MSVC support also) if that proves useful for
54 # GROMACS. However, the FindCUDA.cmake in CMake 3.9.0 still does not
55 # use include functionality for headers internal to CUDA that would
56 # permit the GROMACS use of -Wundef to lead to warning-free CUDA
59 # Standard FindCUDA documentation
60 # -------------------------------
62 # Tools for building CUDA C files: libraries and build dependencies.
64 # This script locates the NVIDIA CUDA C tools. It should work on linux,
65 # windows, and mac and should be reasonably up to date with CUDA C
68 # This script makes use of the standard find_package arguments of
69 # <VERSION>, REQUIRED and QUIET. CUDA_FOUND will report if an
70 # acceptable version of CUDA was found.
72 # The script will prompt the user to specify CUDA_TOOLKIT_ROOT_DIR if
73 # the prefix cannot be determined by the location of nvcc in the system
74 # path and REQUIRED is specified to find_package(). To use a different
75 # installed version of the toolkit set the environment variable
76 # CUDA_BIN_PATH before running cmake (e.g.
77 # CUDA_BIN_PATH=/usr/local/cuda1.0 instead of the default
78 # /usr/local/cuda) or set CUDA_TOOLKIT_ROOT_DIR after configuring. If
79 # you change the value of CUDA_TOOLKIT_ROOT_DIR, various components that
80 # depend on the path will be relocated.
82 # It might be necessary to set CUDA_TOOLKIT_ROOT_DIR manually on certain
83 # platforms, or to use a cuda runtime not installed in the default
84 # location. In newer versions of the toolkit the cuda library is
85 # included with the graphics driver- be sure that the driver version
86 # matches what is needed by the cuda runtime version.
88 # The following variables affect the behavior of the macros in the
89 # script (in alphebetical order). Note that any of these flags can be
90 # changed multiple times in the same directory before calling
91 # CUDA_ADD_EXECUTABLE, CUDA_ADD_LIBRARY, CUDA_COMPILE, CUDA_COMPILE_PTX,
92 # CUDA_COMPILE_FATBIN, CUDA_COMPILE_CUBIN or CUDA_WRAP_SRCS::
94 # CUDA_64_BIT_DEVICE_CODE (Default matches host bit size)
95 # -- Set to ON to compile for 64 bit device code, OFF for 32 bit device code.
96 # Note that making this different from the host code when generating object
97 # or C files from CUDA code just won't work, because size_t gets defined by
98 # nvcc in the generated source. If you compile to PTX and then load the
99 # file yourself, you can mix bit sizes between device and host.
101 # CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE (Default ON)
102 # -- Set to ON if you want the custom build rule to be attached to the source
103 # file in Visual Studio. Turn OFF if you add the same cuda file to multiple
106 # This allows the user to build the target from the CUDA file; however, bad
107 # things can happen if the CUDA source file is added to multiple targets.
108 # When performing parallel builds it is possible for the custom build
109 # command to be run more than once and in parallel causing cryptic build
110 # errors. VS runs the rules for every source file in the target, and a
111 # source can have only one rule no matter how many projects it is added to.
112 # When the rule is run from multiple targets race conditions can occur on
113 # the generated file. Eventually everything will get built, but if the user
114 # is unaware of this behavior, there may be confusion. It would be nice if
115 # this script could detect the reuse of source files across multiple targets
116 # and turn the option off for the user, but no good solution could be found.
118 # CUDA_BUILD_CUBIN (Default OFF)
119 # -- Set to ON to enable and extra compilation pass with the -cubin option in
120 # Device mode. The output is parsed and register, shared memory usage is
121 # printed during build.
123 # CUDA_BUILD_EMULATION (Default OFF for device mode)
124 # -- Set to ON for Emulation mode. -D_DEVICEEMU is defined for CUDA C files
125 # when CUDA_BUILD_EMULATION is TRUE.
127 # CUDA_GENERATED_OUTPUT_DIR (Default CMAKE_CURRENT_BINARY_DIR)
128 # -- Set to the path you wish to have the generated files placed. If it is
129 # blank output files will be placed in CMAKE_CURRENT_BINARY_DIR.
130 # Intermediate files will always be placed in
131 # CMAKE_CURRENT_BINARY_DIR/CMakeFiles.
133 # CUDA_HOST_COMPILATION_CPP (Default ON)
134 # -- Set to OFF for C compilation of host code.
136 # CUDA_HOST_COMPILER (Default CMAKE_C_COMPILER, $(VCInstallDir)/bin for VS)
137 # -- Set the host compiler to be used by nvcc. Ignored if -ccbin or
138 # --compiler-bindir is already present in the CUDA_NVCC_FLAGS or
139 # CUDA_NVCC_FLAGS_<CONFIG> variables. For Visual Studio targets
140 # $(VCInstallDir)/bin is a special value that expands out to the path when
141 # the command is run from withing VS.
144 # CUDA_NVCC_FLAGS_<CONFIG>
145 # -- Additional NVCC command line arguments. NOTE: multiple arguments must be
146 # semi-colon delimited (e.g. --compiler-options;-Wall)
148 # CUDA_PROPAGATE_HOST_FLAGS (Default ON)
149 # -- Set to ON to propagate CMAKE_{C,CXX}_FLAGS and their configuration
150 # dependent counterparts (e.g. CMAKE_C_FLAGS_DEBUG) automatically to the
151 # host compiler through nvcc's -Xcompiler flag. This helps make the
152 # generated host code match the rest of the system better. Sometimes
153 # certain flags give nvcc problems, and this will help you turn the flag
154 # propagation off. This does not affect the flags supplied directly to nvcc
155 # via CUDA_NVCC_FLAGS or through the OPTION flags specified through
156 # CUDA_ADD_LIBRARY, CUDA_ADD_EXECUTABLE, or CUDA_WRAP_SRCS. Flags used for
157 # shared library compilation are not affected by this flag.
159 # CUDA_SEPARABLE_COMPILATION (Default OFF)
160 # -- If set this will enable separable compilation for all CUDA runtime object
161 # files. If used outside of CUDA_ADD_EXECUTABLE and CUDA_ADD_LIBRARY
162 # (e.g. calling CUDA_WRAP_SRCS directly),
163 # CUDA_COMPUTE_SEPARABLE_COMPILATION_OBJECT_FILE_NAME and
164 # CUDA_LINK_SEPARABLE_COMPILATION_OBJECTS should be called.
166 # CUDA_SOURCE_PROPERTY_FORMAT
167 # -- If this source file property is set, it can override the format specified
168 # to CUDA_WRAP_SRCS (OBJ, PTX, CUBIN, or FATBIN). If an input source file
169 # is not a .cu file, setting this file will cause it to be treated as a .cu
170 # file. See documentation for set_source_files_properties on how to set
173 # CUDA_USE_STATIC_CUDA_RUNTIME (Default ON)
174 # -- When enabled the static version of the CUDA runtime library will be used
175 # in CUDA_LIBRARIES. If the version of CUDA configured doesn't support
176 # this option, then it will be silently disabled.
178 # CUDA_VERBOSE_BUILD (Default OFF)
179 # -- Set to ON to see all the commands used when building the CUDA file. When
180 # using a Makefile generator the value defaults to VERBOSE (run make
181 # VERBOSE=1 to see output), although setting CUDA_VERBOSE_BUILD to ON will
182 # always print the output.
184 # The script creates the following macros (in alphebetical order)::
186 # CUDA_ADD_CUFFT_TO_TARGET( cuda_target )
187 # -- Adds the cufft library to the target (can be any target). Handles whether
188 # you are in emulation mode or not.
190 # CUDA_ADD_CUBLAS_TO_TARGET( cuda_target )
191 # -- Adds the cublas library to the target (can be any target). Handles
192 # whether you are in emulation mode or not.
194 # CUDA_ADD_EXECUTABLE( cuda_target file0 file1 ...
195 # [WIN32] [MACOSX_BUNDLE] [EXCLUDE_FROM_ALL] [OPTIONS ...] )
196 # -- Creates an executable "cuda_target" which is made up of the files
197 # specified. All of the non CUDA C files are compiled using the standard
198 # build rules specified by CMAKE and the cuda files are compiled to object
199 # files using nvcc and the host compiler. In addition CUDA_INCLUDE_DIRS is
200 # added automatically to include_directories(). Some standard CMake target
201 # calls can be used on the target after calling this macro
202 # (e.g. set_target_properties and target_link_libraries), but setting
203 # properties that adjust compilation flags will not affect code compiled by
204 # nvcc. Such flags should be modified before calling CUDA_ADD_EXECUTABLE,
205 # CUDA_ADD_LIBRARY or CUDA_WRAP_SRCS.
207 # CUDA_ADD_LIBRARY( cuda_target file0 file1 ...
208 # [STATIC | SHARED | MODULE] [EXCLUDE_FROM_ALL] [OPTIONS ...] )
209 # -- Same as CUDA_ADD_EXECUTABLE except that a library is created.
211 # CUDA_BUILD_CLEAN_TARGET()
212 # -- Creates a convience target that deletes all the dependency files
213 # generated. You should make clean after running this target to ensure the
214 # dependency files get regenerated.
216 # CUDA_COMPILE( generated_files file0 file1 ... [STATIC | SHARED | MODULE]
218 # -- Returns a list of generated files from the input source files to be used
219 # with ADD_LIBRARY or ADD_EXECUTABLE.
221 # CUDA_COMPILE_PTX( generated_files file0 file1 ... [OPTIONS ...] )
222 # -- Returns a list of PTX files generated from the input source files.
224 # CUDA_COMPILE_FATBIN( generated_files file0 file1 ... [OPTIONS ...] )
225 # -- Returns a list of FATBIN files generated from the input source files.
227 # CUDA_COMPILE_CUBIN( generated_files file0 file1 ... [OPTIONS ...] )
228 # -- Returns a list of CUBIN files generated from the input source files.
230 # CUDA_COMPUTE_SEPARABLE_COMPILATION_OBJECT_FILE_NAME( output_file_var
233 # -- Compute the name of the intermediate link file used for separable
234 # compilation. This file name is typically passed into
235 # CUDA_LINK_SEPARABLE_COMPILATION_OBJECTS. output_file_var is produced
236 # based on cuda_target the list of objects files that need separable
237 # compilation as specified by object_files. If the object_files list is
238 # empty, then output_file_var will be empty. This function is called
239 # automatically for CUDA_ADD_LIBRARY and CUDA_ADD_EXECUTABLE. Note that
240 # this is a function and not a macro.
242 # CUDA_INCLUDE_DIRECTORIES( path0 path1 ... )
243 # -- Sets the directories that should be passed to nvcc
244 # (e.g. nvcc -Ipath0 -Ipath1 ... ). These paths usually contain other .cu
249 # CUDA_LINK_SEPARABLE_COMPILATION_OBJECTS( output_file_var cuda_target
250 # nvcc_flags object_files)
252 # -- Generates the link object required by separable compilation from the given
253 # object files. This is called automatically for CUDA_ADD_EXECUTABLE and
254 # CUDA_ADD_LIBRARY, but can be called manually when using CUDA_WRAP_SRCS
255 # directly. When called from CUDA_ADD_LIBRARY or CUDA_ADD_EXECUTABLE the
256 # nvcc_flags passed in are the same as the flags passed in via the OPTIONS
257 # argument. The only nvcc flag added automatically is the bitness flag as
258 # specified by CUDA_64_BIT_DEVICE_CODE. Note that this is a function
259 # instead of a macro.
261 # CUDA_WRAP_SRCS ( cuda_target format generated_files file0 file1 ...
262 # [STATIC | SHARED | MODULE] [OPTIONS ...] )
263 # -- This is where all the magic happens. CUDA_ADD_EXECUTABLE,
264 # CUDA_ADD_LIBRARY, CUDA_COMPILE, and CUDA_COMPILE_PTX all call this
265 # function under the hood.
267 # Given the list of files (file0 file1 ... fileN) this macro generates
268 # custom commands that generate either PTX or linkable objects (use "PTX" or
269 # "OBJ" for the format argument to switch). Files that don't end with .cu
270 # or have the HEADER_FILE_ONLY property are ignored.
272 # The arguments passed in after OPTIONS are extra command line options to
273 # give to nvcc. You can also specify per configuration options by
274 # specifying the name of the configuration followed by the options. General
275 # options must preceed configuration specific options. Not all
276 # configurations need to be specified, only the ones provided will be used.
278 # OPTIONS -DFLAG=2 "-DFLAG_OTHER=space in flag"
280 # RELEASE --use_fast_math
281 # RELWITHDEBINFO --use_fast_math;-g
282 # MINSIZEREL --use_fast_math
284 # For certain configurations (namely VS generating object files with
285 # CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE set to ON), no generated file will
286 # be produced for the given cuda file. This is because when you add the
287 # cuda file to Visual Studio it knows that this file produces an object file
288 # and will link in the resulting object file automatically.
290 # This script will also generate a separate cmake script that is used at
291 # build time to invoke nvcc. This is for several reasons.
293 # 1. nvcc can return negative numbers as return values which confuses
294 # Visual Studio into thinking that the command succeeded. The script now
295 # checks the error codes and produces errors when there was a problem.
297 # 2. nvcc has been known to not delete incomplete results when it
298 # encounters problems. This confuses build systems into thinking the
299 # target was generated when in fact an unusable file exists. The script
300 # now deletes the output files if there was an error.
302 # 3. By putting all the options that affect the build into a file and then
303 # make the build rule dependent on the file, the output files will be
304 # regenerated when the options change.
306 # This script also looks at optional arguments STATIC, SHARED, or MODULE to
307 # determine when to target the object compilation for a shared library.
308 # BUILD_SHARED_LIBS is ignored in CUDA_WRAP_SRCS, but it is respected in
309 # CUDA_ADD_LIBRARY. On some systems special flags are added for building
310 # objects intended for shared libraries. A preprocessor macro,
311 # <target_name>_EXPORTS is defined when a shared library compilation is
314 # Flags passed into add_definitions with -D or /D are passed along to nvcc.
318 # The script defines the following variables::
320 # CUDA_VERSION_MAJOR -- The major version of cuda as reported by nvcc.
321 # CUDA_VERSION_MINOR -- The minor version.
323 # CUDA_VERSION_STRING -- CUDA_VERSION_MAJOR.CUDA_VERSION_MINOR
325 # CUDA_TOOLKIT_ROOT_DIR -- Path to the CUDA Toolkit (defined if not set).
326 # CUDA_SDK_ROOT_DIR -- Path to the CUDA SDK. Use this to find files in the
327 # SDK. This script will not directly support finding
328 # specific libraries or headers, as that isn't
329 # supported by NVIDIA. If you want to change
330 # libraries when the path changes see the
331 # FindCUDA.cmake script for an example of how to clear
332 # these variables. There are also examples of how to
333 # use the CUDA_SDK_ROOT_DIR to locate headers or
334 # libraries, if you so choose (at your own risk).
335 # CUDA_INCLUDE_DIRS -- Include directory for cuda headers. Added automatically
336 # for CUDA_ADD_EXECUTABLE and CUDA_ADD_LIBRARY.
337 # CUDA_LIBRARIES -- Cuda RT library.
338 # CUDA_CUFFT_LIBRARIES -- Device or emulation library for the Cuda FFT
339 # implementation (alternative to:
340 # CUDA_ADD_CUFFT_TO_TARGET macro)
341 # CUDA_CUBLAS_LIBRARIES -- Device or emulation library for the Cuda BLAS
342 # implementation (alterative to:
343 # CUDA_ADD_CUBLAS_TO_TARGET macro).
344 # CUDA_cudart_static_LIBRARY -- Statically linkable cuda runtime library.
345 # Only available for CUDA version 5.5+
346 # CUDA_cupti_LIBRARY -- CUDA Profiling Tools Interface library.
347 # Only available for CUDA version 4.0+.
348 # CUDA_curand_LIBRARY -- CUDA Random Number Generation library.
349 # Only available for CUDA version 3.2+.
350 # CUDA_cusolver_LIBRARY -- CUDA Direct Solver library.
351 # Only available for CUDA version 7.0+.
352 # CUDA_cusparse_LIBRARY -- CUDA Sparse Matrix library.
353 # Only available for CUDA version 3.2+.
354 # CUDA_npp_LIBRARY -- NVIDIA Performance Primitives lib.
355 # Only available for CUDA version 4.0+.
356 # CUDA_nppc_LIBRARY -- NVIDIA Performance Primitives lib (core).
357 # Only available for CUDA version 5.5+.
358 # CUDA_nppi_LIBRARY -- NVIDIA Performance Primitives lib (image processing).
359 # Only available for CUDA version 5.5+.
360 # CUDA_npps_LIBRARY -- NVIDIA Performance Primitives lib (signal processing).
361 # Only available for CUDA version 5.5+.
362 # CUDA_nvcuvenc_LIBRARY -- CUDA Video Encoder library.
363 # Only available for CUDA version 3.2+.
365 # CUDA_nvcuvid_LIBRARY -- CUDA Video Decoder library.
366 # Only available for CUDA version 3.2+.
370 # James Bigler, NVIDIA Corp (nvidia.com - jbigler)
371 # Abe Stephens, SCI Institute -- http://www.sci.utah.edu/~abe/FindCuda.html
373 # Copyright (c) 2008 - 2009 NVIDIA Corporation. All rights reserved.
375 # Copyright (c) 2007-2009
376 # Scientific Computing and Imaging Institute, University of Utah
378 # This code is licensed under the MIT License. See the FindCUDA.cmake script
379 # for the text of the license.
383 # License for the specific language governing rights and limitations under
384 # Permission is hereby granted, free of charge, to any person obtaining a
385 # copy of this software and associated documentation files (the "Software"),
386 # to deal in the Software without restriction, including without limitation
387 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
388 # and/or sell copies of the Software, and to permit persons to whom the
389 # Software is furnished to do so, subject to the following conditions:
391 # The above copyright notice and this permission notice shall be included
392 # in all copies or substantial portions of the Software.
394 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
395 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
396 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
397 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
398 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
399 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
400 # DEALINGS IN THE SOFTWARE.
402 ###############################################################################
406 # This macro helps us find the location of helper files we will need the full path to
407 macro(CUDA_FIND_HELPER_FILE _name _extension)
408 set(_full_name "${_name}.${_extension}")
409 # CMAKE_CURRENT_LIST_FILE contains the full path to the file currently being
410 # processed. Using this variable, we can pull out the current path, and
411 # provide a way to get access to the other files we need local to here.
412 get_filename_component(CMAKE_CURRENT_LIST_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
413 set(CUDA_${_name} "${CMAKE_CURRENT_LIST_DIR}/FindCUDA/${_full_name}")
414 if(NOT EXISTS "${CUDA_${_name}}")
415 set(error_message "${_full_name} not found in ${CMAKE_CURRENT_LIST_DIR}/FindCUDA")
416 if(CUDA_FIND_REQUIRED)
417 message(FATAL_ERROR "${error_message}")
419 if(NOT CUDA_FIND_QUIETLY)
420 message(STATUS "${error_message}")
424 # Set this variable as internal, so the user isn't bugged with it.
425 set(CUDA_${_name} ${CUDA_${_name}} CACHE INTERNAL "Location of ${_full_name}" FORCE)
428 #####################################################################
429 ## CUDA_INCLUDE_NVCC_DEPENDENCIES
432 # So we want to try and include the dependency file if it exists. If
433 # it doesn't exist then we need to create an empty one, so we can
436 # If it does exist, then we need to check to see if all the files it
437 # depends on exist. If they don't then we should clear the dependency
438 # file and regenerate it later. This covers the case where a header
439 # file has disappeared or moved.
441 macro(CUDA_INCLUDE_NVCC_DEPENDENCIES dependency_file)
442 set(CUDA_NVCC_DEPEND)
443 set(CUDA_NVCC_DEPEND_REGENERATE FALSE)
446 # Include the dependency file. Create it first if it doesn't exist . The
447 # INCLUDE puts a dependency that will force CMake to rerun and bring in the
448 # new info when it changes. DO NOT REMOVE THIS (as I did and spent a few
449 # hours figuring out why it didn't work.
450 if(NOT EXISTS ${dependency_file})
451 file(WRITE ${dependency_file} "#FindCUDA.cmake generated file. Do not edit.\n")
453 # Always include this file to force CMake to run again next
454 # invocation and rebuild the dependencies.
455 #message("including dependency_file = ${dependency_file}")
456 include(${dependency_file})
458 # Now we need to verify the existence of all the included files
459 # here. If they aren't there we need to just blank this variable and
460 # make the file regenerate again.
461 # if(DEFINED CUDA_NVCC_DEPEND)
462 # message("CUDA_NVCC_DEPEND set")
464 # message("CUDA_NVCC_DEPEND NOT set")
467 #message("CUDA_NVCC_DEPEND found")
468 foreach(f ${CUDA_NVCC_DEPEND})
469 # message("searching for ${f}")
471 #message("file ${f} not found")
472 set(CUDA_NVCC_DEPEND_REGENERATE TRUE)
476 #message("CUDA_NVCC_DEPEND false")
477 # No dependencies, so regenerate the file.
478 set(CUDA_NVCC_DEPEND_REGENERATE TRUE)
481 #message("CUDA_NVCC_DEPEND_REGENERATE = ${CUDA_NVCC_DEPEND_REGENERATE}")
482 # No incoming dependencies, so we need to generate them. Make the
483 # output depend on the dependency file itself, which should cause the
485 if(CUDA_NVCC_DEPEND_REGENERATE)
486 set(CUDA_NVCC_DEPEND ${dependency_file})
487 #message("Generating an empty dependency_file: ${dependency_file}")
488 file(WRITE ${dependency_file} "#FindCUDA.cmake generated file. Do not edit.\n")
493 ###############################################################################
494 ###############################################################################
495 # Setup variables' defaults
496 ###############################################################################
497 ###############################################################################
499 # Allow the user to specify if the device code is supposed to be 32 or 64 bit.
500 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
501 set(CUDA_64_BIT_DEVICE_CODE_DEFAULT ON)
503 set(CUDA_64_BIT_DEVICE_CODE_DEFAULT OFF)
505 option(CUDA_64_BIT_DEVICE_CODE "Compile device code in 64 bit mode" ${CUDA_64_BIT_DEVICE_CODE_DEFAULT})
507 # Attach the build rule to the source file in VS. This option
508 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)
510 # Prints out extra information about the cuda file during compilation
511 option(CUDA_BUILD_CUBIN "Generate and parse .cubin files in Device mode." OFF)
513 # Set whether we are using emulation or device mode.
514 option(CUDA_BUILD_EMULATION "Build in Emulation mode" OFF)
516 # Where to put the generated output.
517 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")
519 # Parse HOST_COMPILATION mode.
520 option(CUDA_HOST_COMPILATION_CPP "Generated file extension" ON)
522 # Extra user settable flags
523 set(CUDA_NVCC_FLAGS "" CACHE STRING "Semi-colon delimit multiple arguments.")
525 if(CMAKE_GENERATOR MATCHES "Visual Studio")
526 set(CUDA_HOST_COMPILER "$(VCInstallDir)bin" CACHE FILEPATH "Host side compiler used by NVCC")
529 AND "${CMAKE_C_COMPILER_ID}" MATCHES "Clang"
530 AND "${CMAKE_C_COMPILER}" MATCHES "/cc$")
531 # Using cc which is symlink to clang may let NVCC think it is GCC and issue
532 # unhandled -dumpspecs option to clang. Also in case neither
533 # CMAKE_C_COMPILER is defined (project does not use C language) nor
534 # CUDA_HOST_COMPILER is specified manually we should skip -ccbin and let
535 # nvcc use its own default C compiler.
536 # Only care about this on APPLE with clang to avoid
537 # following symlinks to things like ccache
538 if(DEFINED CMAKE_C_COMPILER AND NOT DEFINED CUDA_HOST_COMPILER)
539 get_filename_component(c_compiler_realpath "${CMAKE_C_COMPILER}" REALPATH)
540 # if the real path does not end up being clang then
541 # go back to using CMAKE_C_COMPILER
542 if(NOT "${c_compiler_realpath}" MATCHES "/clang$")
543 set(c_compiler_realpath "${CMAKE_C_COMPILER}")
546 set(c_compiler_realpath "")
548 set(CUDA_HOST_COMPILER "${c_compiler_realpath}" CACHE FILEPATH "Host side compiler used by NVCC")
550 set(CUDA_HOST_COMPILER "${CMAKE_C_COMPILER}"
551 CACHE FILEPATH "Host side compiler used by NVCC")
555 # Propagate the host flags to the host compiler via -Xcompiler
556 option(CUDA_PROPAGATE_HOST_FLAGS "Propage C/CXX_FLAGS and friends to the host compiler via -Xcompile" ON)
558 # Enable CUDA_SEPARABLE_COMPILATION
559 option(CUDA_SEPARABLE_COMPILATION "Compile CUDA objects with separable compilation enabled. Requires CUDA 5.0+" OFF)
561 # Specifies whether the commands used when compiling the .cu file will be printed out.
562 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)
565 CUDA_64_BIT_DEVICE_CODE
566 CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE
567 CUDA_GENERATED_OUTPUT_DIR
568 CUDA_HOST_COMPILATION_CPP
570 CUDA_PROPAGATE_HOST_FLAGS
574 CUDA_SEPARABLE_COMPILATION
577 # Makefile and similar generators don't define CMAKE_CONFIGURATION_TYPES, so we
578 # need to add another entry for the CMAKE_BUILD_TYPE. We also need to add the
579 # standerd set of 4 build types (Debug, MinSizeRel, Release, and RelWithDebInfo)
580 # for completeness. We need run this loop in order to accomodate the addition
581 # of extra configuration types. Duplicate entries will be removed by
583 set(CUDA_configuration_types ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE} Debug MinSizeRel Release RelWithDebInfo)
584 list(REMOVE_DUPLICATES CUDA_configuration_types)
585 foreach(config ${CUDA_configuration_types})
586 string(TOUPPER ${config} config_upper)
587 set(CUDA_NVCC_FLAGS_${config_upper} "" CACHE STRING "Semi-colon delimit multiple arguments.")
588 mark_as_advanced(CUDA_NVCC_FLAGS_${config_upper})
591 ###############################################################################
592 ###############################################################################
593 # Locate CUDA, Set Build Type, etc.
594 ###############################################################################
595 ###############################################################################
597 macro(cuda_unset_include_and_libraries)
598 unset(CUDA_TOOLKIT_INCLUDE CACHE)
599 unset(CUDA_CUDART_LIBRARY CACHE)
600 unset(CUDA_CUDA_LIBRARY CACHE)
601 # Make sure you run this before you unset CUDA_VERSION.
602 if(CUDA_VERSION VERSION_EQUAL "3.0")
603 # This only existed in the 3.0 version of the CUDA toolkit
604 unset(CUDA_CUDARTEMU_LIBRARY CACHE)
606 unset(CUDA_cudart_static_LIBRARY CACHE)
607 unset(CUDA_cublas_LIBRARY CACHE)
608 unset(CUDA_cublasemu_LIBRARY CACHE)
609 unset(CUDA_cufft_LIBRARY CACHE)
610 unset(CUDA_cufftemu_LIBRARY CACHE)
611 unset(CUDA_cupti_LIBRARY CACHE)
612 unset(CUDA_curand_LIBRARY CACHE)
613 unset(CUDA_cusolver_LIBRARY CACHE)
614 unset(CUDA_cusparse_LIBRARY CACHE)
615 unset(CUDA_npp_LIBRARY CACHE)
616 unset(CUDA_nppc_LIBRARY CACHE)
617 unset(CUDA_nppi_LIBRARY CACHE)
618 unset(CUDA_npps_LIBRARY CACHE)
619 unset(CUDA_nvcuvenc_LIBRARY CACHE)
620 unset(CUDA_nvcuvid_LIBRARY CACHE)
622 unset(CUDA_USE_STATIC_CUDA_RUNTIME CACHE)
625 # Check to see if the CUDA_TOOLKIT_ROOT_DIR and CUDA_SDK_ROOT_DIR have changed,
626 # if they have then clear the cache variables, so that will be detected again.
627 if(NOT "${CUDA_TOOLKIT_ROOT_DIR}" STREQUAL "${CUDA_TOOLKIT_ROOT_DIR_INTERNAL}")
628 unset(CUDA_TOOLKIT_TARGET_DIR CACHE)
629 unset(CUDA_NVCC_EXECUTABLE CACHE)
630 cuda_unset_include_and_libraries()
631 unset(CUDA_VERSION CACHE)
634 if(NOT "${CUDA_TOOLKIT_TARGET_DIR}" STREQUAL "${CUDA_TOOLKIT_TARGET_DIR_INTERNAL}")
635 cuda_unset_include_and_libraries()
638 if(NOT "${CUDA_SDK_ROOT_DIR}" STREQUAL "${CUDA_SDK_ROOT_DIR_INTERNAL}")
639 # No specific variables to catch. Use this kind of code before calling
640 # find_package(CUDA) to clean up any variables that may depend on this path.
642 # unset(MY_SPECIAL_CUDA_SDK_INCLUDE_DIR CACHE)
643 # unset(MY_SPECIAL_CUDA_SDK_LIBRARY CACHE)
646 # Search for the cuda distribution.
647 if(NOT CUDA_TOOLKIT_ROOT_DIR)
649 # Search in the CUDA_BIN_PATH first.
650 find_path(CUDA_TOOLKIT_ROOT_DIR
655 PATH_SUFFIXES bin bin64
656 DOC "Toolkit location."
659 # Now search default paths
660 find_path(CUDA_TOOLKIT_ROOT_DIR
664 DOC "Toolkit location."
667 if (CUDA_TOOLKIT_ROOT_DIR)
668 string(REGEX REPLACE "[/\\\\]?bin[64]*[/\\\\]?$" "" CUDA_TOOLKIT_ROOT_DIR ${CUDA_TOOLKIT_ROOT_DIR})
669 # We need to force this back into the cache.
670 set(CUDA_TOOLKIT_ROOT_DIR ${CUDA_TOOLKIT_ROOT_DIR} CACHE PATH "Toolkit location." FORCE)
672 if (NOT EXISTS ${CUDA_TOOLKIT_ROOT_DIR})
673 if(CUDA_FIND_REQUIRED)
674 message(FATAL_ERROR "Specify CUDA_TOOLKIT_ROOT_DIR")
675 elseif(NOT CUDA_FIND_QUIETLY)
676 message("CUDA_TOOLKIT_ROOT_DIR not found or specified")
681 # CUDA_NVCC_EXECUTABLE
682 find_program(CUDA_NVCC_EXECUTABLE
684 PATHS "${CUDA_TOOLKIT_ROOT_DIR}"
687 PATH_SUFFIXES bin bin64
690 # Search default search paths, after we search our own set of paths.
691 find_program(CUDA_NVCC_EXECUTABLE nvcc)
692 mark_as_advanced(CUDA_NVCC_EXECUTABLE)
694 if(CUDA_NVCC_EXECUTABLE AND NOT CUDA_VERSION)
695 # Compute the version.
696 execute_process (COMMAND ${CUDA_NVCC_EXECUTABLE} "--version" OUTPUT_VARIABLE NVCC_OUT)
697 string(REGEX REPLACE ".*release ([0-9]+)\\.([0-9]+).*" "\\1" CUDA_VERSION_MAJOR ${NVCC_OUT})
698 string(REGEX REPLACE ".*release ([0-9]+)\\.([0-9]+).*" "\\2" CUDA_VERSION_MINOR ${NVCC_OUT})
699 set(CUDA_VERSION "${CUDA_VERSION_MAJOR}.${CUDA_VERSION_MINOR}" CACHE STRING "Version of CUDA as computed from nvcc.")
700 mark_as_advanced(CUDA_VERSION)
702 # Need to set these based off of the cached value
703 string(REGEX REPLACE "([0-9]+)\\.([0-9]+).*" "\\1" CUDA_VERSION_MAJOR "${CUDA_VERSION}")
704 string(REGEX REPLACE "([0-9]+)\\.([0-9]+).*" "\\2" CUDA_VERSION_MINOR "${CUDA_VERSION}")
707 # Always set this convenience variable
708 set(CUDA_VERSION_STRING "${CUDA_VERSION}")
710 # Support for arm cross compilation with CUDA 5.5
711 if(CUDA_VERSION VERSION_GREATER "5.0" AND CMAKE_CROSSCOMPILING AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm" AND EXISTS "${CUDA_TOOLKIT_ROOT_DIR}/targets/armv7-linux-gnueabihf")
712 set(CUDA_TOOLKIT_TARGET_DIR "${CUDA_TOOLKIT_ROOT_DIR}/targets/armv7-linux-gnueabihf" CACHE PATH "Toolkit target location.")
714 set(CUDA_TOOLKIT_TARGET_DIR "${CUDA_TOOLKIT_ROOT_DIR}" CACHE PATH "Toolkit target location.")
716 mark_as_advanced(CUDA_TOOLKIT_TARGET_DIR)
718 # Target CPU architecture
719 if(CUDA_VERSION VERSION_GREATER "5.0" AND CMAKE_CROSSCOMPILING AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm")
720 set(_cuda_target_cpu_arch_initial "ARM")
722 set(_cuda_target_cpu_arch_initial "")
724 set(CUDA_TARGET_CPU_ARCH ${_cuda_target_cpu_arch_initial} CACHE STRING "Specify the name of the class of CPU architecture for which the input files must be compiled.")
725 mark_as_advanced(CUDA_TARGET_CPU_ARCH)
727 # CUDA_TOOLKIT_INCLUDE
728 find_path(CUDA_TOOLKIT_INCLUDE
729 device_functions.h # Header included in toolkit
730 PATHS "${CUDA_TOOLKIT_TARGET_DIR}" "${CUDA_TOOLKIT_ROOT_DIR}"
733 PATH_SUFFIXES include
736 # Search default search paths, after we search our own set of paths.
737 find_path(CUDA_TOOLKIT_INCLUDE device_functions.h)
738 mark_as_advanced(CUDA_TOOLKIT_INCLUDE)
740 # Set the user list of include dir to nothing to initialize it.
741 set (CUDA_NVCC_INCLUDE_ARGS_USER "")
742 set (CUDA_INCLUDE_DIRS ${CUDA_TOOLKIT_INCLUDE})
744 macro(cuda_find_library_local_first_with_path_ext _var _names _doc _path_ext )
745 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
746 # CUDA 3.2+ on Windows moved the library directories, so we need the new
748 set(_cuda_64bit_lib_dir "${_path_ext}lib/x64" "${_path_ext}lib64" "${_path_ext}libx64" )
750 # CUDA 3.2+ on Windows moved the library directories, so we need to new
751 # (lib/Win32) and the old path (lib).
754 PATHS "${CUDA_TOOLKIT_TARGET_DIR}" "${CUDA_TOOLKIT_ROOT_DIR}"
757 PATH_SUFFIXES ${_cuda_64bit_lib_dir} "${_path_ext}lib/Win32" "${_path_ext}lib" "${_path_ext}libWin32"
761 # Search default search paths, after we search our own set of paths.
764 PATHS "/usr/lib/nvidia-current"
769 macro(cuda_find_library_local_first _var _names _doc)
770 cuda_find_library_local_first_with_path_ext( "${_var}" "${_names}" "${_doc}" "" )
773 macro(find_library_local_first _var _names _doc )
774 cuda_find_library_local_first( "${_var}" "${_names}" "${_doc}" "" )
779 cuda_find_library_local_first(CUDA_CUDART_LIBRARY cudart "\"cudart\" library")
780 if(CUDA_VERSION VERSION_EQUAL "3.0")
781 # The cudartemu library only existed for the 3.0 version of CUDA.
782 cuda_find_library_local_first(CUDA_CUDARTEMU_LIBRARY cudartemu "\"cudartemu\" library")
784 CUDA_CUDARTEMU_LIBRARY
787 if(NOT CUDA_VERSION VERSION_LESS "5.5")
788 cuda_find_library_local_first(CUDA_cudart_static_LIBRARY cudart_static "static CUDA runtime library")
789 mark_as_advanced(CUDA_cudart_static_LIBRARY)
791 if(CUDA_cudart_static_LIBRARY)
792 # Set whether to use the static cuda runtime.
793 option(CUDA_USE_STATIC_CUDA_RUNTIME "Use the static version of the CUDA runtime library if available" ON)
795 option(CUDA_USE_STATIC_CUDA_RUNTIME "Use the static version of the CUDA runtime library if available" OFF)
798 if(CUDA_USE_STATIC_CUDA_RUNTIME)
800 # Check for the dependent libraries. Here we look for pthreads.
801 if (DEFINED CMAKE_THREAD_PREFER_PTHREAD)
802 set(_cuda_cmake_thread_prefer_pthread ${CMAKE_THREAD_PREFER_PTHREAD})
804 set(CMAKE_THREAD_PREFER_PTHREAD 1)
806 # Many of the FindXYZ CMake comes with makes use of try_compile with int main(){return 0;}
807 # as the source file. Unfortunately this causes a warning with -Wstrict-prototypes and
808 # -Werror causes the try_compile to fail. We will just temporarily disable other flags
809 # when doing the find_package command here.
810 set(_cuda_cmake_c_flags ${CMAKE_C_FLAGS})
811 set(CMAKE_C_FLAGS "-fPIC")
812 find_package(Threads REQUIRED)
813 set(CMAKE_C_FLAGS ${_cuda_cmake_c_flags})
815 if (DEFINED _cuda_cmake_thread_prefer_pthread)
816 set(CMAKE_THREAD_PREFER_PTHREAD ${_cuda_cmake_thread_prefer_pthread})
817 unset(_cuda_cmake_thread_prefer_pthread)
819 unset(CMAKE_THREAD_PREFER_PTHREAD)
822 # Here is librt that has things such as, clock_gettime, shm_open, and shm_unlink.
823 find_library(CUDA_rt_LIBRARY rt)
824 find_library(CUDA_dl_LIBRARY dl)
825 if (NOT CUDA_rt_LIBRARY)
826 message(WARNING "Expecting to find librt for libcudart_static, but didn't find it.")
828 if (NOT CUDA_dl_LIBRARY)
829 message(WARNING "Expecting to find libdl for libcudart_static, but didn't find it.")
835 # CUPTI library showed up in cuda toolkit 4.0
836 if(NOT CUDA_VERSION VERSION_LESS "4.0")
837 cuda_find_library_local_first_with_path_ext(CUDA_cupti_LIBRARY cupti "\"cupti\" library" "extras/CUPTI/")
838 mark_as_advanced(CUDA_cupti_LIBRARY)
841 # Set the CUDA_LIBRARIES variable. This is the set of stuff to link against if you are
842 # using the CUDA runtime. For the dynamic version of the runtime, most of the
843 # dependencies are brough in, but for the static version there are additional libraries
844 # and linker commands needed.
845 # Initialize to empty
848 # If we are using emulation mode and we found the cudartemu library then use
849 # that one instead of cudart.
850 if(CUDA_BUILD_EMULATION AND CUDA_CUDARTEMU_LIBRARY)
851 list(APPEND CUDA_LIBRARIES ${CUDA_CUDARTEMU_LIBRARY})
852 elseif(CUDA_USE_STATIC_CUDA_RUNTIME AND CUDA_cudart_static_LIBRARY)
853 list(APPEND CUDA_LIBRARIES ${CUDA_cudart_static_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})
855 list(APPEND CUDA_LIBRARIES ${CUDA_rt_LIBRARY})
858 list(APPEND CUDA_LIBRARIES ${CUDA_dl_LIBRARY})
861 # We need to add the default path to the driver (libcuda.dylib) as an rpath, so that
862 # the static cuda runtime can find it at runtime.
863 list(APPEND CUDA_LIBRARIES -Wl,-rpath,/usr/local/cuda/lib)
866 list(APPEND CUDA_LIBRARIES ${CUDA_CUDART_LIBRARY})
869 # 1.1 toolkit on linux doesn't appear to have a separate library on
871 cuda_find_library_local_first(CUDA_CUDA_LIBRARY cuda "\"cuda\" library (older versions only).")
878 #######################
879 # Look for some of the toolkit helper libraries
880 macro(FIND_CUDA_HELPER_LIBS _name)
881 cuda_find_library_local_first(CUDA_${_name}_LIBRARY ${_name} "\"${_name}\" library")
882 mark_as_advanced(CUDA_${_name}_LIBRARY)
885 #######################
886 # Disable emulation for v3.1 onward
887 if(CUDA_VERSION VERSION_GREATER "3.0")
888 if(CUDA_BUILD_EMULATION)
889 message(FATAL_ERROR "CUDA_BUILD_EMULATION is not supported in version 3.1 and onwards. You must disable it to proceed. You have version ${CUDA_VERSION}.")
893 # Search for additional CUDA toolkit libraries.
894 if(CUDA_VERSION VERSION_LESS "3.1")
895 # Emulation libraries aren't available in version 3.1 onward.
896 find_cuda_helper_libs(cufftemu)
897 find_cuda_helper_libs(cublasemu)
899 find_cuda_helper_libs(cufft)
900 find_cuda_helper_libs(cublas)
901 if(NOT CUDA_VERSION VERSION_LESS "3.2")
902 # cusparse showed up in version 3.2
903 find_cuda_helper_libs(cusparse)
904 find_cuda_helper_libs(curand)
906 find_cuda_helper_libs(nvcuvenc)
907 find_cuda_helper_libs(nvcuvid)
910 if(CUDA_VERSION VERSION_GREATER "5.0")
911 # In CUDA 5.5 NPP was splitted onto 3 separate libraries.
912 find_cuda_helper_libs(nppc)
913 find_cuda_helper_libs(nppi)
914 find_cuda_helper_libs(npps)
915 set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppi_LIBRARY};${CUDA_npps_LIBRARY}")
916 elseif(NOT CUDA_VERSION VERSION_LESS "4.0")
917 find_cuda_helper_libs(npp)
919 if(NOT CUDA_VERSION VERSION_LESS "7.0")
920 # cusolver showed up in version 7.0
921 find_cuda_helper_libs(cusolver)
924 if (CUDA_BUILD_EMULATION)
925 set(CUDA_CUFFT_LIBRARIES ${CUDA_cufftemu_LIBRARY})
926 set(CUDA_CUBLAS_LIBRARIES ${CUDA_cublasemu_LIBRARY})
928 set(CUDA_CUFFT_LIBRARIES ${CUDA_cufft_LIBRARY})
929 set(CUDA_CUBLAS_LIBRARIES ${CUDA_cublas_LIBRARY})
932 ########################
933 # Look for the SDK stuff. As of CUDA 3.0 NVSDKCUDA_ROOT has been replaced with
934 # NVSDKCOMPUTE_ROOT with the old CUDA C contents moved into the C subdirectory
935 find_path(CUDA_SDK_ROOT_DIR common/inc/cutil.h
937 "$ENV{NVSDKCOMPUTE_ROOT}/C"
939 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\NVIDIA Corporation\\Installed Products\\NVIDIA SDK 10\\Compute;InstallDir]"
941 "/Developer/GPU\ Computing/C"
944 # Keep the CUDA_SDK_ROOT_DIR first in order to be able to override the
945 # environment variables.
946 set(CUDA_SDK_SEARCH_PATH
947 "${CUDA_SDK_ROOT_DIR}"
948 "${CUDA_TOOLKIT_ROOT_DIR}/local/NVSDK0.2"
949 "${CUDA_TOOLKIT_ROOT_DIR}/NVSDK0.2"
950 "${CUDA_TOOLKIT_ROOT_DIR}/NV_CUDA_SDK"
951 "$ENV{HOME}/NVIDIA_CUDA_SDK"
952 "$ENV{HOME}/NVIDIA_CUDA_SDK_MACOSX"
956 # Example of how to find an include file from the CUDA_SDK_ROOT_DIR
958 # find_path(CUDA_CUT_INCLUDE_DIR
960 # PATHS ${CUDA_SDK_SEARCH_PATH}
961 # PATH_SUFFIXES "common/inc"
962 # DOC "Location of cutil.h"
965 # # Now search system paths
966 # find_path(CUDA_CUT_INCLUDE_DIR cutil.h DOC "Location of cutil.h")
968 # mark_as_advanced(CUDA_CUT_INCLUDE_DIR)
971 # Example of how to find a library in the CUDA_SDK_ROOT_DIR
973 # # cutil library is called cutil64 for 64 bit builds on windows. We don't want
974 # # to get these confused, so we are setting the name based on the word size of
977 # if(CMAKE_SIZEOF_VOID_P EQUAL 8)
978 # set(cuda_cutil_name cutil64)
980 # set(cuda_cutil_name cutil32)
983 # find_library(CUDA_CUT_LIBRARY
984 # NAMES cutil ${cuda_cutil_name}
985 # PATHS ${CUDA_SDK_SEARCH_PATH}
986 # # The new version of the sdk shows up in common/lib, but the old one is in lib
987 # PATH_SUFFIXES "common/lib" "lib"
988 # DOC "Location of cutil library"
991 # # Now search system paths
992 # find_library(CUDA_CUT_LIBRARY NAMES cutil ${cuda_cutil_name} DOC "Location of cutil library")
993 # mark_as_advanced(CUDA_CUT_LIBRARY)
994 # set(CUDA_CUT_LIBRARIES ${CUDA_CUT_LIBRARY})
998 #############################
999 # Check for required components
1000 set(CUDA_FOUND TRUE)
1002 set(CUDA_TOOLKIT_ROOT_DIR_INTERNAL "${CUDA_TOOLKIT_ROOT_DIR}" CACHE INTERNAL
1003 "This is the value of the last time CUDA_TOOLKIT_ROOT_DIR was set successfully." FORCE)
1004 set(CUDA_TOOLKIT_TARGET_DIR_INTERNAL "${CUDA_TOOLKIT_TARGET_DIR}" CACHE INTERNAL
1005 "This is the value of the last time CUDA_TOOLKIT_TARGET_DIR was set successfully." FORCE)
1006 set(CUDA_SDK_ROOT_DIR_INTERNAL "${CUDA_SDK_ROOT_DIR}" CACHE INTERNAL
1007 "This is the value of the last time CUDA_SDK_ROOT_DIR was set successfully." FORCE)
1009 include(FindPackageHandleStandardArgs)
1010 find_package_handle_standard_args(CUDA
1012 CUDA_TOOLKIT_ROOT_DIR
1013 CUDA_NVCC_EXECUTABLE
1022 ###############################################################################
1023 ###############################################################################
1025 ###############################################################################
1026 ###############################################################################
1028 ###############################################################################
1029 # Add include directories to pass to the nvcc command.
1030 macro(CUDA_INCLUDE_DIRECTORIES)
1031 foreach(dir ${ARGN})
1032 list(APPEND CUDA_NVCC_INCLUDE_ARGS_USER -I${dir})
1037 ##############################################################################
1038 cuda_find_helper_file(parse_cubin cmake)
1039 cuda_find_helper_file(make2cmake cmake)
1040 cuda_find_helper_file(run_nvcc cmake)
1042 ##############################################################################
1043 # Separate the OPTIONS out from the sources
1045 macro(CUDA_GET_SOURCES_AND_OPTIONS _sources _cmake_options _options)
1047 set( ${_cmake_options} )
1049 set( _found_options FALSE )
1050 foreach(arg ${ARGN})
1051 if("x${arg}" STREQUAL "xOPTIONS")
1052 set( _found_options TRUE )
1054 "x${arg}" STREQUAL "xWIN32" OR
1055 "x${arg}" STREQUAL "xMACOSX_BUNDLE" OR
1056 "x${arg}" STREQUAL "xEXCLUDE_FROM_ALL" OR
1057 "x${arg}" STREQUAL "xSTATIC" OR
1058 "x${arg}" STREQUAL "xSHARED" OR
1059 "x${arg}" STREQUAL "xMODULE"
1061 list(APPEND ${_cmake_options} ${arg})
1063 if ( _found_options )
1064 list(APPEND ${_options} ${arg})
1066 # Assume this is a file
1067 list(APPEND ${_sources} ${arg})
1073 ##############################################################################
1074 # Parse the OPTIONS from ARGN and set the variables prefixed by _option_prefix
1076 macro(CUDA_PARSE_NVCC_OPTIONS _option_prefix)
1077 set( _found_config )
1078 foreach(arg ${ARGN})
1079 # Determine if we are dealing with a perconfiguration flag
1080 foreach(config ${CUDA_configuration_types})
1081 string(TOUPPER ${config} config_upper)
1082 if (arg STREQUAL "${config_upper}")
1083 set( _found_config _${arg})
1084 # Set arg to nothing to keep it from being processed further
1090 list(APPEND ${_option_prefix}${_found_config} "${arg}")
1095 ##############################################################################
1096 # Helper to add the include directory for CUDA only once
1097 function(CUDA_ADD_CUDA_INCLUDE_ONCE)
1098 get_directory_property(_include_directories INCLUDE_DIRECTORIES)
1100 if(_include_directories)
1101 foreach(dir ${_include_directories})
1102 if("${dir}" STREQUAL "${CUDA_INCLUDE_DIRS}")
1108 include_directories(${CUDA_INCLUDE_DIRS})
1112 function(CUDA_BUILD_SHARED_LIBRARY shared_flag)
1113 set(cmake_args ${ARGN})
1114 # If SHARED, MODULE, or STATIC aren't already in the list of arguments, then
1115 # add SHARED or STATIC based on the value of BUILD_SHARED_LIBS.
1116 list(FIND cmake_args SHARED _cuda_found_SHARED)
1117 list(FIND cmake_args MODULE _cuda_found_MODULE)
1118 list(FIND cmake_args STATIC _cuda_found_STATIC)
1119 if( _cuda_found_SHARED GREATER -1 OR
1120 _cuda_found_MODULE GREATER -1 OR
1121 _cuda_found_STATIC GREATER -1)
1122 set(_cuda_build_shared_libs)
1124 if (BUILD_SHARED_LIBS)
1125 set(_cuda_build_shared_libs SHARED)
1127 set(_cuda_build_shared_libs STATIC)
1130 set(${shared_flag} ${_cuda_build_shared_libs} PARENT_SCOPE)
1133 ##############################################################################
1134 # Helper to avoid clashes of files with the same basename but different paths.
1135 # This doesn't attempt to do exactly what CMake internals do, which is to only
1136 # add this path when there is a conflict, since by the time a second collision
1137 # in names is detected it's already too late to fix the first one. For
1138 # consistency sake the relative path will be added to all files.
1139 function(CUDA_COMPUTE_BUILD_PATH path build_path)
1140 #message("CUDA_COMPUTE_BUILD_PATH([${path}] ${build_path})")
1141 # Only deal with CMake style paths from here on out
1142 file(TO_CMAKE_PATH "${path}" bpath)
1143 if (IS_ABSOLUTE "${bpath}")
1144 # Absolute paths are generally unnessary, especially if something like
1145 # file(GLOB_RECURSE) is used to pick up the files.
1147 string(FIND "${bpath}" "${CMAKE_CURRENT_BINARY_DIR}" _binary_dir_pos)
1148 if (_binary_dir_pos EQUAL 0)
1149 file(RELATIVE_PATH bpath "${CMAKE_CURRENT_BINARY_DIR}" "${bpath}")
1151 file(RELATIVE_PATH bpath "${CMAKE_CURRENT_SOURCE_DIR}" "${bpath}")
1155 # This recipe is from cmLocalGenerator::CreateSafeUniqueObjectFileName in the
1159 string(REGEX REPLACE "^[/]+" "" bpath "${bpath}")
1160 # Avoid absolute paths by removing ':'
1161 string(REPLACE ":" "_" bpath "${bpath}")
1162 # Avoid relative paths that go up the tree
1163 string(REPLACE "../" "__/" bpath "${bpath}")
1165 string(REPLACE " " "_" bpath "${bpath}")
1167 # Strip off the filename. I wait until here to do it, since removin the
1168 # basename can make a path that looked like path/../basename turn into
1169 # path/.. (notice the trailing slash).
1170 get_filename_component(bpath "${bpath}" PATH)
1172 set(${build_path} "${bpath}" PARENT_SCOPE)
1173 #message("${build_path} = ${bpath}")
1176 ##############################################################################
1177 # This helper macro populates the following variables and setups up custom
1178 # commands and targets to invoke the nvcc compiler to generate C or PTX source
1179 # dependent upon the format parameter. The compiler is invoked once with -M
1180 # to generate a dependency file and a second time with -cuda or -ptx to generate
1181 # a .cpp or .ptx file.
1183 # cuda_target - Target name
1184 # format - PTX, CUBIN, FATBIN or OBJ
1185 # FILE1 .. FILEN - The remaining arguments are the sources to be wrapped.
1186 # OPTIONS - Extra options to NVCC
1188 # generated_files - List of generated files
1189 ##############################################################################
1190 ##############################################################################
1192 macro(CUDA_WRAP_SRCS cuda_target format generated_files)
1194 # If CMake doesn't support separable compilation, complain
1195 if(CUDA_SEPARABLE_COMPILATION AND CMAKE_VERSION VERSION_LESS "2.8.10.1")
1196 message(SEND_ERROR "CUDA_SEPARABLE_COMPILATION isn't supported for CMake versions less than 2.8.10.1")
1199 # Set up all the command line flags here, so that they can be overridden on a per target basis.
1203 # Emulation if the card isn't present.
1204 if (CUDA_BUILD_EMULATION)
1206 set(nvcc_flags ${nvcc_flags} --device-emulation -D_DEVICEEMU -g)
1208 # Device mode. No flags necessary.
1211 if(CUDA_HOST_COMPILATION_CPP)
1212 set(CUDA_C_OR_CXX CXX)
1214 if(CUDA_VERSION VERSION_LESS "3.0")
1215 set(nvcc_flags ${nvcc_flags} --host-compilation C)
1217 message(WARNING "--host-compilation flag is deprecated in CUDA version >= 3.0. Removing --host-compilation C flag" )
1219 set(CUDA_C_OR_CXX C)
1222 set(generated_extension ${CMAKE_${CUDA_C_OR_CXX}_OUTPUT_EXTENSION})
1224 if(CUDA_64_BIT_DEVICE_CODE)
1225 set(nvcc_flags ${nvcc_flags} -m64)
1227 set(nvcc_flags ${nvcc_flags} -m32)
1230 if(CUDA_TARGET_CPU_ARCH)
1231 set(nvcc_flags ${nvcc_flags} "--target-cpu-architecture=${CUDA_TARGET_CPU_ARCH}")
1234 # This needs to be passed in at this stage, because VS needs to fill out the
1235 # value of VCInstallDir from within VS. Note that CCBIN is only used if
1236 # -ccbin or --compiler-bindir isn't used and CUDA_HOST_COMPILER matches
1237 # $(VCInstallDir)/bin.
1238 if(CMAKE_GENERATOR MATCHES "Visual Studio")
1239 set(ccbin_flags -D "\"CCBIN:PATH=$(VCInstallDir)bin\"" )
1244 # Figure out which configure we will use and pass that in as an argument to
1245 # the script. We need to defer the decision until compilation time, because
1246 # for VS projects we won't know if we are making a debug or release build
1248 if(CMAKE_GENERATOR MATCHES "Visual Studio")
1249 set( CUDA_build_configuration "$(ConfigurationName)" )
1251 set( CUDA_build_configuration "${CMAKE_BUILD_TYPE}")
1254 # Initialize our list of includes with the user ones followed by the CUDA system ones.
1255 set(CUDA_NVCC_INCLUDE_ARGS ${CUDA_NVCC_INCLUDE_ARGS_USER} "-I${CUDA_INCLUDE_DIRS}")
1256 # Get the include directories for this directory and use them for our nvcc command.
1257 # Remove duplicate entries which may be present since include_directories
1258 # in CMake >= 2.8.8 does not remove them.
1259 get_directory_property(CUDA_NVCC_INCLUDE_DIRECTORIES INCLUDE_DIRECTORIES)
1260 list(REMOVE_DUPLICATES CUDA_NVCC_INCLUDE_DIRECTORIES)
1261 if(CUDA_NVCC_INCLUDE_DIRECTORIES)
1262 foreach(dir ${CUDA_NVCC_INCLUDE_DIRECTORIES})
1263 list(APPEND CUDA_NVCC_INCLUDE_ARGS -I${dir})
1267 # Reset these variables
1268 set(CUDA_WRAP_OPTION_NVCC_FLAGS)
1269 foreach(config ${CUDA_configuration_types})
1270 string(TOUPPER ${config} config_upper)
1271 set(CUDA_WRAP_OPTION_NVCC_FLAGS_${config_upper})
1274 CUDA_GET_SOURCES_AND_OPTIONS(_cuda_wrap_sources _cuda_wrap_cmake_options _cuda_wrap_options ${ARGN})
1275 CUDA_PARSE_NVCC_OPTIONS(CUDA_WRAP_OPTION_NVCC_FLAGS ${_cuda_wrap_options})
1277 # Figure out if we are building a shared library. BUILD_SHARED_LIBS is
1278 # respected in CUDA_ADD_LIBRARY.
1279 set(_cuda_build_shared_libs FALSE)
1281 list(FIND _cuda_wrap_cmake_options SHARED _cuda_found_SHARED)
1282 list(FIND _cuda_wrap_cmake_options MODULE _cuda_found_MODULE)
1283 if(_cuda_found_SHARED GREATER -1 OR _cuda_found_MODULE GREATER -1)
1284 set(_cuda_build_shared_libs TRUE)
1287 list(FIND _cuda_wrap_cmake_options STATIC _cuda_found_STATIC)
1288 if(_cuda_found_STATIC GREATER -1)
1289 set(_cuda_build_shared_libs FALSE)
1293 if(_cuda_build_shared_libs)
1294 # If we are setting up code for a shared library, then we need to add extra flags for
1295 # compiling objects for shared libraries.
1296 set(CUDA_HOST_SHARED_FLAGS ${CMAKE_SHARED_LIBRARY_${CUDA_C_OR_CXX}_FLAGS})
1298 set(CUDA_HOST_SHARED_FLAGS)
1300 # Only add the CMAKE_{C,CXX}_FLAGS if we are propagating host flags. We
1301 # always need to set the SHARED_FLAGS, though.
1302 if(CUDA_PROPAGATE_HOST_FLAGS)
1303 set(_cuda_host_flags "set(CMAKE_HOST_FLAGS ${CMAKE_${CUDA_C_OR_CXX}_FLAGS} ${CUDA_HOST_SHARED_FLAGS})")
1305 set(_cuda_host_flags "set(CMAKE_HOST_FLAGS ${CUDA_HOST_SHARED_FLAGS})")
1308 set(_cuda_nvcc_flags_config "# Build specific configuration flags")
1309 # Loop over all the configuration types to generate appropriate flags for run_nvcc.cmake
1310 foreach(config ${CUDA_configuration_types})
1311 string(TOUPPER ${config} config_upper)
1312 # CMAKE_FLAGS are strings and not lists. By not putting quotes around CMAKE_FLAGS
1313 # we convert the strings to lists (like we want).
1315 if(CUDA_PROPAGATE_HOST_FLAGS)
1316 # nvcc chokes on -g3 in versions previous to 3.0, so replace it with -g
1317 set(_cuda_fix_g3 FALSE)
1319 if(CMAKE_COMPILER_IS_GNUCC)
1320 if (CUDA_VERSION VERSION_LESS "3.0" OR
1321 CUDA_VERSION VERSION_EQUAL "4.1" OR
1322 CUDA_VERSION VERSION_EQUAL "4.2"
1324 set(_cuda_fix_g3 TRUE)
1328 string(REPLACE "-g3" "-g" _cuda_C_FLAGS "${CMAKE_${CUDA_C_OR_CXX}_FLAGS_${config_upper}}")
1330 set(_cuda_C_FLAGS "${CMAKE_${CUDA_C_OR_CXX}_FLAGS_${config_upper}}")
1333 set(_cuda_host_flags "${_cuda_host_flags}\nset(CMAKE_HOST_FLAGS_${config_upper} ${_cuda_C_FLAGS})")
1336 # Note that if we ever want CUDA_NVCC_FLAGS_<CONFIG> to be string (instead of a list
1337 # like it is currently), we can remove the quotes around the
1338 # ${CUDA_NVCC_FLAGS_${config_upper}} variable like the CMAKE_HOST_FLAGS_<CONFIG> variable.
1339 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}})")
1342 # Process the C++11 flag. If the host sets the flag, we need to add it to nvcc and
1343 # remove it from the host. This is because -Xcompile -std=c++ will choke nvcc (it uses
1344 # the C preprocessor). In order to get this to work correctly, we need to use nvcc's
1345 # specific c++11 flag.
1346 if( "${_cuda_host_flags}" MATCHES "-std=c\\+\\+11")
1347 # Add the c++11 flag to nvcc if it isn't already present. Note that we only look at
1348 # the main flag instead of the configuration specific flags.
1349 if( NOT "${CUDA_NVCC_FLAGS}" MATCHES "-std;c\\+\\+11" )
1350 list(APPEND nvcc_flags --std c++11)
1352 string(REGEX REPLACE "[-]+std=c\\+\\+11" "" _cuda_host_flags "${_cuda_host_flags}")
1355 # Get the list of definitions from the directory property
1356 get_directory_property(CUDA_NVCC_DEFINITIONS COMPILE_DEFINITIONS)
1357 if(CUDA_NVCC_DEFINITIONS)
1358 foreach(_definition ${CUDA_NVCC_DEFINITIONS})
1359 list(APPEND nvcc_flags "-D${_definition}")
1363 if(_cuda_build_shared_libs)
1364 list(APPEND nvcc_flags "-D${cuda_target}_EXPORTS")
1367 # Reset the output variable
1368 set(_cuda_wrap_generated_files "")
1370 # Iterate over the macro arguments and create custom
1371 # commands for all the .cu files.
1372 foreach(file ${ARGN})
1373 # Ignore any file marked as a HEADER_FILE_ONLY
1374 get_source_file_property(_is_header ${file} HEADER_FILE_ONLY)
1375 # Allow per source file overrides of the format. Also allows compiling non-.cu files.
1376 get_source_file_property(_cuda_source_format ${file} CUDA_SOURCE_PROPERTY_FORMAT)
1377 if((${file} MATCHES "\\.cu$" OR _cuda_source_format) AND NOT _is_header)
1379 if(NOT _cuda_source_format)
1380 set(_cuda_source_format ${format})
1382 # If file isn't a .cu file, we need to tell nvcc to treat it as such.
1383 if(NOT ${file} MATCHES "\\.cu$")
1384 set(cuda_language_flag -x=cu)
1386 set(cuda_language_flag)
1389 if( ${_cuda_source_format} MATCHES "OBJ")
1390 set( cuda_compile_to_external_module OFF )
1392 set( cuda_compile_to_external_module ON )
1393 if( ${_cuda_source_format} MATCHES "PTX" )
1394 set( cuda_compile_to_external_module_type "ptx" )
1395 elseif( ${_cuda_source_format} MATCHES "CUBIN")
1396 set( cuda_compile_to_external_module_type "cubin" )
1397 elseif( ${_cuda_source_format} MATCHES "FATBIN")
1398 set( cuda_compile_to_external_module_type "fatbin" )
1400 message( FATAL_ERROR "Invalid format flag passed to CUDA_WRAP_SRCS or set with CUDA_SOURCE_PROPERTY_FORMAT file property for file '${file}': '${_cuda_source_format}'. Use OBJ, PTX, CUBIN or FATBIN.")
1404 if(cuda_compile_to_external_module)
1405 # Don't use any of the host compilation flags for PTX targets.
1406 set(CUDA_HOST_FLAGS)
1407 set(CUDA_NVCC_FLAGS_CONFIG)
1409 set(CUDA_HOST_FLAGS ${_cuda_host_flags})
1410 set(CUDA_NVCC_FLAGS_CONFIG ${_cuda_nvcc_flags_config})
1413 # Determine output directory
1414 cuda_compute_build_path("${file}" cuda_build_path)
1415 set(cuda_compile_intermediate_directory "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${cuda_target}.dir/${cuda_build_path}")
1416 if(CUDA_GENERATED_OUTPUT_DIR)
1417 set(cuda_compile_output_dir "${CUDA_GENERATED_OUTPUT_DIR}")
1419 if ( cuda_compile_to_external_module )
1420 set(cuda_compile_output_dir "${CMAKE_CURRENT_BINARY_DIR}")
1422 set(cuda_compile_output_dir "${cuda_compile_intermediate_directory}")
1426 # Add a custom target to generate a c or ptx file. ######################
1428 get_filename_component( basename ${file} NAME )
1429 if( cuda_compile_to_external_module )
1430 set(generated_file_path "${cuda_compile_output_dir}")
1431 set(generated_file_basename "${cuda_target}_generated_${basename}.${cuda_compile_to_external_module_type}")
1432 set(format_flag "-${cuda_compile_to_external_module_type}")
1433 file(MAKE_DIRECTORY "${cuda_compile_output_dir}")
1435 set(generated_file_path "${cuda_compile_output_dir}/${CMAKE_CFG_INTDIR}")
1436 set(generated_file_basename "${cuda_target}_generated_${basename}${generated_extension}")
1437 if(CUDA_SEPARABLE_COMPILATION)
1438 set(format_flag "-dc")
1440 set(format_flag "-c")
1444 # Set all of our file names. Make sure that whatever filenames that have
1445 # generated_file_path in them get passed in through as a command line
1446 # argument, so that the ${CMAKE_CFG_INTDIR} gets expanded at run time
1447 # instead of configure time.
1448 set(generated_file "${generated_file_path}/${generated_file_basename}")
1449 set(cmake_dependency_file "${cuda_compile_intermediate_directory}/${generated_file_basename}.depend")
1450 set(NVCC_generated_dependency_file "${cuda_compile_intermediate_directory}/${generated_file_basename}.NVCC-depend")
1451 set(generated_cubin_file "${generated_file_path}/${generated_file_basename}.cubin.txt")
1452 set(custom_target_script "${cuda_compile_intermediate_directory}/${generated_file_basename}.cmake")
1454 # Setup properties for obj files:
1455 if( NOT cuda_compile_to_external_module )
1456 set_source_files_properties("${generated_file}"
1458 EXTERNAL_OBJECT true # This is an object file not to be compiled, but only be linked.
1462 # Don't add CMAKE_CURRENT_SOURCE_DIR if the path is already an absolute path.
1463 get_filename_component(file_path "${file}" PATH)
1464 if(IS_ABSOLUTE "${file_path}")
1465 set(source_file "${file}")
1467 set(source_file "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
1470 if( NOT cuda_compile_to_external_module AND CUDA_SEPARABLE_COMPILATION)
1471 list(APPEND ${cuda_target}_SEPARABLE_COMPILATION_OBJECTS "${generated_file}")
1474 # Bring in the dependencies. Creates a variable CUDA_NVCC_DEPEND #######
1475 cuda_include_nvcc_dependencies(${cmake_dependency_file})
1477 # Convience string for output ###########################################
1478 if(CUDA_BUILD_EMULATION)
1479 set(cuda_build_type "Emulation")
1481 set(cuda_build_type "Device")
1484 # Build the NVCC made dependency file ###################################
1485 set(build_cubin OFF)
1486 if ( NOT CUDA_BUILD_EMULATION AND CUDA_BUILD_CUBIN )
1487 if ( NOT cuda_compile_to_external_module )
1488 set ( build_cubin ON )
1492 # Configure the build script
1493 configure_file("${CUDA_run_nvcc}" "${custom_target_script}" @ONLY)
1495 # So if a user specifies the same cuda file as input more than once, you
1496 # can have bad things happen with dependencies. Here we check an option
1497 # to see if this is the behavior they want.
1498 if(CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE)
1499 set(main_dep MAIN_DEPENDENCY ${source_file})
1501 set(main_dep DEPENDS ${source_file})
1504 if(CUDA_VERBOSE_BUILD)
1505 set(verbose_output ON)
1506 elseif(CMAKE_GENERATOR MATCHES "Makefiles")
1507 set(verbose_output "$(VERBOSE)")
1509 set(verbose_output OFF)
1512 # Create up the comment string
1513 file(RELATIVE_PATH generated_file_relative_path "${CMAKE_BINARY_DIR}" "${generated_file}")
1514 if(cuda_compile_to_external_module)
1515 set(cuda_build_comment_string "Building NVCC ${cuda_compile_to_external_module_type} file ${generated_file_relative_path}")
1517 set(cuda_build_comment_string "Building NVCC (${cuda_build_type}) object ${generated_file_relative_path}")
1520 # Build the generated file and dependency file ##########################
1522 OUTPUT ${generated_file}
1523 # These output files depend on the source_file and the contents of cmake_dependency_file
1525 DEPENDS ${CUDA_NVCC_DEPEND}
1526 DEPENDS ${custom_target_script}
1527 # Make sure the output directory exists before trying to write to it.
1528 COMMAND ${CMAKE_COMMAND} -E make_directory "${generated_file_path}"
1529 COMMAND ${CMAKE_COMMAND} ARGS
1530 -D verbose:BOOL=${verbose_output}
1532 -D build_configuration:STRING=${CUDA_build_configuration}
1533 -D "generated_file:STRING=${generated_file}"
1534 -D "generated_cubin_file:STRING=${generated_cubin_file}"
1535 -P "${custom_target_script}"
1536 WORKING_DIRECTORY "${cuda_compile_intermediate_directory}"
1537 COMMENT "${cuda_build_comment_string}"
1540 # Make sure the build system knows the file is generated.
1541 set_source_files_properties(${generated_file} PROPERTIES GENERATED TRUE)
1543 list(APPEND _cuda_wrap_generated_files ${generated_file})
1545 # Add the other files that we want cmake to clean on a cleanup ##########
1546 list(APPEND CUDA_ADDITIONAL_CLEAN_FILES "${cmake_dependency_file}")
1547 list(REMOVE_DUPLICATES CUDA_ADDITIONAL_CLEAN_FILES)
1548 set(CUDA_ADDITIONAL_CLEAN_FILES ${CUDA_ADDITIONAL_CLEAN_FILES} CACHE INTERNAL "List of intermediate files that are part of the cuda dependency scanning.")
1553 # Set the return parameter
1554 set(${generated_files} ${_cuda_wrap_generated_files})
1557 function(_cuda_get_important_host_flags important_flags flag_string)
1558 if(CMAKE_GENERATOR MATCHES "Visual Studio")
1559 string(REGEX MATCHALL "/M[DT][d]?" flags "${flag_string}")
1560 list(APPEND ${important_flags} ${flags})
1562 string(REGEX MATCHALL "-fPIC" flags "${flag_string}")
1563 list(APPEND ${important_flags} ${flags})
1565 set(${important_flags} ${${important_flags}} PARENT_SCOPE)
1568 ###############################################################################
1569 ###############################################################################
1570 # Separable Compilation Link
1571 ###############################################################################
1572 ###############################################################################
1574 # Compute the filename to be used by CUDA_LINK_SEPARABLE_COMPILATION_OBJECTS
1575 function(CUDA_COMPUTE_SEPARABLE_COMPILATION_OBJECT_FILE_NAME output_file_var cuda_target object_files)
1577 set(generated_extension ${CMAKE_${CUDA_C_OR_CXX}_OUTPUT_EXTENSION})
1578 set(output_file "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${cuda_target}.dir/${CMAKE_CFG_INTDIR}/${cuda_target}_intermediate_link${generated_extension}")
1583 set(${output_file_var} "${output_file}" PARENT_SCOPE)
1586 # Setup the build rule for the separable compilation intermediate link file.
1587 function(CUDA_LINK_SEPARABLE_COMPILATION_OBJECTS output_file cuda_target options object_files)
1590 set_source_files_properties("${output_file}"
1592 EXTERNAL_OBJECT TRUE # This is an object file not to be compiled, but only
1594 GENERATED TRUE # This file is generated during the build
1597 # For now we are ignoring all the configuration specific flags.
1599 CUDA_PARSE_NVCC_OPTIONS(nvcc_flags ${options})
1600 if(CUDA_64_BIT_DEVICE_CODE)
1601 list(APPEND nvcc_flags -m64)
1603 list(APPEND nvcc_flags -m32)
1605 # If -ccbin, --compiler-bindir has been specified, don't do anything. Otherwise add it here.
1606 list( FIND nvcc_flags "-ccbin" ccbin_found0 )
1607 list( FIND nvcc_flags "--compiler-bindir" ccbin_found1 )
1608 if( ccbin_found0 LESS 0 AND ccbin_found1 LESS 0 AND CUDA_HOST_COMPILER )
1609 list(APPEND nvcc_flags -ccbin "\"${CUDA_HOST_COMPILER}\"")
1612 # Create a list of flags specified by CUDA_NVCC_FLAGS_${CONFIG} and CMAKE_${CUDA_C_OR_CXX}_FLAGS*
1613 set(config_specific_flags)
1615 foreach(config ${CUDA_configuration_types})
1616 string(TOUPPER ${config} config_upper)
1617 # Add config specific flags
1618 foreach(f ${CUDA_NVCC_FLAGS_${config_upper}})
1619 list(APPEND config_specific_flags $<$<CONFIG:${config}>:${f}>)
1621 set(important_host_flags)
1622 _cuda_get_important_host_flags(important_host_flags "${CMAKE_${CUDA_C_OR_CXX}_FLAGS_${config_upper}}")
1623 foreach(f ${important_host_flags})
1624 list(APPEND flags $<$<CONFIG:${config}>:-Xcompiler> $<$<CONFIG:${config}>:${f}>)
1627 # Add CMAKE_${CUDA_C_OR_CXX}_FLAGS
1628 set(important_host_flags)
1629 _cuda_get_important_host_flags(important_host_flags "${CMAKE_${CUDA_C_OR_CXX}_FLAGS}")
1630 foreach(f ${important_host_flags})
1631 list(APPEND flags -Xcompiler ${f})
1634 # Add our general CUDA_NVCC_FLAGS with the configuration specifig flags
1635 set(nvcc_flags ${CUDA_NVCC_FLAGS} ${config_specific_flags} ${nvcc_flags})
1637 file(RELATIVE_PATH output_file_relative_path "${CMAKE_BINARY_DIR}" "${output_file}")
1639 # Some generators don't handle the multiple levels of custom command
1640 # dependencies correctly (obj1 depends on file1, obj2 depends on obj1), so
1641 # we work around that issue by compiling the intermediate link object as a
1642 # pre-link custom command in that situation.
1643 set(do_obj_build_rule TRUE)
1644 if (MSVC_VERSION GREATER 1599 AND MSVC_VERSION LESS 1800)
1645 # VS 2010 and 2012 have this problem.
1646 set(do_obj_build_rule FALSE)
1649 if (do_obj_build_rule)
1651 OUTPUT ${output_file}
1652 DEPENDS ${object_files}
1653 COMMAND ${CUDA_NVCC_EXECUTABLE} ${nvcc_flags} -dlink ${object_files} -o ${output_file}
1655 COMMENT "Building NVCC intermediate link file ${output_file_relative_path}"
1658 get_filename_component(output_file_dir "${output_file}" DIRECTORY)
1660 TARGET ${cuda_target}
1662 COMMAND ${CMAKE_COMMAND} -E echo "Building NVCC intermediate link file ${output_file_relative_path}"
1663 COMMAND ${CMAKE_COMMAND} -E make_directory "${output_file_dir}"
1664 COMMAND ${CUDA_NVCC_EXECUTABLE} ${nvcc_flags} ${flags} -dlink ${object_files} -o "${output_file}"
1670 ###############################################################################
1671 ###############################################################################
1673 ###############################################################################
1674 ###############################################################################
1675 macro(CUDA_ADD_LIBRARY cuda_target)
1677 CUDA_ADD_CUDA_INCLUDE_ONCE()
1679 # Separate the sources from the options
1680 CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
1681 CUDA_BUILD_SHARED_LIBRARY(_cuda_shared_flag ${ARGN})
1682 # Create custom commands and targets for each file.
1683 CUDA_WRAP_SRCS( ${cuda_target} OBJ _generated_files ${_sources}
1684 ${_cmake_options} ${_cuda_shared_flag}
1685 OPTIONS ${_options} )
1687 # Compute the file name of the intermedate link file used for separable
1689 CUDA_COMPUTE_SEPARABLE_COMPILATION_OBJECT_FILE_NAME(link_file ${cuda_target} "${${cuda_target}_SEPARABLE_COMPILATION_OBJECTS}")
1692 add_library(${cuda_target} ${_cmake_options}
1698 # Add a link phase for the separable compilation if it has been enabled. If
1699 # it has been enabled then the ${cuda_target}_SEPARABLE_COMPILATION_OBJECTS
1700 # variable will have been defined.
1701 CUDA_LINK_SEPARABLE_COMPILATION_OBJECTS("${link_file}" ${cuda_target} "${_options}" "${${cuda_target}_SEPARABLE_COMPILATION_OBJECTS}")
1703 target_link_libraries(${cuda_target}
1707 # We need to set the linker language based on what the expected generated file
1708 # would be. CUDA_C_OR_CXX is computed based on CUDA_HOST_COMPILATION_CPP.
1709 set_target_properties(${cuda_target}
1711 LINKER_LANGUAGE ${CUDA_C_OR_CXX}
1717 ###############################################################################
1718 ###############################################################################
1720 ###############################################################################
1721 ###############################################################################
1722 macro(CUDA_ADD_EXECUTABLE cuda_target)
1724 CUDA_ADD_CUDA_INCLUDE_ONCE()
1726 # Separate the sources from the options
1727 CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
1728 # Create custom commands and targets for each file.
1729 CUDA_WRAP_SRCS( ${cuda_target} OBJ _generated_files ${_sources} OPTIONS ${_options} )
1731 # Compute the file name of the intermedate link file used for separable
1733 CUDA_COMPUTE_SEPARABLE_COMPILATION_OBJECT_FILE_NAME(link_file ${cuda_target} "${${cuda_target}_SEPARABLE_COMPILATION_OBJECTS}")
1736 add_executable(${cuda_target} ${_cmake_options}
1742 # Add a link phase for the separable compilation if it has been enabled. If
1743 # it has been enabled then the ${cuda_target}_SEPARABLE_COMPILATION_OBJECTS
1744 # variable will have been defined.
1745 CUDA_LINK_SEPARABLE_COMPILATION_OBJECTS("${link_file}" ${cuda_target} "${_options}" "${${cuda_target}_SEPARABLE_COMPILATION_OBJECTS}")
1747 target_link_libraries(${cuda_target}
1751 # We need to set the linker language based on what the expected generated file
1752 # would be. CUDA_C_OR_CXX is computed based on CUDA_HOST_COMPILATION_CPP.
1753 set_target_properties(${cuda_target}
1755 LINKER_LANGUAGE ${CUDA_C_OR_CXX}
1761 ###############################################################################
1762 ###############################################################################
1763 # (Internal) helper for manually added cuda source files with specific targets
1764 ###############################################################################
1765 ###############################################################################
1766 macro(cuda_compile_base cuda_target format generated_files)
1768 # Separate the sources from the options
1769 CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
1770 # Create custom commands and targets for each file.
1771 CUDA_WRAP_SRCS( ${cuda_target} ${format} _generated_files ${_sources} ${_cmake_options}
1772 OPTIONS ${_options} )
1774 set( ${generated_files} ${_generated_files})
1778 ###############################################################################
1779 ###############################################################################
1781 ###############################################################################
1782 ###############################################################################
1783 macro(CUDA_COMPILE generated_files)
1784 cuda_compile_base(cuda_compile OBJ ${generated_files} ${ARGN})
1787 ###############################################################################
1788 ###############################################################################
1790 ###############################################################################
1791 ###############################################################################
1792 macro(CUDA_COMPILE_PTX generated_files)
1793 cuda_compile_base(cuda_compile_ptx PTX ${generated_files} ${ARGN})
1796 ###############################################################################
1797 ###############################################################################
1798 # CUDA COMPILE FATBIN
1799 ###############################################################################
1800 ###############################################################################
1801 macro(CUDA_COMPILE_FATBIN generated_files)
1802 cuda_compile_base(cuda_compile_fatbin FATBIN ${generated_files} ${ARGN})
1805 ###############################################################################
1806 ###############################################################################
1807 # CUDA COMPILE CUBIN
1808 ###############################################################################
1809 ###############################################################################
1810 macro(CUDA_COMPILE_CUBIN generated_files)
1811 cuda_compile_base(cuda_compile_cubin CUBIN ${generated_files} ${ARGN})
1815 ###############################################################################
1816 ###############################################################################
1817 # CUDA ADD CUFFT TO TARGET
1818 ###############################################################################
1819 ###############################################################################
1820 macro(CUDA_ADD_CUFFT_TO_TARGET target)
1821 if (CUDA_BUILD_EMULATION)
1822 target_link_libraries(${target} ${CUDA_cufftemu_LIBRARY})
1824 target_link_libraries(${target} ${CUDA_cufft_LIBRARY})
1828 ###############################################################################
1829 ###############################################################################
1830 # CUDA ADD CUBLAS TO TARGET
1831 ###############################################################################
1832 ###############################################################################
1833 macro(CUDA_ADD_CUBLAS_TO_TARGET target)
1834 if (CUDA_BUILD_EMULATION)
1835 target_link_libraries(${target} ${CUDA_cublasemu_LIBRARY})
1837 target_link_libraries(${target} ${CUDA_cublas_LIBRARY})
1841 ###############################################################################
1842 ###############################################################################
1843 # CUDA BUILD CLEAN TARGET
1844 ###############################################################################
1845 ###############################################################################
1846 macro(CUDA_BUILD_CLEAN_TARGET)
1847 # Call this after you add all your CUDA targets, and you will get a convience
1848 # target. You should also make clean after running this target to get the
1849 # build system to generate all the code again.
1851 set(cuda_clean_target_name clean_cuda_depends)
1852 if (CMAKE_GENERATOR MATCHES "Visual Studio")
1853 string(TOUPPER ${cuda_clean_target_name} cuda_clean_target_name)
1855 add_custom_target(${cuda_clean_target_name}
1856 COMMAND ${CMAKE_COMMAND} -E remove ${CUDA_ADDITIONAL_CLEAN_FILES})
1858 # Clear out the variable, so the next time we configure it will be empty.
1859 # This is useful so that the files won't persist in the list after targets
1860 # have been removed.
1861 set(CUDA_ADDITIONAL_CLEAN_FILES "" CACHE INTERNAL "List of intermediate files that are part of the cuda dependency scanning.")