blob: 52627bcda0d0c99a2fb6ff42f053d47265de7cfd [file] [log] [blame]
James Kuszmaulf5eb4682023-09-22 17:16:59 -07001load("@aspect_bazel_lib//lib:run_binary.bzl", "run_binary")
Austin Schuh8f99c822024-05-05 22:43:40 -07002load("@com_github_google_flatbuffers//:build_defs.bzl", "flatbuffer_cc_library")
James Kuszmaulf5eb4682023-09-22 17:16:59 -07003
James Kuszmaul9a2d5f02023-12-14 11:38:35 -08004def static_flatbuffer(name, visibility = None, deps = [], srcs = [], **kwargs):
James Kuszmaulf5eb4682023-09-22 17:16:59 -07005 """Generates the code for the static C++ flatbuffer API for the specified fbs file.
6
7 Generates a cc_library of name name that can be depended on by C++ code and other
8 static_flatbuffer rules.
9
10 The cc_library will consist of a single file suffixed with _static.h and prefixed
11 with the name of the flatbuffer file itself (i.e., if you have a src of foo.fbs, then
12 the resulting header will be foo_static.h).
13
14 Args:
15 name: Target name.
James Kuszmaul9a2d5f02023-12-14 11:38:35 -080016 srcs: List of fbs files to codegen.
James Kuszmaulf5eb4682023-09-22 17:16:59 -070017 visibility: Desired rule visibility.
18 deps: List of static_flatbuffer dependencies of this rule.
19 """
20 fbs_suffix = "_fbs"
James Kuszmaul9a2d5f02023-12-14 11:38:35 -080021
James Kuszmaulf5eb4682023-09-22 17:16:59 -070022 flatbuffer_cc_library(
23 name = name + fbs_suffix,
James Kuszmaul9a2d5f02023-12-14 11:38:35 -080024 srcs = srcs,
James Kuszmaulf5eb4682023-09-22 17:16:59 -070025 deps = [dep + fbs_suffix for dep in deps],
26 gen_reflections = True,
27 visibility = visibility,
28 **kwargs
29 )
30
31 # Until we make this a proper rule with providers or the such, we just manage headers
32 # by having a strong convention where the header will be a function of the fbs name
33 # rather than a function of the rule name.
James Kuszmaul9a2d5f02023-12-14 11:38:35 -080034 header_names = [file.removesuffix(".fbs") + "_static.h" for file in srcs]
James Kuszmaulf5eb4682023-09-22 17:16:59 -070035 reflection_out = name + fbs_suffix + "_reflection_out"
36
37 run_binary(
38 name = name + "_gen",
39 tool = "@org_frc971//aos/flatbuffers:generate_wrapper",
40 srcs = [reflection_out],
James Kuszmaul9a2d5f02023-12-14 11:38:35 -080041 outs = header_names,
42 env = {
43 "BFBS_FILES": "$(execpaths %s)" % (reflection_out,),
44 "BASE_FILES": " ".join(srcs),
45 "OUT_FILES": " ".join(["$(execpath %s)" % (name,) for name in header_names]),
46 },
James Kuszmaulf5eb4682023-09-22 17:16:59 -070047 )
48 native.cc_library(
49 name = name,
James Kuszmaul9a2d5f02023-12-14 11:38:35 -080050 hdrs = header_names,
James Kuszmaulf5eb4682023-09-22 17:16:59 -070051 deps = ["@org_frc971//aos/flatbuffers:static_table", "@org_frc971//aos/flatbuffers:static_vector", name + fbs_suffix] + deps,
52 visibility = visibility,
53 )
54 native.alias(
55 name = name + "_reflection_out",
56 actual = name + fbs_suffix + "_reflection_out",
James Kuszmaul9068b082023-12-08 12:25:57 -080057 visibility = visibility,
James Kuszmaulf5eb4682023-09-22 17:16:59 -070058 )