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.

BuildFlatBuffers.cmake 6.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # Copyright 2015 Google Inc. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # General function to create FlatBuffer build rules for the given list of
  15. # schemas.
  16. #
  17. # flatbuffers_schemas: A list of flatbuffer schema files to process.
  18. #
  19. # schema_include_dirs: A list of schema file include directories, which will be
  20. # passed to flatc via the -I parameter.
  21. #
  22. # custom_target_name: The generated files will be added as dependencies for a
  23. # new custom target with this name. You should add that target as a dependency
  24. # for your main target to ensure these files are built. You can also retrieve
  25. # various properties from this target, such as GENERATED_INCLUDES_DIR,
  26. # BINARY_SCHEMAS_DIR, and COPY_TEXT_SCHEMAS_DIR.
  27. #
  28. # additional_dependencies: A list of additional dependencies that you'd like
  29. # all generated files to depend on. Pass in a blank string if you have none.
  30. #
  31. # generated_includes_dir: Where to generate the C++ header files for these
  32. # schemas. The generated includes directory will automatically be added to
  33. # CMake's include directories, and will be where generated header files are
  34. # placed. This parameter is optional; pass in empty string if you don't want to
  35. # generate include files for these schemas.
  36. #
  37. # binary_schemas_dir: If you specify an optional binary schema directory, binary
  38. # schemas will be generated for these schemas as well, and placed into the given
  39. # directory.
  40. #
  41. # copy_text_schemas_dir: If you want all text schemas (including schemas from
  42. # all schema include directories) copied into a directory (for example, if you
  43. # need them within your project to build JSON files), you can specify that
  44. # folder here. All text schemas will be copied to that folder.
  45. #
  46. # IMPORTANT: Make sure you quote all list arguments you pass to this function!
  47. # Otherwise CMake will only pass in the first element.
  48. # Example: build_flatbuffers("${fb_files}" "${include_dirs}" target_name ...)
  49. function(build_flatbuffers flatbuffers_schemas
  50. schema_include_dirs
  51. custom_target_name
  52. additional_dependencies
  53. generated_includes_dir
  54. binary_schemas_dir
  55. copy_text_schemas_dir)
  56. # Test if including from FindFlatBuffers
  57. if(FLATBUFFERS_FLATC_EXECUTABLE)
  58. set(FLATC_TARGET "")
  59. set(FLATC ${FLATBUFFERS_FLATC_EXECUTABLE})
  60. else()
  61. set(FLATC_TARGET flatbuffers::flatc)
  62. set(FLATC flatc)
  63. endif()
  64. set(FLATC_SCHEMA_ARGS --gen-mutable)
  65. if(FLATBUFFERS_FLATC_SCHEMA_EXTRA_ARGS)
  66. set(FLATC_SCHEMA_ARGS
  67. ${FLATBUFFERS_FLATC_SCHEMA_EXTRA_ARGS}
  68. ${FLATC_SCHEMA_ARGS}
  69. )
  70. endif()
  71. set(working_dir "${CMAKE_CURRENT_SOURCE_DIR}")
  72. set(schema_glob "*.fbs")
  73. # Generate the include files parameters.
  74. set(include_params "")
  75. set(all_generated_files "")
  76. foreach (include_dir ${schema_include_dirs})
  77. set(include_params -I ${include_dir} ${include_params})
  78. if (NOT ${copy_text_schemas_dir} STREQUAL "")
  79. # Copy text schemas from dependent folders.
  80. file(GLOB_RECURSE dependent_schemas ${include_dir}/${schema_glob})
  81. foreach (dependent_schema ${dependent_schemas})
  82. file(COPY ${dependent_schema} DESTINATION ${copy_text_schemas_dir})
  83. endforeach()
  84. endif()
  85. endforeach()
  86. foreach(schema ${flatbuffers_schemas})
  87. get_filename_component(filename ${schema} NAME_WE)
  88. # For each schema, do the things we requested.
  89. if (NOT ${generated_includes_dir} STREQUAL "")
  90. set(generated_include ${generated_includes_dir}/${filename}_generated.h)
  91. add_custom_command(
  92. OUTPUT ${generated_include}
  93. COMMAND ${FLATC} ${FLATC_SCHEMA_ARGS}
  94. -o ${generated_includes_dir}
  95. ${include_params}
  96. -c ${schema}
  97. DEPENDS ${FLATC_TARGET} ${schema} ${additional_dependencies}
  98. WORKING_DIRECTORY "${working_dir}")
  99. list(APPEND all_generated_files ${generated_include})
  100. endif()
  101. if (NOT ${binary_schemas_dir} STREQUAL "")
  102. set(binary_schema ${binary_schemas_dir}/${filename}.bfbs)
  103. add_custom_command(
  104. OUTPUT ${binary_schema}
  105. COMMAND ${FLATC} -b --schema
  106. -o ${binary_schemas_dir}
  107. ${include_params}
  108. ${schema}
  109. DEPENDS ${FLATC_TARGET} ${schema} ${additional_dependencies}
  110. WORKING_DIRECTORY "${working_dir}")
  111. list(APPEND all_generated_files ${binary_schema})
  112. endif()
  113. if (NOT ${copy_text_schemas_dir} STREQUAL "")
  114. file(COPY ${schema} DESTINATION ${copy_text_schemas_dir})
  115. endif()
  116. endforeach()
  117. # Create a custom target that depends on all the generated files.
  118. # This is the target that you can depend on to trigger all these
  119. # to be built.
  120. add_custom_target(${custom_target_name}
  121. DEPENDS ${all_generated_files} ${additional_dependencies})
  122. # Register the include directory we are using.
  123. if (NOT ${generated_includes_dir} STREQUAL "")
  124. include_directories(${generated_includes_dir})
  125. set_property(TARGET ${custom_target_name}
  126. PROPERTY GENERATED_INCLUDES_DIR
  127. ${generated_includes_dir})
  128. endif()
  129. # Register the binary schemas dir we are using.
  130. if (NOT ${binary_schemas_dir} STREQUAL "")
  131. set_property(TARGET ${custom_target_name}
  132. PROPERTY BINARY_SCHEMAS_DIR
  133. ${binary_schemas_dir})
  134. endif()
  135. # Register the text schema copy dir we are using.
  136. if (NOT ${copy_text_schemas_dir} STREQUAL "")
  137. set_property(TARGET ${custom_target_name}
  138. PROPERTY COPY_TEXT_SCHEMAS_DIR
  139. ${copy_text_schemas_dir})
  140. endif()
  141. endfunction()

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