run_nvcc.cmake 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. # James Bigler, NVIDIA Corp (nvidia.com - jbigler)
  2. #
  3. # Copyright (c) 2008-2009
  4. # NVIDIA Corp.
  5. #
  6. # This code is licensed under the MIT License. See the FindCUDA.cmake script
  7. # for the text of the license.
  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. # This file runs the nvcc commands to produce the desired output file along with
  30. # the dependency file needed by CMake to compute dependencies. In addition the
  31. # file checks the output of each command and if the command fails it deletes the
  32. # output files.
  33. # Input variables
  34. #
  35. # verbose:BOOL=<> OFF: Be as quiet as possible (default)
  36. # ON : Describe each step
  37. #
  38. # build_configuration:STRING=<> Typically one of Debug, MinSizeRel, Release, or
  39. # RelWithDebInfo, but it should match one of the
  40. # entries in CUDA_HOST_FLAGS. This is the build
  41. # configuration used when compiling the code. If
  42. # blank or unspecified Debug is assumed as this is
  43. # what CMake does.
  44. #
  45. # generated_file:STRING=<> File to generate. This argument must be passed in.
  46. #
  47. # generated_cubin_file:STRING=<> File to generate. This argument must be passed
  48. # in if build_cubin is true.
  49. if(NOT generated_file)
  50. message(FATAL_ERROR "You must specify generated_file on the command line")
  51. endif()
  52. # Set these up as variables to make reading the generated file easier
  53. set(CMAKE_COMMAND "@CMAKE_COMMAND@")
  54. set(source_file "@source_file@")
  55. set(NVCC_generated_dependency_file "@NVCC_generated_dependency_file@")
  56. set(cmake_dependency_file "@cmake_dependency_file@")
  57. set(CUDA_make2cmake "@CUDA_make2cmake@")
  58. set(CUDA_parse_cubin "@CUDA_parse_cubin@")
  59. set(build_cubin @build_cubin@)
  60. # We won't actually use these variables for now, but we need to set this, in
  61. # order to force this file to be run again if it changes.
  62. set(generated_file_path "@generated_file_path@")
  63. set(generated_file_internal "@generated_file@")
  64. set(generated_cubin_file_internal "@generated_cubin_file@")
  65. set(CUDA_NVCC_EXECUTABLE "@CUDA_NVCC_EXECUTABLE@")
  66. set(CUDA_NVCC_FLAGS "@CUDA_NVCC_FLAGS@;;@CUDA_WRAP_OPTION_NVCC_FLAGS@")
  67. @CUDA_NVCC_FLAGS_CONFIG@
  68. set(nvcc_flags "@nvcc_flags@")
  69. set(CUDA_NVCC_INCLUDE_ARGS "@CUDA_NVCC_INCLUDE_ARGS@")
  70. set(format_flag "@format_flag@")
  71. if(build_cubin AND NOT generated_cubin_file)
  72. message(FATAL_ERROR "You must specify generated_cubin_file on the command line")
  73. endif()
  74. # This is the list of host compilation flags. It C or CXX should already have
  75. # been chosen by FindCUDA.cmake.
  76. @CUDA_HOST_FLAGS@
  77. # Take the compiler flags and package them up to be sent to the compiler via -Xcompiler
  78. set(nvcc_host_compiler_flags "")
  79. # If we weren't given a build_configuration, use Debug.
  80. if(NOT build_configuration)
  81. set(build_configuration Debug)
  82. endif()
  83. string(TOUPPER "${build_configuration}" build_configuration)
  84. #message("CUDA_NVCC_HOST_COMPILER_FLAGS = ${CUDA_NVCC_HOST_COMPILER_FLAGS}")
  85. foreach(flag ${CMAKE_HOST_FLAGS} ${CMAKE_HOST_FLAGS_${build_configuration}})
  86. # Extra quotes are added around each flag to help nvcc parse out flags with spaces.
  87. set(nvcc_host_compiler_flags "${nvcc_host_compiler_flags},\"${flag}\"")
  88. endforeach()
  89. if (nvcc_host_compiler_flags)
  90. set(nvcc_host_compiler_flags "-Xcompiler" ${nvcc_host_compiler_flags})
  91. endif()
  92. #message("nvcc_host_compiler_flags = \"${nvcc_host_compiler_flags}\"")
  93. # Add the build specific configuration flags
  94. list(APPEND CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS_${build_configuration}})
  95. if(DEFINED CCBIN)
  96. set(CCBIN -ccbin "${CCBIN}")
  97. endif()
  98. # cuda_execute_process - Executes a command with optional command echo and status message.
  99. #
  100. # status - Status message to print if verbose is true
  101. # command - COMMAND argument from the usual execute_process argument structure
  102. # ARGN - Remaining arguments are the command with arguments
  103. #
  104. # CUDA_result - return value from running the command
  105. #
  106. # Make this a macro instead of a function, so that things like RESULT_VARIABLE
  107. # and other return variables are present after executing the process.
  108. macro(cuda_execute_process status command)
  109. set(_command ${command})
  110. if(NOT _command STREQUAL "COMMAND")
  111. message(FATAL_ERROR "Malformed call to cuda_execute_process. Missing COMMAND as second argument. (command = ${command})")
  112. endif()
  113. if(verbose)
  114. execute_process(COMMAND "${CMAKE_COMMAND}" -E echo -- ${status})
  115. # Now we need to build up our command string. We are accounting for quotes
  116. # and spaces, anything else is left up to the user to fix if they want to
  117. # copy and paste a runnable command line.
  118. set(cuda_execute_process_string)
  119. foreach(arg ${ARGN})
  120. # If there are quotes, excape them, so they come through.
  121. string(REPLACE "\"" "\\\"" arg ${arg})
  122. # Args with spaces need quotes around them to get them to be parsed as a single argument.
  123. if(arg MATCHES " ")
  124. list(APPEND cuda_execute_process_string "\"${arg}\"")
  125. else()
  126. list(APPEND cuda_execute_process_string ${arg})
  127. endif()
  128. endforeach()
  129. # Echo the command
  130. execute_process(COMMAND ${CMAKE_COMMAND} -E echo ${cuda_execute_process_string})
  131. endif(verbose)
  132. # Run the command
  133. execute_process(COMMAND ${ARGN} RESULT_VARIABLE CUDA_result )
  134. endmacro()
  135. # Delete the target file
  136. cuda_execute_process(
  137. "Removing ${generated_file}"
  138. COMMAND "${CMAKE_COMMAND}" -E remove "${generated_file}"
  139. )
  140. # Make sure the output directory is present
  141. cuda_execute_process(
  142. "Creating output directory: ${generated_file_path}"
  143. COMMAND "${CMAKE_COMMAND}" -E make_directory "${generated_file_path}"
  144. )
  145. # Generate the dependency file
  146. cuda_execute_process(
  147. "Generating dependency file: ${NVCC_generated_dependency_file}"
  148. COMMAND "${CUDA_NVCC_EXECUTABLE}"
  149. "${source_file}"
  150. ${CUDA_NVCC_FLAGS}
  151. ${nvcc_flags}
  152. ${CCBIN}
  153. ${nvcc_host_compiler_flags}
  154. -DNVCC
  155. -M
  156. -o "${NVCC_generated_dependency_file}"
  157. ${CUDA_NVCC_INCLUDE_ARGS}
  158. )
  159. if(CUDA_result)
  160. message(FATAL_ERROR "Error generating ${generated_file}")
  161. endif()
  162. # Generate the cmake readable dependency file to a temp file. Don't put the
  163. # quotes just around the filenames for the input_file and output_file variables.
  164. # CMake will pass the quotes through and not be able to find the file.
  165. cuda_execute_process(
  166. "Generating temporary cmake readable file: ${cmake_dependency_file}.tmp"
  167. COMMAND "${CMAKE_COMMAND}"
  168. -D "input_file:FILEPATH=${NVCC_generated_dependency_file}"
  169. -D "output_file:FILEPATH=${cmake_dependency_file}.tmp"
  170. -P "${CUDA_make2cmake}"
  171. )
  172. if(CUDA_result)
  173. message(FATAL_ERROR "Error generating ${generated_file}")
  174. endif()
  175. # Copy the file if it is different
  176. cuda_execute_process(
  177. "Copy if different ${cmake_dependency_file}.tmp to ${cmake_dependency_file}"
  178. COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${cmake_dependency_file}.tmp" "${cmake_dependency_file}"
  179. )
  180. if(CUDA_result)
  181. message(FATAL_ERROR "Error generating ${generated_file}")
  182. endif()
  183. # Delete the temporary file
  184. cuda_execute_process(
  185. "Removing ${cmake_dependency_file}.tmp and ${NVCC_generated_dependency_file}"
  186. COMMAND "${CMAKE_COMMAND}" -E remove "${cmake_dependency_file}.tmp" "${NVCC_generated_dependency_file}"
  187. )
  188. if(CUDA_result)
  189. message(FATAL_ERROR "Error generating ${generated_file}")
  190. endif()
  191. # Generate the code
  192. cuda_execute_process(
  193. "Generating ${generated_file}"
  194. COMMAND "${CUDA_NVCC_EXECUTABLE}"
  195. "${source_file}"
  196. ${CUDA_NVCC_FLAGS}
  197. ${nvcc_flags}
  198. ${CCBIN}
  199. ${nvcc_host_compiler_flags}
  200. -DNVCC
  201. ${format_flag} -o "${generated_file}"
  202. ${CUDA_NVCC_INCLUDE_ARGS}
  203. )
  204. if(CUDA_result)
  205. # Since nvcc can sometimes leave half done files make sure that we delete the output file.
  206. cuda_execute_process(
  207. "Removing ${generated_file}"
  208. COMMAND "${CMAKE_COMMAND}" -E remove "${generated_file}"
  209. )
  210. message(FATAL_ERROR "Error generating file ${generated_file}")
  211. else()
  212. if(verbose)
  213. message("Generated ${generated_file} successfully.")
  214. endif()
  215. endif()
  216. # Cubin resource report commands.
  217. if( build_cubin )
  218. # Run with -cubin to produce resource usage report.
  219. cuda_execute_process(
  220. "Generating ${generated_cubin_file}"
  221. COMMAND "${CUDA_NVCC_EXECUTABLE}"
  222. "${source_file}"
  223. ${CUDA_NVCC_FLAGS}
  224. ${nvcc_flags}
  225. ${CCBIN}
  226. ${nvcc_host_compiler_flags}
  227. -DNVCC
  228. -cubin
  229. -o "${generated_cubin_file}"
  230. ${CUDA_NVCC_INCLUDE_ARGS}
  231. )
  232. # Execute the parser script.
  233. cuda_execute_process(
  234. "Executing the parser script"
  235. COMMAND "${CMAKE_COMMAND}"
  236. -D "input_file:STRING=${generated_cubin_file}"
  237. -P "${CUDA_parse_cubin}"
  238. )
  239. endif( build_cubin )