blob: da26cecff6f467314292fa0e2f1f0eab42e04e87 [file] [log] [blame]
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001# Description:
2# BUILD rules for generating flatbuffer files in various languages.
3
4"""
5Rules for building C++ flatbuffers with Bazel.
6"""
Austin Schuh7c75e582020-11-14 16:41:18 -08007
Alex Perry0d0aae32022-02-09 21:10:17 -08008load("@io_bazel_rules_go//go:def.bzl", "go_library")
Brian Silvermandae15a12022-07-23 12:55:20 -07009load("@rules_rust//rust:defs.bzl", "rust_library")
10load("@rules_rust//rust:rust_common.bzl", "CrateInfo")
Philipp Schrader175a93c2023-02-19 13:13:40 -080011load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
James Kuszmauldac091f2022-03-22 09:35:06 -070012load("@rules_cc//cc:defs.bzl", "cc_library")
Austin Schuhe89fa2d2019-08-14 20:24:23 -070013
14flatc_path = "@com_github_google_flatbuffers//:flatc"
15
16DEFAULT_INCLUDE_PATHS = [
17 "./",
Austin Schuhe89fa2d2019-08-14 20:24:23 -070018]
19
20DEFAULT_FLATC_ARGS = [
21 "--gen-object-api",
22 "--gen-compare",
Alex Perrycb7da4b2019-08-28 19:35:56 -070023 "--keep-prefix",
James Kuszmaul79794f22023-11-06 13:35:59 -080024 "--bfbs-builtins",
milind upadhyay328d3392020-11-25 19:34:00 -080025 "--cpp-std",
26 "c++17",
Austin Schuhf13042b2020-11-25 23:11:41 -080027 "--require-explicit-ids",
Austin Schuhe89fa2d2019-08-14 20:24:23 -070028 "--gen-mutable",
29 "--reflect-names",
Brian Silvermandae15a12022-07-23 12:55:20 -070030 "--cpp-ptr-type",
31 "flatbuffers::unique_ptr",
Alex Perrycb7da4b2019-08-28 19:35:56 -070032 "--force-empty",
Austin Schuh872723c2019-12-25 14:38:09 -080033 "--scoped-enums",
Alex Perrycb7da4b2019-08-28 19:35:56 -070034 "--gen-name-strings",
Austin Schuhe89fa2d2019-08-14 20:24:23 -070035]
36
Alex Perry0d0aae32022-02-09 21:10:17 -080037DEFAULT_FLATC_GO_ARGS = [
38 "--gen-onefile",
Philipp Schradercdb5cfc2022-02-20 14:57:07 -080039 "--gen-object-api",
Philipp Schrader2d4ad412022-02-25 15:56:20 -080040 "--require-explicit-ids",
Alex Perry0d0aae32022-02-09 21:10:17 -080041]
42
Brian Silvermandae15a12022-07-23 12:55:20 -070043DEFAULT_FLATC_RUST_ARGS = [
44 "--gen-object-api",
45 "--require-explicit-ids",
Brian Silverman90221f82022-08-22 23:46:09 -070046 "--gen-name-strings",
Brian Silvermandae15a12022-07-23 12:55:20 -070047]
48
Brian Silvermandae15a12022-07-23 12:55:20 -070049"""Contains information about a set of flatbuffers which have their code for
50reading/writing generated in a single library-style rule.
51
52Fields:
53 srcs: [File], the .fbs source files
54"""
55FlatbufferLibraryInfo = provider()
56
57def _flatbuffer_library_compile_impl(ctx):
58 outs = []
59 commands = []
60 for src in ctx.files.srcs:
61 if ctx.attr.tables_for_filenames:
62 out_dir = None
63 for table in ctx.attr.tables_for_filenames:
64 out = ctx.actions.declare_file(ctx.attr.out_prefix + table + ctx.attr.output_suffix)
65 this_out_dir = "/".join(out.dirname.split("/")[:-(len(ctx.attr.out_prefix.split("/")) - 1)])
66 if out_dir:
67 if this_out_dir != out_dir:
68 fail("Trying to write to multiple directories")
69 else:
70 out_dir = this_out_dir
71 outs.append(out)
72 else:
73 out = ctx.actions.declare_file(ctx.attr.out_prefix + src.basename.replace(".fbs", "") + ctx.attr.output_suffix)
74 outs.append(out)
75 out_dir = out.dirname
76 arguments = [ctx.executable._flatc.path]
77 for path in ctx.attr.include_paths:
78 for subpath in ["", ctx.bin_dir.path + "/"]:
79 arguments.append("-I")
80 arguments.append(subpath + path)
81 arguments.append("-I")
82 arguments.append("%s.runfiles/com_github_google_flatbuffers" % ctx.executable._flatc.path)
83 arguments.extend(ctx.attr.flatc_args)
84 arguments.extend(ctx.attr.language_flags)
85 arguments.extend([
86 "-o",
87 out_dir,
88 ])
89 arguments.append(src.path)
90 commands.append(arguments)
91 ctx.actions.run_shell(
92 outputs = outs,
93 inputs = ctx.files.srcs + ctx.files.includes,
94 tools = [ctx.executable._flatc],
95 command = " && ".join([" ".join(arguments) for arguments in commands]),
96 mnemonic = "Flatc",
97 progress_message = "Generating flatbuffer files for %{input}:",
98 )
Philipp Schrader46f40042024-01-24 17:16:03 -080099 return [DefaultInfo(files = depset(outs)), FlatbufferLibraryInfo(srcs = ctx.files.srcs)]
Brian Silvermandae15a12022-07-23 12:55:20 -0700100
101_flatbuffer_library_compile = rule(
102 implementation = _flatbuffer_library_compile_impl,
103 attrs = {
104 "srcs": attr.label_list(mandatory = True, allow_files = True),
105 "output_suffix": attr.string(mandatory = True),
106 "tables_for_filenames": attr.string_list(mandatory = False),
107 "language_flags": attr.string_list(mandatory = True),
108 "includes": attr.label_list(default = [], allow_files = True),
109 "include_paths": attr.string_list(default = []),
110 "flatc_args": attr.string_list(default = []),
111 "out_prefix": attr.string(default = ""),
112 "_flatc": attr.label(executable = True, cfg = "exec", default = Label(flatc_path)),
113 },
114)
115
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700116def flatbuffer_library_public(
117 name,
118 srcs,
Brian Silvermandae15a12022-07-23 12:55:20 -0700119 output_suffix,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700120 language_flag,
121 out_prefix = "",
Brian Silvermandae15a12022-07-23 12:55:20 -0700122 tables_for_filenames = None,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700123 includes = [],
124 include_paths = DEFAULT_INCLUDE_PATHS,
125 flatc_args = DEFAULT_FLATC_ARGS,
126 reflection_name = "",
127 reflection_visibility = None,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700128 compatible_with = None,
Austin Schuh7c75e582020-11-14 16:41:18 -0800129 restricted_to = None,
Philipp Schraderdada1072020-11-24 11:34:46 -0800130 target_compatible_with = None,
Brian Silvermandae15a12022-07-23 12:55:20 -0700131 output_to_bindir = False,
132 visibility = None):
Philipp Schraderdada1072020-11-24 11:34:46 -0800133 """Generates code files for reading/writing the given flatbuffers in the
134 requested language using the public compiler.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700135
136 Args:
137 name: Rule name.
138 srcs: Source .fbs files. Sent in order to the compiler.
Brian Silvermandae15a12022-07-23 12:55:20 -0700139 output_suffix: Suffix for output files from flatc.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700140 language_flag: Target language flag. One of [-c, -j, -js].
141 out_prefix: Prepend this path to the front of all generated files except on
142 single source targets. Usually is a directory name.
143 includes: Optional, list of filegroups of schemas that the srcs depend on.
144 include_paths: Optional, list of paths the includes files can be found in.
145 flatc_args: Optional, list of additional arguments to pass to flatc.
146 reflection_name: Optional, if set this will generate the flatbuffer
147 reflection binaries for the schemas.
148 reflection_visibility: The visibility of the generated reflection Fileset.
149 output_to_bindir: Passed to genrule for output to bin directory.
Austin Schuh7c75e582020-11-14 16:41:18 -0800150 compatible_with: Optional, The list of environments this rule can be
151 built for, in addition to default-supported environments.
152 restricted_to: Optional, The list of environments this rule can be built
153 for, instead of default-supported environments.
James Kuszmauldac091f2022-03-22 09:35:06 -0700154 target_compatible_with: Optional, The list of target platform constraints
155 to use.
Austin Schuh7c75e582020-11-14 16:41:18 -0800156 output_to_bindir: Passed to genrule for output to bin directory.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700157
158
159 This rule creates a filegroup(name) with all generated source files, and
160 optionally a Fileset([reflection_name]) with all generated reflection
161 binaries.
162 """
Brian Silvermandae15a12022-07-23 12:55:20 -0700163 _flatbuffer_library_compile(
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700164 name = name,
Brian Silvermandae15a12022-07-23 12:55:20 -0700165 srcs = srcs,
166 output_suffix = output_suffix,
167 language_flags = [language_flag],
168 includes = includes,
169 include_paths = include_paths,
170 flatc_args = flatc_args,
171 out_prefix = out_prefix,
172 tables_for_filenames = tables_for_filenames,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700173 compatible_with = compatible_with,
Philipp Schraderdada1072020-11-24 11:34:46 -0800174 target_compatible_with = target_compatible_with,
James Kuszmauldac091f2022-03-22 09:35:06 -0700175 restricted_to = restricted_to,
Brian Silvermandae15a12022-07-23 12:55:20 -0700176 visibility = visibility,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700177 )
Brian Silvermandae15a12022-07-23 12:55:20 -0700178
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700179 if reflection_name:
Brian Silvermandae15a12022-07-23 12:55:20 -0700180 _flatbuffer_library_compile(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700181 name = "%s_out" % reflection_name,
Brian Silvermandae15a12022-07-23 12:55:20 -0700182 srcs = srcs,
183 output_suffix = ".bfbs",
184 language_flags = ["-b", "--schema"],
185 includes = includes,
186 include_paths = include_paths,
187 flatc_args = flatc_args,
188 out_prefix = out_prefix,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700189 compatible_with = compatible_with,
Brian Silvermandae15a12022-07-23 12:55:20 -0700190 target_compatible_with = target_compatible_with,
Austin Schuh7c75e582020-11-14 16:41:18 -0800191 restricted_to = restricted_to,
Brian Silvermandae15a12022-07-23 12:55:20 -0700192 visibility = reflection_visibility,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700193 )
194
195def flatbuffer_cc_library(
196 name,
197 srcs,
198 srcs_filegroup_name = "",
199 out_prefix = "",
James Kuszmauldac091f2022-03-22 09:35:06 -0700200 deps = [],
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700201 includes = [],
202 include_paths = DEFAULT_INCLUDE_PATHS,
James Kuszmauldac091f2022-03-22 09:35:06 -0700203 cc_include_paths = [],
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700204 flatc_args = DEFAULT_FLATC_ARGS,
205 visibility = None,
Austin Schuh7c75e582020-11-14 16:41:18 -0800206 compatible_with = None,
207 restricted_to = None,
Philipp Schraderdada1072020-11-24 11:34:46 -0800208 target_compatible_with = None,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700209 srcs_filegroup_visibility = None,
210 gen_reflections = False):
James Kuszmauldac091f2022-03-22 09:35:06 -0700211 """A cc_library with the generated reader/writers for the given flatbuffer definitions.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700212
213 Args:
214 name: Rule name.
215 srcs: Source .fbs files. Sent in order to the compiler.
216 srcs_filegroup_name: Name of the output filegroup that holds srcs. Pass this
217 filegroup into the `includes` parameter of any other
218 flatbuffer_cc_library that depends on this one's schemas.
219 out_prefix: Prepend this path to the front of all generated files. Usually
220 is a directory name.
James Kuszmauldac091f2022-03-22 09:35:06 -0700221 deps: Optional, list of other flatbuffer_cc_library's to depend on. Cannot be specified
222 alongside includes.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700223 includes: Optional, list of filegroups of schemas that the srcs depend on.
James Kuszmauldac091f2022-03-22 09:35:06 -0700224 Use of this is discouraged, and may be deprecated.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700225 include_paths: Optional, list of paths the includes files can be found in.
James Kuszmauldac091f2022-03-22 09:35:06 -0700226 cc_include_paths: Optional, list of paths to add to the cc_library includes attribute.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700227 flatc_args: Optional list of additional arguments to pass to flatc
228 (e.g. --gen-mutable).
229 visibility: The visibility of the generated cc_library. By default, use the
230 default visibility of the project.
Philipp Schraderdada1072020-11-24 11:34:46 -0800231 target_compatible_with: Optional, the list of constraints the target
232 platform must satisfy for this target to be considered compatible.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700233 srcs_filegroup_visibility: The visibility of the generated srcs filegroup.
234 By default, use the value of the visibility parameter above.
235 gen_reflections: Optional, if true this will generate the flatbuffer
236 reflection binaries for the schemas.
Austin Schuh7c75e582020-11-14 16:41:18 -0800237 compatible_with: Optional, The list of environments this rule can be built
238 for, in addition to default-supported environments.
239 restricted_to: Optional, The list of environments this rule can be built
240 for, instead of default-supported environments.
James Kuszmauldac091f2022-03-22 09:35:06 -0700241 target_compatible_with: Optional, The list of target platform constraints
242 to use.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700243
244 This produces:
245 filegroup([name]_srcs): all generated .h files.
246 filegroup(srcs_filegroup_name if specified, or [name]_includes if not):
247 Other flatbuffer_cc_library's can pass this in for their `includes`
248 parameter, if they depend on the schemas in this library.
249 Fileset([name]_reflection): (Optional) all generated reflection binaries.
250 cc_library([name]): library with sources and flatbuffers deps.
James Kuszmauldac091f2022-03-22 09:35:06 -0700251 """
James Kuszmauldac091f2022-03-22 09:35:06 -0700252 if deps and includes:
253 # There is no inherent reason we couldn't support both, but this discourages
254 # use of includes without good reason.
255 fail("Cannot specify both deps and include in flatbuffer_cc_library.")
256 if deps:
257 includes = [d + "_includes" for d in deps]
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700258 reflection_name = "%s_reflection" % name if gen_reflections else ""
259
260 srcs_lib = "%s_srcs" % (name)
261 flatbuffer_library_public(
262 name = srcs_lib,
263 srcs = srcs,
Brian Silvermandae15a12022-07-23 12:55:20 -0700264 output_suffix = "_generated.h",
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700265 language_flag = "-c",
266 out_prefix = out_prefix,
267 includes = includes,
268 include_paths = include_paths,
269 flatc_args = flatc_args,
Austin Schuh7c75e582020-11-14 16:41:18 -0800270 compatible_with = compatible_with,
271 restricted_to = restricted_to,
James Kuszmauldac091f2022-03-22 09:35:06 -0700272 target_compatible_with = target_compatible_with,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700273 reflection_name = reflection_name,
274 reflection_visibility = visibility,
275 )
276 native.cc_library(
277 name = name,
278 hdrs = [
279 ":" + srcs_lib,
280 ],
281 srcs = [
282 ":" + srcs_lib,
283 ],
284 features = [
285 "-parse_headers",
286 ],
287 deps = [
288 "@com_github_google_flatbuffers//:runtime_cc",
James Kuszmauldac091f2022-03-22 09:35:06 -0700289 "@com_github_google_flatbuffers//:flatbuffers",
290 ] + deps,
291 includes = cc_include_paths,
Austin Schuh7c75e582020-11-14 16:41:18 -0800292 compatible_with = compatible_with,
293 restricted_to = restricted_to,
Philipp Schraderdada1072020-11-24 11:34:46 -0800294 target_compatible_with = target_compatible_with,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700295 linkstatic = 1,
296 visibility = visibility,
297 )
298
299 # A filegroup for the `srcs`. That is, all the schema files for this
300 # Flatbuffer set.
301 native.filegroup(
302 name = srcs_filegroup_name if srcs_filegroup_name else "%s_includes" % (name),
James Kuszmauldac091f2022-03-22 09:35:06 -0700303 srcs = srcs + includes,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700304 compatible_with = compatible_with,
Austin Schuh7c75e582020-11-14 16:41:18 -0800305 restricted_to = restricted_to,
306 visibility = srcs_filegroup_visibility if srcs_filegroup_visibility != None else visibility,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700307 )
James Kuszmaulf385c462019-12-24 09:37:34 -0800308
Brian Silverman28760272020-02-02 13:21:51 -0800309def flatbuffer_py_library(
James Kuszmaulf385c462019-12-24 09:37:34 -0800310 name,
311 srcs,
312 namespace,
313 tables,
314 compatible_with = None,
Philipp Schraderdada1072020-11-24 11:34:46 -0800315 target_compatible_with = None,
James Kuszmaulf385c462019-12-24 09:37:34 -0800316 includes = [],
317 include_paths = DEFAULT_INCLUDE_PATHS,
318 flatc_args = DEFAULT_FLATC_ARGS,
319 visibility = None,
320 srcs_filegroup_visibility = None):
321 """Generates a py_library rule for a given flatbuffer definition.
322
323 Args:
324 name: Name of the generated py_library rule.
325 srcs: Source .fbs file(s).
326 namespace: Namespace of the specified flatbuffer schema. Until
327 we make the rules sophisticated enough to figure out what
328 python files will be generated from a given schema, the user
329 has to manually specify this.
330 tables: List of table names--currently, we don't do anything to
331 automatically figure out how to handle the fact that a separate
332 python file will be generated for every table definition, and that
333 we can't know what files will be output until after the file has
334 been parsed. As such, we just force the user to manually specify
335 things.
336 """
James Kuszmaulf385c462019-12-24 09:37:34 -0800337
338 srcs_lib = "%s_srcs" % (name)
Brian Silvermandae15a12022-07-23 12:55:20 -0700339 if not tables:
340 fail("Must specify the list of tables")
James Kuszmaulf385c462019-12-24 09:37:34 -0800341 flatbuffer_library_public(
342 name = srcs_lib,
343 srcs = srcs,
Brian Silvermandae15a12022-07-23 12:55:20 -0700344 output_suffix = ".py",
345 out_prefix = namespace.replace(".", "/") + "/",
346 tables_for_filenames = tables,
James Kuszmaulf385c462019-12-24 09:37:34 -0800347 language_flag = "--python",
348 includes = includes,
349 include_paths = include_paths,
350 flatc_args = flatc_args,
351 compatible_with = compatible_with,
Philipp Schraderdada1072020-11-24 11:34:46 -0800352 target_compatible_with = target_compatible_with,
Brian Silvermandae15a12022-07-23 12:55:20 -0700353 visibility = ["//visibility:private"],
James Kuszmaulf385c462019-12-24 09:37:34 -0800354 )
355 native.py_library(
356 name = name,
Brian Silvermandae15a12022-07-23 12:55:20 -0700357 srcs = [srcs_lib],
James Kuszmaulf385c462019-12-24 09:37:34 -0800358 visibility = visibility,
359 compatible_with = compatible_with,
Philipp Schraderdada1072020-11-24 11:34:46 -0800360 target_compatible_with = target_compatible_with,
James Kuszmaulf385c462019-12-24 09:37:34 -0800361 imports = ["."],
362 deps = ["@com_github_google_flatbuffers//:flatpy"],
363 )
Alex Perryb3b50792020-01-18 16:13:45 -0800364
Alex Perry0d0aae32022-02-09 21:10:17 -0800365def flatbuffer_go_library(
366 name,
367 srcs,
368 importpath,
369 compatible_with = None,
370 target_compatible_with = None,
371 includes = [],
372 include_paths = DEFAULT_INCLUDE_PATHS,
373 flatc_args = DEFAULT_FLATC_GO_ARGS,
374 visibility = None,
375 srcs_filegroup_visibility = None):
376 srcs_lib = "%s_srcs" % (name)
Alex Perry0d0aae32022-02-09 21:10:17 -0800377 flatc_args = flatc_args + ["--go-namespace", importpath.split("/")[-1]]
378
379 flatbuffer_library_public(
380 name = srcs_lib,
381 srcs = srcs,
Brian Silvermandae15a12022-07-23 12:55:20 -0700382 output_suffix = "_generated.go",
Alex Perry0d0aae32022-02-09 21:10:17 -0800383 language_flag = "--go",
384 includes = includes,
385 include_paths = include_paths,
386 flatc_args = flatc_args,
387 compatible_with = compatible_with,
388 target_compatible_with = target_compatible_with,
Brian Silvermandae15a12022-07-23 12:55:20 -0700389 visibility = ["//visibility:private"],
Alex Perry0d0aae32022-02-09 21:10:17 -0800390 )
391 go_library(
392 name = name,
Brian Silvermandae15a12022-07-23 12:55:20 -0700393 srcs = [srcs_lib],
Alex Perry0d0aae32022-02-09 21:10:17 -0800394 deps = ["@com_github_google_flatbuffers//go"],
395 importpath = importpath,
396 visibility = visibility,
397 compatible_with = compatible_with,
398 target_compatible_with = target_compatible_with,
399 )
400
Brian Silvermandae15a12022-07-23 12:55:20 -0700401def _flatbuffer_rust_lib_gen_impl(ctx):
402 # TODO(Brian): I think this needs changes to properly handle multiple .fbs files in a rule.
403 uses = []
404 for (dep, dep_srcs) in zip(ctx.attr.deps, ctx.attr.dep_srcs):
405 for dep_src in dep_srcs[FlatbufferLibraryInfo].srcs:
406 uses.append((dep[CrateInfo].name, dep_src.basename.replace(".fbs", "_generated")))
407 lib_rs_content = "\n".join(
408 [
409 "// Automatically generated by the Flatbuffers Bazel rules. Do not modify",
410 "#![allow(unused_imports)]",
411 ] + ["use %s as %s;" % (crate, use_as) for (crate, use_as) in uses] +
412 ["include!(\"%s\");" % src.basename for src in ctx.files.srcs_lib],
413 )
414 output = ctx.actions.declare_file(ctx.attr.name + "_lib.rs")
415 ctx.actions.write(
416 output = output,
417 content = lib_rs_content,
418 )
419 return [DefaultInfo(files = depset([output]))]
420
421"""Generates a lib.rs for a flatbuffer_rust_library.
422
423flatc generates individual .rs files for us. It can also generate a top-level mod.rs to be included
424in a crate, but that is laid out to include all flatbuffers files in a project. That's not a good
425fit for Bazel rules and monorepos, so we generate an alternative that imports all dependencies under
426their expected names."""
427_flatbuffer_rust_lib_gen = rule(
428 implementation = _flatbuffer_rust_lib_gen_impl,
429 attrs = {
430 "srcs_lib": attr.label(mandatory = True, doc = "The generated srcs for this rule"),
431 "dep_srcs": attr.label_list(mandatory = True, providers = [FlatbufferLibraryInfo], doc = "The _srcs rules for all our direct dependencies"),
432 "deps": attr.label_list(mandatory = True, providers = [CrateInfo]),
433 },
434)
435
436def flatbuffer_rust_library(
437 name,
438 srcs,
439 compatible_with = None,
440 target_compatible_with = None,
441 deps = [],
442 include_paths = DEFAULT_INCLUDE_PATHS,
443 flatc_args = DEFAULT_FLATC_RUST_ARGS,
444 include_reflection = True,
445 crate_name = None,
446 visibility = None,
447 srcs_filegroup_visibility = None):
448 includes = [d + "_includes" for d in deps]
449 srcs_lib = "%s_srcs" % (name)
450 lib_gen = "%s_lib_gen" % (name)
451 deps = list(deps)
452 if include_reflection:
453 deps.append("@com_github_google_flatbuffers//reflection:reflection_rust_fbs")
454
455 flatbuffer_library_public(
456 name = srcs_lib,
457 srcs = srcs,
458 language_flag = "--rust",
459 output_suffix = "_generated.rs",
460 includes = includes,
461 include_paths = include_paths,
462 flatc_args = flatc_args,
463 compatible_with = compatible_with,
464 target_compatible_with = target_compatible_with,
465 visibility = visibility,
466 )
467 _flatbuffer_rust_lib_gen(
468 name = lib_gen,
469 deps = deps,
470 dep_srcs = [dep + "_srcs" for dep in deps],
471 srcs_lib = srcs_lib,
472 visibility = ["//visibility:private"],
473 compatible_with = compatible_with,
474 target_compatible_with = target_compatible_with,
475 )
476 rust_library(
477 name = name,
478 srcs = [srcs_lib, lib_gen],
479 crate_root = lib_gen,
480 crate_name = crate_name,
481 deps = ["@com_github_google_flatbuffers//rust"] + deps,
482 edition = "2018",
483 visibility = visibility,
484 compatible_with = compatible_with,
485 target_compatible_with = target_compatible_with,
486 )