2 # BUILD rules for generating flatbuffer files in various languages.
5 Rules for building C++ flatbuffers with Bazel.
8 load("@rules_cc//cc:defs.bzl", "cc_library")
10 flatc_path = "@com_github_google_flatbuffers//:flatc"
12 DEFAULT_INCLUDE_PATHS = [
18 DEFAULT_FLATC_ARGS = [
24 "--cpp-ptr-type flatbuffers::unique_ptr",
27 def flatbuffer_library_public(
34 include_paths = DEFAULT_INCLUDE_PATHS,
35 flatc_args = DEFAULT_FLATC_ARGS,
37 reflection_visibility = None,
38 compatible_with = None,
40 output_to_bindir = False):
41 """Generates code files for reading/writing the given flatbuffers in the requested language using the public compiler.
45 srcs: Source .fbs files. Sent in order to the compiler.
46 outs: Output files from flatc.
47 language_flag: Target language flag. One of [-c, -j, -js].
48 out_prefix: Prepend this path to the front of all generated files except on
49 single source targets. Usually is a directory name.
50 includes: Optional, list of filegroups of schemas that the srcs depend on.
51 include_paths: Optional, list of paths the includes files can be found in.
52 flatc_args: Optional, list of additional arguments to pass to flatc.
53 reflection_name: Optional, if set this will generate the flatbuffer
54 reflection binaries for the schemas.
55 reflection_visibility: The visibility of the generated reflection Fileset.
56 output_to_bindir: Passed to genrule for output to bin directory.
57 compatible_with: Optional, The list of environments this rule can be
58 built for, in addition to default-supported environments.
59 restricted_to: Optional, The list of environments this rule can be built
60 for, instead of default-supported environments.
61 output_to_bindir: Passed to genrule for output to bin directory.
64 This rule creates a filegroup(name) with all generated source files, and
65 optionally a Fileset([reflection_name]) with all generated reflection
68 include_paths_cmd = ["-I %s" % (s) for s in include_paths]
70 # '$(@D)' when given a single source target will give the appropriate
71 # directory. Appending 'out_prefix' is only necessary when given a build
72 # target with multiple sources.
74 ("-o $(@D)/%s" % (out_prefix)) if len(srcs) > 1 else ("-o $(@D)")
76 genrule_cmd = " ".join([
78 "for f in $${SRCS[@]:0:%s}; do" % len(srcs),
79 "$(location %s)" % (flatc_path),
80 " ".join(include_paths_cmd),
89 srcs = srcs + includes,
91 output_to_bindir = output_to_bindir,
94 compatible_with = compatible_with,
95 restricted_to = restricted_to,
96 message = "Generating flatbuffer files for %s:" % (name),
99 reflection_genrule_cmd = " ".join([
101 "for f in $${SRCS[@]:0:%s}; do" % len(srcs),
102 "$(location %s)" % (flatc_path),
104 " ".join(flatc_args),
105 " ".join(include_paths_cmd),
112 (out_prefix + "%s.bfbs") % (s.replace(".fbs", "").split("/")[-1])
116 name = "%s_srcs" % reflection_name,
117 srcs = srcs + includes,
118 outs = reflection_outs,
119 output_to_bindir = output_to_bindir,
120 tools = [flatc_path],
121 compatible_with = compatible_with,
122 restricted_to = restricted_to,
123 cmd = reflection_genrule_cmd,
124 message = "Generating flatbuffer reflection binary for %s:" % (name),
127 name = "%s_out" % reflection_name,
128 srcs = reflection_outs,
129 visibility = reflection_visibility,
130 compatible_with = compatible_with,
131 restricted_to = restricted_to,
134 def flatbuffer_cc_library(
137 srcs_filegroup_name = "",
140 include_paths = DEFAULT_INCLUDE_PATHS,
141 flatc_args = DEFAULT_FLATC_ARGS,
143 compatible_with = None,
144 restricted_to = None,
145 srcs_filegroup_visibility = None,
146 gen_reflections = False):
147 '''A cc_library with the generated reader/writers for the given flatbuffer definitions.
151 srcs: Source .fbs files. Sent in order to the compiler.
152 srcs_filegroup_name: Name of the output filegroup that holds srcs. Pass this
153 filegroup into the `includes` parameter of any other
154 flatbuffer_cc_library that depends on this one's schemas.
155 out_prefix: Prepend this path to the front of all generated files. Usually
157 includes: Optional, list of filegroups of schemas that the srcs depend on.
158 ** SEE REMARKS BELOW **
159 include_paths: Optional, list of paths the includes files can be found in.
160 flatc_args: Optional list of additional arguments to pass to flatc
161 (e.g. --gen-mutable).
162 visibility: The visibility of the generated cc_library. By default, use the
163 default visibility of the project.
164 srcs_filegroup_visibility: The visibility of the generated srcs filegroup.
165 By default, use the value of the visibility parameter above.
166 gen_reflections: Optional, if true this will generate the flatbuffer
167 reflection binaries for the schemas.
168 compatible_with: Optional, The list of environments this rule can be built
169 for, in addition to default-supported environments.
170 restricted_to: Optional, The list of environments this rule can be built
171 for, instead of default-supported environments.
174 filegroup([name]_srcs): all generated .h files.
175 filegroup(srcs_filegroup_name if specified, or [name]_includes if not):
176 Other flatbuffer_cc_library's can pass this in for their `includes`
177 parameter, if they depend on the schemas in this library.
178 Fileset([name]_reflection): (Optional) all generated reflection binaries.
179 cc_library([name]): library with sources and flatbuffers deps.
182 ** Because the genrule used to call flatc does not have any trivial way of
183 computing the output list of files transitively generated by includes and
184 --gen-includes (the default) being defined for flatc, the --gen-includes
185 flag will not work as expected. The way around this is to add a dependency
186 to the flatbuffer_cc_library defined alongside the flatc included Fileset.
187 For example you might define:
189 flatbuffer_cc_library(
191 srcs = [ "schemas/foo.fbs" ],
192 includes = [ "//third_party/bazz:bazz_fbs_includes" ],
195 In which foo.fbs includes a few files from the Fileset defined at
196 //third_party/bazz:bazz_fbs_includes. When compiling the library that
197 includes foo_generated.h, and therefore has my_fbs as a dependency, it
198 will fail to find any of the bazz *_generated.h files unless you also
199 add bazz's flatbuffer_cc_library to your own dependency list, e.g.:
205 "//third_party/bazz:bazz_fbs"
209 Happy dependent Flatbuffering!
212 (out_prefix + "%s_generated.h") % (s.replace(".fbs", "").split("/")[-1])
215 reflection_name = "%s_reflection" % name if gen_reflections else ""
217 srcs_lib = "%s_srcs" % (name)
218 flatbuffer_library_public(
221 outs = output_headers,
222 language_flag = "-c",
223 out_prefix = out_prefix,
225 include_paths = include_paths,
226 flatc_args = flatc_args,
227 compatible_with = compatible_with,
228 restricted_to = restricted_to,
229 reflection_name = reflection_name,
230 reflection_visibility = visibility,
244 "@com_github_google_flatbuffers//:runtime_cc",
247 compatible_with = compatible_with,
248 restricted_to = restricted_to,
250 visibility = visibility,
253 # A filegroup for the `srcs`. That is, all the schema files for this
256 name = srcs_filegroup_name if srcs_filegroup_name else "%s_includes" % (name),
258 compatible_with = compatible_with,
259 restricted_to = restricted_to,
260 visibility = srcs_filegroup_visibility if srcs_filegroup_visibility != None else visibility,