run_nvcc.cmake 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. # The MIT License
  8. #
  9. # License for the specific language governing rights and limitations under
  10. # Permission is hereby granted, free of charge, to any person obtaining a
  11. # copy of this software and associated documentation files (the "Software"),
  12. # to deal in the Software without restriction, including without limitation
  13. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  14. # and/or sell copies of the Software, and to permit persons to whom the
  15. # Software is furnished to do so, subject to the following conditions:
  16. #
  17. # The above copyright notice and this permission notice shall be included
  18. # in all copies or substantial portions of the Software.
  19. #
  20. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  21. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  23. # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  26. # DEALINGS IN THE SOFTWARE.
  27. ##########################################################################
  28. # This file runs the nvcc commands to produce the desired output file along with
  29. # the dependency file needed by CMake to compute dependencies. In addition the
  30. # file checks the output of each command and if the command fails it deletes the
  31. # output files.
  32. # Input variables
  33. #
  34. # verbose:BOOL=<> OFF: Be as quiet as possible (default)
  35. # ON : Describe each step
  36. #
  37. # build_configuration:STRING=<> Typically one of Debug, MinSizeRel, Release, or
  38. # RelWithDebInfo, but it should match one of the
  39. # entries in CUDA_HOST_FLAGS. This is the build
  40. # configuration used when compiling the code. If
  41. # blank or unspecified Debug is assumed as this is
  42. # what CMake does.
  43. #
  44. # generated_file:STRING=<> File to generate. This argument must be passed in.
  45. #
  46. # generated_cubin_file:STRING=<> File to generate. This argument must be passed
  47. # in if build_cubin is true.
  48. if(NOT generated_file)
  49. message(FATAL_ERROR "You must specify generated_file on the command line")
  50. endif()
  51. # Set these up as variables to make reading the generated file easier
  52. set(CMAKE_COMMAND "@CMAKE_COMMAND@")
  53. set(source_file "@source_file@")
  54. set(NVCC_generated_dependency_file "@NVCC_generated_dependency_file@")
  55. set(cmake_dependency_file "@cmake_dependency_file@")
  56. set(CUDA_make2cmake "@CUDA_make2cmake@")
  57. set(CUDA_parse_cubin "@CUDA_parse_cubin@")
  58. set(build_cubin @build_cubin@)
  59. # We won't actually use these variables for now, but we need to set this, in
  60. # order to force this file to be run again if it changes.
  61. set(generated_file_path "@generated_file_path@")
  62. set(generated_file_internal "@generated_file@")
  63. set(generated_cubin_file_internal "@generated_cubin_file@")
  64. set(CUDA_NVCC_EXECUTABLE "@CUDA_NVCC_EXECUTABLE@")
  65. set(CUDA_NVCC_FLAGS "@CUDA_NVCC_FLAGS@;;@CUDA_WRAP_OPTION_NVCC_FLAGS@")
  66. @CUDA_NVCC_FLAGS_CONFIG@
  67. set(nvcc_flags "@nvcc_flags@")
  68. set(CUDA_NVCC_INCLUDE_ARGS "@CUDA_NVCC_INCLUDE_ARGS@")
  69. set(format_flag "@format_flag@")
  70. if(build_cubin AND NOT generated_cubin_file)
  71. message(FATAL_ERROR "You must specify generated_cubin_file on the command line")
  72. endif()
  73. # This is the list of host compilation flags. It C or CXX should already have
  74. # been chosen by FindCUDA.cmake.
  75. @CUDA_HOST_FLAGS@
  76. # Take the compiler flags and package them up to be sent to the compiler via -Xcompiler
  77. set(nvcc_host_compiler_flags "")
  78. # If we weren't given a build_configuration, use Debug.
  79. if(NOT build_configuration)
  80. set(build_configuration Debug)
  81. endif()
  82. string(TOUPPER "${build_configuration}" build_configuration)
  83. #message("CUDA_NVCC_HOST_COMPILER_FLAGS = ${CUDA_NVCC_HOST_COMPILER_FLAGS}")
  84. foreach(flag ${CMAKE_HOST_FLAGS} ${CMAKE_HOST_FLAGS_${build_configuration}})
  85. # Extra quotes are added around each flag to help nvcc parse out flags with spaces.
  86. set(nvcc_host_compiler_flags "${nvcc_host_compiler_flags},\"${flag}\"")
  87. endforeach()
  88. if (nvcc_host_compiler_flags)
  89. set(nvcc_host_compiler_flags "-Xcompiler" ${nvcc_host_compiler_flags})
  90. endif()
  91. #message("nvcc_host_compiler_flags = \"${nvcc_host_compiler_flags}\"")
  92. # Add the build specific configuration flags
  93. list(APPEND CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS_${build_configuration}})
  94. if(DEFINED CCBIN)
  95. set(CCBIN -ccbin "${CCBIN}")
  96. endif()
  97. # cuda_execute_process - Executes a command with optional command echo and status message.
  98. #
  99. # status - Status message to print if verbose is true
  100. # command - COMMAND argument from the usual execute_process argument structure
  101. # ARGN - Remaining arguments are the command with arguments
  102. #
  103. # CUDA_result - return value from running the command
  104. #
  105. # Make this a macro instead of a function, so that things like RESULT_VARIABLE
  106. # and other return variables are present after executing the process.
  107. macro(cuda_execute_process status command)
  108. set(_command ${command})
  109. if(NOT _command STREQUAL "COMMAND")
  110. message(FATAL_ERROR "Malformed call to cuda_execute_process. Missing COMMAND as second argument. (command = ${command})")
  111. endif()
  112. if(verbose)
  113. execute_process(COMMAND "${CMAKE_COMMAND}" -E echo -- ${status})
  114. # Now we need to build up our command string. We are accounting for quotes
  115. # and spaces, anything else is left up to the user to fix if they want to
  116. # copy and paste a runnable command line.
  117. set(cuda_execute_process_string)
  118. foreach(arg ${ARGN})
  119. # If there are quotes, excape them, so they come through.
  120. string(REPLACE "\"" "\\\"" arg ${arg})
  121. # Args with spaces need quotes around them to get them to be parsed as a single argument.
  122. if(arg MATCHES " ")
  123. list(APPEND cuda_execute_process_string "\"${arg}\"")
  124. else()
  125. list(APPEND cuda_execute_process_string ${arg})
  126. endif()
  127. endforeach()
  128. # Echo the command
  129. execute_process(COMMAND ${CMAKE_COMMAND} -E echo ${cuda_execute_process_string})
  130. endif(verbose)
  131. # Run the command
  132. execute_process(COMMAND ${ARGN} RESULT_VARIABLE CUDA_result )
  133. endmacro()
  134. # Delete the target file
  135. cuda_execute_process(
  136. "Removing ${generated_file}"
  137. COMMAND "${CMAKE_COMMAND}" -E remove "${generated_file}"
  138. )
  139. # Generate the dependency file
  140. cuda_execute_process(
  141. "Generating dependency file: ${NVCC_generated_dependency_file}"
  142. COMMAND "${CUDA_NVCC_EXECUTABLE}"
  143. "${source_file}"
  144. ${CUDA_NVCC_FLAGS}
  145. ${nvcc_flags}
  146. ${CCBIN}
  147. ${nvcc_host_compiler_flags}
  148. -DNVCC
  149. -M
  150. -o "${NVCC_generated_dependency_file}"
  151. ${CUDA_NVCC_INCLUDE_ARGS}
  152. )
  153. if(CUDA_result)
  154. message(FATAL_ERROR "Error generating ${generated_file}")
  155. endif()
  156. # Generate the cmake readable dependency file to a temp file. Don't put the
  157. # quotes just around the filenames for the input_file and output_file variables.
  158. # CMake will pass the quotes through and not be able to find the file.
  159. cuda_execute_process(
  160. "Generating temporary cmake readable file: ${cmake_dependency_file}.tmp"
  161. COMMAND "${CMAKE_COMMAND}"
  162. -D "input_file:FILEPATH=${NVCC_generated_dependency_file}"
  163. -D "output_file:FILEPATH=${cmake_dependency_file}.tmp"
  164. -P "${CUDA_make2cmake}"
  165. )
  166. if(CUDA_result)
  167. message(FATAL_ERROR "Error generating ${generated_file}")
  168. endif()
  169. # Copy the file if it is different
  170. cuda_execute_process(
  171. "Copy if different ${cmake_dependency_file}.tmp to ${cmake_dependency_file}"
  172. COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${cmake_dependency_file}.tmp" "${cmake_dependency_file}"
  173. )
  174. if(CUDA_result)
  175. message(FATAL_ERROR "Error generating ${generated_file}")
  176. endif()
  177. # Delete the temporary file
  178. cuda_execute_process(
  179. "Removing ${cmake_dependency_file}.tmp and ${NVCC_generated_dependency_file}"
  180. COMMAND "${CMAKE_COMMAND}" -E remove "${cmake_dependency_file}.tmp" "${NVCC_generated_dependency_file}"
  181. )
  182. if(CUDA_result)
  183. message(FATAL_ERROR "Error generating ${generated_file}")
  184. endif()
  185. # Generate the code
  186. cuda_execute_process(
  187. "Generating ${generated_file}"
  188. COMMAND "${CUDA_NVCC_EXECUTABLE}"
  189. "${source_file}"
  190. ${CUDA_NVCC_FLAGS}
  191. ${nvcc_flags}
  192. ${CCBIN}
  193. ${nvcc_host_compiler_flags}
  194. -DNVCC
  195. ${format_flag} -o "${generated_file}"
  196. ${CUDA_NVCC_INCLUDE_ARGS}
  197. )
  198. if(CUDA_result)
  199. # Since nvcc can sometimes leave half done files make sure that we delete the output file.
  200. cuda_execute_process(
  201. "Removing ${generated_file}"
  202. COMMAND "${CMAKE_COMMAND}" -E remove "${generated_file}"
  203. )
  204. message(FATAL_ERROR "Error generating file ${generated_file}")
  205. else()
  206. if(verbose)
  207. message("Generated ${generated_file} successfully.")
  208. endif()
  209. endif()
  210. # Cubin resource report commands.
  211. if( build_cubin )
  212. # Run with -cubin to produce resource usage report.
  213. cuda_execute_process(
  214. "Generating ${generated_cubin_file}"
  215. COMMAND "${CUDA_NVCC_EXECUTABLE}"
  216. "${source_file}"
  217. ${CUDA_NVCC_FLAGS}
  218. ${nvcc_flags}
  219. ${CCBIN}
  220. ${nvcc_host_compiler_flags}
  221. -DNVCC
  222. -cubin
  223. -o "${generated_cubin_file}"
  224. ${CUDA_NVCC_INCLUDE_ARGS}
  225. )
  226. # Execute the parser script.
  227. cuda_execute_process(
  228. "Executing the parser script"
  229. COMMAND "${CMAKE_COMMAND}"
  230. -D "input_file:STRING=${generated_cubin_file}"
  231. -P "${CUDA_parse_cubin}"
  232. )
  233. endif( build_cubin )