blob: 4ebd87293c110578b15649eb82a1791e97775ad4 [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")
James Kuszmauldac091f2022-03-22 09:35:06 -070011load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
12load("@npm//@bazel/typescript:index.bzl", "ts_project")
13load("@rules_cc//cc:defs.bzl", "cc_library")
Austin Schuhe89fa2d2019-08-14 20:24:23 -070014
15flatc_path = "@com_github_google_flatbuffers//:flatc"
16
17DEFAULT_INCLUDE_PATHS = [
18 "./",
Austin Schuhe89fa2d2019-08-14 20:24:23 -070019]
20
21DEFAULT_FLATC_ARGS = [
22 "--gen-object-api",
23 "--gen-compare",
Alex Perrycb7da4b2019-08-28 19:35:56 -070024 "--keep-prefix",
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
Alex Perryb3b50792020-01-18 16:13:45 -080049DEFAULT_FLATC_TS_ARGS = [
James Kuszmauldac091f2022-03-22 09:35:06 -070050 "--gen-object-api",
51 "--gen-mutable",
Alex Perryb3b50792020-01-18 16:13:45 -080052 "--reflect-names",
Alex Perry5f474f22020-02-01 12:14:24 -080053 "--gen-name-strings",
James Kuszmauldac091f2022-03-22 09:35:06 -070054 "--ts-flat-files",
55 "--keep-prefix",
Alex Perryb3b50792020-01-18 16:13:45 -080056]
57
Brian Silvermandae15a12022-07-23 12:55:20 -070058"""Contains information about a set of flatbuffers which have their code for
59reading/writing generated in a single library-style rule.
60
61Fields:
62 srcs: [File], the .fbs source files
63"""
64FlatbufferLibraryInfo = provider()
65
66def _flatbuffer_library_compile_impl(ctx):
67 outs = []
68 commands = []
69 for src in ctx.files.srcs:
70 if ctx.attr.tables_for_filenames:
71 out_dir = None
72 for table in ctx.attr.tables_for_filenames:
73 out = ctx.actions.declare_file(ctx.attr.out_prefix + table + ctx.attr.output_suffix)
74 this_out_dir = "/".join(out.dirname.split("/")[:-(len(ctx.attr.out_prefix.split("/")) - 1)])
75 if out_dir:
76 if this_out_dir != out_dir:
77 fail("Trying to write to multiple directories")
78 else:
79 out_dir = this_out_dir
80 outs.append(out)
81 else:
82 out = ctx.actions.declare_file(ctx.attr.out_prefix + src.basename.replace(".fbs", "") + ctx.attr.output_suffix)
83 outs.append(out)
84 out_dir = out.dirname
85 arguments = [ctx.executable._flatc.path]
86 for path in ctx.attr.include_paths:
87 for subpath in ["", ctx.bin_dir.path + "/"]:
88 arguments.append("-I")
89 arguments.append(subpath + path)
90 arguments.append("-I")
91 arguments.append("%s.runfiles/com_github_google_flatbuffers" % ctx.executable._flatc.path)
92 arguments.extend(ctx.attr.flatc_args)
93 arguments.extend(ctx.attr.language_flags)
94 arguments.extend([
95 "-o",
96 out_dir,
97 ])
98 arguments.append(src.path)
99 commands.append(arguments)
100 ctx.actions.run_shell(
101 outputs = outs,
102 inputs = ctx.files.srcs + ctx.files.includes,
103 tools = [ctx.executable._flatc],
104 command = " && ".join([" ".join(arguments) for arguments in commands]),
105 mnemonic = "Flatc",
106 progress_message = "Generating flatbuffer files for %{input}:",
107 )
108 return [DefaultInfo(files = depset(outs), runfiles = ctx.runfiles(files = outs)), FlatbufferLibraryInfo(srcs = ctx.files.srcs)]
109
110_flatbuffer_library_compile = rule(
111 implementation = _flatbuffer_library_compile_impl,
112 attrs = {
113 "srcs": attr.label_list(mandatory = True, allow_files = True),
114 "output_suffix": attr.string(mandatory = True),
115 "tables_for_filenames": attr.string_list(mandatory = False),
116 "language_flags": attr.string_list(mandatory = True),
117 "includes": attr.label_list(default = [], allow_files = True),
118 "include_paths": attr.string_list(default = []),
119 "flatc_args": attr.string_list(default = []),
120 "out_prefix": attr.string(default = ""),
121 "_flatc": attr.label(executable = True, cfg = "exec", default = Label(flatc_path)),
122 },
123)
124
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700125def flatbuffer_library_public(
126 name,
127 srcs,
Brian Silvermandae15a12022-07-23 12:55:20 -0700128 output_suffix,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700129 language_flag,
130 out_prefix = "",
Brian Silvermandae15a12022-07-23 12:55:20 -0700131 tables_for_filenames = None,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700132 includes = [],
133 include_paths = DEFAULT_INCLUDE_PATHS,
134 flatc_args = DEFAULT_FLATC_ARGS,
135 reflection_name = "",
136 reflection_visibility = None,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700137 compatible_with = None,
Austin Schuh7c75e582020-11-14 16:41:18 -0800138 restricted_to = None,
Philipp Schraderdada1072020-11-24 11:34:46 -0800139 target_compatible_with = None,
Brian Silvermandae15a12022-07-23 12:55:20 -0700140 output_to_bindir = False,
141 visibility = None):
Philipp Schraderdada1072020-11-24 11:34:46 -0800142 """Generates code files for reading/writing the given flatbuffers in the
143 requested language using the public compiler.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700144
145 Args:
146 name: Rule name.
147 srcs: Source .fbs files. Sent in order to the compiler.
Brian Silvermandae15a12022-07-23 12:55:20 -0700148 output_suffix: Suffix for output files from flatc.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700149 language_flag: Target language flag. One of [-c, -j, -js].
150 out_prefix: Prepend this path to the front of all generated files except on
151 single source targets. Usually is a directory name.
152 includes: Optional, list of filegroups of schemas that the srcs depend on.
153 include_paths: Optional, list of paths the includes files can be found in.
154 flatc_args: Optional, list of additional arguments to pass to flatc.
155 reflection_name: Optional, if set this will generate the flatbuffer
156 reflection binaries for the schemas.
157 reflection_visibility: The visibility of the generated reflection Fileset.
158 output_to_bindir: Passed to genrule for output to bin directory.
Austin Schuh7c75e582020-11-14 16:41:18 -0800159 compatible_with: Optional, The list of environments this rule can be
160 built for, in addition to default-supported environments.
161 restricted_to: Optional, The list of environments this rule can be built
162 for, instead of default-supported environments.
James Kuszmauldac091f2022-03-22 09:35:06 -0700163 target_compatible_with: Optional, The list of target platform constraints
164 to use.
Austin Schuh7c75e582020-11-14 16:41:18 -0800165 output_to_bindir: Passed to genrule for output to bin directory.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700166
167
168 This rule creates a filegroup(name) with all generated source files, and
169 optionally a Fileset([reflection_name]) with all generated reflection
170 binaries.
171 """
Brian Silvermandae15a12022-07-23 12:55:20 -0700172 _flatbuffer_library_compile(
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700173 name = name,
Brian Silvermandae15a12022-07-23 12:55:20 -0700174 srcs = srcs,
175 output_suffix = output_suffix,
176 language_flags = [language_flag],
177 includes = includes,
178 include_paths = include_paths,
179 flatc_args = flatc_args,
180 out_prefix = out_prefix,
181 tables_for_filenames = tables_for_filenames,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700182 compatible_with = compatible_with,
Philipp Schraderdada1072020-11-24 11:34:46 -0800183 target_compatible_with = target_compatible_with,
James Kuszmauldac091f2022-03-22 09:35:06 -0700184 restricted_to = restricted_to,
Brian Silvermandae15a12022-07-23 12:55:20 -0700185 visibility = visibility,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700186 )
Brian Silvermandae15a12022-07-23 12:55:20 -0700187
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700188 if reflection_name:
Brian Silvermandae15a12022-07-23 12:55:20 -0700189 _flatbuffer_library_compile(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700190 name = "%s_out" % reflection_name,
Brian Silvermandae15a12022-07-23 12:55:20 -0700191 srcs = srcs,
192 output_suffix = ".bfbs",
193 language_flags = ["-b", "--schema"],
194 includes = includes,
195 include_paths = include_paths,
196 flatc_args = flatc_args,
197 out_prefix = out_prefix,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700198 compatible_with = compatible_with,
Brian Silvermandae15a12022-07-23 12:55:20 -0700199 target_compatible_with = target_compatible_with,
Austin Schuh7c75e582020-11-14 16:41:18 -0800200 restricted_to = restricted_to,
Brian Silvermandae15a12022-07-23 12:55:20 -0700201 visibility = reflection_visibility,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700202 )
203
204def flatbuffer_cc_library(
205 name,
206 srcs,
207 srcs_filegroup_name = "",
208 out_prefix = "",
James Kuszmauldac091f2022-03-22 09:35:06 -0700209 deps = [],
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700210 includes = [],
211 include_paths = DEFAULT_INCLUDE_PATHS,
James Kuszmauldac091f2022-03-22 09:35:06 -0700212 cc_include_paths = [],
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700213 flatc_args = DEFAULT_FLATC_ARGS,
214 visibility = None,
Austin Schuh7c75e582020-11-14 16:41:18 -0800215 compatible_with = None,
216 restricted_to = None,
Philipp Schraderdada1072020-11-24 11:34:46 -0800217 target_compatible_with = None,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700218 srcs_filegroup_visibility = None,
219 gen_reflections = False):
James Kuszmauldac091f2022-03-22 09:35:06 -0700220 """A cc_library with the generated reader/writers for the given flatbuffer definitions.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700221
222 Args:
223 name: Rule name.
224 srcs: Source .fbs files. Sent in order to the compiler.
225 srcs_filegroup_name: Name of the output filegroup that holds srcs. Pass this
226 filegroup into the `includes` parameter of any other
227 flatbuffer_cc_library that depends on this one's schemas.
228 out_prefix: Prepend this path to the front of all generated files. Usually
229 is a directory name.
James Kuszmauldac091f2022-03-22 09:35:06 -0700230 deps: Optional, list of other flatbuffer_cc_library's to depend on. Cannot be specified
231 alongside includes.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700232 includes: Optional, list of filegroups of schemas that the srcs depend on.
James Kuszmauldac091f2022-03-22 09:35:06 -0700233 Use of this is discouraged, and may be deprecated.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700234 include_paths: Optional, list of paths the includes files can be found in.
James Kuszmauldac091f2022-03-22 09:35:06 -0700235 cc_include_paths: Optional, list of paths to add to the cc_library includes attribute.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700236 flatc_args: Optional list of additional arguments to pass to flatc
237 (e.g. --gen-mutable).
238 visibility: The visibility of the generated cc_library. By default, use the
239 default visibility of the project.
Philipp Schraderdada1072020-11-24 11:34:46 -0800240 target_compatible_with: Optional, the list of constraints the target
241 platform must satisfy for this target to be considered compatible.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700242 srcs_filegroup_visibility: The visibility of the generated srcs filegroup.
243 By default, use the value of the visibility parameter above.
244 gen_reflections: Optional, if true this will generate the flatbuffer
245 reflection binaries for the schemas.
Austin Schuh7c75e582020-11-14 16:41:18 -0800246 compatible_with: Optional, The list of environments this rule can be built
247 for, in addition to default-supported environments.
248 restricted_to: Optional, The list of environments this rule can be built
249 for, instead of default-supported environments.
James Kuszmauldac091f2022-03-22 09:35:06 -0700250 target_compatible_with: Optional, The list of target platform constraints
251 to use.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700252
253 This produces:
254 filegroup([name]_srcs): all generated .h files.
255 filegroup(srcs_filegroup_name if specified, or [name]_includes if not):
256 Other flatbuffer_cc_library's can pass this in for their `includes`
257 parameter, if they depend on the schemas in this library.
258 Fileset([name]_reflection): (Optional) all generated reflection binaries.
259 cc_library([name]): library with sources and flatbuffers deps.
James Kuszmauldac091f2022-03-22 09:35:06 -0700260 """
James Kuszmauldac091f2022-03-22 09:35:06 -0700261 if deps and includes:
262 # There is no inherent reason we couldn't support both, but this discourages
263 # use of includes without good reason.
264 fail("Cannot specify both deps and include in flatbuffer_cc_library.")
265 if deps:
266 includes = [d + "_includes" for d in deps]
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700267 reflection_name = "%s_reflection" % name if gen_reflections else ""
268
269 srcs_lib = "%s_srcs" % (name)
270 flatbuffer_library_public(
271 name = srcs_lib,
272 srcs = srcs,
Brian Silvermandae15a12022-07-23 12:55:20 -0700273 output_suffix = "_generated.h",
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700274 language_flag = "-c",
275 out_prefix = out_prefix,
276 includes = includes,
277 include_paths = include_paths,
278 flatc_args = flatc_args,
Austin Schuh7c75e582020-11-14 16:41:18 -0800279 compatible_with = compatible_with,
280 restricted_to = restricted_to,
James Kuszmauldac091f2022-03-22 09:35:06 -0700281 target_compatible_with = target_compatible_with,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700282 reflection_name = reflection_name,
283 reflection_visibility = visibility,
284 )
285 native.cc_library(
286 name = name,
287 hdrs = [
288 ":" + srcs_lib,
289 ],
290 srcs = [
291 ":" + srcs_lib,
292 ],
293 features = [
294 "-parse_headers",
295 ],
296 deps = [
297 "@com_github_google_flatbuffers//:runtime_cc",
James Kuszmauldac091f2022-03-22 09:35:06 -0700298 "@com_github_google_flatbuffers//:flatbuffers",
299 ] + deps,
300 includes = cc_include_paths,
Austin Schuh7c75e582020-11-14 16:41:18 -0800301 compatible_with = compatible_with,
302 restricted_to = restricted_to,
Philipp Schraderdada1072020-11-24 11:34:46 -0800303 target_compatible_with = target_compatible_with,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700304 linkstatic = 1,
305 visibility = visibility,
306 )
307
308 # A filegroup for the `srcs`. That is, all the schema files for this
309 # Flatbuffer set.
310 native.filegroup(
311 name = srcs_filegroup_name if srcs_filegroup_name else "%s_includes" % (name),
James Kuszmauldac091f2022-03-22 09:35:06 -0700312 srcs = srcs + includes,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700313 compatible_with = compatible_with,
Austin Schuh7c75e582020-11-14 16:41:18 -0800314 restricted_to = restricted_to,
315 visibility = srcs_filegroup_visibility if srcs_filegroup_visibility != None else visibility,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700316 )
James Kuszmaulf385c462019-12-24 09:37:34 -0800317
Brian Silverman28760272020-02-02 13:21:51 -0800318def flatbuffer_py_library(
James Kuszmaulf385c462019-12-24 09:37:34 -0800319 name,
320 srcs,
321 namespace,
322 tables,
323 compatible_with = None,
Philipp Schraderdada1072020-11-24 11:34:46 -0800324 target_compatible_with = None,
James Kuszmaulf385c462019-12-24 09:37:34 -0800325 includes = [],
326 include_paths = DEFAULT_INCLUDE_PATHS,
327 flatc_args = DEFAULT_FLATC_ARGS,
328 visibility = None,
329 srcs_filegroup_visibility = None):
330 """Generates a py_library rule for a given flatbuffer definition.
331
332 Args:
333 name: Name of the generated py_library rule.
334 srcs: Source .fbs file(s).
335 namespace: Namespace of the specified flatbuffer schema. Until
336 we make the rules sophisticated enough to figure out what
337 python files will be generated from a given schema, the user
338 has to manually specify this.
339 tables: List of table names--currently, we don't do anything to
340 automatically figure out how to handle the fact that a separate
341 python file will be generated for every table definition, and that
342 we can't know what files will be output until after the file has
343 been parsed. As such, we just force the user to manually specify
344 things.
345 """
James Kuszmaulf385c462019-12-24 09:37:34 -0800346
347 srcs_lib = "%s_srcs" % (name)
Brian Silvermandae15a12022-07-23 12:55:20 -0700348 if not tables:
349 fail("Must specify the list of tables")
James Kuszmaulf385c462019-12-24 09:37:34 -0800350 flatbuffer_library_public(
351 name = srcs_lib,
352 srcs = srcs,
Brian Silvermandae15a12022-07-23 12:55:20 -0700353 output_suffix = ".py",
354 out_prefix = namespace.replace(".", "/") + "/",
355 tables_for_filenames = tables,
James Kuszmaulf385c462019-12-24 09:37:34 -0800356 language_flag = "--python",
357 includes = includes,
358 include_paths = include_paths,
359 flatc_args = flatc_args,
360 compatible_with = compatible_with,
Philipp Schraderdada1072020-11-24 11:34:46 -0800361 target_compatible_with = target_compatible_with,
Brian Silvermandae15a12022-07-23 12:55:20 -0700362 visibility = ["//visibility:private"],
James Kuszmaulf385c462019-12-24 09:37:34 -0800363 )
364 native.py_library(
365 name = name,
Brian Silvermandae15a12022-07-23 12:55:20 -0700366 srcs = [srcs_lib],
James Kuszmaulf385c462019-12-24 09:37:34 -0800367 visibility = visibility,
368 compatible_with = compatible_with,
Philipp Schraderdada1072020-11-24 11:34:46 -0800369 target_compatible_with = target_compatible_with,
James Kuszmaulf385c462019-12-24 09:37:34 -0800370 imports = ["."],
371 deps = ["@com_github_google_flatbuffers//:flatpy"],
372 )
Alex Perryb3b50792020-01-18 16:13:45 -0800373
Alex Perry0d0aae32022-02-09 21:10:17 -0800374def flatbuffer_go_library(
375 name,
376 srcs,
377 importpath,
378 compatible_with = None,
379 target_compatible_with = None,
380 includes = [],
381 include_paths = DEFAULT_INCLUDE_PATHS,
382 flatc_args = DEFAULT_FLATC_GO_ARGS,
383 visibility = None,
384 srcs_filegroup_visibility = None):
385 srcs_lib = "%s_srcs" % (name)
Alex Perry0d0aae32022-02-09 21:10:17 -0800386 flatc_args = flatc_args + ["--go-namespace", importpath.split("/")[-1]]
387
388 flatbuffer_library_public(
389 name = srcs_lib,
390 srcs = srcs,
Brian Silvermandae15a12022-07-23 12:55:20 -0700391 output_suffix = "_generated.go",
Alex Perry0d0aae32022-02-09 21:10:17 -0800392 language_flag = "--go",
393 includes = includes,
394 include_paths = include_paths,
395 flatc_args = flatc_args,
396 compatible_with = compatible_with,
397 target_compatible_with = target_compatible_with,
Brian Silvermandae15a12022-07-23 12:55:20 -0700398 visibility = ["//visibility:private"],
Alex Perry0d0aae32022-02-09 21:10:17 -0800399 )
400 go_library(
401 name = name,
Brian Silvermandae15a12022-07-23 12:55:20 -0700402 srcs = [srcs_lib],
Alex Perry0d0aae32022-02-09 21:10:17 -0800403 deps = ["@com_github_google_flatbuffers//go"],
404 importpath = importpath,
405 visibility = visibility,
406 compatible_with = compatible_with,
407 target_compatible_with = target_compatible_with,
408 )
409
Brian Silvermandae15a12022-07-23 12:55:20 -0700410def _flatbuffer_rust_lib_gen_impl(ctx):
411 # TODO(Brian): I think this needs changes to properly handle multiple .fbs files in a rule.
412 uses = []
413 for (dep, dep_srcs) in zip(ctx.attr.deps, ctx.attr.dep_srcs):
414 for dep_src in dep_srcs[FlatbufferLibraryInfo].srcs:
415 uses.append((dep[CrateInfo].name, dep_src.basename.replace(".fbs", "_generated")))
416 lib_rs_content = "\n".join(
417 [
418 "// Automatically generated by the Flatbuffers Bazel rules. Do not modify",
419 "#![allow(unused_imports)]",
420 ] + ["use %s as %s;" % (crate, use_as) for (crate, use_as) in uses] +
421 ["include!(\"%s\");" % src.basename for src in ctx.files.srcs_lib],
422 )
423 output = ctx.actions.declare_file(ctx.attr.name + "_lib.rs")
424 ctx.actions.write(
425 output = output,
426 content = lib_rs_content,
427 )
428 return [DefaultInfo(files = depset([output]))]
429
430"""Generates a lib.rs for a flatbuffer_rust_library.
431
432flatc generates individual .rs files for us. It can also generate a top-level mod.rs to be included
433in a crate, but that is laid out to include all flatbuffers files in a project. That's not a good
434fit for Bazel rules and monorepos, so we generate an alternative that imports all dependencies under
435their expected names."""
436_flatbuffer_rust_lib_gen = rule(
437 implementation = _flatbuffer_rust_lib_gen_impl,
438 attrs = {
439 "srcs_lib": attr.label(mandatory = True, doc = "The generated srcs for this rule"),
440 "dep_srcs": attr.label_list(mandatory = True, providers = [FlatbufferLibraryInfo], doc = "The _srcs rules for all our direct dependencies"),
441 "deps": attr.label_list(mandatory = True, providers = [CrateInfo]),
442 },
443)
444
445def flatbuffer_rust_library(
446 name,
447 srcs,
448 compatible_with = None,
449 target_compatible_with = None,
450 deps = [],
451 include_paths = DEFAULT_INCLUDE_PATHS,
452 flatc_args = DEFAULT_FLATC_RUST_ARGS,
453 include_reflection = True,
454 crate_name = None,
455 visibility = None,
456 srcs_filegroup_visibility = None):
457 includes = [d + "_includes" for d in deps]
458 srcs_lib = "%s_srcs" % (name)
459 lib_gen = "%s_lib_gen" % (name)
460 deps = list(deps)
461 if include_reflection:
462 deps.append("@com_github_google_flatbuffers//reflection:reflection_rust_fbs")
463
464 flatbuffer_library_public(
465 name = srcs_lib,
466 srcs = srcs,
467 language_flag = "--rust",
468 output_suffix = "_generated.rs",
469 includes = includes,
470 include_paths = include_paths,
471 flatc_args = flatc_args,
472 compatible_with = compatible_with,
473 target_compatible_with = target_compatible_with,
474 visibility = visibility,
475 )
476 _flatbuffer_rust_lib_gen(
477 name = lib_gen,
478 deps = deps,
479 dep_srcs = [dep + "_srcs" for dep in deps],
480 srcs_lib = srcs_lib,
481 visibility = ["//visibility:private"],
482 compatible_with = compatible_with,
483 target_compatible_with = target_compatible_with,
484 )
485 rust_library(
486 name = name,
487 srcs = [srcs_lib, lib_gen],
488 crate_root = lib_gen,
489 crate_name = crate_name,
490 deps = ["@com_github_google_flatbuffers//rust"] + deps,
491 edition = "2018",
492 visibility = visibility,
493 compatible_with = compatible_with,
494 target_compatible_with = target_compatible_with,
495 )
496
Alex Perryb3b50792020-01-18 16:13:45 -0800497def flatbuffer_ts_library(
498 name,
499 srcs,
500 compatible_with = None,
Philipp Schraderdada1072020-11-24 11:34:46 -0800501 target_compatible_with = None,
James Kuszmauldac091f2022-03-22 09:35:06 -0700502 deps = [],
Alex Perryb3b50792020-01-18 16:13:45 -0800503 include_paths = DEFAULT_INCLUDE_PATHS,
504 flatc_args = DEFAULT_FLATC_TS_ARGS,
505 visibility = None,
James Kuszmauldac091f2022-03-22 09:35:06 -0700506 restricted_to = None,
James Kuszmaul136aa2b2022-04-02 14:50:56 -0700507 include_reflection = True,
508 package_name = None):
Alex Perryb3b50792020-01-18 16:13:45 -0800509 """Generates a ts_library rule for a given flatbuffer definition.
510
511 Args:
512 name: Name of the generated ts_library rule.
513 srcs: Source .fbs file(s).
James Kuszmauldac091f2022-03-22 09:35:06 -0700514 deps: Other flatbuffer_ts_library's to depend on. Note that currently
515 you must specify all your transitive dependencies manually.
516 include_paths: Optional, list of paths the includes files can be found in.
517 flatc_args: Optional list of additional arguments to pass to flatc
518 (e.g. --gen-mutable).
519 visibility: The visibility of the generated cc_library. By default, use the
520 default visibility of the project.
521 compatible_with: Optional, The list of environments this rule can be built
522 for, in addition to default-supported environments.
523 restricted_to: Optional, The list of environments this rule can be built
524 for, instead of default-supported environments.
525 target_compatible_with: Optional, The list of target platform constraints
526 to use.
527 include_reflection: Optional, Whether to depend on the flatbuffer
528 reflection library automatically. Only really relevant for the
529 target that builds the reflection library itself.
James Kuszmaul136aa2b2022-04-02 14:50:56 -0700530 package_name: Optional, Package name to use for the generated code.
Alex Perryb3b50792020-01-18 16:13:45 -0800531 """
532 srcs_lib = "%s_srcs" % (name)
James Kuszmauldac091f2022-03-22 09:35:06 -0700533
534 # frc971-specific modification: Add a genrule that overwrites the imports for any flatbuffer
535 # types (mostly just for reflection) because they need to point to external/, not to
536 # third_party/.
537 # TODO(james): There absolutely are better ways to do this, but this was the quick and dirty
538 # one....
Alex Perryb3b50792020-01-18 16:13:45 -0800539 outs = ["%s_generated.ts" % (s.replace(".fbs", "").split("/")[-1]) for s in srcs]
James Kuszmauldac091f2022-03-22 09:35:06 -0700540 includes = [d + "_includes" for d in deps]
Alex Perryb3b50792020-01-18 16:13:45 -0800541 flatbuffer_library_public(
542 name = srcs_lib,
543 srcs = srcs,
Brian Silvermandae15a12022-07-23 12:55:20 -0700544 output_suffix = "_pregenerated.ts",
Alex Perryb3b50792020-01-18 16:13:45 -0800545 language_flag = "--ts",
546 includes = includes,
547 include_paths = include_paths,
James Kuszmauldac091f2022-03-22 09:35:06 -0700548 flatc_args = flatc_args + ["--filename-suffix _pregenerated"],
Alex Perryb3b50792020-01-18 16:13:45 -0800549 compatible_with = compatible_with,
James Kuszmauldac091f2022-03-22 09:35:06 -0700550 restricted_to = restricted_to,
Philipp Schraderdada1072020-11-24 11:34:46 -0800551 target_compatible_with = target_compatible_with,
Alex Perryb3b50792020-01-18 16:13:45 -0800552 )
James Kuszmauldac091f2022-03-22 09:35:06 -0700553 genrule_cmd = " ".join([
554 "SRCS=($(SRCS));",
555 "OUTS=($(OUTS));",
556 "for i in $${!SRCS[@]}; do",
James Kuszmaul136aa2b2022-04-02 14:50:56 -0700557 "sed \"s/'.*reflection\\/reflection_pregenerated/'flatbuffers_reflection\\/reflection_generated/\" $${SRCS[i]} > $${OUTS[i]};",
James Kuszmauldac091f2022-03-22 09:35:06 -0700558 "sed -i 's/_pregenerated/_generated/' $${OUTS[i]};",
559 "done",
560 ])
561 native.genrule(
Brian Silvermandae15a12022-07-23 12:55:20 -0700562 name = name + "_reimporter.ts",
563 srcs = [srcs_lib],
James Kuszmauldac091f2022-03-22 09:35:06 -0700564 outs = outs,
565 cmd = genrule_cmd,
566 )
567 ts_project(
568 name = name + "_ts",
Alex Perryb3b50792020-01-18 16:13:45 -0800569 srcs = outs,
James Kuszmauldac091f2022-03-22 09:35:06 -0700570 declaration = True,
Alex Perryb3b50792020-01-18 16:13:45 -0800571 visibility = visibility,
572 compatible_with = compatible_with,
James Kuszmauldac091f2022-03-22 09:35:06 -0700573 restricted_to = restricted_to,
Philipp Schraderdada1072020-11-24 11:34:46 -0800574 target_compatible_with = target_compatible_with,
James Kuszmauldac091f2022-03-22 09:35:06 -0700575 tsconfig = {
576 "compilerOptions": {
577 "declaration": True,
578 "lib": [
579 "ES2015",
580 "ES2020.BigInt",
581 "DOM",
582 ],
583 "module": "es2015",
584 "moduleResolution": "node",
585 "strict": True,
586 "types": ["node"],
587 },
588 },
589 deps = deps + ["@com_github_google_flatbuffers//ts:flatbuffers"] + (["@com_github_google_flatbuffers//reflection:reflection_ts_fbs"] if include_reflection else []),
590 )
591 js_library(
592 name = name,
593 visibility = visibility,
594 compatible_with = compatible_with,
595 restricted_to = restricted_to,
596 target_compatible_with = target_compatible_with,
597 deps = [name + "_ts"],
James Kuszmaul136aa2b2022-04-02 14:50:56 -0700598 package_name = package_name,
James Kuszmauldac091f2022-03-22 09:35:06 -0700599 )
600 native.filegroup(
601 name = "%s_includes" % (name),
602 srcs = srcs + includes,
603 compatible_with = compatible_with,
604 restricted_to = restricted_to,
605 visibility = visibility,
Alex Perryb3b50792020-01-18 16:13:45 -0800606 )