/normxcorr/trunk

To get this branch, use:
bzr branch http://suren.me/webbzr/normxcorr/trunk

« back to all changes in this revision

Viewing changes to dict_hw/cmake/FindCUDA.cmake

  • Committer: Suren A. Chilingaryan
  • Date: 2009-12-12 01:38:41 UTC
  • Revision ID: csa@dside.dyndns.org-20091212013841-feih3qa4i28x75j4
Provide stand-alone library

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
    )
 
410
  # Now search default paths
 
411
  find_path(CUDA_TOOLKIT_ROOT_DIR
 
412
    NAMES nvcc nvcc.exe
 
413
    PATHS /usr/local/bin
 
414
          /usr/local/cuda/bin
 
415
    DOC "Toolkit location."
 
416
    )
 
417
 
 
418
  if (CUDA_TOOLKIT_ROOT_DIR)
 
419
    string(REGEX REPLACE "[/\\\\]?bin[/\\\\]?$" "" CUDA_TOOLKIT_ROOT_DIR ${CUDA_TOOLKIT_ROOT_DIR})
 
420
    # We need to force this back into the cache.
 
421
    set(CUDA_TOOLKIT_ROOT_DIR ${CUDA_TOOLKIT_ROOT_DIR} CACHE PATH "Toolkit location." FORCE)
 
422
  endif(CUDA_TOOLKIT_ROOT_DIR)
 
423
  if (NOT EXISTS ${CUDA_TOOLKIT_ROOT_DIR})
 
424
    if(CUDA_FIND_REQUIRED)
 
425
      message(FATAL_ERROR "Specify CUDA_TOOLKIT_ROOT_DIR")
 
426
    elseif(NOT CUDA_FIND_QUIETLY)
 
427
      message("CUDA_TOOLKIT_ROOT_DIR not found or specified")
 
428
    endif()
 
429
  endif (NOT EXISTS ${CUDA_TOOLKIT_ROOT_DIR})
 
430
endif (NOT CUDA_TOOLKIT_ROOT_DIR)
 
431
 
 
432
# CUDA_NVCC_EXECUTABLE
 
433
find_program(CUDA_NVCC_EXECUTABLE
 
434
  NAMES nvcc
 
435
  PATHS "${CUDA_TOOLKIT_ROOT_DIR}/bin"
 
436
  ENV CUDA_BIN_PATH
 
437
  NO_DEFAULT_PATH
 
438
  )
 
439
# Search default search paths, after we search our own set of paths.
 
440
find_program(CUDA_NVCC_EXECUTABLE nvcc)
 
441
mark_as_advanced(CUDA_NVCC_EXECUTABLE)
 
442
 
 
443
if(CUDA_NVCC_EXECUTABLE AND NOT CUDA_VERSION)
 
444
  # Compute the version.
 
445
  exec_program(${CUDA_NVCC_EXECUTABLE} ARGS "--version" OUTPUT_VARIABLE NVCC_OUT)
 
446
  string(REGEX REPLACE ".*release ([0-9]+)\\.([0-9]+).*" "\\1" CUDA_VERSION_MAJOR ${NVCC_OUT})
 
447
  string(REGEX REPLACE ".*release ([0-9]+)\\.([0-9]+).*" "\\2" CUDA_VERSION_MINOR ${NVCC_OUT})
 
448
  set(CUDA_VERSION "${CUDA_VERSION_MAJOR}.${CUDA_VERSION_MINOR}" CACHE STRING "Version of CUDA as computed from nvcc.")
 
449
  mark_as_advanced(CUDA_VERSION)
 
450
endif()
 
451
 
 
452
# Always set this convenience variable
 
453
set(CUDA_VERSION_STRING "${CUDA_VERSION}")
 
454
 
 
455
# Here we need to determine if the version we found is acceptable.  We will
 
456
# assume that is unless CUDA_FIND_VERSION_EXACT or CUDA_FIND_VERSION is
 
457
# specified.  The presence of either of these options checks the version
 
458
# string and signals if the version is acceptable or not.
 
459
set(_cuda_version_acceptable TRUE)
 
460
#
 
461
if(CUDA_FIND_VERSION_EXACT AND NOT CUDA_VERSION VERSION_EQUAL CUDA_FIND_VERSION)
 
462
  set(_cuda_version_acceptable FALSE)
 
463
endif()
 
464
#
 
465
if(CUDA_FIND_VERSION       AND     CUDA_VERSION VERSION_LESS  CUDA_FIND_VERSION)
 
466
  set(_cuda_version_acceptable FALSE)
 
467
endif()
 
468
#
 
469
if(NOT _cuda_version_acceptable)
 
470
  set(_cuda_error_message "Requested CUDA version ${CUDA_FIND_VERSION}, but found unacceptable version ${CUDA_VERSION}")
 
471
  if(CUDA_FIND_REQUIRED)
 
472
    message("${_cuda_error_message}")
 
473
  elseif(NOT CUDA_FIND_QUIETLY)
 
474
    message("${_cuda_error_message}")
 
475
  endif()
 
476
endif()
 
477
 
 
478
# CUDA_TOOLKIT_INCLUDE
 
