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.0 kB

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