/normxcorr/trunk

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