479
find_path(CUDA_TOOLKIT_INCLUDE
 
480
  device_functions.h # Header included in toolkit
 
481
  PATHS "${CUDA_TOOLKIT_ROOT_DIR}/include"
 
482
  ENV CUDA_INC_PATH
 
483
  NO_DEFAULT_PATH
 
484
  )
 
485
# Search default search paths, after we search our own set of paths.
 
486
find_path(CUDA_TOOLKIT_INCLUDE device_functions.h)
 
487
mark_as_advanced(CUDA_TOOLKIT_INCLUDE)
 
488
 
 
489
# Set the user list of include dir to nothing to initialize it.
 
490
set (CUDA_NVCC_INCLUDE_ARGS_USER "")
 
491
set (CUDA_INCLUDE_DIRS ${CUDA_TOOLKIT_INCLUDE})
 
492
 
 
493
macro(FIND_LIBRARY_LOCAL_FIRST _var _names _doc)
 
494
  find_library(${_var}
 
495
    NAMES ${_names}
 
496
    PATHS "${CUDA_TOOLKIT_ROOT_DIR}/lib"
 
497
    ENV CUDA_LIB_PATH
 
498
    DOC ${_doc}
 
499
    NO_DEFAULT_PATH
 
500
    )
 
501
  # Search default search paths, after we search our own set of paths.
 
502
  find_library(${_var} NAMES ${_names} DOC ${_doc})
 
503
endmacro()
 
504
 
 
505
# CUDA_LIBRARIES
 
506
find_library_local_first(CUDA_CUDART_LIBRARY cudart "\"cudart\" library")
 
507
set(CUDA_LIBRARIES ${CUDA_CUDART_LIBRARY})
 
508
 
 
509
# 1.1 toolkit on linux doesn't appear to have a separate library on
 
510
# some platforms.
 
511
find_library_local_first(CUDA_CUDA_LIBRARY cuda "\"cuda\" library (older versions only).")
 
512
 
 
513
# Add cuda library to the link line only if it is found.
 
514
if (CUDA_CUDA_LIBRARY)
 
515
  set(CUDA_LIBRARIES ${CUDA_LIBRARIES} ${CUDA_CUDA_LIBRARY})
 
516
endif(CUDA_CUDA_LIBRARY)
 
517
 
 
518
mark_as_advanced(
 
519
  CUDA_CUDA_LIBRARY
 
520
  CUDA_CUDART_LIBRARY
 
521
  )
 
522
 
 
523
#######################
 
524
# Look for some of the toolkit helper libraries
 
525
macro(FIND_CUDA_HELPER_LIBS _name)
 
526
  find_library_local_first(CUDA_${_name}_LIBRARY ${_name} "\"${_name}\" library")
 
527
  mark_as_advanced(CUDA_${_name}_LIBRARY)
 
528
endmacro(FIND_CUDA_HELPER_LIBS)
 
529
 
 
530
# Search for cufft and cublas libraries.
 
531
find_cuda_helper_libs(cufftemu)
 
532
find_cuda_helper_libs(cublasemu)
 
533
find_cuda_helper_libs(cufft)
 
534
find_cuda_helper_libs(cublas)
 
535
 
 
536
if (CUDA_BUILD_EMULATION)
 
537
  set(CUDA_CUFFT_LIBRARIES ${CUDA_cufftemu_LIBRARY})
 
538
  set(CUDA_CUBLAS_LIBRARIES ${CUDA_cublasemu_LIBRARY})
 
539
else()
 
540
  set(CUDA_CUFFT_LIBRARIES ${CUDA_cufft_LIBRARY})
 
541
  set(CUDA_CUBLAS_LIBRARIES ${CUDA_cublas_LIBRARY})
 
542
endif()
 
543
 
 
544
########################
 
545
# Look for the SDK stuff
 
546
find_path(CUDA_SDK_ROOT_DIR common/inc/cutil.h
 
547
  "$ENV{NVSDKCUDA_ROOT}"
 
548
  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\NVIDIA Corporation\\Installed Products\\NVIDIA SDK 10\\Compute;InstallDir]"
 
549
  )
 
550
 
 
551
# Keep the CUDA_SDK_ROOT_DIR first in order to be able to override the
 
552
# environment variables.
 
553
# SDS some directories are added
 
554
set(CUDA_SDK_SEARCH_PATH
 
555
  "${CUDA_SDK_ROOT_DIR}"
 
556
  "${CUDA_TOOLKIT_ROOT_DIR}"
 
557
  "${CUDA_TOOLKIT_ROOT_DIR}/sdk"
 
558
  "${CUDA_TOOLKIT_ROOT_DIR}/local/NVSDK0.2"
 
559
  "${CUDA_TOOLKIT_ROOT_DIR}/NVSDK0.2"
 
560
  "${CUDA_TOOLKIT_ROOT_DIR}/NV_CUDA_SDK"
 
561
  "$ENV{HOME}/NVIDIA_CUDA_SDK"
 
562
  "$ENV{HOME}/NVIDIA_CUDA_SDK_MACOSX"
 
563
  "/Developer/CUDA"
 
564
  )
 
