run_nvcc.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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@") # path
  53. set(source_file "@source_file@") # path
  54. set(NVCC_generated_dependency_file "@NVCC_generated_dependency_file@") # path
  55. set(cmake_dependency_file "@cmake_dependency_file@") # path
  56. set(CUDA_make2cmake "@CUDA_make2cmake@") # path
  57. set(CUDA_parse_cubin "@CUDA_parse_cubin@") # path
  58. set(build_cubin @build_cubin@) # bool
  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@") # path
  62. set(generated_file_internal "@generated_file@") # path
  63. set(generated_cubin_file_internal "@generated_cubin_file@") # path
  64. set(CUDA_NVCC_EXECUTABLE "@CUDA_NVCC_EXECUTABLE@") # path
  65. set(CUDA_NVCC_FLAGS @CUDA_NVCC_FLAGS@ ;; @CUDA_WRAP_OPTION_NVCC_FLAGS@) # list
  66. @CUDA_NVCC_FLAGS_CONFIG@
  67. set(nvcc_flags @nvcc_flags@) # list
  68. set(CUDA_NVCC_INCLUDE_ARGS "@CUDA_NVCC_INCLUDE_ARGS@") # list (needs to be in quotes to handle spaces properly).
  69. set(format_flag "@format_flag@") # string
  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()
  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. # For CUDA 2.3 and below, -G -M doesn't work, so remove the -G flag
  140. # for dependency generation and hope for the best.
  141. set(depends_CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS}")
  142. set(CUDA_VERSION @CUDA_VERSION@)
  143. if(CUDA_VERSION VERSION_LESS "3.0")
  144. cmake_policy(PUSH)
  145. # CMake policy 0007 NEW states that empty list elements are not
  146. # ignored. I'm just setting it to avoid the warning that's printed.
  147. cmake_policy(SET CMP0007 NEW)
  148. # Note that this will remove all occurances of -G.
  149. list(REMOVE_ITEM depends_CUDA_NVCC_FLAGS "-G")
  150. cmake_policy(POP)
  151. endif()
  152. # nvcc doesn't define __CUDACC__ for some reason when generating dependency files. This
  153. # can cause incorrect dependencies when #including files based on this macro which is
  154. # defined in the generating passes of nvcc invokation. We will go ahead and manually
  155. # define this for now until a future version fixes this bug.
  156. set(CUDACC_DEFINE -D__CUDACC__)
  157. # Generate the dependency file
  158. cuda_execute_process(
  159. "Generating dependency file: ${NVCC_generated_dependency_file}"
  160. COMMAND "${CUDA_NVCC_EXECUTABLE}"
  161. -M
  162. ${CUDACC_DEFINE}
  163. "${source_file}"
  164. -o "${NVCC_generated_dependency_file}"
  165. ${CCBIN}
  166. ${nvcc_flags}
  167. ${nvcc_host_compiler_flags}
  168. ${depends_CUDA_NVCC_FLAGS}
  169. -DNVCC
  170. ${CUDA_NVCC_INCLUDE_ARGS}
  171. )
  172. if(CUDA_result)
  173. message(FATAL_ERROR "Error generating ${generated_file}")
  174. endif()
  175. # Generate the cmake readable dependency file to a temp file. Don't put the
  176. # quotes just around the filenames for the input_file and output_file variables.
  177. # CMake will pass the quotes through and not be able to find the file.
  178. cuda_execute_process(
  179. "Generating temporary cmake readable file: ${cmake_dependency_file}.tmp"
  180. COMMAND "${CMAKE_COMMAND}"
  181. -D "input_file:FILEPATH=${NVCC_generated_dependency_file}"
  182. -D "output_file:FILEPATH=${cmake_dependency_file}.tmp"
  183. -P "${CUDA_make2cmake}"
  184. )
  185. if(CUDA_result)
  186. message(FATAL_ERROR "Error generating ${generated_file}")
  187. endif()
  188. # Copy the file if it is different
  189. cuda_execute_process(
  190. "Copy if different ${cmake_dependency_file}.tmp to ${cmake_dependency_file}"
  191. COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${cmake_dependency_file}.tmp" "${cmake_dependency_file}"
  192. )
  193. if(CUDA_result)
  194. message(FATAL_ERROR "Error generating ${generated_file}")
  195. endif()
  196. # Delete the temporary file
  197. cuda_execute_process(
  198. "Removing ${cmake_dependency_file}.tmp and ${NVCC_generated_dependency_file}"
  199. COMMAND "${CMAKE_COMMAND}" -E remove "${cmake_dependency_file}.tmp" "${NVCC_generated_dependency_file}"
  200. )
  201. if(CUDA_result)
  202. message(FATAL_ERROR "Error generating ${generated_file}")
  203. endif()
  204. # Generate the code
  205. cuda_execute_process(
  206. "Generating ${generated_file}"
  207. COMMAND "${CUDA_NVCC_EXECUTABLE}"
  208. "${source_file}"
  209. ${format_flag} -o "${generated_file}"
  210. ${CCBIN}
  211. ${nvcc_flags}
  212. ${nvcc_host_compiler_flags}
  213. ${CUDA_NVCC_FLAGS}
  214. -DNVCC
  215. ${CUDA_NVCC_INCLUDE_ARGS}
  216. )
  217. if(CUDA_result)
  218. # Since nvcc can sometimes leave half done files make sure that we delete the output file.
  219. cuda_execute_process(
  220. "Removing ${generated_file}"
  221. COMMAND "${CMAKE_COMMAND}" -E remove "${generated_file}"
  222. )
  223. message(FATAL_ERROR "Error generating file ${generated_file}")
  224. else()
  225. if(verbose)
  226. message("Generated ${generated_file} successfully.")
  227. endif()
  228. endif()
  229. # Cubin resource report commands.
  230. if( build_cubin )
  231. # Run with -cubin to produce resource usage report.
  232. cuda_execute_process(
  233. "Generating ${generated_cubin_file}"
  234. COMMAND "${CUDA_NVCC_EXECUTABLE}"
  235. "${source_file}"
  236. ${CUDA_NVCC_FLAGS}
  237. ${nvcc_flags}
  238. ${CCBIN}
  239. ${nvcc_host_compiler_flags}
  240. -DNVCC
  241. -cubin
  242. -o "${generated_cubin_file}"
  243. ${CUDA_NVCC_INCLUDE_ARGS}
  244. )
  245. # Execute the parser script.
  246. cuda_execute_process(
  247. "Executing the parser script"
  248. COMMAND "${CMAKE_COMMAND}"
  249. -D "input_file:STRING=${generated_cubin_file}"
  250. -P "${CUDA_parse_cubin}"
  251. )
  252. endif()