run_nvcc.cmake 12 KB

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