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