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