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) 55if(NOT generated_file) 56 message(FATAL_ERROR "You must specify generated_file on the command line") 57endif() 58 59# Set these up as variables to make reading the generated file easier 60set(CMAKE_COMMAND "@CMAKE_COMMAND@") # path 61set(source_file "@source_file@") # path 62set(NVCC_generated_dependency_file "@NVCC_generated_dependency_file@") # path 63set(cmake_dependency_file "@cmake_dependency_file@") # path 64set(CUDA_make2cmake "@CUDA_make2cmake@") # path 65set(CUDA_parse_cubin "@CUDA_parse_cubin@") # path 66set(build_cubin @build_cubin@) # bool 67set(CUDA_HOST_COMPILER "@CUDA_HOST_COMPILER@") # path 68# We won't actually use these variables for now, but we need to set this, in 69# order to force this file to be run again if it changes. 70set(generated_file_path "@generated_file_path@") # path 71set(generated_file_internal "@generated_file@") # path 72set(generated_cubin_file_internal "@generated_cubin_file@") # path 73 74set(CUDA_NVCC_EXECUTABLE "@CUDA_NVCC_EXECUTABLE@") # path 75set(CUDA_NVCC_FLAGS @CUDA_NVCC_FLAGS@ ;; @CUDA_WRAP_OPTION_NVCC_FLAGS@) # list 76@CUDA_NVCC_FLAGS_CONFIG@ 77set(nvcc_flags @nvcc_flags@) # list 78set(CUDA_NVCC_INCLUDE_DIRS [==[@CUDA_NVCC_INCLUDE_DIRS@]==]) # list (needs to be in lua quotes to address backslashes) 79string(REPLACE "\\" "/" CUDA_NVCC_INCLUDE_DIRS "${CUDA_NVCC_INCLUDE_DIRS}") 80set(CUDA_NVCC_COMPILE_DEFINITIONS [==[@CUDA_NVCC_COMPILE_DEFINITIONS@]==]) # list (needs to be in lua quotes see #16510 ). 81set(format_flag "@format_flag@") # string 82set(cuda_language_flag @cuda_language_flag@) # list 83 84# Clean up list of include directories and add -I flags 85list(REMOVE_DUPLICATES CUDA_NVCC_INCLUDE_DIRS) 86set(CUDA_NVCC_INCLUDE_ARGS) 87foreach(dir ${CUDA_NVCC_INCLUDE_DIRS}) 88 # Extra quotes are added around each flag to help nvcc parse out flags with spaces. 89 list(APPEND CUDA_NVCC_INCLUDE_ARGS "-I${dir}") 90endforeach() 91 92# Clean up list of compile definitions, add -D flags, and append to nvcc_flags 93list(REMOVE_DUPLICATES CUDA_NVCC_COMPILE_DEFINITIONS) 94foreach(def ${CUDA_NVCC_COMPILE_DEFINITIONS}) 95 list(APPEND nvcc_flags "-D${def}") 96endforeach() 97 98if(build_cubin AND NOT generated_cubin_file) 99 message(FATAL_ERROR "You must specify generated_cubin_file on the command line") 100endif() 101 102# This is the list of host compilation flags. It C or CXX should already have 103# been chosen by FindCUDA.cmake. 104@CUDA_HOST_FLAGS@ 105 106# Take the compiler flags and package them up to be sent to the compiler via -Xcompiler 107set(nvcc_host_compiler_flags "") 108# If we weren't given a build_configuration, use Debug. 109if(NOT build_configuration) 110 set(build_configuration Debug) 111endif() 112string(TOUPPER "${build_configuration}" build_configuration) 113#message("CUDA_NVCC_HOST_COMPILER_FLAGS = ${CUDA_NVCC_HOST_COMPILER_FLAGS}") 114foreach(flag ${CMAKE_HOST_FLAGS} ${CMAKE_HOST_FLAGS_${build_configuration}}) 115 # Extra quotes are added around each flag to help nvcc parse out flags with spaces. 116 string(APPEND nvcc_host_compiler_flags ",\"${flag}\"") 117endforeach() 118if (nvcc_host_compiler_flags) 119 set(nvcc_host_compiler_flags "-Xcompiler" ${nvcc_host_compiler_flags}) 120endif() 121#message("nvcc_host_compiler_flags = \"${nvcc_host_compiler_flags}\"") 122# Add the build specific configuration flags 123list(APPEND CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS_${build_configuration}}) 124 125# Any -ccbin existing in CUDA_NVCC_FLAGS gets highest priority 126list( FIND CUDA_NVCC_FLAGS "-ccbin" ccbin_found0 ) 127list( FIND CUDA_NVCC_FLAGS "--compiler-bindir" ccbin_found1 ) 128if( ccbin_found0 LESS 0 AND ccbin_found1 LESS 0 AND CUDA_HOST_COMPILER ) 129 if (CUDA_HOST_COMPILER STREQUAL "@_CUDA_MSVC_HOST_COMPILER@" AND DEFINED CCBIN) 130 set(CCBIN -ccbin "${CCBIN}") 131 else() 132 set(CCBIN -ccbin "${CUDA_HOST_COMPILER}") 133 endif() 134endif() 135 136# cuda_execute_process - Executes a command with optional command echo and status message. 137# 138# status - Status message to print if verbose is true 139# command - COMMAND argument from the usual execute_process argument structure 140# ARGN - Remaining arguments are the command with arguments 141# 142# CUDA_result - return value from running the command 143# 144# Make this a macro instead of a function, so that things like RESULT_VARIABLE 145# and other return variables are present after executing the process. 146macro(cuda_execute_process status command) 147 set(_command ${command}) 148 if(NOT "x${_command}" STREQUAL "xCOMMAND") 149 message(FATAL_ERROR "Malformed call to cuda_execute_process. Missing COMMAND as second argument. (command = ${command})") 150 endif() 151 if(verbose) 152 execute_process(COMMAND "${CMAKE_COMMAND}" -E echo -- ${status}) 153 # Now we need to build up our command string. We are accounting for quotes 154 # and spaces, anything else is left up to the user to fix if they want to 155 # copy and paste a runnable command line. 156 set(cuda_execute_process_string) 157 foreach(arg ${ARGN}) 158 # If there are quotes, escape them, so they come through. 159 string(REPLACE "\"" "\\\"" arg ${arg}) 160 # Args with spaces need quotes around them to get them to be parsed as a single argument. 161 if(arg MATCHES " ") 162 list(APPEND cuda_execute_process_string "\"${arg}\"") 163 else() 164 list(APPEND cuda_execute_process_string ${arg}) 165 endif() 166 endforeach() 167 # Echo the command 168 execute_process(COMMAND ${CMAKE_COMMAND} -E echo ${cuda_execute_process_string}) 169 endif() 170 # Run the command 171 execute_process(COMMAND ${ARGN} RESULT_VARIABLE CUDA_result ) 172endmacro() 173 174# Delete the target file 175cuda_execute_process( 176 "Removing ${generated_file}" 177 COMMAND "${CMAKE_COMMAND}" -E rm -f "${generated_file}" 178 ) 179 180# For CUDA 2.3 and below, -G -M doesn't work, so remove the -G flag 181# for dependency generation and hope for the best. 182set(depends_CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS}") 183set(CUDA_VERSION @CUDA_VERSION@) 184if(CUDA_VERSION VERSION_LESS "3.0") 185 # Note that this will remove all occurrences of -G. 186 list(REMOVE_ITEM depends_CUDA_NVCC_FLAGS "-G") 187endif() 188 189# nvcc doesn't define __CUDACC__ for some reason when generating dependency files. This 190# can cause incorrect dependencies when #including files based on this macro which is 191# defined in the generating passes of nvcc invocation. We will go ahead and manually 192# define this for now until a future version fixes this bug. 193set(CUDACC_DEFINE -D__CUDACC__) 194 195# Generate the dependency file 196cuda_execute_process( 197 "Generating dependency file: ${NVCC_generated_dependency_file}" 198 COMMAND "${CUDA_NVCC_EXECUTABLE}" 199 -M 200 ${CUDACC_DEFINE} 201 "${source_file}" 202 -o "${NVCC_generated_dependency_file}" 203 ${CCBIN} 204 ${nvcc_flags} 205 ${nvcc_host_compiler_flags} 206 ${depends_CUDA_NVCC_FLAGS} 207 -DNVCC 208 ${CUDA_NVCC_INCLUDE_ARGS} 209 ) 210 211if(CUDA_result) 212 message(FATAL_ERROR "Error generating ${generated_file}") 213endif() 214 215# Generate the cmake readable dependency file to a temp file. Don't put the 216# quotes just around the filenames for the input_file and output_file variables. 217# CMake will pass the quotes through and not be able to find the file. 218cuda_execute_process( 219 "Generating temporary cmake readable file: ${cmake_dependency_file}.tmp" 220 COMMAND "${CMAKE_COMMAND}" 221 -D "input_file:FILEPATH=${NVCC_generated_dependency_file}" 222 -D "output_file:FILEPATH=${cmake_dependency_file}.tmp" 223 -D "verbose=${verbose}" 224 -P "${CUDA_make2cmake}" 225 ) 226 227if(CUDA_result) 228 message(FATAL_ERROR "Error generating ${generated_file}") 229endif() 230 231# Copy the file if it is different 232cuda_execute_process( 233 "Copy if different ${cmake_dependency_file}.tmp to ${cmake_dependency_file}" 234 COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${cmake_dependency_file}.tmp" "${cmake_dependency_file}" 235 ) 236 237if(CUDA_result) 238 message(FATAL_ERROR "Error generating ${generated_file}") 239endif() 240 241# Delete the temporary file 242cuda_execute_process( 243 "Removing ${cmake_dependency_file}.tmp and ${NVCC_generated_dependency_file}" 244 COMMAND "${CMAKE_COMMAND}" -E rm -f "${cmake_dependency_file}.tmp" "${NVCC_generated_dependency_file}" 245 ) 246 247if(CUDA_result) 248 message(FATAL_ERROR "Error generating ${generated_file}") 249endif() 250 251# Generate the code 252cuda_execute_process( 253 "Generating ${generated_file}" 254 COMMAND "${CUDA_NVCC_EXECUTABLE}" 255 "${source_file}" 256 ${cuda_language_flag} 257 ${format_flag} -o "${generated_file}" 258 ${CCBIN} 259 ${nvcc_flags} 260 ${nvcc_host_compiler_flags} 261 ${CUDA_NVCC_FLAGS} 262 -DNVCC 263 ${CUDA_NVCC_INCLUDE_ARGS} 264 ) 265 266if(CUDA_result) 267 # Since nvcc can sometimes leave half done files make sure that we delete the output file. 268 cuda_execute_process( 269 "Removing ${generated_file}" 270 COMMAND "${CMAKE_COMMAND}" -E rm -f "${generated_file}" 271 ) 272 message(FATAL_ERROR "Error generating file ${generated_file}") 273else() 274 if(verbose) 275 message("Generated ${generated_file} successfully.") 276 endif() 277endif() 278 279# Cubin resource report commands. 280if( build_cubin ) 281 # Run with -cubin to produce resource usage report. 282 cuda_execute_process( 283 "Generating ${generated_cubin_file}" 284 COMMAND "${CUDA_NVCC_EXECUTABLE}" 285 "${source_file}" 286 ${CUDA_NVCC_FLAGS} 287 ${nvcc_flags} 288 ${CCBIN} 289 ${nvcc_host_compiler_flags} 290 -DNVCC 291 -cubin 292 -o "${generated_cubin_file}" 293 ${CUDA_NVCC_INCLUDE_ARGS} 294 ) 295 296 # Execute the parser script. 297 cuda_execute_process( 298 "Executing the parser script" 299 COMMAND "${CMAKE_COMMAND}" 300 -D "input_file:STRING=${generated_cubin_file}" 301 -P "${CUDA_parse_cubin}" 302 ) 303 304endif() 305 306cmake_policy(POP) 307