flatbuffers
/
build_defs.bzl
284 строки · 10.9 Кб
1# Description:
2# BUILD rules for generating flatbuffer files in various languages.
3
4"""
5Rules for building C++ flatbuffers with Bazel.
6"""
7
8load("@rules_cc//cc:defs.bzl", "cc_library")
9
10TRUE_FLATC_PATH = "@com_github_google_flatbuffers//:flatc"
11
12DEFAULT_INCLUDE_PATHS = [
13"./",
14"$(GENDIR)",
15"$(BINDIR)",
16"$(execpath @com_github_google_flatbuffers//:flatc).runfiles/com_github_google_flatbuffers",
17]
18
19def default_include_paths(flatc_path):
20return [
21"./",
22"$(GENDIR)",
23"$(BINDIR)",
24"$(execpath %s).runfiles/com_github_google_flatbuffers" % (flatc_path),
25]
26
27DEFAULT_FLATC_ARGS = [
28"--gen-object-api",
29"--gen-compare",
30"--no-includes",
31"--gen-mutable",
32"--reflect-names",
33"--cpp-ptr-type flatbuffers::unique_ptr",
34]
35
36def flatbuffer_library_public(
37name,
38srcs,
39outs,
40language_flag,
41out_prefix = "",
42includes = [],
43include_paths = None,
44flatc_args = DEFAULT_FLATC_ARGS,
45reflection_name = "",
46reflection_visibility = None,
47compatible_with = None,
48restricted_to = None,
49target_compatible_with = None,
50flatc_path = "@com_github_google_flatbuffers//:flatc",
51output_to_bindir = False,
52tools = None,
53extra_env = None,
54**kwargs):
55"""Generates code files for reading/writing the given flatbuffers in the requested language using the public compiler.
56
57Args:
58name: Rule name.
59srcs: Source .fbs files. Sent in order to the compiler.
60outs: Output files from flatc.
61language_flag: Target language flag. One of [-c, -j, -js].
62out_prefix: Prepend this path to the front of all generated files except on
63single source targets. Usually is a directory name.
64includes: Optional, list of filegroups of schemas that the srcs depend on.
65include_paths: Optional, list of paths the includes files can be found in.
66flatc_args: Optional, list of additional arguments to pass to flatc.
67reflection_name: Optional, if set this will generate the flatbuffer
68reflection binaries for the schemas.
69reflection_visibility: The visibility of the generated reflection Fileset.
70output_to_bindir: Passed to genrule for output to bin directory.
71compatible_with: Optional, The list of environments this rule can be
72built for, in addition to default-supported environments.
73restricted_to: Optional, The list of environments this rule can be built
74for, instead of default-supported environments.
75target_compatible_with: Optional, The list of target platform constraints
76to use.
77flatc_path: Bazel target corresponding to the flatc compiler to use.
78output_to_bindir: Passed to genrule for output to bin directory.
79tools: Optional, passed to genrule for list of tools to make available
80during the action.
81extra_env: Optional, must be a string of "VAR1=VAL1 VAR2=VAL2". These get
82set as environment variables that "flatc_path" sees.
83**kwargs: Passed to the underlying genrule.
84
85
86This rule creates a filegroup(name) with all generated source files, and
87optionally a Fileset([reflection_name]) with all generated reflection
88binaries.
89"""
90reflection_include_paths = include_paths
91if include_paths == None:
92include_paths = default_include_paths(flatc_path)
93include_paths_cmd = ["-I %s" % (s) for s in include_paths]
94
95extra_env = extra_env or ""
96
97# '$(@D)' when given a single source target will give the appropriate
98# directory. Appending 'out_prefix' is only necessary when given a build
99# target with multiple sources.
100output_directory = (
101("-o $(@D)/%s" % (out_prefix)) if len(srcs) > 1 else ("-o $(@D)")
102)
103genrule_cmd = " ".join([
104"SRCS=($(SRCS));",
105"for f in $${SRCS[@]:0:%s}; do" % len(srcs),
106"OUTPUT_FILE=\"$(OUTS)\" %s $(location %s)" % (extra_env, flatc_path),
107" ".join(include_paths_cmd),
108" ".join(flatc_args),
109language_flag,
110output_directory,
111"$$f;",
112"done",
113])
114native.genrule(
115name = name,
116srcs = srcs + includes,
117outs = outs,
118output_to_bindir = output_to_bindir,
119tools = (tools or []) + [flatc_path],
120cmd = genrule_cmd,
121compatible_with = compatible_with,
122target_compatible_with = target_compatible_with,
123restricted_to = restricted_to,
124message = "Generating flatbuffer files for %s:" % (name),
125**kwargs
126)
127if reflection_name:
128if reflection_include_paths == None:
129reflection_include_paths = default_include_paths(TRUE_FLATC_PATH)
130reflection_include_paths_cmd = ["-I %s" % (s) for s in reflection_include_paths]
131reflection_genrule_cmd = " ".join([
132"SRCS=($(SRCS));",
133"for f in $${SRCS[@]:0:%s}; do" % len(srcs),
134"$(location %s)" % (TRUE_FLATC_PATH),
135"-b --schema",
136" ".join(flatc_args),
137" ".join(reflection_include_paths_cmd),
138language_flag,
139output_directory,
140"$$f;",
141"done",
142])
143reflection_outs = [
144(out_prefix + "%s.bfbs") % (s.replace(".fbs", "").split("/")[-1])
145for s in srcs
146]
147native.genrule(
148name = "%s_srcs" % reflection_name,
149srcs = srcs + includes,
150outs = reflection_outs,
151output_to_bindir = output_to_bindir,
152tools = [TRUE_FLATC_PATH],
153compatible_with = compatible_with,
154restricted_to = restricted_to,
155target_compatible_with = target_compatible_with,
156cmd = reflection_genrule_cmd,
157message = "Generating flatbuffer reflection binary for %s:" % (name),
158visibility = reflection_visibility,
159)
160native.filegroup(
161name = "%s_out" % reflection_name,
162srcs = reflection_outs,
163visibility = reflection_visibility,
164compatible_with = compatible_with,
165restricted_to = restricted_to,
166)
167
168def flatbuffer_cc_library(
169name,
170srcs,
171srcs_filegroup_name = "",
172outs = [],
173out_prefix = "",
174deps = [],
175includes = [],
176include_paths = None,
177cc_include_paths = [],
178flatc_args = DEFAULT_FLATC_ARGS,
179visibility = None,
180compatible_with = None,
181restricted_to = None,
182target_compatible_with = None,
183srcs_filegroup_visibility = None,
184gen_reflections = False):
185"""A cc_library with the generated reader/writers for the given flatbuffer definitions.
186
187Args:
188name: Rule name.
189srcs: Source .fbs files. Sent in order to the compiler.
190srcs_filegroup_name: Name of the output filegroup that holds srcs. Pass this
191filegroup into the `includes` parameter of any other
192flatbuffer_cc_library that depends on this one's schemas.
193outs: Additional outputs expected to be generated by flatc.
194out_prefix: Prepend this path to the front of all generated files. Usually
195is a directory name.
196deps: Optional, list of other flatbuffer_cc_library's to depend on. Cannot be specified
197alongside includes.
198includes: Optional, list of filegroups of schemas that the srcs depend on.
199Use of this is discouraged, and may be deprecated.
200include_paths: Optional, list of paths the includes files can be found in.
201cc_include_paths: Optional, list of paths to add to the cc_library includes attribute.
202flatc_args: Optional list of additional arguments to pass to flatc
203(e.g. --gen-mutable).
204visibility: The visibility of the generated cc_library. By default, use the
205default visibility of the project.
206srcs_filegroup_visibility: The visibility of the generated srcs filegroup.
207By default, use the value of the visibility parameter above.
208gen_reflections: Optional, if true this will generate the flatbuffer
209reflection binaries for the schemas.
210compatible_with: Optional, The list of environments this rule can be built
211for, in addition to default-supported environments.
212restricted_to: Optional, The list of environments this rule can be built
213for, instead of default-supported environments.
214target_compatible_with: Optional, The list of target platform constraints
215to use.
216
217This produces:
218filegroup([name]_srcs): all generated .h files.
219filegroup(srcs_filegroup_name if specified, or [name]_includes if not):
220Other flatbuffer_cc_library's can pass this in for their `includes`
221parameter, if they depend on the schemas in this library.
222Fileset([name]_reflection): (Optional) all generated reflection binaries.
223cc_library([name]): library with sources and flatbuffers deps.
224"""
225output_headers = [
226(out_prefix + "%s_generated.h") % (s.replace(".fbs", "").split("/")[-1].split(":")[-1])
227for s in srcs
228]
229if deps and includes:
230# There is no inherent reason we couldn't support both, but this discourages
231# use of includes without good reason.
232fail("Cannot specify both deps and include in flatbuffer_cc_library.")
233if deps:
234includes = [d + "_includes" for d in deps]
235reflection_name = "%s_reflection" % name if gen_reflections else ""
236
237srcs_lib = "%s_srcs" % (name)
238flatbuffer_library_public(
239name = srcs_lib,
240srcs = srcs,
241outs = outs + output_headers,
242language_flag = "-c",
243out_prefix = out_prefix,
244includes = includes,
245include_paths = include_paths,
246flatc_args = flatc_args,
247compatible_with = compatible_with,
248restricted_to = restricted_to,
249target_compatible_with = target_compatible_with,
250reflection_name = reflection_name,
251reflection_visibility = visibility,
252)
253cc_library(
254name = name,
255hdrs = [
256":" + srcs_lib,
257],
258srcs = [
259":" + srcs_lib,
260],
261features = [
262"-parse_headers",
263],
264deps = [
265"@com_github_google_flatbuffers//:runtime_cc",
266"@com_github_google_flatbuffers//:flatbuffers",
267] + deps,
268includes = cc_include_paths,
269compatible_with = compatible_with,
270restricted_to = restricted_to,
271target_compatible_with = target_compatible_with,
272linkstatic = 1,
273visibility = visibility,
274)
275
276# A filegroup for the `srcs`. That is, all the schema files for this
277# Flatbuffer set.
278native.filegroup(
279name = srcs_filegroup_name if srcs_filegroup_name else "%s_includes" % (name),
280srcs = srcs + includes,
281compatible_with = compatible_with,
282restricted_to = restricted_to,
283visibility = srcs_filegroup_visibility if srcs_filegroup_visibility != None else visibility,
284)
285