565
#EDS
 
566
# CUDA_CUT_INCLUDE_DIR
 
567
find_path(CUDA_CUT_INCLUDE_DIR
 
568
  cutil.h
 
569
  PATHS ${CUDA_SDK_SEARCH_PATH}
 
570
  PATH_SUFFIXES "common/inc"
 
571
  DOC "Location of cutil.h"
 
572
  NO_DEFAULT_PATH
 
573
  )
 
574
# Now search system paths
 
575
find_path(CUDA_CUT_INCLUDE_DIR cutil.h DOC "Location of cutil.h")
 
576
 
 
577
mark_as_advanced(CUDA_CUT_INCLUDE_DIR)
 
578
 
 
579
 
 
580
# CUDA_CUT_LIBRARIES
 
581
 
 
582
# cutil library is called cutil64 for 64 bit builds on windows.  We don't want
 
583
# to get these confused, so we are setting the name based on the word size of
 
584
# the build.
 
585
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
 
586
  set(cuda_cutil_name cutil64)
 
587
else(CMAKE_SIZEOF_VOID_P EQUAL 8)
 
588
  set(cuda_cutil_name cutil32)
 
589
endif(CMAKE_SIZEOF_VOID_P EQUAL 8)
 
590
 
 
591
find_library(CUDA_CUT_LIBRARY
 
592
  NAMES cutil ${cuda_cutil_name}
 
593
  PATHS ${CUDA_SDK_SEARCH_PATH}
 
594
  # The new version of the sdk shows up in common/lib, but the old one is in lib
 
595
  PATH_SUFFIXES "common/lib" "lib"
 
596
  DOC "Location of cutil library"
 
597
  NO_DEFAULT_PATH
 
598
  )
 
599
# Now search system paths
 
600
find_library(CUDA_CUT_LIBRARY NAMES cutil ${cuda_cutil_name} DOC "Location of cutil library")
 
601
mark_as_advanced(CUDA_CUT_LIBRARY)
 
602
set(CUDA_CUT_LIBRARIES ${CUDA_CUT_LIBRARY})
 
603
 
 
604
 
 
605
 
 
606
#############################
 
607
# Check for required components
 
608
set(CUDA_FOUND TRUE)
 
609
 
 
610
set(CUDA_TOOLKIT_ROOT_DIR_INTERNAL "${CUDA_TOOLKIT_ROOT_DIR}" CACHE INTERNAL
 
611
  "This is the value of the last time CUDA_TOOLKIT_ROOT_DIR was set successfully." FORCE)
 
612
set(CUDA_SDK_ROOT_DIR_INTERNAL "${CUDA_SDK_ROOT_DIR}" CACHE INTERNAL
 
613
  "This is the value of the last time CUDA_SDK_ROOT_DIR was set successfully." FORCE)
 
614
 
 
615
include(FindPackageHandleStandardArgs)
 
616
find_package_handle_standard_args(CUDA DEFAULT_MSG
 
617
  CUDA_TOOLKIT_ROOT_DIR
 
618
  CUDA_NVCC_EXECUTABLE
 
619
  CUDA_INCLUDE_DIRS
 
620
  CUDA_CUDART_LIBRARY
 
621
  _cuda_version_acceptable
 
622
  )
 
623
 
 
624
 
 
625
 
 
626
###############################################################################
 
627
###############################################################################
 
628
# Macros
 
629
###############################################################################
 
630
###############################################################################
 
631
 
 
632
###############################################################################
 
633
# Add include directories to pass to the nvcc command.
 
634
macro(CUDA_INCLUDE_DIRECTORIES)
 
635
  foreach(dir ${ARGN})
 
636
    list(APPEND CUDA_NVCC_INCLUDE_ARGS_USER "-I${dir}")
 
637
  endforeach(dir ${ARGN})
 
638
endmacro(CUDA_INCLUDE_DIRECTORIES)
 
639
 
 
640
 
 
641
##############################################################################
 
642
cuda_find_helper_file(parse_cubin cmake)
 
643
cuda_find_helper_file(make2cmake cmake)
 
644
cuda_find_helper_file(run_nvcc cmake)
 
645
 
 
646
##############################################################################
 
647
# Separate the OPTIONS out from the sources
 
648
#
 
649
macro(CUDA_GET_SOURCES_AND_OPTIONS _sources _options)
 
650
  set( ${_sources} )
 
651
  set( ${_options} )
 
652
  set( _found_options FALSE )
 
653
  foreach(arg ${ARGN})
 
654
    if(arg STREQUAL "OPTIONS")
 
655
      set( _found_options TRUE )
 
656
    else()
 
657
      if ( _found_options )
 
658
        list(APPEND ${_options} "${arg}")
 
659
      else()
 
660
        # Assume this is a file
 
661
        list(APPEND ${_sources} "${arg}")
 
662
      endif()
 
663
    endif()
 
664
  endforeach()
 
665
endmacro()
 
666
 
 
667
##############################################################################
 
668
# Parse the OPTIONS from ARGN and set the variables prefixed by _option_prefix
 
669
#
 
670
macro(CUDA_PARSE_NVCC_OPTIONS _option_prefix)
 
