blob: c1e568f072bdb0848affc22900b07e97b5623489 [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",
milind upadhyay328d3392020-11-25 19:34:00 -080024 "--cpp-std",
25 "c++17",
Austin Schuhf13042b2020-11-25 23:11:41 -080026 "--require-explicit-ids",
Austin Schuhe89fa2d2019-08-14 20:24:23 -070027 "--gen-mutable",
28 "--reflect-names",
Brian Silvermandae15a12022-07-23 12:55:20 -070029 "--cpp-ptr-type",
30 "flatbuffers::unique_ptr",
Alex Perrycb7da4b2019-08-28 19:35:56 -070031 "--force-empty",
Austin Schuh872723c2019-12-25 14:38:09 -080032 "--scoped-enums",
Alex Perrycb7da4b2019-08-28 19:35:56 -070033 "--gen-name-strings",
Austin Schuhe89fa2d2019-08-14 20:24:23 -070034]
35
Alex Perry0d0aae32022-02-09 21:10:17 -080036DEFAULT_FLATC_GO_ARGS = [
37 "--gen-onefile",
Philipp Schradercdb5cfc2022-02-20 14:57:07 -080038 "--gen-object-api",
Philipp Schrader2d4ad412022-02-25 15:56:20 -080039 "--require-explicit-ids",
Alex Perry0d0aae32022-02-09 21:10:17 -080040]
41
Brian Silvermandae15a12022-07-23 12:55:20 -070042DEFAULT_FLATC_RUST_ARGS = [
43 "--gen-object-api",
44 "--require-explicit-ids",
Brian Silverman90221f82022-08-22 23:46:09 -070045 "--gen-name-strings",
Brian Silvermandae15a12022-07-23 12:55:20 -070046]
47
Brian Silvermandae15a12022-07-23 12:55:20 -070048"""Contains information about a set of flatbuffers which have their code for
49reading/writing generated in a single library-style rule.
50
51Fields:
52 srcs: [File], the .fbs source files
53"""
54FlatbufferLibraryInfo = provider()
55
56def _flatbuffer_library_compile_impl(ctx):
57 outs = []
58 commands = []
59 for src in ctx.files.srcs:
60 if ctx.attr.tables_for_filenames:
61 out_dir = None
62 for table in ctx.attr.tables_for_filenames:
63 out = ctx.actions.declare_file(ctx.attr.out_prefix + table + ctx.attr.output_suffix)
64 this_out_dir = "/".join(out.dirname.split("/")[:-(len(ctx.attr.out_prefix.split("/")) - 1)])
65 if out_dir:
66 if this_out_dir != out_dir:
67 fail("Trying to write to multiple directories")
68 else:
69 out_dir = this_out_dir
70 outs.append(out)
71 else:
72 out = ctx.actions.declare_file(ctx.attr.out_prefix + src.basename.replace(".fbs", "") + ctx.attr.output_suffix)
73 outs.append(out)
74 out_dir = out.dirname
75 arguments = [ctx.executable._flatc.path]
76 for path in ctx.attr.include_paths:
77 for subpath in ["", ctx.bin_dir.path + "/"]:
78 arguments.append("-I")
79 arguments.append(subpath + path)
80 arguments.append("-I")
81 arguments.append("%s.runfiles/com_github_google_flatbuffers" % ctx.executable._flatc.path)
82 arguments.extend(ctx.attr.flatc_args)
83 arguments.extend(ctx.attr.language_flags)
84 arguments.extend([
85 "-o",
86 out_dir,
87 ])
88 arguments.append(src.path)
89 commands.append(arguments)
90 ctx.actions.run_shell(
91 outputs = outs,
92 inputs = ctx.files.srcs + ctx.files.includes,
93 tools = [ctx.executable._flatc],
94 command = " && ".join([" ".join(arguments) for arguments in commands]),
95 mnemonic = "Flatc",
96 progress_message = "Generating flatbuffer files for %{input}:",
97 )
98 return [DefaultInfo(files = depset(outs), runfiles = ctx.runfiles(files = outs)), FlatbufferLibraryInfo(srcs = ctx.files.srcs)]
99
100_flatbuffer_library_compile = rule(
101 implementation = _flatbuffer_library_compile_impl,
102 attrs = {
103 "srcs": attr.label_list(mandatory = True, allow_files = True),
104 "output_suffix": attr.string(mandatory = True),
105 "tables_for_filenames": attr.string_list(mandatory = False),
106 "language_flags": attr.string_list(mandatory = True),
107 "includes": attr.label_list(default = [], allow_files = True),
108 "include_paths": attr.string_list(default = []),
109 "flatc_args": attr.string_list(default = []),
110 "out_prefix": attr.string(default = ""),
111 "_flatc": attr.label(executable = True, cfg = "exec", default = Label(flatc_path)),
112 },
113)
114
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700115def flatbuffer_library_public(
116 name,
117 srcs,
Brian Silvermandae15a12022-07-23 12:55:20 -0700118 output_suffix,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700119 language_flag,
120 out_prefix = "",
Brian Silvermandae15a12022-07-23 12:55:20 -0700121 tables_for_filenames = None,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700122 includes = [],
123 include_paths = DEFAULT_INCLUDE_PATHS,
124 flatc_args = DEFAULT_FLATC_ARGS,
125 reflection_name = "",
126 reflection_visibility = None,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700127 compatible_with = None,
Austin Schuh7c75e582020-11-14 16:41:18 -0800128 restricted_to = None,
Philipp Schraderdada1072020-11-24 11:34:46 -0800129 target_compatible_with = None,
Brian Silvermandae15a12022-07-23 12:55:20 -0700130 output_to_bindir = False,
131 visibility = None):
Philipp Schraderdada1072020-11-24 11:34:46 -0800132 """Generates code files for reading/writing the given flatbuffers in the
133 requested language using the public compiler.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700134
135 Args:
136 name: Rule name.
137 srcs: Source .fbs files. Sent in order to the compiler.
Brian Silvermandae15a12022-07-23 12:55:20 -0700138 output_suffix: Suffix for output files from flatc.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700139 language_flag: Target language flag. One of [-c, -j, -js].
140 out_prefix: Prepend this path to the front of all generated files except on
141 single source targets. Usually is a directory name.
142 includes: Optional, list of filegroups of schemas that the srcs depend on.
143 include_paths: Optional, list of paths the includes files can be found in.
144 flatc_args: Optional, list of additional arguments to pass to flatc.
145 reflection_name: Optional, if set this will generate the flatbuffer
146 reflection binaries for the schemas.
147 reflection_visibility: The visibility of the generated reflection Fileset.
148 output_to_bindir: Passed to genrule for output to bin directory.
Austin Schuh7c75e582020-11-14 16:41:18 -0800149 compatible_with: Optional, The list of environments this rule can be
150 built for, in addition to default-supported environments.
151 restricted_to: Optional, The list of environments this rule can be built
152 for, instead of default-supported environments.
James Kuszmauldac091f2022-03-22 09:35:06 -0700153 target_compatible_with: Optional, The list of target platform constraints
154 to use.
Austin Schuh7c75e582020-11-14 16:41:18 -0800155 output_to_bindir: Passed to genrule for output to bin directory.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700156
157
158 This rule creates a filegroup(name) with all generated source files, and
159 optionally a Fileset([reflection_name]) with all generated reflection
160 binaries.
161 """
Brian Silvermandae15a12022-07-23 12:55:20 -0700162 _flatbuffer_library_compile(
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700163 name = name,
Brian Silvermandae15a12022-07-23 12:55:20 -0700164 srcs = srcs,
165 output_suffix = output_suffix,
166 language_flags = [language_flag],
167 includes = includes,
168 include_paths = include_paths,
169 flatc_args = flatc_args,
170 out_prefix = out_prefix,
171 tables_for_filenames = tables_for_filenames,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700172 compatible_with = compatible_with,
Philipp Schraderdada1072020-11-24 11:34:46 -0800173 target_compatible_with = target_compatible_with,
James Kuszmauldac091f2022-03-22 09:35:06 -0700174 restricted_to = restricted_to,
Brian Silvermandae15a12022-07-23 12:55:20 -0700175 visibility = visibility,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700176 )
Brian Silvermandae15a12022-07-23 12:55:20 -0700177
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700178 if reflection_name:
Brian Silvermandae15a12022-07-23 12:55:20 -0700179 _flatbuffer_library_compile(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700180 name = "%s_out" % reflection_name,
Brian Silvermandae15a12022-07-23 12:55:20 -0700181 srcs = srcs,
182 output_suffix = ".bfbs",
183 language_flags = ["-b", "--schema"],
184 includes = includes,
185 include_paths = include_paths,
186 flatc_args = flatc_args,
187 out_prefix = out_prefix,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700188 compatible_with = compatible_with,
Brian Silvermandae15a12022-07-23 12:55:20 -0700189 target_compatible_with = target_compatible_with,
Austin Schuh7c75e582020-11-14 16:41:18 -0800190 restricted_to = restricted_to,
Brian Silvermandae15a12022-07-23 12:55:20 -0700191 visibility = reflection_visibility,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700192 )
193
194def flatbuffer_cc_library(
195 name,
196 srcs,
197 srcs_filegroup_name = "",
198 out_prefix = "",
James Kuszmauldac091f2022-03-22 09:35:06 -0700199 deps = [],
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700200 includes = [],
201 include_paths = DEFAULT_INCLUDE_PATHS,
James Kuszmauldac091f2022-03-22 09:35:06 -0700202 cc_include_paths = [],
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700203 flatc_args = DEFAULT_FLATC_ARGS,
204 visibility = None,
Austin Schuh7c75e582020-11-14 16:41:18 -0800205 compatible_with = None,
206 restricted_to = None,
Philipp Schraderdada1072020-11-24 11:34:46 -0800207 target_compatible_with = None,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700208 srcs_filegroup_visibility = None,
209 gen_reflections = False):
James Kuszmauldac091f2022-03-22 09:35:06 -0700210 """A cc_library with the generated reader/writers for the given flatbuffer definitions.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700211
212 Args:
213 name: Rule name.
214 srcs: Source .fbs files. Sent in order to the compiler.
215 srcs_filegroup_name: Name of the output filegroup that holds srcs. Pass this
216 filegroup into the `includes` parameter of any other
217 flatbuffer_cc_library that depends on this one's schemas.
218 out_prefix: Prepend this path to the front of all generated files. Usually
219 is a directory name.
James Kuszmauldac091f2022-03-22 09:35:06 -0700220 deps: Optional, list of other flatbuffer_cc_library's to depend on. Cannot be specified
221 alongside includes.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700222 includes: Optional, list of filegroups of schemas that the srcs depend on.
James Kuszmauldac091f2022-03-22 09:35:06 -0700223 Use of this is discouraged, and may be deprecated.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700224 include_paths: Optional, list of paths the includes files can be found in.
James Kuszmauldac091f2022-03-22 09:35:06 -0700225 cc_include_paths: Optional, list of paths to add to the cc_library includes attribute.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700226 flatc_args: Optional list of additional arguments to pass to flatc
227 (e.g. --gen-mutable).
228 visibility: The visibility of the generated cc_library. By default, use the
229 default visibility of the project.
Philipp Schraderdada1072020-11-24 11:34:46 -0800230 target_compatible_with: Optional, the list of constraints the target
231 platform must satisfy for this target to be considered compatible.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700232 srcs_filegroup_visibility: The visibility of the generated srcs filegroup.
233 By default, use the value of the visibility parameter above.
234 gen_reflections: Optional, if true this will generate the flatbuffer
235 reflection binaries for the schemas.
Austin Schuh7c75e582020-11-14 16:41:18 -0800236 compatible_with: Optional, The list of environments this rule can be built
237 for, in addition to default-supported environments.
238 restricted_to: Optional, The list of environments this rule can be built
239 for, instead of default-supported environments.
James Kuszmauldac091f2022-03-22 09:35:06 -0700240 target_compatible_with: Optional, The list of target platform constraints
241 to use.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700242
243 This produces:
244 filegroup([name]_srcs): all generated .h files.
245 filegroup(srcs_filegroup_name if specified, or [name]_includes if not):
246 Other flatbuffer_cc_library's can pass this in for their `includes`
247 parameter, if they depend on the schemas in this library.
248 Fileset([name]_reflection): (Optional) all generated reflection binaries.
249 cc_library([name]): library with sources and flatbuffers deps.
James Kuszmauldac091f2022-03-22 09:35:06 -0700250 """
James Kuszmauldac091f2022-03-22 09:35:06 -0700251 if deps and includes:
252 # There is no inherent reason we couldn't support both, but this discourages
253 # use of includes without good reason.
254 fail("Cannot specify both deps and include in flatbuffer_cc_library.")
255 if deps:
256 includes = [d + "_includes" for d in deps]
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700257 reflection_name = "%s_reflection" % name if gen_reflections else ""
258
259 srcs_lib = "%s_srcs" % (name)
260 flatbuffer_library_public(
261 name = srcs_lib,
262 srcs = srcs,
Brian Silvermandae15a12022-07-23 12:55:20 -0700263 output_suffix = "_generated.h",
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700264 language_flag = "-c",
265 out_prefix = out_prefix,
266 includes = includes,
267 include_paths = include_paths,
268 flatc_args = flatc_args,
Austin Schuh7c75e582020-11-14 16:41:18 -0800269 compatible_with = compatible_with,
270 restricted_to = restricted_to,
James Kuszmauldac091f2022-03-22 09:35:06 -0700271 target_compatible_with = target_compatible_with,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700272 reflection_name = reflection_name,
273 reflection_visibility = visibility,
274 )
275 native.cc_library(
276 name = name,
277 hdrs = [
278 ":" + srcs_lib,
279 ],
280 srcs = [
281 ":" + srcs_lib,
282 ],
283 features = [
284 "-parse_headers",
285 ],
286 deps = [
287 "@com_github_google_flatbuffers//:runtime_cc",
James Kuszmauldac091f2022-03-22 09:35:06 -0700288 "@com_github_google_flatbuffers//:flatbuffers",
289 ] + deps,
290 includes = cc_include_paths,
Austin Schuh7c75e582020-11-14 16:41:18 -0800291 compatible_with = compatible_with,
292 restricted_to = restricted_to,
Philipp Schraderdada1072020-11-24 11:34:46 -0800293 target_compatible_with = target_compatible_with,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700294 linkstatic = 1,
295 visibility = visibility,
296 )
297
298 # A filegroup for the `srcs`. That is, all the schema files for this
299 # Flatbuffer set.
300 native.filegroup(
301 name = srcs_filegroup_name if srcs_filegroup_name else "%s_includes" % (name),
James Kuszmauldac091f2022-03-22 09:35:06 -0700302 srcs = srcs + includes,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700303 compatible_with = compatible_with,
Austin Schuh7c75e582020-11-14 16:41:18 -0800304 restricted_to = restricted_to,
305 visibility = srcs_filegroup_visibility if srcs_filegroup_visibility != None else visibility,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700306 )
James Kuszmaulf385c462019-12-24 09:37:34 -0800307
Brian Silverman28760272020-02-02 13:21:51 -0800308def flatbuffer_py_library(
James Kuszmaulf385c462019-12-24 09:37:34 -0800309 name,
310 srcs,
311 namespace,
312 tables,
313 compatible_with = None,
Philipp Schraderdada1072020-11-24 11:34:46 -0800314 target_compatible_with = None,
James Kuszmaulf385c462019-12-24 09:37:34 -0800315 includes = [],
316 include_paths = DEFAULT_INCLUDE_PATHS,
317 flatc_args = DEFAULT_FLATC_ARGS,
318 visibility = None,
319 srcs_filegroup_visibility = None):
320 """Generates a py_library rule for a given flatbuffer definition.
321
322 Args:
323 name: Name of the generated py_library rule.
324 srcs: Source .fbs file(s).
325 namespace: Namespace of the specified flatbuffer schema. Until
326 we make the rules sophisticated enough to figure out what
327 python files will be generated from a given schema, the user
328 has to manually specify this.
329 tables: List of table names--currently, we don't do anything to
330 automatically figure out how to handle the fact that a separate
331 python file will be generated for every table definition, and that
332 we can't know what files will be output until after the file has
333 been parsed. As such, we just force the user to manually specify
334 things.
335 """
James Kuszmaulf385c462019-12-24 09:37:34 -0800336
337 srcs_lib = "%s_srcs" % (name)
Brian Silvermandae15a12022-07-23 12:55:20 -0700338 if not tables:
339 fail("Must specify the list of tables")
James Kuszmaulf385c462019-12-24 09:37:34 -0800340 flatbuffer_library_public(
341 name = srcs_lib,
342 srcs = srcs,
Brian Silvermandae15a12022-07-23 12:55:20 -0700343 output_suffix = ".py",
344 out_prefix = namespace.replace(".", "/") + "/",
345 tables_for_filenames = tables,
James Kuszmaulf385c462019-12-24 09:37:34 -0800346 language_flag = "--python",
347 includes = includes,
348 include_paths = include_paths,
349 flatc_args = flatc_args,
350 compatible_with = compatible_with,
Philipp Schraderdada1072020-11-24 11:34:46 -0800351 target_compatible_with = target_compatible_with,
Brian Silvermandae15a12022-07-23 12:55:20 -0700352 visibility = ["//visibility:private"],
James Kuszmaulf385c462019-12-24 09:37:34 -0800353 )
354 native.py_library(
355 name = name,
Brian Silvermandae15a12022-07-23 12:55:20 -0700356 srcs = [srcs_lib],
James Kuszmaulf385c462019-12-24 09:37:34 -0800357 visibility = visibility,
358 compatible_with = compatible_with,
Philipp Schraderdada1072020-11-24 11:34:46 -0800359 target_compatible_with = target_compatible_with,
James Kuszmaulf385c462019-12-24 09:37:34 -0800360 imports = ["."],
361 deps = ["@com_github_google_flatbuffers//:flatpy"],
362 )
Alex Perryb3b50792020-01-18 16:13:45 -0800363
Alex Perry0d0aae32022-02-09 21:10:17 -0800364def flatbuffer_go_library(
365 name,
366 srcs,
367 importpath,
368 compatible_with = None,
369 target_compatible_with = None,
370 includes = [],
371 include_paths = DEFAULT_INCLUDE_PATHS,
372 flatc_args = DEFAULT_FLATC_GO_ARGS,
373 visibility = None,
374 srcs_filegroup_visibility = None):
375 srcs_lib = "%s_srcs" % (name)
Alex Perry0d0aae32022-02-09 21:10:17 -0800376 flatc_args = flatc_args + ["--go-namespace", importpath.split("/")[-1]]
377
378 flatbuffer_library_public(
379 name = srcs_lib,
380 srcs = srcs,
Brian Silvermandae15a12022-07-23 12:55:20 -0700381 output_suffix = "_generated.go",
Alex Perry0d0aae32022-02-09 21:10:17 -0800382 language_flag = "--go",
383 includes = includes,
384 include_paths = include_paths,
385 flatc_args = flatc_args,
386 compatible_with = compatible_with,
387 target_compatible_with = target_compatible_with,
Brian Silvermandae15a12022-07-23 12:55:20 -0700388 visibility = ["//visibility:private"],
Alex Perry0d0aae32022-02-09 21:10:17 -0800389 )
390 go_library(
391 name = name,
Brian Silvermandae15a12022-07-23 12:55:20 -0700392 srcs = [srcs_lib],
Alex Perry0d0aae32022-02-09 21:10:17 -0800393 deps = ["@com_github_google_flatbuffers//go"],
394 importpath = importpath,
395 visibility = visibility,
396 compatible_with = compatible_with,
397 target_compatible_with = target_compatible_with,
398 )
399
Brian Silvermandae15a12022-07-23 12:55:20 -0700400def _flatbuffer_rust_lib_gen_impl(ctx):
401 # TODO(Brian): I think this needs changes to properly handle multiple .fbs files in a rule.
402 uses = []
403 for (dep, dep_srcs) in zip(ctx.attr.deps, ctx.attr.dep_srcs):
404 for dep_src in dep_srcs[FlatbufferLibraryInfo].srcs:
405 uses.append((dep[CrateInfo].name, dep_src.basename.replace(".fbs", "_generated")))
406 lib_rs_content = "\n".join(
407 [
408 "// Automatically generated by the Flatbuffers Bazel rules. Do not modify",
409 "#![allow(unused_imports)]",
410 ] + ["use %s as %s;" % (crate, use_as) for (crate, use_as) in uses] +
411 ["include!(\"%s\");" % src.basename for src in ctx.files.srcs_lib],
412 )
413 output = ctx.actions.declare_file(ctx.attr.name + "_lib.rs")
414 ctx.actions.write(
415 output = output,
416 content = lib_rs_content,
417 )
418 return [DefaultInfo(files = depset([output]))]
419
420"""Generates a lib.rs for a flatbuffer_rust_library.
421
422flatc generates individual .rs files for us. It can also generate a top-level mod.rs to be included
423in a crate, but that is laid out to include all flatbuffers files in a project. That's not a good
424fit for Bazel rules and monorepos, so we generate an alternative that imports all dependencies under
425their expected names."""
426_flatbuffer_rust_lib_gen = rule(
427 implementation = _flatbuffer_rust_lib_gen_impl,
428 attrs = {
429 "srcs_lib": attr.label(mandatory = True, doc = "The generated srcs for this rule"),
430 "dep_srcs": attr.label_list(mandatory = True, providers = [FlatbufferLibraryInfo], doc = "The _srcs rules for all our direct dependencies"),
431 "deps": attr.label_list(mandatory = True, providers = [CrateInfo]),
432 },
433)
434
435def flatbuffer_rust_library(
436 name,
437 srcs,
438 compatible_with = None,
439 target_compatible_with = None,
440 deps = [],
441 include_paths = DEFAULT_INCLUDE_PATHS,
442 flatc_args = DEFAULT_FLATC_RUST_ARGS,
443 include_reflection = True,
444 crate_name = None,
445 visibility = None,
446 srcs_filegroup_visibility = None):
447 includes = [d + "_includes" for d in deps]
448 srcs_lib = "%s_srcs" % (name)
449 lib_gen = "%s_lib_gen" % (name)
450 deps = list(deps)
451 if include_reflection:
452 deps.append("@com_github_google_flatbuffers//reflection:reflection_rust_fbs")
453
454 flatbuffer_library_public(
455 name = srcs_lib,
456 srcs = srcs,
457 language_flag = "--rust",
458 output_suffix = "_generated.rs",
459 includes = includes,
460 include_paths = include_paths,
461 flatc_args = flatc_args,
462 compatible_with = compatible_with,
463 target_compatible_with = target_compatible_with,
464 visibility = visibility,
465 )
466 _flatbuffer_rust_lib_gen(
467 name = lib_gen,
468 deps = deps,
469 dep_srcs = [dep + "_srcs" for dep in deps],
470 srcs_lib = srcs_lib,
471 visibility = ["//visibility:private"],
472 compatible_with = compatible_with,
473 target_compatible_with = target_compatible_with,
474 )
475 rust_library(
476 name = name,
477 srcs = [srcs_lib, lib_gen],
478 crate_root = lib_gen,
479 crate_name = crate_name,
480 deps = ["@com_github_google_flatbuffers//rust"] + deps,
481 edition = "2018",
482 visibility = visibility,
483 compatible_with = compatible_with,
484 target_compatible_with = target_compatible_with,
485 )