xref: /aosp_15_r20/external/executorch/schema/targets.bzl (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
2
3# Construct the input and output file names. All input and output files rely on scalar_type file.
4PROGRAM_STEM = "program"
5SCALAR_TYPE_STEM = "scalar_type"
6
7INPUT_PROGRAM = PROGRAM_STEM + ".fbs"
8INPUT_SCALAR_TYPE = SCALAR_TYPE_STEM + ".fbs"
9
10OUTPUT_PROGRAM_HEADER = PROGRAM_STEM + "_generated.h"
11OUTPUT_SCALAR_TYPE_HEADER = SCALAR_TYPE_STEM + "_generated.h"
12
13PROGRAM_GEN_RULE_NAME = "generate_program"
14
15PROGRAM_LIRRARY_NAME = PROGRAM_STEM
16
17def _generate_schema_header(rule_name, srcs, headers, default_header):
18    """Generate header file given flatbuffer schema
19    """
20    runtime.genrule(
21        name = rule_name,
22        srcs = srcs,
23        # We're only generating a single file, so it seems like we could use
24        # `out`, but `flatc` takes a directory as a parameter, not a single
25        # file. Use `outs` so that `${OUT}` is expanded as the containing
26        # directory instead of the file itself.
27        outs = {header: [header] for header in headers},
28        default_outs = [default_header],
29        cmd = " ".join([
30            "$(exe {})".format(runtime.external_dep_location("flatc")),
31            "--cpp",
32            "--cpp-std c++11",
33            "--gen-mutable",
34            "--scoped-enums",
35            "-o ${OUT}",
36            "${SRCS}",
37            # Let our infra know that the file was generated.
38            " ".join(["&& echo // @" + "generated >> ${OUT}/" + header for header in headers]),
39        ]),
40        visibility = [],  # Private
41    )
42
43def define_common_targets():
44    """Defines targets that should be shared between fbcode and xplat.
45
46    The directory containing this targets.bzl file should also contain both
47    TARGETS and BUCK files that call this function.
48    """
49
50    runtime.export_file(
51        name = INPUT_PROGRAM,
52        visibility = [
53            "//executorch/exir/_serialize/...",
54        ],
55    )
56    runtime.export_file(
57        name = INPUT_SCALAR_TYPE,
58        visibility = [
59            "//executorch/exir/_serialize/...",
60            "//executorch/devtools/etdump/...",
61        ],
62    )
63
64    _generate_schema_header(
65        PROGRAM_GEN_RULE_NAME,
66        [INPUT_PROGRAM, INPUT_SCALAR_TYPE],
67        [OUTPUT_PROGRAM_HEADER, OUTPUT_SCALAR_TYPE_HEADER],
68        OUTPUT_PROGRAM_HEADER,
69    )
70
71    # Header-only library target with the generate executorch program schema header.
72    runtime.cxx_library(
73        name = PROGRAM_LIRRARY_NAME,
74        srcs = [],
75        visibility = [
76            # Lock this down as tightly as possible to ensure that flatbuffers
77            # are an implementation detail. Ideally this list would only include
78            # //executorch/runtime/executor/...
79            "//executorch/codegen/tools/...",
80            "//executorch/runtime/executor/...",
81        ],
82        exported_headers = {
83            OUTPUT_PROGRAM_HEADER: ":{}[{}]".format(PROGRAM_GEN_RULE_NAME, OUTPUT_PROGRAM_HEADER),
84            OUTPUT_SCALAR_TYPE_HEADER: ":{}[{}]".format(PROGRAM_GEN_RULE_NAME, OUTPUT_SCALAR_TYPE_HEADER),
85        },
86        exported_external_deps = ["flatbuffers-api"],
87    )
88
89    runtime.cxx_library(
90        name = "extended_header",
91        srcs = ["extended_header.cpp"],
92        exported_headers = [
93            "extended_header.h",
94        ],
95        visibility = [
96            "//executorch/runtime/executor/...",
97            "//executorch/schema/test/...",
98        ],
99        exported_deps = [
100            "//executorch/runtime/core:core",
101        ],
102    )
103