run_nvcc.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. set(CUDA_HOST_COMPILER "@CUDA_HOST_COMPILER@") # path
  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@") # path
  63. set(generated_file_internal "@generated_file@") # path
  64. set(generated_cubin_file_internal "@generated_cubin_file@") # path
  65. set(CUDA_NVCC_EXECUTABLE "@CUDA_NVCC_EXECUTABLE@") # path
  66. set(CUDA_NVCC_FLAGS @CUDA_NVCC_FLAGS@ ;; @CUDA_WRAP_OPTION_NVCC_FLAGS@) # list
  67. @CUDA_NVCC_FLAGS_CONFIG@
  68. set(nvcc_flags "@nvcc_flags@") # list
  69. set(CUDA_NVCC_INCLUDE_ARGS "@CUDA_NVCC_INCLUDE_ARGS@") # list (needs to be in quotes to handle spaces properly).
  70. set(format_flag "@format_flag@") # string
  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. # Any -ccbin existing in CUDA_NVCC_FLAGS gets highest priority
  96. list( FIND CUDA_NVCC_FLAGS "-ccbin" ccbin_found0 )
  97. list( FIND CUDA_NVCC_FLAGS "--compiler-bindir" ccbin_found1 )
  98. if( ccbin_found0 LESS 0 AND ccbin_found1 LESS 0 AND CUDA_HOST_COMPILER )
  99. if (CUDA_HOST_COMPILER STREQUAL "@_CUDA_MSVC_HOST_COMPILER@" AND DEFINED CCBIN)
  100. set(CCBIN -ccbin "${CCBIN}")
  101. else()
  102. set(CCBIN -ccbin "${CUDA_HOST_COMPILER}")
  103. endif()
  104. endif()
  105. # cuda_execute_process - Executes a command with optional command echo and status message.
  106. #
  107. # status - Status message to print if verbose is true
  108. # command - COMMAND argument from the usual execute_process argument structure
  109. # ARGN - Remaining arguments are the command with arguments
  110. #
  111. # CUDA_result - return value from running the command
  112. #
  113. # Make this a macro instead of a function, so that things like RESULT_VARIABLE
  114. # and other return variables are present after executing the process.
  115. macro(cuda_execute_process status command)
  116. set(_command ${command})
  117. if(NOT "x${_command}" STREQUAL "xCOMMAND")
  118. message(FATAL_ERROR "Malformed call to cuda_execute_process. Missing COMMAND as second argument. (command = ${command})")
  119. endif()
  120. if(verbose)
  121. execute_process(COMMAND "${CMAKE_COMMAND}" -E echo -- ${status})
  122. # Now we need to build up our command string. We are accounting for quotes
  123. # and spaces, anything else is left up to the user to fix if they want to
  124. # copy and paste a runnable command line.
  125. set(cuda_execute_process_string)
  126. foreach(arg ${ARGN})
  127. # If there are quotes, escape them, so they come through.
  128. string(REPLACE "\"" "\\\"" arg ${arg})
  129. # Args with spaces need quotes around them to get them to be parsed as a single argument.
  130. if(arg MATCHES " ")
  131. list(APPEND cuda_execute_process_string "\"${arg}\"")
  132. else()
  133. list(APPEND cuda_execute_process_string ${arg})
  134. endif()
  135. endforeach()
  136. # Echo the command
  137. execute_process(COMMAND ${CMAKE_COMMAND} -E echo ${cuda_execute_process_string})
  138. endif()
  139. # Run the command
  140. execute_process(COMMAND ${ARGN} RESULT_VARIABLE CUDA_result )
  141. endmacro()
  142. # Delete the target file
  143. cuda_execute_process(
  144. "Removing ${generated_file}"
  145. COMMAND "${CMAKE_COMMAND}" -E remove "${generated_file}"
  146. )
  147. # For CUDA 2.3 and below, -G -M doesn't work, so remove the -G flag
  148. # for dependency generation and hope for the best.
  149. set(depends_CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS}")
  150. set(CUDA_VERSION @CUDA_VERSION@)
  151. if(CUDA_VERSION VERSION_LESS "3.0")
  152. cmake_policy(PUSH)
  153. # CMake policy 0007 NEW states that empty list elements are not
  154. # ignored. I'm just setting it to avoid the warning that's printed.
  155. cmake_policy(SET CMP0007 NEW)
  156. # Note that this will remove all occurrences of -G.
  157. list(REMOVE_ITEM depends_CUDA_NVCC_FLAGS "-G")
  158. cmake_policy(POP)
  159. endif()
  160. # nvcc doesn't define __CUDACC__ for some reason when generating dependency files. This
  161. # can cause incorrect dependencies when #including files based on this macro which is
  162. # defined in the generating passes of nvcc invocation. We will go ahead and manually
  163. # define this for now until a future version fixes this bug.
  164. set(CUDACC_DEFINE -D__CUDACC__)
  165. # Generate the dependency file
  166. cuda_execute_process(
  167. "Generating dependency file: ${NVCC_generated_dependency_file}"
  168. COMMAND "${CUDA_NVCC_EXECUTABLE}"
  169. -M
  170. ${CUDACC_DEFINE}
  171. "${source_file}"
  172. -o "${NVCC_generated_dependency_file}"
  173. ${CCBIN}
  174. ${nvcc_flags}
  175. ${nvcc_host_compiler_flags}
  176. ${depends_CUDA_NVCC_FLAGS}
  177. -DNVCC
  178. ${CUDA_NVCC_INCLUDE_ARGS}
  179. )
  180. if(CUDA_result)
  181. message(FATAL_ERROR "Error generating ${generated_file}")
  182. endif()
  183. # Generate the cmake readable dependency file to a temp file. Don't put the
  184. # quotes just around the filenames for the input_file and output_file variables.
  185. # CMake will pass the quotes through and not be able to find the file.
  186. cuda_execute_process(
  187. "Generating temporary cmake readable file: ${cmake_dependency_file}.tmp"
  188. COMMAND "${CMAKE_COMMAND}"
  189. -D "input_file:FILEPATH=${NVCC_generated_dependency_file}"
  190. -D "output_file:FILEPATH=${cmake_dependency_file}.tmp"
  191. -P "${CUDA_make2cmake}"
  192. )
  193. if(CUDA_result)
  194. message(FATAL_ERROR "Error generating ${generated_file}")
  195. endif()
  196. # Copy the file if it is different
  197. cuda_execute_process(
  198. "Copy if different ${cmake_dependency_file}.tmp to ${cmake_dependency_file}"
  199. COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${cmake_dependency_file}.tmp" "${cmake_dependency_file}"
  200. )
  201. if(CUDA_result)
  202. message(FATAL_ERROR "Error generating ${generated_file}")
  203. endif()
  204. # Delete the temporary file
  205. cuda_execute_process(
  206. "Removing ${cmake_dependency_file}.tmp and ${NVCC_generated_dependency_file}"
  207. COMMAND "${CMAKE_COMMAND}" -E remove "${cmake_dependency_file}.tmp" "${NVCC_generated_dependency_file}"
  208. )
  209. if(CUDA_result)
  210. message(FATAL_ERROR "Error generating ${generated_file}")
  211. endif()
  212. # Generate the code
  213. cuda_execute_process(
  214. "Generating ${generated_file}"
  215. COMMAND "${CUDA_NVCC_EXECUTABLE}"
  216. "${source_file}"
  217. ${format_flag} -o "${generated_file}"
  218. ${CCBIN}
  219. ${nvcc_flags}
  220. ${nvcc_host_compiler_flags}
  221. ${CUDA_NVCC_FLAGS}
  222. -DNVCC
  223. ${CUDA_NVCC_INCLUDE_ARGS}
  224. )
  225. if(CUDA_result)
  226. # Since nvcc can sometimes leave half done files make sure that we delete the output file.
  227. cuda_execute_process(
  228. "Removing ${generated_file}"
  229. COMMAND "${CMAKE_COMMAND}" -E remove "${generated_file}"
  230. )
  231. message(FATAL_ERROR "Error generating file ${generated_file}")
  232. else()
  233. if(verbose)
  234. message("Generated ${generated_file} successfully.")
  235. endif()
  236. endif()
  237. # Cubin resource report commands.
  238. if( build_cubin )
  239. # Run with -cubin to produce resource usage report.
  240. cuda_execute_process(
  241. "Generating ${generated_cubin_file}"
  242. COMMAND "${CUDA_NVCC_EXECUTABLE}"
  243. "${source_file}"
  244. ${CUDA_NVCC_FLAGS}
  245. ${nvcc_flags}
  246. ${CCBIN}
  247. ${nvcc_host_compiler_flags}
  248. -DNVCC
  249. -cubin
  250. -o "${generated_cubin_file}"
  251. ${CUDA_NVCC_INCLUDE_ARGS}
  252. )
  253. # Execute the parser script.
  254. cuda_execute_process(
  255. "Executing the parser script"
  256. COMMAND "${CMAKE_COMMAND}"
  257. -D "input_file:STRING=${generated_cubin_file}"
  258. -P "${CUDA_parse_cubin}"
  259. )
  260. endif()