xref: /aosp_15_r20/external/bazelbuild-rules_license/examples/manifest/android_mock.bzl (revision f578df4fd057ffe2023728444759535685631548)
1load("@rules_license//rules:compliance.bzl", "manifest")
2
3"""This is a proof of concept to show how to modify a macro definition to
4create a sub-graph allowing for build time injection of license information. We
5use Android-inspired rule names since these are a likely candidate for this
6sort of injection."""
7
8def android_library(name, **kwargs):
9    # This is an approximation for demo purposes.
10
11    data = kwargs.pop("data", [])
12    native.filegroup(
13        name = name,
14        srcs = data + kwargs.get("srcs", []),
15    )
16
17    # Inject the data dependency into the library, preserving any other data it has.
18    native.sh_library(
19        name = name + "_w_licenses",
20        data = data + [name + "_manifest.txt"],
21        **kwargs
22    )
23
24def android_binary(name, **kwargs):
25    # Same observation about not being sloppy with mapping deps, but I think the only important attribute
26    # in android_binary is deps, but need to double-check.
27    native.filegroup(
28        name = name + "_no_licenses",
29        srcs = kwargs.get("data", []),
30    )
31
32    mf_name = name + "_manifest"
33    manifest(
34        name = mf_name,
35        deps = [":" + name + "_no_licenses"],
36    )
37
38    # This uses the conditions tool to generate an approximation of a compliance report
39    # to demonstrate how license data can be plumbed and made available at build time.
40    native.genrule(
41        name = "gen_" + name + "_manifest",
42        srcs = [":" + mf_name],
43        outs = ["licenses_manifest.txt"],
44        cmd = "cat $(locations :%s) > $@" % mf_name,
45    )
46
47    # Swap out the :licenses dep for our new :licenses_w_licenses dep
48    newdeps = []
49    deps = kwargs.get("data", [])
50    for dep in deps:
51        if dep == ":licenses":
52            newdeps.append(":licenses_w_licenses")
53        else:
54            newdeps.append(dep)
55    kwargs["data"] = newdeps
56
57    # Compile the executable with the user's originally supplied name, but with the new content.
58    native.sh_binary(
59        name = name,
60        **kwargs
61    )
62