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@") # path
59 set(source_file "@source_file@") # path
60 set(NVCC_generated_dependency_file "@NVCC_generated_dependency_file@") # path
61 set(cmake_dependency_file "@cmake_dependency_file@") # path
62 set(CUDA_make2cmake "@CUDA_make2cmake@") # path
63 set(CUDA_parse_cubin "@CUDA_parse_cubin@") # path
64 set(build_cubin @build_cubin@) # bool
65 set(CUDA_HOST_COMPILER "@CUDA_HOST_COMPILER@") # path
66 # We won't actually use these variables for now, but we need to set this, in
67 # order to force this file to be run again if it changes.
68 set(generated_file_path "@generated_file_path@") # path
69 set(generated_file_internal "@generated_file@") # path
70 set(generated_cubin_file_internal "@generated_cubin_file@") # path
72 set(CUDA_NVCC_EXECUTABLE "@CUDA_NVCC_EXECUTABLE@") # path
73 set(CUDA_NVCC_FLAGS @CUDA_NVCC_FLAGS@ ;; @CUDA_WRAP_OPTION_NVCC_FLAGS@) # list
74 @CUDA_NVCC_FLAGS_CONFIG@
75 set(nvcc_flags @nvcc_flags@) # list
76 set(CUDA_NVCC_INCLUDE_DIRS "@CUDA_NVCC_INCLUDE_DIRS@") # list (needs to be in quotes to handle spaces properly).
77 set(CUDA_NVCC_COMPILE_DEFINITIONS "@CUDA_NVCC_COMPILE_DEFINITIONS@") # list (needs to be in quotes to handle spaces properly).
78 set(format_flag "@format_flag@") # string
79 set(cuda_language_flag @cuda_language_flag@) # list
81 # Clean up list of include directories and add -I flags
82 list(REMOVE_DUPLICATES CUDA_NVCC_INCLUDE_DIRS)
83 set(CUDA_NVCC_INCLUDE_ARGS)
84 foreach(dir ${CUDA_NVCC_INCLUDE_DIRS})
85 # Extra quotes are added around each flag to help nvcc parse out flags with spaces.
86 list(APPEND CUDA_NVCC_INCLUDE_ARGS "-I${dir}")
89 # Clean up list of compile definitions, add -D flags, and append to nvcc_flags
90 list(REMOVE_DUPLICATES CUDA_NVCC_COMPILE_DEFINITIONS)
91 foreach(def ${CUDA_NVCC_COMPILE_DEFINITIONS})
92 list(APPEND nvcc_flags "-D${def}")
95 if(build_cubin AND NOT generated_cubin_file)
96 message(FATAL_ERROR "You must specify generated_cubin_file on the command line")
99 # This is the list of host compilation flags. It C or CXX should already have
100 # been chosen by FindCUDA.cmake.
103 # Take the compiler flags and package them up to be sent to the compiler via -Xcompiler
104 set(nvcc_host_compiler_flags "")
105 # If we weren't given a build_configuration, use Debug.
106 if(NOT build_configuration)
107 set(build_configuration Debug)
109 string(TOUPPER "${build_configuration}" build_configuration)
110 #message("CUDA_NVCC_HOST_COMPILER_FLAGS = ${CUDA_NVCC_HOST_COMPILER_FLAGS}")
111 foreach(flag ${CMAKE_HOST_FLAGS} ${CMAKE_HOST_FLAGS_${build_configuration}})
112 # Extra quotes are added around each flag to help nvcc parse out flags with spaces.
113 string(APPEND nvcc_host_compiler_flags ",\"${flag}\"")
115 if (nvcc_host_compiler_flags)
116 set(nvcc_host_compiler_flags "-Xcompiler" ${nvcc_host_compiler_flags})
118 #message("nvcc_host_compiler_flags = \"${nvcc_host_compiler_flags}\"")
119 # Add the build specific configuration flags
120 list(APPEND CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS_${build_configuration}})
122 # Any -ccbin existing in CUDA_NVCC_FLAGS gets highest priority
123 list( FIND CUDA_NVCC_FLAGS "-ccbin" ccbin_found0 )
124 list( FIND CUDA_NVCC_FLAGS "--compiler-bindir" ccbin_found1 )
125 if( ccbin_found0 LESS 0 AND ccbin_found1 LESS 0 AND CUDA_HOST_COMPILER )
126 if (CUDA_HOST_COMPILER STREQUAL "$(VCInstallDir)bin" AND DEFINED CCBIN)
127 set(CCBIN -ccbin "${CCBIN}")
129 set(CCBIN -ccbin "${CUDA_HOST_COMPILER}")
133 # cuda_execute_process - Executes a command with optional command echo and status message.
135 # status - Status message to print if verbose is true
136 # command - COMMAND argument from the usual execute_process argument structure
137 # ARGN - Remaining arguments are the command with arguments
139 # CUDA_result - return value from running the command
141 # Make this a macro instead of a function, so that things like RESULT_VARIABLE
142 # and other return variables are present after executing the process.
143 macro(cuda_execute_process status command)
144 set(_command ${command})
145 if(NOT "x${_command}" STREQUAL "xCOMMAND")
146 message(FATAL_ERROR "Malformed call to cuda_execute_process. Missing COMMAND as second argument. (command = ${command})")
149 execute_process(COMMAND "${CMAKE_COMMAND}" -E echo -- ${status})
150 # Now we need to build up our command string. We are accounting for quotes
151 # and spaces, anything else is left up to the user to fix if they want to
152 # copy and paste a runnable command line.
153 set(cuda_execute_process_string)
155 # If there are quotes, excape them, so they come through.
156 string(REPLACE "\"" "\\\"" arg ${arg})
157 # Args with spaces need quotes around them to get them to be parsed as a single argument.
159 list(APPEND cuda_execute_process_string "\"${arg}\"")
161 list(APPEND cuda_execute_process_string ${arg})
165 execute_process(COMMAND ${CMAKE_COMMAND} -E echo ${cuda_execute_process_string})
168 execute_process(COMMAND ${ARGN} RESULT_VARIABLE CUDA_result )
171 # Delete the target file
172 cuda_execute_process(
173 "Removing ${generated_file}"
174 COMMAND "${CMAKE_COMMAND}" -E remove "${generated_file}"
177 # For CUDA 2.3 and below, -G -M doesn't work, so remove the -G flag
178 # for dependency generation and hope for the best.
179 set(depends_CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS}")
180 set(CUDA_VERSION @CUDA_VERSION@)
181 if(CUDA_VERSION VERSION_LESS "3.0")
183 # CMake policy 0007 NEW states that empty list elements are not
184 # ignored. I'm just setting it to avoid the warning that's printed.
185 cmake_policy(SET CMP0007 NEW)
186 # Note that this will remove all occurances of -G.
187 list(REMOVE_ITEM depends_CUDA_NVCC_FLAGS "-G")
191 # nvcc doesn't define __CUDACC__ for some reason when generating dependency files. This
192 # can cause incorrect dependencies when #including files based on this macro which is
193 # defined in the generating passes of nvcc invokation. We will go ahead and manually
194 # define this for now until a future version fixes this bug.
195 set(CUDACC_DEFINE -D__CUDACC__)
197 # Generate the dependency file
198 cuda_execute_process(
199 "Generating dependency file: ${NVCC_generated_dependency_file}"
200 COMMAND "${CUDA_NVCC_EXECUTABLE}"
204 -o "${NVCC_generated_dependency_file}"
207 ${nvcc_host_compiler_flags}
208 ${depends_CUDA_NVCC_FLAGS}
210 ${CUDA_NVCC_INCLUDE_ARGS}
214 message(FATAL_ERROR "Error generating ${generated_file}")
217 # Generate the cmake readable dependency file to a temp file. Don't put the
218 # quotes just around the filenames for the input_file and output_file variables.
219 # CMake will pass the quotes through and not be able to find the file.
220 cuda_execute_process(
221 "Generating temporary cmake readable file: ${cmake_dependency_file}.tmp"
222 COMMAND "${CMAKE_COMMAND}"
223 -D "input_file:FILEPATH=${NVCC_generated_dependency_file}"
224 -D "output_file:FILEPATH=${cmake_dependency_file}.tmp"
225 -D "verbose=${verbose}"
226 -P "${CUDA_make2cmake}"
230 message(FATAL_ERROR "Error generating ${generated_file}")
233 # Copy the file if it is different
234 cuda_execute_process(
235 "Copy if different ${cmake_dependency_file}.tmp to ${cmake_dependency_file}"
236 COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${cmake_dependency_file}.tmp" "${cmake_dependency_file}"
240 message(FATAL_ERROR "Error generating ${generated_file}")
243 # Delete the temporary file
244 cuda_execute_process(
245 "Removing ${cmake_dependency_file}.tmp and ${NVCC_generated_dependency_file}"
246 COMMAND "${CMAKE_COMMAND}" -E remove "${cmake_dependency_file}.tmp" "${NVCC_generated_dependency_file}"
250 message(FATAL_ERROR "Error generating ${generated_file}")
254 cuda_execute_process(
255 "Generating ${generated_file}"
256 COMMAND "${CUDA_NVCC_EXECUTABLE}"
258 ${cuda_language_flag}
259 ${format_flag} -o "${generated_file}"
262 ${nvcc_host_compiler_flags}
265 ${CUDA_NVCC_INCLUDE_ARGS}
269 # Since nvcc can sometimes leave half done files make sure that we delete the output file.
270 cuda_execute_process(
271 "Removing ${generated_file}"
272 COMMAND "${CMAKE_COMMAND}" -E remove "${generated_file}"
274 message(FATAL_ERROR "Error generating file ${generated_file}")
277 message("Generated ${generated_file} successfully.")
281 # Cubin resource report commands.
283 # Run with -cubin to produce resource usage report.
284 cuda_execute_process(
285 "Generating ${generated_cubin_file}"
286 COMMAND "${CUDA_NVCC_EXECUTABLE}"
291 ${nvcc_host_compiler_flags}
294 -o "${generated_cubin_file}"
295 ${CUDA_NVCC_INCLUDE_ARGS}
298 # Execute the parser script.
299 cuda_execute_process(
300 "Executing the parser script"
301 COMMAND "${CMAKE_COMMAND}"
302 -D "input_file:STRING=${generated_cubin_file}"
303 -P "${CUDA_parse_cubin}"