xref: /aosp_15_r20/external/executorch/devtools/bundled_program/schema/targets.bzl (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
2
3BUNLDED_STEM = "bundled_program_schema"
4SCALAR_TYPE_STEM = "scalar_type"
5
6INPUT_BUNDLED = BUNLDED_STEM + ".fbs"
7INPUT_SCALAR_TYPE = SCALAR_TYPE_STEM + ".fbs"
8
9OUTPUT_BUNDLED_HEADER = BUNLDED_STEM + "_generated.h"
10OUTPUT_SCALAR_TYPE_HEADER = SCALAR_TYPE_STEM + "_generated.h"
11
12BUNDLED_GEN_RULE_NAME = "generate_bundled_program"
13
14BUNDLED_LIBRARY_NAME = BUNLDED_STEM + "_fbs"
15
16def _generate_schema_header(rule_name, srcs, headers, default_header):
17    """Generate header file given flatbuffer schema
18    """
19    runtime.genrule(
20        name = rule_name,
21        srcs = srcs,
22        # We're only generating a single file, so it seems like we could use
23        # `out`, but `flatc` takes a directory as a parameter, not a single
24        # file. Use `outs` so that `${OUT}` is expanded as the containing
25        # directory instead of the file itself.
26        outs = {header: [header] for header in headers},
27        default_outs = [default_header],
28        cmd = " ".join([
29            "$(exe {})".format(runtime.external_dep_location("flatc")),
30            "--cpp",
31            "--cpp-std c++11",
32            "--gen-mutable",
33            "--scoped-enums",
34            "-o ${OUT}",
35            "${SRCS}",
36            # Let our infra know that the file was generated.
37            " ".join(["&& echo // @" + "generated >> ${OUT}/" + header for header in headers]),
38        ]),
39        visibility = [],  # Private
40    )
41
42def define_common_targets():
43    """Defines targets that should be shared between fbcode and xplat.
44
45    The directory containing this targets.bzl file should also contain both
46    TARGETS and BUCK files that call this function.
47    """
48
49    runtime.export_file(
50        name = INPUT_BUNDLED,
51        visibility = [
52            "//executorch/devtools/bundled_program/serialize/...",
53        ],
54    )
55
56    runtime.export_file(
57        name = INPUT_SCALAR_TYPE,
58        visibility = [
59            "//executorch/devtools/bundled_program/serialize/...",
60        ],
61    )
62
63    _generate_schema_header(
64        BUNDLED_GEN_RULE_NAME,
65        [INPUT_BUNDLED, INPUT_SCALAR_TYPE],
66        [OUTPUT_BUNDLED_HEADER, OUTPUT_SCALAR_TYPE_HEADER],
67        OUTPUT_BUNDLED_HEADER,
68    )
69
70    # Header-only library target with the generate bundled program schema header.
71    runtime.cxx_library(
72        name = BUNDLED_LIBRARY_NAME,
73        srcs = [],
74        visibility = [
75            "//executorch/devtools/bundled_program/...",
76            "//executorch/extension/pybindings/...",
77        ],
78        exported_headers = {
79            OUTPUT_BUNDLED_HEADER: ":{}[{}]".format(BUNDLED_GEN_RULE_NAME, OUTPUT_BUNDLED_HEADER),
80            OUTPUT_SCALAR_TYPE_HEADER: ":{}[{}]".format(BUNDLED_GEN_RULE_NAME, OUTPUT_SCALAR_TYPE_HEADER),
81        },
82        exported_external_deps = ["flatbuffers-api"],
83    )
84