xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/bazel/embed_data.bzl (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1# Copyright 2019 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Embeds binary data in cc_*() rules."""
16
17_FILEWRAPPER = "//sandboxed_api/tools/filewrapper"
18
19# TODO(cblichmann): Convert this to use a "_cc_toolchain" once Bazel #4370 is
20#                   fixed.
21def _sapi_cc_embed_data_impl(ctx):
22    cc_file_artifact = None
23    h_file_artifact = None
24    for output in ctx.outputs.outs:
25        if output.path.endswith(".h"):
26            h_file_artifact = output
27        elif output.path.endswith(".cc") or output.path.endswith(".cpp"):
28            cc_file_artifact = output
29
30    args = ctx.actions.args()
31    args.add(ctx.label.package)
32    args.add(ctx.attr.ident)
33    args.add(ctx.attr.namespace if ctx.attr.namespace else "")
34    args.add(h_file_artifact)
35    args.add(cc_file_artifact)
36    args.add_all(ctx.files.srcs)
37
38    ctx.actions.run(
39        executable = ctx.executable._filewrapper,
40        inputs = ctx.files.srcs,
41        outputs = [h_file_artifact, cc_file_artifact],
42        arguments = [args],
43        mnemonic = "CcEmbedData",
44        progress_message = (
45            "Creating sapi_cc_embed_data file for {}".format(ctx.label)
46        ),
47    )
48
49_sapi_cc_embed_data = rule(
50    implementation = _sapi_cc_embed_data_impl,
51    output_to_genfiles = True,
52    attrs = {
53        "srcs": attr.label_list(
54            allow_files = True,
55        ),
56        "namespace": attr.string(),
57        "ident": attr.string(),
58        "_filewrapper": attr.label(
59            executable = True,
60            cfg = "exec",
61            allow_files = True,
62            default = Label(_FILEWRAPPER),
63        ),
64        "outs": attr.output_list(),
65    },
66)
67
68def sapi_cc_embed_data(name, srcs = [], namespace = "", **kwargs):
69    """Embeds arbitrary binary data in cc_*() rules.
70
71    Args:
72      name: Name for this rule.
73      srcs: A list of files to be embedded.
74      namespace: C++ namespace to wrap the generated types in.
75      **kwargs: extra arguments like testonly, visibility, etc.
76    """
77    embed_rule = "_%s_sapi" % name
78    _sapi_cc_embed_data(
79        name = embed_rule,
80        srcs = srcs,
81        namespace = namespace,
82        ident = name,
83        outs = [
84            "%s.h" % name,
85            "%s.cc" % name,
86        ],
87    )
88    native.cc_library(
89        name = name,
90        hdrs = [":%s.h" % name],
91        srcs = [":%s.cc" % name],
92        deps = [
93            "@com_google_absl//absl/base:core_headers",
94            "@com_google_absl//absl/strings",
95        ],
96        **kwargs
97    )
98