blob: c3e2bb0e4d32be506ea60d192ea822157d68389c [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",
46]
47
Alex Perryb3b50792020-01-18 16:13:45 -080048DEFAULT_FLATC_TS_ARGS = [
James Kuszmauldac091f2022-03-22 09:35:06 -070049 "--gen-object-api",
50 "--gen-mutable",
Alex Perryb3b50792020-01-18 16:13:45 -080051 "--reflect-names",
Alex Perry5f474f22020-02-01 12:14:24 -080052 "--gen-name-strings",
James Kuszmauldac091f2022-03-22 09:35:06 -070053 "--ts-flat-files",
54 "--keep-prefix",
Alex Perryb3b50792020-01-18 16:13:45 -080055]
56
Brian Silvermandae15a12022-07-23 12:55:20 -070057"""Contains information about a set of flatbuffers which have their code for
58reading/writing generated in a single library-style rule.
59
60Fields:
61 srcs: [File], the .fbs source files
62"""
63FlatbufferLibraryInfo = provider()
64
65def _flatbuffer_library_compile_impl(ctx):
66 outs = []
67 commands = []
68 for src in ctx.files.srcs:
69 if ctx.attr.tables_for_filenames:
70 out_dir = None
71 for table in ctx.attr.tables_for_filenames:
72 out = ctx.actions.declare_file(ctx.attr.out_prefix + table + ctx.attr.output_suffix)
73 this_out_dir = "/".join(out.dirname.split("/")[:-(len(ctx.attr.out_prefix.split("/")) - 1)])
74 if out_dir:
75 if this_out_dir != out_dir:
76 fail("Trying to write to multiple directories")
77 else:
78 out_dir = this_out_dir
79 outs.append(out)
80 else:
81 out = ctx.actions.declare_file(ctx.attr.out_prefix + src.basename.replace(".fbs", "") + ctx.attr.output_suffix)
82 outs.append(out)
83 out_dir = out.dirname
84 arguments = [ctx.executable._flatc.path]
85 for path in ctx.attr.include_paths:
86 for subpath in ["", ctx.bin_dir.path + "/"]:
87 arguments.append("-I")
88 arguments.append(subpath + path)
89 arguments.append("-I")
90 arguments.append("%s.runfiles/com_github_google_flatbuffers" % ctx.executable._flatc.path)
91 arguments.extend(ctx.attr.flatc_args)
92 arguments.extend(ctx.attr.language_flags)
93 arguments.extend([
94 "-o",
95 out_dir,
96 ])
97 arguments.append(src.path)
98 commands.append(arguments)
99 ctx.actions.run_shell(
100 outputs = outs,
101 inputs = ctx.files.srcs + ctx.files.includes,
102 tools = [ctx.executable._flatc],
103 command = " && ".join([" ".join(arguments) for arguments in commands]),
104 mnemonic = "Flatc",
105 progress_message = "Generating flatbuffer files for %{input}:",
106 )
107 return [DefaultInfo(files = depset(outs), runfiles = ctx.runfiles(files = outs)), FlatbufferLibraryInfo(srcs = ctx.files.srcs)]
108
109_flatbuffer_library_compile = rule(
110 implementation = _flatbuffer_library_compile_impl,
111 attrs = {
112 "srcs": attr.label_list(mandatory = True, allow_files = True),
113 "output_suffix": attr.string(mandatory = True),
114 "tables_for_filenames": attr.string_list(mandatory = False),
115 "language_flags": attr.string_list(mandatory = True),
116 "includes": attr.label_list(default = [], allow_files = True),
117 "include_paths": attr.string_list(default = []),
118 "flatc_args": attr.string_list(default = []),
119 "out_prefix": attr.string(default = ""),
120 "_flatc": attr.label(executable = True, cfg = "exec", default = Label(flatc_path)),
121 },
122)
123
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700124def flatbuffer_library_public(
125 name,
126 srcs,
Brian Silvermandae15a12022-07-23 12:55:20 -0700127 output_suffix,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700128 language_flag,
129 out_prefix = "",
Brian Silvermandae15a12022-07-23 12:55:20 -0700130 tables_for_filenames = None,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700131 includes = [],
132 include_paths = DEFAULT_INCLUDE_PATHS,
133 flatc_args = DEFAULT_FLATC_ARGS,
134 reflection_name = "",
135 reflection_visibility = None,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700136 compatible_with = None,
Austin Schuh7c75e582020-11-14 16:41:18 -0800137 restricted_to = None,
Philipp Schraderdada1072020-11-24 11:34:46 -0800138 target_compatible_with = None,
Brian Silvermandae15a12022-07-23 12:55:20 -0700139 output_to_bindir = False,
140 visibility = None):
Philipp Schraderdada1072020-11-24 11:34:46 -0800141 """Generates code files for reading/writing the given flatbuffers in the
142 requested language using the public compiler.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700143
144 Args:
145 name: Rule name.
146 srcs: Source .fbs files. Sent in order to the compiler.
Brian Silvermandae15a12022-07-23 12:55:20 -0700147 output_suffix: Suffix for output files from flatc.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700148 language_flag: Target language flag. One of [-c, -j, -js].
149 out_prefix: Prepend this path to the front of all generated files except on
150 single source targets. Usually is a directory name.
151 includes: Optional, list of filegroups of schemas that the srcs depend on.
152 include_paths: Optional, list of paths the includes files can be found in.
153 flatc_args: Optional, list of additional arguments to pass to flatc.
154 reflection_name: Optional, if set this will generate the flatbuffer
155 reflection binaries for the schemas.
156 reflection_visibility: The visibility of the generated reflection Fileset.
157 output_to_bindir: Passed to genrule for output to bin directory.
Austin Schuh7c75e582020-11-14 16:41:18 -0800158 compatible_with: Optional, The list of environments this rule can be
159 built for, in addition to default-supported environments.
160 restricted_to: Optional, The list of environments this rule can be built
161 for, instead of default-supported environments.
James Kuszmauldac091f2022-03-22 09:35:06 -0700162 target_compatible_with: Optional, The list of target platform constraints
163 to use.
Austin Schuh7c75e582020-11-14 16:41:18 -0800164 output_to_bindir: Passed to genrule for output to bin directory.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700165
166
167 This rule creates a filegroup(name) with all generated source files, and
168 optionally a Fileset([reflection_name]) with all generated reflection
169 binaries.
170 """
Brian Silvermandae15a12022-07-23 12:55:20 -0700171 _flatbuffer_library_compile(
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700172 name = name,
Brian Silvermandae15a12022-07-23 12:55:20 -0700173 srcs = srcs,
174 output_suffix = output_suffix,
175 language_flags = [language_flag],
176 includes = includes,
177 include_paths = include_paths,
178 flatc_args = flatc_args,
179 out_prefix = out_prefix,
180 tables_for_filenames = tables_for_filenames,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700181 compatible_with = compatible_with,
Philipp Schraderdada1072020-11-24 11:34:46 -0800182 target_compatible_with = target_compatible_with,
James Kuszmauldac091f2022-03-22 09:35:06 -0700183 restricted_to = restricted_to,
Brian Silvermandae15a12022-07-23 12:55:20 -0700184 visibility = visibility,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700185 )
Brian Silvermandae15a12022-07-23 12:55:20 -0700186
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700187 if reflection_name:
Brian Silvermandae15a12022-07-23 12:55:20 -0700188 _flatbuffer_library_compile(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700189 name = "%s_out" % reflection_name,
Brian Silvermandae15a12022-07-23 12:55:20 -0700190 srcs = srcs,
191 output_suffix = ".bfbs",
192 language_flags = ["-b", "--schema"],
193 includes = includes,
194 include_paths = include_paths,
195 flatc_args = flatc_args,
196 out_prefix = out_prefix,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700197 compatible_with = compatible_with,
Brian Silvermandae15a12022-07-23 12:55:20 -0700198 target_compatible_with = target_compatible_with,
Austin Schuh7c75e582020-11-14 16:41:18 -0800199 restricted_to = restricted_to,
Brian Silvermandae15a12022-07-23 12:55:20 -0700200 visibility = reflection_visibility,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700201 )
202
203def flatbuffer_cc_library(
204 name,
205 srcs,
206 srcs_filegroup_name = "",
207 out_prefix = "",
James Kuszmauldac091f2022-03-22 09:35:06 -0700208 deps = [],
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700209 includes = [],
210 include_paths = DEFAULT_INCLUDE_PATHS,
James Kuszmauldac091f2022-03-22 09:35:06 -0700211 cc_include_paths = [],
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700212 flatc_args = DEFAULT_FLATC_ARGS,
213 visibility = None,
Austin Schuh7c75e582020-11-14 16:41:18 -0800214 compatible_with = None,
215 restricted_to = None,
Philipp Schraderdada1072020-11-24 11:34:46 -0800216 target_compatible_with = None,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700217 srcs_filegroup_visibility = None,
218 gen_reflections = False):
James Kuszmauldac091f2022-03-22 09:35:06 -0700219 """A cc_library with the generated reader/writers for the given flatbuffer definitions.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700220
221 Args:
222 name: Rule name.
223 srcs: Source .fbs files. Sent in order to the compiler.
224 srcs_filegroup_name: Name of the output filegroup that holds srcs. Pass this
225 filegroup into the `includes` parameter of any other
226 flatbuffer_cc_library that depends on this one's schemas.
227 out_prefix: Prepend this path to the front of all generated files. Usually
228 is a directory name.
James Kuszmauldac091f2022-03-22 09:35:06 -0700229 deps: Optional, list of other flatbuffer_cc_library's to depend on. Cannot be specified
230 alongside includes.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700231 includes: Optional, list of filegroups of schemas that the srcs depend on.
James Kuszmauldac091f2022-03-22 09:35:06 -0700232 Use of this is discouraged, and may be deprecated.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700233 include_paths: Optional, list of paths the includes files can be found in.
James Kuszmauldac091f2022-03-22 09:35:06 -0700234 cc_include_paths: Optional, list of paths to add to the cc_library includes attribute.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700235 flatc_args: Optional list of additional arguments to pass to flatc
236 (e.g. --gen-mutable).
237 visibility: The visibility of the generated cc_library. By default, use the
238 default visibility of the project.
Philipp Schraderdada1072020-11-24 11:34:46 -0800239 target_compatible_with: Optional, the list of constraints the target
240 platform must satisfy for this target to be considered compatible.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700241 srcs_filegroup_visibility: The visibility of the generated srcs filegroup.
242 By default, use the value of the visibility parameter above.
243 gen_reflections: Optional, if true this will generate the flatbuffer
244 reflection binaries for the schemas.
Austin Schuh7c75e582020-11-14 16:41:18 -0800245 compatible_with: Optional, The list of environments this rule can be built
246 for, in addition to default-supported environments.
247 restricted_to: Optional, The list of environments this rule can be built
248 for, instead of default-supported environments.
James Kuszmauldac091f2022-03-22 09:35:06 -0700249 target_compatible_with: Optional, The list of target platform constraints
250 to use.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700251
252 This produces:
253 filegroup([name]_srcs): all generated .h files.
254 filegroup(srcs_filegroup_name if specified, or [name]_includes if not):
255 Other flatbuffer_cc_library's can pass this in for their `includes`
256 parameter, if they depend on the schemas in this library.
257 Fileset([name]_reflection): (Optional) all generated reflection binaries.
258 cc_library([name]): library with sources and flatbuffers deps.
James Kuszmauldac091f2022-03-22 09:35:06 -0700259 """
James Kuszmauldac091f2022-03-22 09:35:06 -0700260 if deps and includes:
261 # There is no inherent reason we couldn't support both, but this discourages
262 # use of includes without good reason.
263 fail("Cannot specify both deps and include in flatbuffer_cc_library.")
264 if deps:
265 includes = [d + "_includes" for d in deps]
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700266 reflection_name = "%s_reflection" % name if gen_reflections else ""
267
268 srcs_lib = "%s_srcs" % (name)
269 flatbuffer_library_public(
270 name = srcs_lib,
271 srcs = srcs,
Brian Silvermandae15a12022-07-23 12:55:20 -0700272 output_suffix = "_generated.h",
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700273 language_flag = "-c",
274 out_prefix = out_prefix,
275 includes = includes,
276 include_paths = include_paths,
277 flatc_args = flatc_args,
Austin Schuh7c75e582020-11-14 16:41:18 -0800278 compatible_with = compatible_with,
279 restricted_to = restricted_to,
James Kuszmauldac091f2022-03-22 09:35:06 -0700280 target_compatible_with = target_compatible_with,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700281 reflection_name = reflection_name,
282 reflection_visibility = visibility,
283 )
284 native.cc_library(
285 name = name,
286 hdrs = [
287 ":" + srcs_lib,
288 ],
289 srcs = [
290 ":" + srcs_lib,
291 ],
292 features = [
293 "-parse_headers",
294 ],
295 deps = [
296 "@com_github_google_flatbuffers//:runtime_cc",
James Kuszmauldac091f2022-03-22 09:35:06 -0700297 "@com_github_google_flatbuffers//:flatbuffers",
298 ] + deps,
299 includes = cc_include_paths,
Austin Schuh7c75e582020-11-14 16:41:18 -0800300 compatible_with = compatible_with,
301 restricted_to = restricted_to,
Philipp Schraderdada1072020-11-24 11:34:46 -0800302 target_compatible_with = target_compatible_with,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700303 linkstatic = 1,
304 visibility = visibility,
305 )
306
307 # A filegroup for the `srcs`. That is, all the schema files for this
308 # Flatbuffer set.
309 native.filegroup(
310 name = srcs_filegroup_name if srcs_filegroup_name else "%s_includes" % (name),
James Kuszmauldac091f2022-03-22 09:35:06 -0700311 srcs = srcs + includes,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700312 compatible_with = compatible_with,
Austin Schuh7c75e582020-11-14 16:41:18 -0800313 restricted_to = restricted_to,
314 visibility = srcs_filegroup_visibility if srcs_filegroup_visibility != None else visibility,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700315 )
James Kuszmaulf385c462019-12-24 09:37:34 -0800316
Brian Silverman28760272020-02-02 13:21:51 -0800317def flatbuffer_py_library(
James Kuszmaulf385c462019-12-24 09:37:34 -0800318 name,
319 srcs,
320 namespace,
321 tables,
322 compatible_with = None,
Philipp Schraderdada1072020-11-24 11:34:46 -0800323 target_compatible_with = None,
James Kuszmaulf385c462019-12-24 09:37:34 -0800324 includes = [],
325 include_paths = DEFAULT_INCLUDE_PATHS,
326 flatc_args = DEFAULT_FLATC_ARGS,
327 visibility = None,
328 srcs_filegroup_visibility = None):
329 """Generates a py_library rule for a given flatbuffer definition.
330
331 Args:
332 name: Name of the generated py_library rule.
333 srcs: Source .fbs file(s).
334 namespace: Namespace of the specified flatbuffer schema. Until
335 we make the rules sophisticated enough to figure out what
336 python files will be generated from a given schema, the user
337 has to manually specify this.
338 tables: List of table names--currently, we don't do anything to
339 automatically figure out how to handle the fact that a separate
340 python file will be generated for every table definition, and that
341 we can't know what files will be output until after the file has
342 been parsed. As such, we just force the user to manually specify
343 things.
344 """
James Kuszmaulf385c462019-12-24 09:37:34 -0800345
346 srcs_lib = "%s_srcs" % (name)
Brian Silvermandae15a12022-07-23 12:55:20 -0700347 if not tables:
348 fail("Must specify the list of tables")
James Kuszmaulf385c462019-12-24 09:37:34 -0800349 flatbuffer_library_public(
350 name = srcs_lib,
351 srcs = srcs,
Brian Silvermandae15a12022-07-23 12:55:20 -0700352 output_suffix = ".py",
353 out_prefix = namespace.replace(".", "/") + "/",
354 tables_for_filenames = tables,
James Kuszmaulf385c462019-12-24 09:37:34 -0800355 language_flag = "--python",
356 includes = includes,
357 include_paths = include_paths,
358 flatc_args = flatc_args,
359 compatible_with = compatible_with,
Philipp Schraderdada1072020-11-24 11:34:46 -0800360 target_compatible_with = target_compatible_with,
Brian Silvermandae15a12022-07-23 12:55:20 -0700361 visibility = ["//visibility:private"],
James Kuszmaulf385c462019-12-24 09:37:34 -0800362 )
363 native.py_library(
364 name = name,
Brian Silvermandae15a12022-07-23 12:55:20 -0700365 srcs = [srcs_lib],
James Kuszmaulf385c462019-12-24 09:37:34 -0800366 visibility = visibility,
367 compatible_with = compatible_with,
Philipp Schraderdada1072020-11-24 11:34:46 -0800368 target_compatible_with = target_compatible_with,
James Kuszmaulf385c462019-12-24 09:37:34 -0800369 imports = ["."],
370 deps = ["@com_github_google_flatbuffers//:flatpy"],
371 )
Alex Perryb3b50792020-01-18 16:13:45 -0800372
Alex Perry0d0aae32022-02-09 21:10:17 -0800373def flatbuffer_go_library(
374 name,
375 srcs,
376 importpath,
377 compatible_with = None,
378 target_compatible_with = None,
379 includes = [],
380 include_paths = DEFAULT_INCLUDE_PATHS,
381 flatc_args = DEFAULT_FLATC_GO_ARGS,
382 visibility = None,
383 srcs_filegroup_visibility = None):
384 srcs_lib = "%s_srcs" % (name)
Alex Perry0d0aae32022-02-09 21:10:17 -0800385 flatc_args = flatc_args + ["--go-namespace", importpath.split("/")[-1]]
386
387 flatbuffer_library_public(
388 name = srcs_lib,
389 srcs = srcs,
Brian Silvermandae15a12022-07-23 12:55:20 -0700390 output_suffix = "_generated.go",
Alex Perry0d0aae32022-02-09 21:10:17 -0800391 language_flag = "--go",
392 includes = includes,
393 include_paths = include_paths,
394 flatc_args = flatc_args,
395 compatible_with = compatible_with,
396 target_compatible_with = target_compatible_with,
Brian Silvermandae15a12022-07-23 12:55:20 -0700397 visibility = ["//visibility:private"],
Alex Perry0d0aae32022-02-09 21:10:17 -0800398 )
399 go_library(
400 name = name,
Brian Silvermandae15a12022-07-23 12:55:20 -0700401 srcs = [srcs_lib],
Alex Perry0d0aae32022-02-09 21:10:17 -0800402 deps = ["@com_github_google_flatbuffers//go"],
403 importpath = importpath,
404 visibility = visibility,
405 compatible_with = compatible_with,
406 target_compatible_with = target_compatible_with,
407 )
408
Brian Silvermandae15a12022-07-23 12:55:20 -0700409def _flatbuffer_rust_lib_gen_impl(ctx):
410 # TODO(Brian): I think this needs changes to properly handle multiple .fbs files in a rule.
411 uses = []
412 for (dep, dep_srcs) in zip(ctx.attr.deps, ctx.attr.dep_srcs):
413 for dep_src in dep_srcs[FlatbufferLibraryInfo].srcs:
414 uses.append((dep[CrateInfo].name, dep_src.basename.replace(".fbs", "_generated")))
415 lib_rs_content = "\n".join(
416 [
417 "// Automatically generated by the Flatbuffers Bazel rules. Do not modify",
418 "#![allow(unused_imports)]",
419 ] + ["use %s as %s;" % (crate, use_as) for (crate, use_as) in uses] +
420 ["include!(\"%s\");" % src.basename for src in ctx.files.srcs_lib],
421 )
422 output = ctx.actions.declare_file(ctx.attr.name + "_lib.rs")
423 ctx.actions.write(
424 output = output,
425 content = lib_rs_content,
426 )
427 return [DefaultInfo(files = depset([output]))]
428
429"""Generates a lib.rs for a flatbuffer_rust_library.
430
431flatc generates individual .rs files for us. It can also generate a top-level mod.rs to be included
432in a crate, but that is laid out to include all flatbuffers files in a project. That's not a good
433fit for Bazel rules and monorepos, so we generate an alternative that imports all dependencies under
434their expected names."""
435_flatbuffer_rust_lib_gen = rule(
436 implementation = _flatbuffer_rust_lib_gen_impl,
437 attrs = {
438 "srcs_lib": attr.label(mandatory = True, doc = "The generated srcs for this rule"),
439 "dep_srcs": attr.label_list(mandatory = True, providers = [FlatbufferLibraryInfo], doc = "The _srcs rules for all our direct dependencies"),
440 "deps": attr.label_list(mandatory = True, providers = [CrateInfo]),
441 },
442)
443
444def flatbuffer_rust_library(
445 name,
446 srcs,
447 compatible_with = None,
448 target_compatible_with = None,
449 deps = [],
450 include_paths = DEFAULT_INCLUDE_PATHS,
451 flatc_args = DEFAULT_FLATC_RUST_ARGS,
452 include_reflection = True,
453 crate_name = None,
454 visibility = None,
455 srcs_filegroup_visibility = None):
456 includes = [d + "_includes" for d in deps]
457 srcs_lib = "%s_srcs" % (name)
458 lib_gen = "%s_lib_gen" % (name)
459 deps = list(deps)
460 if include_reflection:
461 deps.append("@com_github_google_flatbuffers//reflection:reflection_rust_fbs")
462
463 flatbuffer_library_public(
464 name = srcs_lib,
465 srcs = srcs,
466 language_flag = "--rust",
467 output_suffix = "_generated.rs",
468 includes = includes,
469 include_paths = include_paths,
470 flatc_args = flatc_args,
471 compatible_with = compatible_with,
472 target_compatible_with = target_compatible_with,
473 visibility = visibility,
474 )
475 _flatbuffer_rust_lib_gen(
476 name = lib_gen,
477 deps = deps,
478 dep_srcs = [dep + "_srcs" for dep in deps],
479 srcs_lib = srcs_lib,
480 visibility = ["//visibility:private"],
481 compatible_with = compatible_with,
482 target_compatible_with = target_compatible_with,
483 )
484 rust_library(
485 name = name,
486 srcs = [srcs_lib, lib_gen],
487 crate_root = lib_gen,
488 crate_name = crate_name,
489 deps = ["@com_github_google_flatbuffers//rust"] + deps,
490 edition = "2018",
491 visibility = visibility,
492 compatible_with = compatible_with,
493 target_compatible_with = target_compatible_with,
494 )
495
Alex Perryb3b50792020-01-18 16:13:45 -0800496def flatbuffer_ts_library(
497 name,
498 srcs,
499 compatible_with = None,
Philipp Schraderdada1072020-11-24 11:34:46 -0800500 target_compatible_with = None,
James Kuszmauldac091f2022-03-22 09:35:06 -0700501 deps = [],
Alex Perryb3b50792020-01-18 16:13:45 -0800502 include_paths = DEFAULT_INCLUDE_PATHS,
503 flatc_args = DEFAULT_FLATC_TS_ARGS,
504 visibility = None,
James Kuszmauldac091f2022-03-22 09:35:06 -0700505 restricted_to = None,
James Kuszmaul136aa2b2022-04-02 14:50:56 -0700506 include_reflection = True,
507 package_name = None):
Alex Perryb3b50792020-01-18 16:13:45 -0800508 """Generates a ts_library rule for a given flatbuffer definition.
509
510 Args:
511 name: Name of the generated ts_library rule.
512 srcs: Source .fbs file(s).
James Kuszmauldac091f2022-03-22 09:35:06 -0700513 deps: Other flatbuffer_ts_library's to depend on. Note that currently
514 you must specify all your transitive dependencies manually.
515 include_paths: Optional, list of paths the includes files can be found in.
516 flatc_args: Optional list of additional arguments to pass to flatc
517 (e.g. --gen-mutable).
518 visibility: The visibility of the generated cc_library. By default, use the
519 default visibility of the project.
520 compatible_with: Optional, The list of environments this rule can be built
521 for, in addition to default-supported environments.
522 restricted_to: Optional, The list of environments this rule can be built
523 for, instead of default-supported environments.
524 target_compatible_with: Optional, The list of target platform constraints
525 to use.
526 include_reflection: Optional, Whether to depend on the flatbuffer
527 reflection library automatically. Only really relevant for the
528 target that builds the reflection library itself.
James Kuszmaul136aa2b2022-04-02 14:50:56 -0700529 package_name: Optional, Package name to use for the generated code.
Alex Perryb3b50792020-01-18 16:13:45 -0800530 """
531 srcs_lib = "%s_srcs" % (name)
James Kuszmauldac091f2022-03-22 09:35:06 -0700532
533 # frc971-specific modification: Add a genrule that overwrites the imports for any flatbuffer
534 # types (mostly just for reflection) because they need to point to external/, not to
535 # third_party/.
536 # TODO(james): There absolutely are better ways to do this, but this was the quick and dirty
537 # one....
Alex Perryb3b50792020-01-18 16:13:45 -0800538 outs = ["%s_generated.ts" % (s.replace(".fbs", "").split("/")[-1]) for s in srcs]
James Kuszmauldac091f2022-03-22 09:35:06 -0700539 includes = [d + "_includes" for d in deps]
Alex Perryb3b50792020-01-18 16:13:45 -0800540 flatbuffer_library_public(
541 name = srcs_lib,
542 srcs = srcs,
Brian Silvermandae15a12022-07-23 12:55:20 -0700543 output_suffix = "_pregenerated.ts",
Alex Perryb3b50792020-01-18 16:13:45 -0800544 language_flag = "--ts",
545 includes = includes,
546 include_paths = include_paths,
James Kuszmauldac091f2022-03-22 09:35:06 -0700547 flatc_args = flatc_args + ["--filename-suffix _pregenerated"],
Alex Perryb3b50792020-01-18 16:13:45 -0800548 compatible_with = compatible_with,
James Kuszmauldac091f2022-03-22 09:35:06 -0700549 restricted_to = restricted_to,
Philipp Schraderdada1072020-11-24 11:34:46 -0800550 target_compatible_with = target_compatible_with,
Alex Perryb3b50792020-01-18 16:13:45 -0800551 )
James Kuszmauldac091f2022-03-22 09:35:06 -0700552 genrule_cmd = " ".join([
553 "SRCS=($(SRCS));",
554 "OUTS=($(OUTS));",
555 "for i in $${!SRCS[@]}; do",
James Kuszmaul136aa2b2022-04-02 14:50:56 -0700556 "sed \"s/'.*reflection\\/reflection_pregenerated/'flatbuffers_reflection\\/reflection_generated/\" $${SRCS[i]} > $${OUTS[i]};",
James Kuszmauldac091f2022-03-22 09:35:06 -0700557 "sed -i 's/_pregenerated/_generated/' $${OUTS[i]};",
558 "done",
559 ])
560 native.genrule(
Brian Silvermandae15a12022-07-23 12:55:20 -0700561 name = name + "_reimporter.ts",
562 srcs = [srcs_lib],
James Kuszmauldac091f2022-03-22 09:35:06 -0700563 outs = outs,
564 cmd = genrule_cmd,
565 )
566 ts_project(
567 name = name + "_ts",
Alex Perryb3b50792020-01-18 16:13:45 -0800568 srcs = outs,
James Kuszmauldac091f2022-03-22 09:35:06 -0700569 declaration = True,
Alex Perryb3b50792020-01-18 16:13:45 -0800570 visibility = visibility,
571 compatible_with = compatible_with,
James Kuszmauldac091f2022-03-22 09:35:06 -0700572 restricted_to = restricted_to,
Philipp Schraderdada1072020-11-24 11:34:46 -0800573 target_compatible_with = target_compatible_with,
James Kuszmauldac091f2022-03-22 09:35:06 -0700574 tsconfig = {
575 "compilerOptions": {
576 "declaration": True,
577 "lib": [
578 "ES2015",
579 "ES2020.BigInt",
580 "DOM",
581 ],
582 "module": "es2015",
583 "moduleResolution": "node",
584 "strict": True,
585 "types": ["node"],
586 },
587 },
588 deps = deps + ["@com_github_google_flatbuffers//ts:flatbuffers"] + (["@com_github_google_flatbuffers//reflection:reflection_ts_fbs"] if include_reflection else []),
589 )
590 js_library(
591 name = name,
592 visibility = visibility,
593 compatible_with = compatible_with,
594 restricted_to = restricted_to,
595 target_compatible_with = target_compatible_with,
596 deps = [name + "_ts"],
James Kuszmaul136aa2b2022-04-02 14:50:56 -0700597 package_name = package_name,
James Kuszmauldac091f2022-03-22 09:35:06 -0700598 )
599 native.filegroup(
600 name = "%s_includes" % (name),
601 srcs = srcs + includes,
602 compatible_with = compatible_with,
603 restricted_to = restricted_to,
604 visibility = visibility,
Alex Perryb3b50792020-01-18 16:13:45 -0800605 )