You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

run_cncc.cmake 8.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. # This code is licensed under the MIT License. See the FindBANG.cmake script
  2. # for the text of the license.
  3. # The MIT License
  4. #
  5. # License for the specific language governing rights and limitations under
  6. # Permission is hereby granted, free of charge, to any person obtaining a
  7. # copy of this software and associated documentation files (the "Software"),
  8. # to deal in the Software without restriction, including without limitation
  9. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. # and/or sell copies of the Software, and to permit persons to whom the
  11. # Software is furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included
  14. # in all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. # DEALINGS IN THE SOFTWARE.
  23. ##########################################################################
  24. # This file runs the cncc commands to produce the desired output file along with
  25. # the dependency file needed by CMake to compute dependencies. In addition the
  26. # file checks the output of each command and if the command fails it deletes the
  27. # output files.
  28. # Input variables
  29. #
  30. # verbose:BOOL=<> OFF: Be as quiet as possible (default)
  31. # ON : Describe each step
  32. #
  33. # build_configuration:STRING=<> Typically one of Debug, MinSizeRel, Release, or
  34. # RelWithDebInfo, but it should match one of the
  35. # entries in BANG_HOST_FLAGS. This is the build
  36. # configuration used when compiling the code. If
  37. # blank or unspecified Debug is assumed as this is
  38. # what CMake does.
  39. #
  40. # generated_file:STRING=<> File to generate. This argument must be passed in.
  41. #
  42. # generated_cnbin_file:STRING=<> File to generate. This argument must be passed
  43. # in if build_cnbin is true.
  44. if(NOT generated_file)
  45. message(FATAL_ERROR "You must specify generated_file on the command line")
  46. endif()
  47. # Set these up as variables to make reading the generated file easier
  48. set(CMAKE_COMMAND "@CMAKE_COMMAND@") # path
  49. set(source_file "@source_file@") # path
  50. set(CNCC_generated_dependency_file "@CNCC_generated_dependency_file@") # path
  51. set(cmake_dependency_file "@cmake_dependency_file@") # path
  52. set(BANG_make2cmake "@BANG_make2cmake@") # path
  53. set(BANG_parse_cnbin "@BANG_parse_cnbin@") # path
  54. set(build_cnbin @build_cnbin@) # bool
  55. set(BANG_HOST_COMPILER "@BANG_HOST_COMPILER@") # path
  56. # We won't actually use these variables for now, but we need to set this, in
  57. # order to force this file to be run again if it changes.
  58. set(generated_file_path "@generated_file_path@") # path
  59. set(generated_file_internal "@generated_file@") # path
  60. set(generated_cnbin_file_internal "@generated_cnbin_file@") # path
  61. set(BANG_CNCC_EXECUTABLE "@BANG_CNCC_EXECUTABLE@") # path
  62. set(BANG_CNCC_FLAGS @BANG_CNCC_FLAGS@ ;; @BANG_WRAP_OPTION_CNCC_FLAGS@) # list
  63. @BANG_CNCC_FLAGS_CONFIG@
  64. set(cncc_flags @cncc_flags@) # list
  65. set(BANG_CNCC_INCLUDE_ARGS "@BANG_CNCC_INCLUDE_ARGS@") # list (needs to be in quotes to handle spaces properly).
  66. set(format_flag "@format_flag@") # string
  67. set(bang_language_flag @bang_language_flag@) # list
  68. if(build_cnbin AND NOT generated_cnbin_file)
  69. message(FATAL_ERROR "You must specify generated_cnbin_file on the command line")
  70. endif()
  71. # This is the list of host compilation flags. It C or CXX should already have
  72. # been chosen by FindBANG.cmake.
  73. @BANG_HOST_FLAGS@
  74. # Take the compiler flags and package them up to be sent to the compiler via -Xcompiler
  75. set(cncc_host_compiler_flags "")
  76. # If we weren't given a build_configuration, use Debug.
  77. if(NOT build_configuration)
  78. set(build_configuration Debug)
  79. endif()
  80. string(TOUPPER "${build_configuration}" build_configuration)
  81. #message("BANG_CNCC_HOST_COMPILER_FLAGS = ${BANG_CNCC_HOST_COMPILER_FLAGS}")
  82. foreach(flag ${CMAKE_HOST_FLAGS} ${CMAKE_HOST_FLAGS_${build_configuration}})
  83. # Extra quotes are added around each flag to help cncc parse out flags with spaces.
  84. set(cncc_host_compiler_flags ${cncc_host_compiler_flags} ${flag})
  85. endforeach()
  86. # message("cncc_host_compiler_flags = ${cncc_host_compiler_flags}")
  87. # Add the build specific configuration flags
  88. list(APPEND BANG_CNCC_FLAGS ${BANG_CNCC_FLAGS_${build_configuration}})
  89. # Remove the duplicated flags and including
  90. list(REMOVE_DUPLICATES BANG_CNCC_FLAGS)
  91. list(REMOVE_DUPLICATES BANG_CNCC_INCLUDE_ARGS)
  92. # bang_execute_process - Executes a command with optional command echo and status message.
  93. #
  94. # status - Status message to print if verbose is true
  95. # command - COMMAND argument from the usual execute_process argument structure
  96. # ARGN - Remaining arguments are the command with arguments
  97. #
  98. # BANG_result - return value from running the command
  99. #
  100. # Make this a macro instead of a function, so that things like RESULT_VARIABLE
  101. # and other return variables are present after executing the process.
  102. macro(bang_execute_process status command)
  103. set(_command ${command})
  104. if(NOT "x${_command}" STREQUAL "xCOMMAND")
  105. message(FATAL_ERROR "Malformed call to bang_execute_process. Missing COMMAND as second argument. (command = ${command})")
  106. endif()
  107. if(verbose)
  108. execute_process(COMMAND "${CMAKE_COMMAND}" -E echo -- ${status})
  109. # Now we need to build up our command string. We are accounting for quotes
  110. # and spaces, anything else is left up to the user to fix if they want to
  111. # copy and paste a runnable command line.
  112. set(bang_execute_process_string)
  113. foreach(arg ${ARGN})
  114. # If there are quotes, excape them, so they come through.
  115. string(REPLACE "\"" "\\\"" arg ${arg})
  116. # Args with spaces need quotes around them to get them to be parsed as a single argument.
  117. if(arg MATCHES " ")
  118. list(APPEND bang_execute_process_string "\"${arg}\"")
  119. else()
  120. list(APPEND bang_execute_process_string ${arg})
  121. endif()
  122. endforeach()
  123. # Echo the command
  124. execute_process(COMMAND ${CMAKE_COMMAND} -E echo ${bang_execute_process_string})
  125. endif()
  126. # Run the command
  127. execute_process(COMMAND ${ARGN} RESULT_VARIABLE BANG_result )
  128. endmacro()
  129. # Delete the target file
  130. bang_execute_process(
  131. "Removing ${generated_file}"
  132. COMMAND "${CMAKE_COMMAND}" -E remove "${generated_file}"
  133. )
  134. # cncc ignore host flags
  135. set(cncc_host_compiler_flags "")
  136. # Generate the code
  137. bang_execute_process(
  138. "Generating ${generated_file}"
  139. COMMAND "${BANG_CNCC_EXECUTABLE}"
  140. "${source_file}"
  141. ${bang_language_flag}
  142. ${format_flag} -o "${generated_file}"
  143. ${cncc_flags}
  144. ${cncc_host_compiler_flags}
  145. ${BANG_CNCC_FLAGS}
  146. -DCNCC
  147. ${BANG_CNCC_INCLUDE_ARGS}
  148. )
  149. if(BANG_result)
  150. # Since cncc can sometimes leave half done files make sure that we delete the output file.
  151. bang_execute_process(
  152. "Removing ${generated_file}"
  153. COMMAND "${CMAKE_COMMAND}" -E remove "${generated_file}"
  154. )
  155. message(FATAL_ERROR "Error generating file ${generated_file}")
  156. else()
  157. message(VERBOSE "Generated ${generated_file} successfully.")
  158. endif()
  159. # Cnbin resource report commands.
  160. if( build_cnbin )
  161. # Run with -cnbin to produce resource usage report.
  162. bang_execute_process(
  163. "Generating ${generated_cnbin_file}"
  164. COMMAND "${BANG_CNCC_EXECUTABLE}"
  165. "${source_file}"
  166. ${BANG_CNCC_FLAGS}
  167. ${cncc_flags}
  168. ${cncc_host_compiler_flags}
  169. -DCNCC
  170. -cnbin
  171. -o "${generated_cnbin_file}"
  172. ${BANG_CNCC_INCLUDE_ARGS}
  173. )
  174. # Execute the parser script.
  175. bang_execute_process(
  176. "Executing the parser script"
  177. COMMAND "${CMAKE_COMMAND}"
  178. -D "input_file:STRING=${generated_cnbin_file}"
  179. -P "${BANG_parse_cnbin}"
  180. )
  181. endif()

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台