Added build_flatbuffers function to CMake.
This function is designed to be called by projects that use FlatBuffers.
It takes a list of FlatBuffers schemas and a list of schema include
directories, and generates build rules to create generated headers
and/or binary schemas for those files, as well as optionally copying
all of the text schemas into a directory for your use (for example if
you need to parse text schemas within your app).
See function comments for more details.
Change-Id: I181684b916e91d965e14849d8f83ec2c83e9a4a4
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f0691dd..299cecd 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -129,6 +129,105 @@
DEPENDS flatc)
endfunction()
+# General function to create FlatBuffer build rules for the given list of
+# schemas.
+#
+# flatbuffers_schemas: A list of flatbuffer schema files to process.
+#
+# schema_include_dirs: A list of schema file include directories, which will be
+# passed to flatc via the -I parameter.
+#
+# custom_target_name: The generated files will be added as dependencies for a
+# new custom target with this name. You should add that target as a dependency
+# for your main target to ensure these files are built.
+#
+# additional_dependencies: A list of additional dependencies that you'd like
+# all generated files to depend on. Pass in a blank string if you have none.
+#
+# generated_includes_dir: Where to generate the C++ header files for these
+# schemas. The generated includes directory will automatically be added to
+# CMake's include directories, and will be where generated header files are
+# placed. This parameter is optional; pass in empty string if you don't want to
+# generate include files for these schemas.
+#
+# binary_schemas_dir: If you specify an optional binary schema directory, binary
+# schemas will be generated for these schemas as well, and placed into the given
+# directory.
+#
+# copy_text_schemas_dir: If you want all text schemas (including schemas from
+# all schema include directories) copied into a directory (for example, if you
+# need them within your project to build JSON files), you can specify that
+# folder here. All text schemas will be copied to that folder.
+#
+# IMPORTANT: Make sure you quote all list arguments you pass to this function!
+# Otherwise CMake will only pass in the first element.
+# Example: build_flatbuffers("${fb_files}" "${include_dirs}" target_name ...)
+function(build_flatbuffers flatbuffers_schemas
+ schema_include_dirs
+ custom_target_name
+ additional_dependencies
+ generated_includes_dir
+ binary_schemas_dir
+ copy_text_schemas_dir)
+ set(schema_glob "*.fbs")
+ # Generate the include files parameters.
+ set(include_params "")
+ set(all_generated_files "")
+ foreach (include_dir ${schema_include_dirs})
+ set(include_params -I ${include_dir} ${include_params})
+ if (NOT ${copy_text_schemas_dir} STREQUAL "")
+ # Copy text schemas from dependent folders.
+ file(GLOB_RECURSE dependent_schemas ${include_dir}/${schema_glob})
+ foreach (dependent_schema ${dependent_schemas})
+ file(COPY ${dependent_schema} DESTINATION ${copy_text_schemas_dir})
+ endforeach()
+ endif()
+ endforeach()
+
+ # Register the include directory we are using.
+ if (NOT ${generated_includes_dir} STREQUAL "")
+ include_directories(${generated_includes_dir})
+ endif()
+
+ foreach(schema ${flatbuffers_schemas})
+ get_filename_component(filename ${schema} NAME_WE)
+ # For each schema, do the things we requested.
+ if (NOT ${generated_includes_dir} STREQUAL "")
+ set(generated_include ${generated_includes_dir}/${filename}_generated.h)
+ add_custom_command(
+ OUTPUT ${generated_include}
+ COMMAND flatc --gen-mutable
+ -o ${generated_includes_dir}
+ ${include_params}
+ -c ${schema}
+ DEPENDS flatc ${schema} ${additional_dependencies})
+ list(APPEND all_generated_files ${generated_include})
+ endif()
+
+ if (NOT ${binary_schemas_dir} STREQUAL "")
+ set(binary_schema ${binary_schemas_dir}/${filename}.bfbs)
+ add_custom_command(
+ OUTPUT ${binary_schema}
+ COMMAND flatc -b --schema
+ -o ${binary_schemas_dir}
+ ${include_params}
+ ${schema}
+ DEPENDS flatc ${schema} ${additional_dependencies})
+ list(APPEND all_generated_files ${binary_schema})
+ endif()
+
+ if (NOT ${copy_text_schemas_dir} STREQUAL "")
+ file(COPY ${schema} DESTINATION ${copy_text_schemas_dir})
+ endif()
+ endforeach()
+
+ # Create a custom target that depends on all the generated files.
+ # This is the target that you can depend on to trigger all these
+ # to be built.
+ add_custom_target(${custom_target_name}
+ DEPENDS ${all_generated_files} ${additional_dependencies})
+endfunction()
+
if(FLATBUFFERS_BUILD_TESTS)
compile_flatbuffers_schema_to_cpp(tests/monster_test.fbs)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/tests)