671
  set( _found_config )
 
672
  foreach(arg ${ARGN})
 
673
    # Determine if we are dealing with a perconfiguration flag
 
674
    foreach(config ${CUDA_configuration_types})
 
675
      string(TOUPPER ${config} config_upper)
 
676
      if (arg STREQUAL "${config_upper}")
 
677
        set( _found_config _${arg})
 
678
        # Set arg to nothing to keep it from being processed further
 
679
        set( arg )
 
680
      endif()
 
681
    endforeach()
 
682
 
 
683
    if ( arg )
 
684
      list(APPEND ${_option_prefix}${_found_config} "${arg}")
 
685
    endif()
 
686
  endforeach()
 
687
endmacro()
 
688
 
 
689
##############################################################################
 
690
# Helper to add the include directory for CUDA only once
 
691
function(CUDA_ADD_CUDA_INCLUDE_ONCE)
 
692
  get_directory_property(_include_directories INCLUDE_DIRECTORIES)
 
693
  set(_add TRUE)
 
694
  if(_include_directories)
 
695
    foreach(dir ${_include_directories})
 
696
      if("${dir}" STREQUAL "${CUDA_INCLUDE_DIRS}")
 
697
        set(_add FALSE)
 
698
      endif()
 
699
    endforeach()
 
700
  endif()
 
701
  if(_add)
 
702
    include_directories(${CUDA_INCLUDE_DIRS})
 
703
  endif()
 
704
endfunction()
 
705
 
 
706
##############################################################################
 
707
# This helper macro populates the following variables and setups up custom
 
708
# commands and targets to invoke the nvcc compiler to generate C or PTX source
 
709
# dependant upon the format parameter.  The compiler is invoked once with -M
 
710
# to generate a dependency file and a second time with -cuda or -ptx to generate
 
711
# a .cpp or .ptx file.
 
712
# INPUT:
 
713
#   cuda_target         - Target name
 
714
#   format              - PTX or OBJ
 
715
#   FILE1 .. FILEN      - The remaining arguments are the sources to be wrapped.
 
716
#   OPTIONS             - Extra options to NVCC
 
717
# OUTPUT:
 
718
#   generated_files     - List of generated files
 
719
##############################################################################
 
720
##############################################################################
 
721
 
 
722
macro(CUDA_WRAP_SRCS cuda_target format generated_files)
 
723
 
 
724
  if( ${format} MATCHES "PTX" )
 
725
    set( compile_to_ptx ON )
 
726
  elseif( ${format} MATCHES "OBJ")
 
727
    set( compile_to_ptx OFF )
 
728
  else()
 
729
    message( FATAL_ERROR "Invalid format flag passed to CUDA_WRAP_SRCS: '${format}'.  Use OBJ or PTX.")
 
730
  endif()
 
731
 
 
732
  # Set up all the command line flags here, so that they can be overriden on a per target basis.
 
733
 
 
734
  set(nvcc_flags "")
 
735
 
 
736
  # Emulation if the card isn't present.
 
737
  if (CUDA_BUILD_EMULATION)
 
738
    # Emulation.
 
739
    set(nvcc_flags ${nvcc_flags} --device-emulation -D_DEVICEEMU -g)
 
740
  else(CUDA_BUILD_EMULATION)
 
741
    # Device mode.  No flags necessary.
 
742
  endif(CUDA_BUILD_EMULATION)
 
743
 
 
744
  if(CUDA_HOST_COMPILATION_CPP)
 
745
    set(CUDA_C_OR_CXX CXX)
 
746
  else(CUDA_HOST_COMPILATION_CPP)
 
747
    set(nvcc_flags ${nvcc_flags} --host-compilation C)
 
748
    set(CUDA_C_OR_CXX C)
 
749
  endif(CUDA_HOST_COMPILATION_CPP)
 
750
 
 
751
  set(generated_extension ${CMAKE_${CUDA_C_OR_CXX}_OUTPUT_EXTENSION})
 
752
 
 
753
  if(CUDA_64_BIT_DEVICE_CODE)
 
754
    set(nvcc_flags ${nvcc_flags} -m64)
 
755
  else()
 
756
    set(nvcc_flags ${nvcc_flags} -m32)
 
757
  endif()
 
758
 
 
759
  # This needs to be passed in at this stage, because VS needs to fill out the
 
760
  # value of VCInstallDir from within VS.
 
761
  if(CMAKE_GENERATOR MATCHES "Visual Studio")
 
762
    if( CMAKE_SIZEOF_VOID_P EQUAL 8 )
 
763
      # Add nvcc flag for 64b Windows
 
764
      set(ccbin_flags -D "\"CCBIN:PATH=$(VCInstallDir)bin\"" )
 
765
    endif()
 
766
  endif()
 
767
 
 
768
  # Figure out which configure we will use and pass that in as an argument to
 
769
  # the script.  We need to defer the decision until compilation time, because
 
770
  # for VS projects we won't know if we are making a debug or release build
 
771
  # until build time.
 
772
  if(CMAKE_GENERATOR MATCHES "Visual Studio")
 
