xref: /aosp_15_r20/external/federated-compute/fcp/tracing/build_defs.bzl (revision 14675a029014e728ec732f129a32e299b2da0601)
1"""Build rule for tracing schemas to be used with fcp/tracing library.
2"""
3
4load("//fcp:config.bzl", "FCP_COPTS")
5load("@flatbuffers//:build_defs.bzl", "flatbuffer_cc_library")
6
7def tracing_schema_cc_library(
8        name,
9        srcs,
10        includes = [],
11        visibility = None,
12        testonly = None):
13    """Rule to generate tracing-schema C++ files from .fbs file.
14
15    This macro produces the following output:
16
17    <name>: a cc_library including C++ code generated from .fbs file.
18
19    <srcs>.h: header file to be included by the code using the tracing schema.
20
21    <srcs>.bfbs: binary representation of the tracing schema.
22
23    <srcs>_generated.h: a header file produced by flatc. Note: this file is
24      already included by <srcs>.h, not to be consumed directly.
25
26    Args:
27      name: The label for the library, typically "tracing_schema".
28      srcs: Single .fbs source file, typically [ "tracing_schema.fbs" ].
29      includes: optional list of .fbs includes
30      visibility: standard visibility
31      testonly: standard testonly
32    """
33
34    # Validate only 1 src file is specified in the build rule.
35    if (len(srcs) != 1):
36        fail("Only 1 .fbs file can be specified per build rule.")
37
38    # Rule to invoke flatc on the fbs file (produces <name>_generated.h):
39    flatbuffer_cc_library(
40        name = name + "_fb",
41        srcs = srcs,
42        includes =
43            includes + ["//fcp/tracing:tracing_schema_common_fbs"],
44        flatc_args = [
45            "--gen-object-api",
46            "--gen-generated",
47            "--reflect-names",
48            "--bfbs-comments",
49            "--keep-prefix",
50        ],
51        gen_reflections = True,
52        include_paths = [".", "third_party/fcp/tracing"],
53    )
54
55    # Get generated flatbuff files from flatbuffer_cc_library rule.
56    src_bfbs = srcs[0].replace(".fbs", ".bfbs")
57    src_generated_h = srcs[0].replace(".fbs", "_generated.h")
58    src_generated_h_rootpath = "$(rootpath " + src_generated_h + ") "
59    src_bfbs_rootpath = "$(location " + src_bfbs + ") "
60    src_fbs_rootpath = "$(rootpath " + srcs[0] + ")"
61    out_header = srcs[0].replace(".fbs", ".h")
62
63    # Generating <name>.h with additional traits
64    native.genrule(
65        name = name + "_h",
66        srcs = [
67            src_bfbs,
68            src_generated_h,
69            srcs[0],
70        ],
71        outs = [out_header],
72        cmd = ("$(location //fcp/tracing/tools:tracing_traits_generator) " +
73               src_generated_h_rootpath + src_bfbs_rootpath + src_fbs_rootpath + "> $@"),
74        tools = [
75            "//fcp/tracing/tools:tracing_traits_generator",
76        ],
77    )
78
79    # Packaging everything into cc_library:
80    native.cc_library(
81        name = name,
82        hdrs = [
83            ":" + out_header,
84            "//fcp/tracing:tracing_schema_common_generated.h",
85            "//fcp/tracing:tracing_severity.h",
86            "//fcp/tracing:tracing_tag.h",
87            "//fcp/tracing:tracing_traits.h",
88        ],
89        deps = [
90            ":" + name + "_fb",
91            "@com_google_absl//absl/base:core_headers",
92            "@com_google_absl//absl/strings",
93            "@com_google_absl//absl/memory",
94            "@flatbuffers//:flatbuffers",
95            "//fcp/base",
96        ],
97        data = [
98            srcs[0],
99            "//fcp/tracing:tracing_schema_common.fbs",
100        ],
101        copts = FCP_COPTS,
102        visibility = visibility,
103        testonly = testonly,
104    )
105