• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#  James Bigler, NVIDIA Corp (nvidia.com - jbigler)
2#
3#  Copyright (c) 2008 - 2009 NVIDIA Corporation.  All rights reserved.
4#
5#  This code is licensed under the MIT License.  See the FindCUDA.cmake script
6#  for the text of the license.
7
8# The MIT License
9#
10# License for the specific language governing rights and limitations under
11# Permission is hereby granted, free of charge, to any person obtaining a
12# copy of this software and associated documentation files (the "Software"),
13# to deal in the Software without restriction, including without limitation
14# the rights to use, copy, modify, merge, publish, distribute, sublicense,
15# and/or sell copies of the Software, and to permit persons to whom the
16# Software is furnished to do so, subject to the following conditions:
17#
18# The above copyright notice and this permission notice shall be included
19# in all copies or substantial portions of the Software.
20#
21# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27# DEALINGS IN THE SOFTWARE.
28
29
30##########################################################################
31# This file runs the nvcc commands to produce the desired output file along with
32# the dependency file needed by CMake to compute dependencies.  In addition the
33# file checks the output of each command and if the command fails it deletes the
34# output files.
35
36# Input variables
37#
38# verbose:BOOL=<>          OFF: Be as quiet as possible (default)
39#                          ON : Describe each step
40#
41# build_configuration:STRING=<> Typically one of Debug, MinSizeRel, Release, or
42#                               RelWithDebInfo, but it should match one of the
43#                               entries in CUDA_HOST_FLAGS. This is the build
44#                               configuration used when compiling the code.  If
45#                               blank or unspecified Debug is assumed as this is
46#                               what CMake does.
47#
48# generated_file:STRING=<> File to generate.  This argument must be passed in.
49#
50# generated_cubin_file:STRING=<> File to generate.  This argument must be passed
51#                                                   in if build_cubin is true.
52
53cmake_policy(PUSH)
54cmake_policy(SET CMP0007 NEW)
55cmake_policy(SET CMP0010 NEW)
56if(NOT generated_file)
57  message(FATAL_ERROR "You must specify generated_file on the command line")
58endif()
59
60# Set these up as variables to make reading the generated file easier
61set(CMAKE_COMMAND "@CMAKE_COMMAND@") # path
62set(source_file "@source_file@") # path
63set(NVCC_generated_dependency_file "@NVCC_generated_dependency_file@") # path
64set(cmake_dependency_file "@cmake_dependency_file@") # path
65set(CUDA_make2cmake "@CUDA_make2cmake@") # path
66set(CUDA_parse_cubin "@CUDA_parse_cubin@") # path
67set(build_cubin @build_cubin@) # bool
68set(CUDA_HOST_COMPILER "@CUDA_HOST_COMPILER@") # path
69# We won't actually use these variables for now, but we need to set this, in
70# order to force this file to be run again if it changes.
71set(generated_file_path "@generated_file_path@") # path
72set(generated_file_internal "@generated_file@") # path
73set(generated_cubin_file_internal "@generated_cubin_file@") # path
74
75set(CUDA_NVCC_EXECUTABLE "@CUDA_NVCC_EXECUTABLE@") # path
76set(CUDA_NVCC_FLAGS @CUDA_NVCC_FLAGS@ ;; @CUDA_WRAP_OPTION_NVCC_FLAGS@) # list
77@CUDA_NVCC_FLAGS_CONFIG@
78set(nvcc_flags @nvcc_flags@) # list
79set(CUDA_NVCC_INCLUDE_DIRS [==[@CUDA_NVCC_INCLUDE_DIRS@]==]) # list (needs to be in lua quotes to address backslashes)
80string(REPLACE "\\" "/" CUDA_NVCC_INCLUDE_DIRS "${CUDA_NVCC_INCLUDE_DIRS}")
81set(CUDA_NVCC_COMPILE_DEFINITIONS [==[@CUDA_NVCC_COMPILE_DEFINITIONS@]==]) # list (needs to be in lua quotes see #16510 ).
82set(format_flag "@format_flag@") # string
83set(cuda_language_flag @cuda_language_flag@) # list
84
85# Clean up list of include directories and add -I flags
86list(REMOVE_DUPLICATES CUDA_NVCC_INCLUDE_DIRS)
87set(CUDA_NVCC_INCLUDE_ARGS)
88foreach(dir ${CUDA_NVCC_INCLUDE_DIRS})
89  # Extra quotes are added around each flag to help nvcc parse out flags with spaces.
90  list(APPEND CUDA_NVCC_INCLUDE_ARGS "-I${dir}")
91endforeach()
92
93# Clean up list of compile definitions, add -D flags, and append to nvcc_flags
94list(REMOVE_DUPLICATES CUDA_NVCC_COMPILE_DEFINITIONS)
95foreach(def ${CUDA_NVCC_COMPILE_DEFINITIONS})
96  list(APPEND nvcc_flags "-D${def}")
97endforeach()
98
99if(build_cubin AND NOT generated_cubin_file)
100  message(FATAL_ERROR "You must specify generated_cubin_file on the command line")
101endif()
102
103# This is the list of host compilation flags.  It C or CXX should already have
104# been chosen by FindCUDA.cmake.
105@CUDA_HOST_FLAGS@
106
107# Take the compiler flags and package them up to be sent to the compiler via -Xcompiler
108set(nvcc_host_compiler_flags "")
109# If we weren't given a build_configuration, use Debug.
110if(NOT build_configuration)
111  set(build_configuration Debug)
112endif()
113string(TOUPPER "${build_configuration}" build_configuration)
114#message("CUDA_NVCC_HOST_COMPILER_FLAGS = ${CUDA_NVCC_HOST_COMPILER_FLAGS}")
115foreach(flag ${CMAKE_HOST_FLAGS} ${CMAKE_HOST_FLAGS_${build_configuration}})
116  # Extra quotes are added around each flag to help nvcc parse out flags with spaces.
117  string(APPEND nvcc_host_compiler_flags ",\"${flag}\"")
118endforeach()
119if (nvcc_host_compiler_flags)
120  set(nvcc_host_compiler_flags "-Xcompiler" ${nvcc_host_compiler_flags})
121endif()
122#message("nvcc_host_compiler_flags = \"${nvcc_host_compiler_flags}\"")
123# Add the build specific configuration flags
124list(APPEND CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS_${build_configuration}})
125
126# Any -ccbin existing in CUDA_NVCC_FLAGS gets highest priority
127list( FIND CUDA_NVCC_FLAGS "-ccbin" ccbin_found0 )
128list( FIND CUDA_NVCC_FLAGS "--compiler-bindir" ccbin_found1 )
129if( ccbin_found0 LESS 0 AND ccbin_found1 LESS 0 AND CUDA_HOST_COMPILER )
130  if (CUDA_HOST_COMPILER STREQUAL "@_CUDA_MSVC_HOST_COMPILER@" AND DEFINED CCBIN)
131    set(CCBIN -ccbin "${CCBIN}")
132  else()
133    set(CCBIN -ccbin "${CUDA_HOST_COMPILER}")
134  endif()
135endif()
136
137# cuda_execute_process - Executes a command with optional command echo and status message.
138#
139#   status  - Status message to print if verbose is true
140#   command - COMMAND argument from the usual execute_process argument structure
141#   ARGN    - Remaining arguments are the command with arguments
142#
143#   CUDA_result - return value from running the command
144#
145# Make this a macro instead of a function, so that things like RESULT_VARIABLE
146# and other return variables are present after executing the process.
147macro(cuda_execute_process status command)
148  set(_command ${command})
149  if(NOT "x${_command}" STREQUAL "xCOMMAND")
150    message(FATAL_ERROR "Malformed call to cuda_execute_process.  Missing COMMAND as second argument. (command = ${command})")
151  endif()
152  if(verbose)
153    execute_process(COMMAND "${CMAKE_COMMAND}" -E echo -- ${status})
154    # Now we need to build up our command string.  We are accounting for quotes
155    # and spaces, anything else is left up to the user to fix if they want to
156    # copy and paste a runnable command line.
157    set(cuda_execute_process_string)
158    foreach(arg ${ARGN})
159      # If there are quotes, excape them, so they come through.
160      string(REPLACE "\"" "\\\"" arg ${arg})
161      # Args with spaces need quotes around them to get them to be parsed as a single argument.
162      if(arg MATCHES " ")
163        list(APPEND cuda_execute_process_string "\"${arg}\"")
164      else()
165        list(APPEND cuda_execute_process_string ${arg})
166      endif()
167    endforeach()
168    # Echo the command
169    execute_process(COMMAND ${CMAKE_COMMAND} -E echo ${cuda_execute_process_string})
170  endif()
171  # Run the command
172  execute_process(COMMAND ${ARGN} RESULT_VARIABLE CUDA_result )
173endmacro()
174
175# Delete the target file
176cuda_execute_process(
177  "Removing ${generated_file}"
178  COMMAND "${CMAKE_COMMAND}" -E remove "${generated_file}"
179  )
180
181# For CUDA 2.3 and below, -G -M doesn't work, so remove the -G flag
182# for dependency generation and hope for the best.
183set(depends_CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS}")
184set(CUDA_VERSION @CUDA_VERSION@)
185
186# nvcc doesn't define __CUDACC__ for some reason when generating dependency files.  This
187# can cause incorrect dependencies when #including files based on this macro which is
188# defined in the generating passes of nvcc invocation.  We will go ahead and manually
189# define this for now until a future version fixes this bug.
190set(CUDACC_DEFINE -D__CUDACC__)
191
192# Generate the dependency file
193cuda_execute_process(
194  "Generating dependency file: ${NVCC_generated_dependency_file}"
195  COMMAND "${CUDA_NVCC_EXECUTABLE}"
196  -M
197  ${CUDACC_DEFINE}
198  "${source_file}"
199  -o "${NVCC_generated_dependency_file}"
200  ${CCBIN}
201  ${nvcc_flags}
202  ${nvcc_host_compiler_flags}
203  ${depends_CUDA_NVCC_FLAGS}
204  -DNVCC
205  ${CUDA_NVCC_INCLUDE_ARGS}
206  )
207
208if(CUDA_result)
209  message(FATAL_ERROR "Error generating ${generated_file}")
210endif()
211
212# Generate the cmake readable dependency file to a temp file.  Don't put the
213# quotes just around the filenames for the input_file and output_file variables.
214# CMake will pass the quotes through and not be able to find the file.
215cuda_execute_process(
216  "Generating temporary cmake readable file: ${cmake_dependency_file}.tmp"
217  COMMAND "${CMAKE_COMMAND}"
218  -D "input_file:FILEPATH=${NVCC_generated_dependency_file}"
219  -D "output_file:FILEPATH=${cmake_dependency_file}.tmp"
220  -D "verbose=${verbose}"
221  -P "${CUDA_make2cmake}"
222  )
223
224if(CUDA_result)
225  message(FATAL_ERROR "Error generating ${generated_file}")
226endif()
227
228# Copy the file if it is different
229cuda_execute_process(
230  "Copy if different ${cmake_dependency_file}.tmp to ${cmake_dependency_file}"
231  COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${cmake_dependency_file}.tmp" "${cmake_dependency_file}"
232  )
233
234if(CUDA_result)
235  message(FATAL_ERROR "Error generating ${generated_file}")
236endif()
237
238# Delete the temporary file
239cuda_execute_process(
240  "Removing ${cmake_dependency_file}.tmp and ${NVCC_generated_dependency_file}"
241  COMMAND "${CMAKE_COMMAND}" -E remove "${cmake_dependency_file}.tmp" "${NVCC_generated_dependency_file}"
242  )
243
244if(CUDA_result)
245  message(FATAL_ERROR "Error generating ${generated_file}")
246endif()
247
248# Generate the code
249cuda_execute_process(
250  "Generating ${generated_file}"
251  COMMAND "${CUDA_NVCC_EXECUTABLE}"
252  "${source_file}"
253  ${cuda_language_flag}
254  ${format_flag} -o "${generated_file}"
255  ${CCBIN}
256  ${nvcc_flags}
257  ${nvcc_host_compiler_flags}
258  ${CUDA_NVCC_FLAGS}
259  -DNVCC
260  ${CUDA_NVCC_INCLUDE_ARGS}
261  )
262
263if(CUDA_result)
264  # Since nvcc can sometimes leave half done files make sure that we delete the output file.
265  cuda_execute_process(
266    "Removing ${generated_file}"
267    COMMAND "${CMAKE_COMMAND}" -E remove "${generated_file}"
268    )
269  message(FATAL_ERROR "Error generating file ${generated_file}")
270else()
271  if(verbose)
272    message("Generated ${generated_file} successfully.")
273  endif()
274endif()
275
276# Cubin resource report commands.
277if( build_cubin )
278  # Run with -cubin to produce resource usage report.
279  cuda_execute_process(
280    "Generating ${generated_cubin_file}"
281    COMMAND "${CUDA_NVCC_EXECUTABLE}"
282    "${source_file}"
283    ${CUDA_NVCC_FLAGS}
284    ${nvcc_flags}
285    ${CCBIN}
286    ${nvcc_host_compiler_flags}
287    -DNVCC
288    -cubin
289    -o "${generated_cubin_file}"
290    ${CUDA_NVCC_INCLUDE_ARGS}
291    )
292
293  # Execute the parser script.
294  cuda_execute_process(
295    "Executing the parser script"
296    COMMAND  "${CMAKE_COMMAND}"
297    -D "input_file:STRING=${generated_cubin_file}"
298    -P "${CUDA_parse_cubin}"
299    )
300
301endif()
302
303cmake_policy(POP)
304