773
    set( CUDA_build_configuration "$(ConfigurationName)" )
 
774
  else()
 
775
    set( CUDA_build_configuration "${CMAKE_BUILD_TYPE}")
 
776
  endif()
 
777
 
 
778
  # Initialize our list of includes with the user ones followed by the CUDA system ones.
 
779
  set(CUDA_NVCC_INCLUDE_ARGS ${CUDA_NVCC_INCLUDE_ARGS_USER} "-I${CUDA_INCLUDE_DIRS}")
 
780
  # Get the include directories for this directory and use them for our nvcc command.
 
781
  get_directory_property(CUDA_NVCC_INCLUDE_DIRECTORIES INCLUDE_DIRECTORIES)
 
782
  if(CUDA_NVCC_INCLUDE_DIRECTORIES)
 
783
    foreach(dir ${CUDA_NVCC_INCLUDE_DIRECTORIES})
 
784
      list(APPEND CUDA_NVCC_INCLUDE_ARGS "-I${dir}")
 
785
    endforeach()
 
786
  endif()
 
787
 
 
788
  # Reset these variables
 
789
  set(CUDA_WRAP_OPTION_NVCC_FLAGS)
 
790
  foreach(config ${CUDA_configuration_types})
 
791
    string(TOUPPER ${config} config_upper)
 
792
    set(CUDA_WRAP_OPTION_NVCC_FLAGS_${config_upper})
 
793
  endforeach()
 
794
 
 
795
  CUDA_GET_SOURCES_AND_OPTIONS(_cuda_wrap_sources _cuda_wrap_options ${ARGN})
 
796
  CUDA_PARSE_NVCC_OPTIONS(CUDA_WRAP_OPTION_NVCC_FLAGS ${_cuda_wrap_options})
 
797
 
 
798
  # CUDA_HOST_FLAGS
 
799
  if(BUILD_SHARED_LIBS)
 
800
    # If BUILD_SHARED_LIBS is true, then we need to add extra flags for
 
801
    # compiling objects for shared libraries.
 
802
    set(CUDA_HOST_SHARED_FLAGS ${CMAKE_SHARED_LIBRARY_${CUDA_C_OR_CXX}_FLAGS})
 
803
  endif()
 
804
  set(CUDA_HOST_FLAGS "set(CMAKE_HOST_FLAGS ${CMAKE_${CUDA_C_OR_CXX}_FLAGS} ${CUDA_HOST_SHARED_FLAGS})")
 
805
  set(CUDA_NVCC_FLAGS_CONFIG "# Build specific configuration flags")
 
806
  # Loop over all the configuration types to generate appropriate flags for run_nvcc.cmake
 
807
  foreach(config ${CUDA_configuration_types})
 
808
    string(TOUPPER ${config} config_upper)
 
809
    # CMAKE_FLAGS are strings and not lists.  By not putting quotes around CMAKE_FLAGS
 
810
    # we convert the strings to lists (like we want).
 
811
 
 
812
    # nvcc chokes on -g3, so replace it with -g
 
813
    if(CMAKE_COMPILER_IS_GNUCC)
 
814
      string(REPLACE "-g3" "-g" _cuda_C_FLAGS "${CMAKE_${CUDA_C_OR_CXX}_FLAGS_${config_upper}}")
 
815
    else()
 
816
      set(_cuda_C_FLAGS "${CMAKE_${CUDA_C_OR_CXX}_FLAGS_${config_upper}}")
 
817
    endif()
 
818
    set(CUDA_HOST_FLAGS "${CUDA_HOST_FLAGS}\nset(CMAKE_HOST_FLAGS_${config_upper} ${_cuda_C_FLAGS})")
 
819
    # Note that if we ever want CUDA_NVCC_FLAGS_<CONFIG> to be string (instead of a list
 
820
    # like it is currently), we can remove the quotes around the
 
821
    # ${CUDA_NVCC_FLAGS_${config_upper}} variable like the CMAKE_HOST_FLAGS_<CONFIG> variable.
 
822
    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}}\")")
 
823
  endforeach()
 
824
 
 
825
  # Get the list of definitions from the directory property
 
826
  get_directory_property(CUDA_NVCC_DEFINITIONS COMPILE_DEFINITIONS)
 
827
  if(CUDA_NVCC_DEFINITIONS)
 
828
    foreach(_definition ${CUDA_NVCC_DEFINITIONS})
 
829
      list(APPEND nvcc_flags "-D${_definition}")
 
830
    endforeach()
 
831
  endif()
 
832
 
 
833
  if(BUILD_SHARED_LIBS)
 
834
    list(APPEND nvcc_flags "-D${cuda_target}_EXPORTS")
 
835
  endif()
 
836
 
 
837
  # Determine output directory
 
838
  if(CUDA_GENERATED_OUTPUT_DIR)
 
839
    set(cuda_compile_output_dir "${CUDA_GENERATED_OUTPUT_DIR}")
 
840
  else()
 
841
    set(cuda_compile_output_dir "${CMAKE_CURRENT_BINARY_DIR}")
 
842
  endif()
 
843
 
 
844
  # Reset the output variable
 
845
  set(_cuda_wrap_generated_files "")
 
