blob: 835f4b80c0ad931c4aca23535582bf935b368669 [file] [log] [blame]
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001# 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
15# General function to create FlatBuffer build rules for the given list of
16# schemas.
17#
18# flatbuffers_schemas: A list of flatbuffer schema files to process.
19#
20# schema_include_dirs: A list of schema file include directories, which will be
21# passed to flatc via the -I parameter.
22#
23# custom_target_name: The generated files will be added as dependencies for a
24# new custom target with this name. You should add that target as a dependency
25# for your main target to ensure these files are built. You can also retrieve
26# various properties from this target, such as GENERATED_INCLUDES_DIR,
27# BINARY_SCHEMAS_DIR, and COPY_TEXT_SCHEMAS_DIR.
28#
29# additional_dependencies: A list of additional dependencies that you'd like
30# all generated files to depend on. Pass in a blank string if you have none.
31#
32# generated_includes_dir: Where to generate the C++ header files for these
33# schemas. The generated includes directory will automatically be added to
34# CMake's include directories, and will be where generated header files are
35# placed. This parameter is optional; pass in empty string if you don't want to
36# generate include files for these schemas.
37#
38# binary_schemas_dir: If you specify an optional binary schema directory, binary
39# schemas will be generated for these schemas as well, and placed into the given
40# directory.
41#
42# copy_text_schemas_dir: If you want all text schemas (including schemas from
43# all schema include directories) copied into a directory (for example, if you
44# need them within your project to build JSON files), you can specify that
45# folder here. All text schemas will be copied to that folder.
46#
47# IMPORTANT: Make sure you quote all list arguments you pass to this function!
48# Otherwise CMake will only pass in the first element.
49# Example: build_flatbuffers("${fb_files}" "${include_dirs}" target_name ...)
50function(build_flatbuffers flatbuffers_schemas
51 schema_include_dirs
52 custom_target_name
53 additional_dependencies
54 generated_includes_dir
55 binary_schemas_dir
56 copy_text_schemas_dir)
57
58 # Test if including from FindFlatBuffers
59 if(FLATBUFFERS_FLATC_EXECUTABLE)
60 set(FLATC_TARGET "")
61 set(FLATC ${FLATBUFFERS_FLATC_EXECUTABLE})
62 else()
63 set(FLATC_TARGET flatc)
64 set(FLATC flatc)
65 endif()
66 set(FLATC_SCHEMA_ARGS --gen-mutable)
67 if(FLATBUFFERS_FLATC_SCHEMA_EXTRA_ARGS)
68 set(FLATC_SCHEMA_ARGS
69 ${FLATBUFFERS_FLATC_SCHEMA_EXTRA_ARGS}
70 ${FLATC_SCHEMA_ARGS}
71 )
72 endif()
73
74 set(working_dir "${CMAKE_CURRENT_SOURCE_DIR}")
75
76 set(schema_glob "*.fbs")
77 # Generate the include files parameters.
78 set(include_params "")
79 set(all_generated_files "")
80 foreach (include_dir ${schema_include_dirs})
81 set(include_params -I ${include_dir} ${include_params})
82 if (NOT ${copy_text_schemas_dir} STREQUAL "")
83 # Copy text schemas from dependent folders.
84 file(GLOB_RECURSE dependent_schemas ${include_dir}/${schema_glob})
85 foreach (dependent_schema ${dependent_schemas})
86 file(COPY ${dependent_schema} DESTINATION ${copy_text_schemas_dir})
87 endforeach()
88 endif()
89 endforeach()
90
91 foreach(schema ${flatbuffers_schemas})
92 get_filename_component(filename ${schema} NAME_WE)
93 # For each schema, do the things we requested.
94 if (NOT ${generated_includes_dir} STREQUAL "")
95 set(generated_include ${generated_includes_dir}/${filename}_generated.h)
96 add_custom_command(
97 OUTPUT ${generated_include}
98 COMMAND ${FLATC} ${FLATC_SCHEMA_ARGS}
99 -o ${generated_includes_dir}
100 ${include_params}
101 -c ${schema}
102 DEPENDS ${FLATC_TARGET} ${schema} ${additional_dependencies}
103 WORKING_DIRECTORY "${working_dir}")
104 list(APPEND all_generated_files ${generated_include})
105 endif()
106
107 if (NOT ${binary_schemas_dir} STREQUAL "")
108 set(binary_schema ${binary_schemas_dir}/${filename}.bfbs)
109 add_custom_command(
110 OUTPUT ${binary_schema}
111 COMMAND ${FLATC} -b --schema
112 -o ${binary_schemas_dir}
113 ${include_params}
114 ${schema}
115 DEPENDS ${FLATC_TARGET} ${schema} ${additional_dependencies}
116 WORKING_DIRECTORY "${working_dir}")
117 list(APPEND all_generated_files ${binary_schema})
118 endif()
119
120 if (NOT ${copy_text_schemas_dir} STREQUAL "")
121 file(COPY ${schema} DESTINATION ${copy_text_schemas_dir})
122 endif()
123 endforeach()
124
125 # Create a custom target that depends on all the generated files.
126 # This is the target that you can depend on to trigger all these
127 # to be built.
128 add_custom_target(${custom_target_name}
129 DEPENDS ${all_generated_files} ${additional_dependencies})
130
131 # Register the include directory we are using.
132 if (NOT ${generated_includes_dir} STREQUAL "")
133 include_directories(${generated_includes_dir})
134 set_property(TARGET ${custom_target_name}
135 PROPERTY GENERATED_INCLUDES_DIR
136 ${generated_includes_dir})
137 endif()
138
139 # Register the binary schemas dir we are using.
140 if (NOT ${binary_schemas_dir} STREQUAL "")
141 set_property(TARGET ${custom_target_name}
142 PROPERTY BINARY_SCHEMAS_DIR
143 ${binary_schemas_dir})
144 endif()
145
146 # Register the text schema copy dir we are using.
147 if (NOT ${copy_text_schemas_dir} STREQUAL "")
148 set_property(TARGET ${custom_target_name}
149 PROPERTY COPY_TEXT_SCHEMAS_DIR
150 ${copy_text_schemas_dir})
151 endif()
152endfunction()