James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 1 | load("@com_github_google_flatbuffers//:build_defs.bzl", "flatbuffer_cc_library") |
| 2 | load("@aspect_bazel_lib//lib:run_binary.bzl", "run_binary") |
| 3 | |
| 4 | def static_flatbuffer(name, src, visibility = None, deps = [], **kwargs): |
| 5 | """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. |
| 16 | src: .fbs file to generated code for. |
| 17 | visibility: Desired rule visibility. |
| 18 | deps: List of static_flatbuffer dependencies of this rule. |
| 19 | """ |
| 20 | fbs_suffix = "_fbs" |
| 21 | flatbuffer_cc_library( |
| 22 | name = name + fbs_suffix, |
| 23 | srcs = [src], |
| 24 | deps = [dep + fbs_suffix for dep in deps], |
| 25 | gen_reflections = True, |
| 26 | visibility = visibility, |
| 27 | **kwargs |
| 28 | ) |
| 29 | |
| 30 | # Until we make this a proper rule with providers or the such, we just manage headers |
| 31 | # by having a strong convention where the header will be a function of the fbs name |
| 32 | # rather than a function of the rule name. |
| 33 | header_name = src.removesuffix(".fbs") + "_static.h" |
| 34 | reflection_out = name + fbs_suffix + "_reflection_out" |
| 35 | |
| 36 | run_binary( |
| 37 | name = name + "_gen", |
| 38 | tool = "@org_frc971//aos/flatbuffers:generate_wrapper", |
| 39 | srcs = [reflection_out], |
| 40 | outs = [header_name], |
| 41 | args = ["$(execpath %s)" % (reflection_out,), "$(execpath %s)" % (header_name,)], |
| 42 | ) |
| 43 | native.cc_library( |
| 44 | name = name, |
| 45 | hdrs = [header_name], |
| 46 | deps = ["@org_frc971//aos/flatbuffers:static_table", "@org_frc971//aos/flatbuffers:static_vector", name + fbs_suffix] + deps, |
| 47 | visibility = visibility, |
| 48 | ) |
| 49 | native.alias( |
| 50 | name = name + "_reflection_out", |
| 51 | actual = name + fbs_suffix + "_reflection_out", |
| 52 | ) |