846
 
 
847
  # Iterate over the macro arguments and create custom
 
848
  # commands for all the .cu files.
 
849
  foreach(file ${ARGN})
 
850
    # Ignore any file marked as a HEADER_FILE_ONLY
 
851
    get_source_file_property(_is_header ${file} HEADER_FILE_ONLY)
 
852
    if(${file} MATCHES ".*\\.cu$" AND NOT _is_header)
 
853
 
 
854
      # Add a custom target to generate a c or ptx file. ######################
 
855
 
 
856
      get_filename_component( basename ${file} NAME )
 
857
      if( compile_to_ptx )
 
858
        set(generated_file_path "${cuda_compile_output_dir}")
 
859
        set(generated_file_basename "${cuda_target}_generated_${basename}.ptx")
 
860
        set(format_flag "-ptx")
 
861
        file(MAKE_DIRECTORY "${cuda_compile_output_dir}")
 
862
      else( compile_to_ptx )
 
863
        set(generated_file_path "${cuda_compile_output_dir}/${CMAKE_CFG_INTDIR}")
 
864
        set(generated_file_basename "${cuda_target}_generated_${basename}${generated_extension}")
 
865
        set(format_flag "-c")
 
866
      endif( compile_to_ptx )
 
867
 
 
868
      # Set all of our file names.  Make sure that whatever filenames that have
 
869
      # generated_file_path in them get passed in through as a command line
 
870
      # argument, so that the ${CMAKE_CFG_INTDIR} gets expanded at run time
 
871
      # instead of configure time.
 
872
#SDS move cu.o.* files into the generated folder
 
873
      set(generated_file "${generated_file_path}/${generated_file_basename}")
 
874
      set(cmake_dependency_file "${generated_file_path}/${generated_file_basename}.depend")
 
875
      set(NVCC_generated_dependency_file "${generated_file_path}/${generated_file_basename}.NVCC-depend")
 
876
      set(generated_cubin_file "${generated_file_path}/${generated_file_basename}.cubin.txt")
 
877
      set(custom_target_script "${generated_file_path}/${generated_file_basename}.cmake")
 
878
#EDS
 
879
      # Setup properties for obj files:
 
880
      if( NOT compile_to_ptx )
 
881
        set_source_files_properties("${generated_file}"
 
882
          PROPERTIES
 
883
          EXTERNAL_OBJECT true # This is an object file not to be compiled, but only be linked.
 
884
          )
 
885
      endif()
 
886
 
 
887
      # Don't add CMAKE_CURRENT_SOURCE_DIR if the path is already an absolute path.
 
888
      get_filename_component(file_path "${file}" PATH)
 
889
      if(IS_ABSOLUTE "${file_path}")
 
890
        set(source_file "${file}")
 
891
      else()
 
