1 # James Bigler, NVIDIA Corp (nvidia.com - jbigler)
3 # Copyright (c) 2008 - 2009 NVIDIA Corporation. All rights reserved.
5 # This code is licensed under the MIT License. See the FindCUDA.cmake script
6 # for the text of the license.
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:
18 # The above copyright notice and this permission notice shall be included
19 # in all copies or substantial portions of the Software.
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.
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
38 # verbose:BOOL=<> OFF: Be as quiet as possible (default)
39 # ON : Describe each step
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
48 # generated_file:STRING=<> File to generate. This argument must be passed in.
50 # generated_cubin_file:STRING=<> File to generate. This argument must be passed
51 # in if build_cubin is true.
53 if(NOT generated_file)
54 message(FATAL_ERROR "You must specify generated_file on the command line")
57 # Set these up as variables to make reading the generated file easier
58 set(CMAKE_COMMAND "@CMAKE_COMMAND@")
59 set(source_file "@source_file@")
60 set(NVCC_generated_dependency_file "@NVCC_generated_dependency_file@")
61 set(cmake_dependency_file "@cmake_dependency_file@")
62 set(CUDA_make2cmake "@CUDA_make2cmake@")
63 set(CUDA_parse_cubin "@CUDA_parse_cubin@")
64 set(build_cubin @build_cubin@)
65 # We won't actually use these variables for now, but we need to set this, in
66 # order to force this file to be run again if it changes.
67 set(generated_file_path "@generated_file_path@")
68 set(generated_file_internal "@generated_file@")
69 set(generated_cubin_file_internal "@generated_cubin_file@")
71 set(CUDA_NVCC_EXECUTABLE "@CUDA_NVCC_EXECUTABLE@")
72 set(CUDA_NVCC_FLAGS "@CUDA_NVCC_FLAGS@;;@CUDA_WRAP_OPTION_NVCC_FLAGS@")
73 @CUDA_NVCC_FLAGS_CONFIG@
74 set(nvcc_flags "@nvcc_flags@")
75 set(CUDA_NVCC_INCLUDE_ARGS "@CUDA_NVCC_INCLUDE_ARGS@")
76 set(format_flag "@format_flag@")
78 if(build_cubin AND NOT generated_cubin_file)
79 message(FATAL_ERROR "You must specify generated_cubin_file on the command line")
82 # This is the list of host compilation flags. It C or CXX should already have
83 # been chosen by FindCUDA.cmake.
86 # Take the compiler flags and package them up to be sent to the compiler via -Xcompiler
87 set(nvcc_host_compiler_flags "")
88 # If we weren't given a build_configuration, use Debug.
89 if(NOT build_configuration)
90 set(build_configuration Debug)
92 string(TOUPPER "${build_configuration}" build_configuration)
93 #message("CUDA_NVCC_HOST_COMPILER_FLAGS = ${CUDA_NVCC_HOST_COMPILER_FLAGS}")
94 foreach(flag ${CMAKE_HOST_FLAGS} ${CMAKE_HOST_FLAGS_${build_configuration}})
95 # Extra quotes are added around each flag to help nvcc parse out flags with spaces.
96 set(nvcc_host_compiler_flags "${nvcc_host_compiler_flags},\"${flag}\"")
98 if (nvcc_host_compiler_flags)
99 set(nvcc_host_compiler_flags "-Xcompiler" ${nvcc_host_compiler_flags})
101 #message("nvcc_host_compiler_flags = \"${nvcc_host_compiler_flags}\"")
102 # Add the build specific configuration flags
103 list(APPEND CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS_${build_configuration}})
106 set(CCBIN -ccbin "${CCBIN}")
109 # cuda_execute_process - Executes a command with optional command echo and status message.
111 # status - Status message to print if verbose is true
112 # command - COMMAND argument from the usual execute_process argument structure
113 # ARGN - Remaining arguments are the command with arguments
115 # CUDA_result - return value from running the command
117 # Make this a macro instead of a function, so that things like RESULT_VARIABLE
118 # and other return variables are present after executing the process.
119 macro(cuda_execute_process status command)
120 set(_command ${command})
121 if(NOT _command STREQUAL "COMMAND")
122 message(FATAL_ERROR "Malformed call to cuda_execute_process. Missing COMMAND as second argument. (command = ${command})")
125 execute_process(COMMAND "${CMAKE_COMMAND}" -E echo -- ${status})
126 # Now we need to build up our command string. We are accounting for quotes
127 # and spaces, anything else is left up to the user to fix if they want to
128 # copy and paste a runnable command line.
129 set(cuda_execute_process_string)
131 # If there are quotes, excape them, so they come through.
132 string(REPLACE "\"" "\\\"" arg ${arg})
133 # Args with spaces need quotes around them to get them to be parsed as a single argument.
135 list(APPEND cuda_execute_process_string "\"${arg}\"")
137 list(APPEND cuda_execute_process_string ${arg})
141 execute_process(COMMAND ${CMAKE_COMMAND} -E echo ${cuda_execute_process_string})
144 execute_process(COMMAND ${ARGN} RESULT_VARIABLE CUDA_result )
147 # Delete the target file
148 cuda_execute_process(
149 "Removing ${generated_file}"
150 COMMAND "${CMAKE_COMMAND}" -E remove "${generated_file}"
153 # Generate the dependency file
154 cuda_execute_process(
155 "Generating dependency file: ${NVCC_generated_dependency_file}"
156 COMMAND "${CUDA_NVCC_EXECUTABLE}"
161 ${nvcc_host_compiler_flags}
164 -o "${NVCC_generated_dependency_file}"
165 ${CUDA_NVCC_INCLUDE_ARGS}
169 message(FATAL_ERROR "Error generating ${generated_file}")
172 # Generate the cmake readable dependency file to a temp file. Don't put the
173 # quotes just around the filenames for the input_file and output_file variables.
174 # CMake will pass the quotes through and not be able to find the file.
175 cuda_execute_process(
176 "Generating temporary cmake readable file: ${cmake_dependency_file}.tmp"
177 COMMAND "${CMAKE_COMMAND}"
178 -D "input_file:FILEPATH=${NVCC_generated_dependency_file}"
179 -D "output_file:FILEPATH=${cmake_dependency_file}.tmp"
180 -P "${CUDA_make2cmake}"
184 message(FATAL_ERROR "Error generating ${generated_file}")
187 # Copy the file if it is different
188 cuda_execute_process(
189 "Copy if different ${cmake_dependency_file}.tmp to ${cmake_dependency_file}"
190 COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${cmake_dependency_file}.tmp" "${cmake_dependency_file}"
194 message(FATAL_ERROR "Error generating ${generated_file}")
197 # Delete the temporary file
198 cuda_execute_process(
199 "Removing ${cmake_dependency_file}.tmp and ${NVCC_generated_dependency_file}"
200 COMMAND "${CMAKE_COMMAND}" -E remove "${cmake_dependency_file}.tmp" "${NVCC_generated_dependency_file}"
204 message(FATAL_ERROR "Error generating ${generated_file}")
208 cuda_execute_process(
209 "Generating ${generated_file}"
210 COMMAND "${CUDA_NVCC_EXECUTABLE}"
215 ${nvcc_host_compiler_flags}
217 ${format_flag} -o "${generated_file}"
218 ${CUDA_NVCC_INCLUDE_ARGS}
222 # Since nvcc can sometimes leave half done files make sure that we delete the output file.
223 cuda_execute_process(
224 "Removing ${generated_file}"
225 COMMAND "${CMAKE_COMMAND}" -E remove "${generated_file}"
227 message(FATAL_ERROR "Error generating file ${generated_file}")
230 message("Generated ${generated_file} successfully.")
234 # Cubin resource report commands.
236 # Run with -cubin to produce resource usage report.
237 cuda_execute_process(
238 "Generating ${generated_cubin_file}"
239 COMMAND "${CUDA_NVCC_EXECUTABLE}"
244 ${nvcc_host_compiler_flags}
247 -o "${generated_cubin_file}"
248 ${CUDA_NVCC_INCLUDE_ARGS}
251 # Execute the parser script.
252 cuda_execute_process(
253 "Executing the parser script"
254 COMMAND "${CMAKE_COMMAND}"
255 -D "input_file:STRING=${generated_cubin_file}"
256 -P "${CUDA_parse_cubin}"