blob: a060639ad8caf5e97c3a49a0ce8e62190a664d11 [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 Kuszmaulac0912d2024-05-21 15:56:59 -07003load("//tools/build_rules:clean_dep.bzl", "aos_repo_name", "clean_dep")
James Kuszmaulf5eb4682023-09-22 17:16:59 -07004
James Kuszmaul9a2d5f02023-12-14 11:38:35 -08005def static_flatbuffer(name, visibility = None, deps = [], srcs = [], **kwargs):
James Kuszmaulf5eb4682023-09-22 17:16:59 -07006 """Generates the code for the static C++ flatbuffer API for the specified fbs file.
7
8 Generates a cc_library of name name that can be depended on by C++ code and other
9 static_flatbuffer rules.
10
11 The cc_library will consist of a single file suffixed with _static.h and prefixed
12 with the name of the flatbuffer file itself (i.e., if you have a src of foo.fbs, then
13 the resulting header will be foo_static.h).
14
15 Args:
16 name: Target name.
James Kuszmaul9a2d5f02023-12-14 11:38:35 -080017 srcs: List of fbs files to codegen.
James Kuszmaulf5eb4682023-09-22 17:16:59 -070018 visibility: Desired rule visibility.
19 deps: List of static_flatbuffer dependencies of this rule.
20 """
21 fbs_suffix = "_fbs"
James Kuszmaul9a2d5f02023-12-14 11:38:35 -080022
James Kuszmaulf5eb4682023-09-22 17:16:59 -070023 flatbuffer_cc_library(
24 name = name + fbs_suffix,
James Kuszmaul9a2d5f02023-12-14 11:38:35 -080025 srcs = srcs,
James Kuszmaulf5eb4682023-09-22 17:16:59 -070026 deps = [dep + fbs_suffix for dep in deps],
27 gen_reflections = True,
28 visibility = visibility,
29 **kwargs
30 )
31
32 # Until we make this a proper rule with providers or the such, we just manage headers
33 # by having a strong convention where the header will be a function of the fbs name
34 # rather than a function of the rule name.
James Kuszmaul9a2d5f02023-12-14 11:38:35 -080035 header_names = [file.removesuffix(".fbs") + "_static.h" for file in srcs]
James Kuszmaulf5eb4682023-09-22 17:16:59 -070036 reflection_out = name + fbs_suffix + "_reflection_out"
37
38 run_binary(
39 name = name + "_gen",
James Kuszmaulac0912d2024-05-21 15:56:59 -070040 tool = clean_dep("//aos/flatbuffers:generate_wrapper"),
James Kuszmaulf5eb4682023-09-22 17:16:59 -070041 srcs = [reflection_out],
James Kuszmaul9a2d5f02023-12-14 11:38:35 -080042 outs = header_names,
43 env = {
James Kuszmaulac0912d2024-05-21 15:56:59 -070044 "AOS_REPO_NAME": aos_repo_name(),
James Kuszmaul9a2d5f02023-12-14 11:38:35 -080045 "BFBS_FILES": "$(execpaths %s)" % (reflection_out,),
46 "BASE_FILES": " ".join(srcs),
47 "OUT_FILES": " ".join(["$(execpath %s)" % (name,) for name in header_names]),
48 },
James Kuszmaulf5eb4682023-09-22 17:16:59 -070049 )
50 native.cc_library(
51 name = name,
James Kuszmaul9a2d5f02023-12-14 11:38:35 -080052 hdrs = header_names,
James Kuszmaulac0912d2024-05-21 15:56:59 -070053 deps = [clean_dep("//aos/flatbuffers:static_table"), clean_dep("//aos/flatbuffers:static_vector"), name + fbs_suffix] + deps,
James Kuszmaulf5eb4682023-09-22 17:16:59 -070054 visibility = visibility,
55 )
56 native.alias(
57 name = name + "_reflection_out",
58 actual = name + fbs_suffix + "_reflection_out",
James Kuszmaul9068b082023-12-08 12:25:57 -080059 visibility = visibility,
James Kuszmaulf5eb4682023-09-22 17:16:59 -070060 )