1
# This file runs the nvcc commands to produce the desired output file along with
2
# the dependency file needed by CMake to compute dependencies. In addition the
3
# file checks the output of each command and if the command fails it deletes the
8
# verbose:BOOL=<> OFF: Be as quiet as possible (default)
9
# ON : Describe each step
11
# build_configuration:STRING=<> Typically one of Debug, MinSizeRel, Release, or
12
# RelWithDebInfo, but it should match one of the
13
# entries in CUDA_HOST_FLAGS. This is the build
14
# configuration used when compiling the code. If
15
# blank or unspecified Debug is assumed as this is
18
# generated_file:STRING=<> File to generate. This argument must be passed in.
20
# generated_cubin_file:STRING=<> File to generate. This argument must be passed
21
# in if build_cubin is true.
23
if(NOT generated_file)
24
message(FATAL_ERROR "You must specify generated_file on the command line")
27
# Set these up as variables to make reading the generated file easier
28
set(CMAKE_COMMAND "@CMAKE_COMMAND@")
29
set(source_file "@source_file@")
30
set(NVCC_generated_dependency_file "@NVCC_generated_dependency_file@")
31
set(cmake_dependency_file "@cmake_dependency_file@")
32
set(CUDA_make2cmake "@CUDA_make2cmake@")
33
set(CUDA_parse_cubin "@CUDA_parse_cubin@")
34
set(build_cubin @build_cubin@)
35
# We won't actually use these variables for now, but we need to set this, in
36
# order to force this file to be run again if it changes.
37
set(generated_file_path "@generated_file_path@")
38
set(generated_file_internal "@generated_file@")
39
set(generated_cubin_file_internal "@generated_cubin_file@")
41
set(CUDA_NVCC_EXECUTABLE "@CUDA_NVCC_EXECUTABLE@")
42
set(CUDA_NVCC_FLAGS "@CUDA_NVCC_FLAGS@;@CUDA_WRAP_OPTION_NVCC_FLAGS@")
43
@CUDA_NVCC_FLAGS_CONFIG@
44
set(nvcc_flags "@nvcc_flags@")
45
set(CUDA_NVCC_INCLUDE_ARGS "@CUDA_NVCC_INCLUDE_ARGS@")
46
set(format_flag "@format_flag@")
48
if(build_cubin AND NOT generated_cubin_file)
49
message(FATAL_ERROR "You must specify generated_cubin_file on the command line")
52
# This is the list of host compilation flags. It C or CXX should already have
53
# been chosen by FindCUDA.cmake.
56
# Take the compiler flags and package them up to be sent to the compiler via -Xcompiler
57
set(nvcc_host_compiler_flags "")
58
# If we weren't given a build_configuration, use Debug.
59
if(NOT build_configuration)
60
set(build_configuration Debug)
62
string(TOUPPER "${build_configuration}" build_configuration)
63
#message("CUDA_NVCC_HOST_COMPILER_FLAGS = ${CUDA_NVCC_HOST_COMPILER_FLAGS}")
64
foreach(flag ${CMAKE_HOST_FLAGS} ${CMAKE_HOST_FLAGS_${build_configuration}})
65
# Extra quotes are added around each flag to help nvcc parse out flags with spaces.
66
set(nvcc_host_compiler_flags "${nvcc_host_compiler_flags},\"${flag}\"")
68
if (nvcc_host_compiler_flags)
69
set(nvcc_host_compiler_flags "-Xcompiler" ${nvcc_host_compiler_flags})
71
#message("nvcc_host_compiler_flags = \"${nvcc_host_compiler_flags}\"")
72
# Add the build specific configuration flags
73
list(APPEND CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS_${build_configuration}})
76
set(CCBIN -ccbin "${CCBIN}")
79
# cuda_execute_process - Executes a command with optional command echo and status message.
81
# status - Status message to print if verbose is true
82
# command - COMMAND argument from the usual execute_process argument structure
83
# ARGN - Remaining arguments are the command with arguments
85
# CUDA_result - return value from running the command
87
# Make this a macro instead of a function, so that things like RESULT_VARIABLE
88
# and other return variables are present after executing the process.
89
macro(cuda_execute_process status command)
90
set(_command ${command})
91
if(NOT _command STREQUAL "COMMAND")
92
message(FATAL_ERROR "Malformed call to cuda_execute_process. Missing COMMAND as second argument. (command = ${command})")
95
execute_process(COMMAND "${CMAKE_COMMAND}" -E echo -- ${status})
96
# Now we need to build up our command string. We are accounting for quotes
97
# and spaces, anything else is left up to the user to fix if they want to
98
# copy and paste a runnable command line.
99
set(cuda_execute_process_string)
101
# If there are quotes, excape them, so they come through.
102
string(REPLACE "\"" "\\\"" arg ${arg})
103
# Args with spaces need quotes around them to get them to be parsed as a single argument.
105
list(APPEND cuda_execute_process_string "\"${arg}\"")
107
list(APPEND cuda_execute_process_string ${arg})
111
execute_process(COMMAND ${CMAKE_COMMAND} -E echo ${cuda_execute_process_string})
114
execute_process(COMMAND ${ARGN} RESULT_VARIABLE CUDA_result )
117
# Delete the target file
118
cuda_execute_process(
119
"Removing ${generated_file}"
120
COMMAND "${CMAKE_COMMAND}" -E remove "${generated_file}"
123
# Generate the dependency file
124
cuda_execute_process(
125
"Generating dependency file: ${NVCC_generated_dependency_file}"
126
COMMAND "${CUDA_NVCC_EXECUTABLE}"
131
${nvcc_host_compiler_flags}
134
-o "${NVCC_generated_dependency_file}"
135
${CUDA_NVCC_INCLUDE_ARGS}
139
message(FATAL_ERROR "Error generating ${generated_file}")
142
# Generate the cmake readable dependency file to a temp file. Don't put the
143
# quotes just around the filenames for the input_file and output_file variables.
144
# CMake will pass the quotes through and not be able to find the file.
145
cuda_execute_process(
146
"Generating temporary cmake readable file: ${cmake_dependency_file}.tmp"
147
COMMAND "${CMAKE_COMMAND}"
148
-D "input_file:FILEPATH=${NVCC_generated_dependency_file}"
149
-D "output_file:FILEPATH=${cmake_dependency_file}.tmp"
150
-P "${CUDA_make2cmake}"
154
message(FATAL_ERROR "Error generating ${generated_file}")
157
# Copy the file if it is different
158
cuda_execute_process(
159
"Copy if different ${cmake_dependency_file}.tmp to ${cmake_dependency_file}"
160
COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${cmake_dependency_file}.tmp" "${cmake_dependency_file}"
164
message(FATAL_ERROR "Error generating ${generated_file}")
167
# Delete the temporary file
168
cuda_execute_process(
169
"Removing ${cmake_dependency_file}.tmp and ${NVCC_generated_dependency_file}"
170
COMMAND "${CMAKE_COMMAND}" -E remove "${cmake_dependency_file}.tmp" "${NVCC_generated_dependency_file}"
174
message(FATAL_ERROR "Error generating ${generated_file}")
178
cuda_execute_process(
179
"Generating ${generated_file}"
180
COMMAND "${CUDA_NVCC_EXECUTABLE}"
185
${nvcc_host_compiler_flags}
187
${format_flag} -o "${generated_file}"
188
${CUDA_NVCC_INCLUDE_ARGS}
192
# Since nvcc can sometimes leave half done files make sure that we delete the output file.
193
cuda_execute_process(
194
"Removing ${generated_file}"
195
COMMAND "${CMAKE_COMMAND}" -E remove "${generated_file}"
197
message(FATAL_ERROR "Error generating file ${generated_file}")
199
message("Generated ${generated_file} successfully.")
202
# Cubin resource report commands.
204
# Run with -cubin to produce resource usage report.
205
cuda_execute_process(
206
"Generating ${generated_cubin_file}"
207
COMMAND "${CUDA_NVCC_EXECUTABLE}"
212
${nvcc_host_compiler_flags}
215
-o "${generated_cubin_file}"
216
${CUDA_NVCC_INCLUDE_ARGS}
219
# Execute the parser script.
220
cuda_execute_process(
221
"Executing the parser script"
222
COMMAND "${CMAKE_COMMAND}"
223
-D "input_file:STRING=${generated_cubin_file}"
224
-P "${CUDA_parse_cubin}"