892
        set(source_file "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
 
893
      endif()
 
894
 
 
895
      # Bring in the dependencies.  Creates a variable CUDA_NVCC_DEPEND #######
 
896
      cuda_include_nvcc_dependencies(${cmake_dependency_file})
 
897
 
 
898
      # Convience string for output ###########################################
 
899
      if(CUDA_BUILD_EMULATION)
 
900
        set(cuda_build_type "Emulation")
 
901
      else(CUDA_BUILD_EMULATION)
 
902
        set(cuda_build_type "Device")
 
903
      endif(CUDA_BUILD_EMULATION)
 
904
 
 
905
      # Build the NVCC made dependency file ###################################
 
906
      set(build_cubin OFF)
 
907
      if ( NOT CUDA_BUILD_EMULATION AND CUDA_BUILD_CUBIN )
 
908
         if ( NOT compile_to_ptx )
 
909
           set ( build_cubin ON )
 
910
         endif( NOT compile_to_ptx )
 
911
      endif( NOT CUDA_BUILD_EMULATION AND CUDA_BUILD_CUBIN )
 
912
 
 
913
      # Configure the build script
 
914
      configure_file("${CUDA_run_nvcc}" "${custom_target_script}" @ONLY)
 
915
 
 
916
      # So if a user specifies the same cuda file as input more than once, you
 
917
      # can have bad things happen with dependencies.  Here we check an option
 
918
      # to see if this is the behavior they want.
 
919
      if(CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE)
 
920
        set(main_dep MAIN_DEPENDENCY ${source_file})
 
921
      else()
 
922
        set(main_dep DEPENDS ${source_file})
 
923
      endif()
 
924
 
 
925
      if(CUDA_VERBOSE_BUILD)
 
926
        set(verbose_output ON)
 
927
      elseif(CMAKE_GENERATOR MATCHES "Makefiles")
 
928
        set(verbose_output "$(VERBOSE)")
 
929
      else()
 
930
        set(verbose_output OFF)
 
931
      endif()
 
932
 
 
933
      # Create up the comment string
 
934
      file(RELATIVE_PATH generated_file_relative_path "${CMAKE_BINARY_DIR}" "${generated_file}")
 
935
      if(compile_to_ptx)
 
936
        set(cuda_build_comment_string "Building NVCC ptx file ${generated_file_relative_path}")
 
937
      else()
 
938
        set(cuda_build_comment_string "Building NVCC (${cuda_build_type}) object ${generated_file_relative_path}")
 
939
      endif()
 
940
 
 
941
      # Build the generated file and dependency file ##########################
 
942
      add_custom_command(
 
943
        OUTPUT ${generated_file}
 
944
        # These output files depend on the source_file and the contents of cmake_dependency_file
 
945
        ${main_dep}
 
946
        DEPENDS ${CUDA_NVCC_DEPEND}
 
947
        DEPENDS ${custom_target_script}
 
948
        COMMAND ${CMAKE_COMMAND} ARGS
 
949
          -D verbose:BOOL=${verbose_output}
 
950
          ${ccbin_flags}
 
951
          -D build_configuration:STRING=${CUDA_build_configuration}
 
952
          -D "generated_file:STRING=${generated_file}"
 
953
          -D "generated_cubin_file:STRING=${generated_cubin_file}"
 
954
          -P "${custom_target_script}"
 
955
        COMMENT "${cuda_build_comment_string}"
 
956
        )
 
957
 
 
958
      # Make sure the build system knows the file is generated.
 
959
      set_source_files_properties(${generated_file} PROPERTIES GENERATED TRUE)
 
960
 
 
961
      # Don't add the object file to the list of generated files if we are using
 
962
      # visual studio and we are attaching the build rule to the cuda file.  VS
 
963
      # will add our object file to the linker automatically for us.
 
964
      set(cuda_add_generated_file TRUE)
 
965
 
 
966
      if(NOT compile_to_ptx AND CMAKE_GENERATOR MATCHES "Visual Studio" AND CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE)
 
967
        # Visual Studio 8 crashes when you close the solution when you don't add the object file.
 
968
        if(NOT CMAKE_GENERATOR MATCHES "Visual Studio 8")
 
969
          set(cuda_add_generated_file FALSE)
 
970
        endif()
 
971
      endif()
 
972
 
 
973
      if(cuda_add_generated_file)
 
974
        list(APPEND _cuda_wrap_generated_files ${generated_file})
 
975
      endif()
 
976
 
 
977
      # Add the other files that we want cmake to clean on a cleanup ##########
 
978
      list(APPEND CUDA_ADDITIONAL_CLEAN_FILES "${cmake_dependency_file}")
 
979
      list(REMOVE_DUPLICATES CUDA_ADDITIONAL_CLEAN_FILES)
 
980
      set(CUDA_ADDITIONAL_CLEAN_FILES ${CUDA_ADDITIONAL_CLEAN_FILES} CACHE INTERNAL "List of intermediate files that are part of the cuda dependency scanning.")
 
981
 
 
982
    endif(${file} MATCHES ".*\\.cu$" AND NOT _is_header)
 
983
  endforeach(file)
 
984
 
 
985
  # Set the return parameter
 
986
  set(${generated_files} ${_cuda_wrap_generated_files})
 
987
endmacro(CUDA_WRAP_SRCS)
 
988
 
 
989
 
 
990
###############################################################################
 
991
###############################################################################
 
992
# ADD LIBRARY
 
993
###############################################################################
 
994
###############################################################################
 
995
macro(CUDA_ADD_LIBRARY cuda_target)
 
996
 
 
997
  CUDA_ADD_CUDA_INCLUDE_ONCE()
 
998
 
 
999
  # Separate the sources from the options
 
1000
  CUDA_GET_SOURCES_AND_OPTIONS(_sources _options ${ARGN})
 
1001
  # Create custom commands and targets for each file.
 
1002
  CUDA_WRAP_SRCS( ${cuda_target} OBJ _generated_files ${_sources} OPTIONS ${_options} )
 
1003
 
 
1004
  # Add the library.
 
1005
  add_library(${cuda_target}
 
1006
    ${_generated_files}
 
1007
    ${_sources}
 
1008
    )
 
1009
 
 
1010
  target_link_libraries(${cuda_target}
 
1011
    ${CUDA_LIBRARIES}
 
1012
    )
 
1013
 
 
1014
  # We need to set the linker language based on what the expected generated file
 
1015
  # would be. CUDA_C_OR_CXX is computed based on CUDA_HOST_COMPILATION_CPP.
 
1016
  set_target_properties(${cuda_target}
 
1017
    PROPERTIES
 
1018
    LINKER_LANGUAGE ${CUDA_C_OR_CXX}
 
1019
    )
 
1020
 
 
1021
endmacro(CUDA_ADD_LIBRARY cuda_target)
 
1022
 
 
1023
 
 
1024
###############################################################################
 
1025
###############################################################################
 
1026
# ADD EXECUTABLE
 
1027
###############################################################################
 
1028
###############################################################################
 
1029
macro(CUDA_ADD_EXECUTABLE cuda_target)
 
1030
 
 
1031
  CUDA_ADD_CUDA_INCLUDE_ONCE()
 
1032
 
 
1033
  # Separate the sources from the options
 
1034
  CUDA_GET_SOURCES_AND_OPTIONS(_sources _options ${ARGN})
 
1035
  # Create custom commands and targets for each file.
 
1036
  CUDA_WRAP_SRCS( ${cuda_target} OBJ _generated_files ${_sources} OPTIONS ${_options} )
 
1037
 
 
1038
  # Add the library.
 
1039
  add_executable(${cuda_target}
 
1040
    ${_generated_files}
 
1041
    ${_sources}
 
1042
    )
 
1043
 
 
1044
  target_link_libraries(${cuda_target}
 
1045
    ${CUDA_LIBRARIES}
 
1046
    )
 
1047
 
 
1048
  # We need to set the linker language based on what the expected generated file
 
1049
  # would be. CUDA_C_OR_CXX is computed based on CUDA_HOST_COMPILATION_CPP.
 
1050
  set_target_properties(${cuda_target}
 
1051
    PROPERTIES
 
1052
    LINKER_LANGUAGE ${CUDA_C_OR_CXX}
 
1053
    )
 
1054
 
 
1055
endmacro(CUDA_ADD_EXECUTABLE cuda_target)
 
1056
 
 
1057
 
 
1058
###############################################################################
 
1059
###############################################################################
 
1060
# CUDA COMPILE
 
1061
###############################################################################
 
1062
###############################################################################
 
1063
macro(CUDA_COMPILE generated_files)
 
1064
 
 
1065
  # Separate the sources from the options
 
1066
  CUDA_GET_SOURCES_AND_OPTIONS(_sources _options ${ARGN})
 
1067
  # Create custom commands and targets for each file.
 
1068
  CUDA_WRAP_SRCS( cuda_compile OBJ _generated_files ${_sources} OPTIONS ${_options} )
 
1069
 
 
1070
  set( ${generated_files} ${_generated_files})
 
1071
 
 
1072
endmacro(CUDA_COMPILE)
 
1073
 
 
1074
 
 
1075
###############################################################################
 
1076
###############################################################################
 
1077
# CUDA COMPILE PTX
 
1078
###############################################################################
 
1079
###############################################################################
 
1080
macro(CUDA_COMPILE_PTX generated_files)
 
1081
 
 
1082
  # Separate the sources from the options
 
1083
  CUDA_GET_SOURCES_AND_OPTIONS(_sources _options ${ARGN})
 
1084
  # Create custom commands and targets for each file.
 
1085
  CUDA_WRAP_SRCS( cuda_compile_ptx PTX _generated_files ${_sources} OPTIONS ${_options} )
 
1086
 
 
1087
  set( ${generated_files} ${_generated_files})
 
1088
 
 
1089
endmacro(CUDA_COMPILE_PTX)
 
1090
 
 
1091
###############################################################################
 
1092
###############################################################################
 
1093
# CUDA ADD CUFFT TO TARGET
 
1094
###############################################################################
 
1095
###############################################################################
 
1096
macro(CUDA_ADD_CUFFT_TO_TARGET target)
 
1097
  if (CUDA_BUILD_EMULATION)
 
1098
    target_link_libraries(${target} ${CUDA_cufftemu_LIBRARY})
 
1099
  else()
 
1100
    target_link_libraries(${target} ${CUDA_cufft_LIBRARY})
 
1101
  endif()
 
1102
endmacro()
 
1103
 
 
1104
###############################################################################
 
1105
###############################################################################
 
1106
# CUDA ADD CUBLAS TO TARGET
 
1107
###############################################################################
 
1108
###############################################################################
 
1109
macro(CUDA_ADD_CUBLAS_TO_TARGET target)
 
1110
  if (CUDA_BUILD_EMULATION)
 
1111
    target_link_libraries(${target} ${CUDA_cublasemu_LIBRARY})
 
1112
  else()
 
1113
    target_link_libraries(${target} ${CUDA_cublas_LIBRARY})
 
1114
  endif()
 
1115
endmacro()
 
1116
 
 
1117
###############################################################################
 
1118
###############################################################################
 
1119
# CUDA BUILD CLEAN TARGET
 
1120
###############################################################################
 
1121
###############################################################################
 
1122
macro(CUDA_BUILD_CLEAN_TARGET)
 
1123
  # Call this after you add all your CUDA targets, and you will get a convience
 
1124
  # target.  You should also make clean after running this target to get the
 
1125
  # build system to generate all the code again.
 
1126
 
 
1127
  set(cuda_clean_target_name clean_cuda_depends)
 
1128
  if (CMAKE_GENERATOR MATCHES "Visual Studio")
 
1129
    string(TOUPPER ${cuda_clean_target_name} cuda_clean_target_name)
 
1130
  endif()
 
1131
  add_custom_target(${cuda_clean_target_name}
 
1132
    COMMAND ${CMAKE_COMMAND} -E remove ${CUDA_ADDITIONAL_CLEAN_FILES})
 
1133
 
 
1134
  # Clear out the variable, so the next time we configure it will be empty.
 
1135
  # This is useful so that the files won't persist in the list after targets
 
1136
  # have been removed.
 
1137
  set(CUDA_ADDITIONAL_CLEAN_FILES "" CACHE INTERNAL "List of intermediate files that are part of the cuda dependency scanning.")
 
1138
endmacro(CUDA_BUILD_CLEAN_TARGET)