aboutsummaryrefslogtreecommitdiff
path: root/examples/manifest/android_mock.bzl
blob: 0dee3c930b46e86b1bc20255c83eb185b7fa6dd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
load("@rules_license//rules:compliance.bzl", "manifest")

"""This is a proof of concept to show how to modify a macro definition to
create a sub-graph allowing for build time injection of license information. We
use Android-inspired rule names since these are a likely candidate for this
sort of injection."""

def android_library(name, **kwargs):
    # This is an approximation for demo purposes.

    data = kwargs.pop("data", [])
    native.filegroup(
        name = name,
        srcs = data + kwargs.get("srcs", []),
    )

    # Inject the data dependency into the library, preserving any other data it has.
    native.sh_library(
        name = name + "_w_licenses",
        data = data + [name + "_manifest.txt"],
        **kwargs
    )

def android_binary(name, **kwargs):
    # Same observation about not being sloppy with mapping deps, but I think the only important attribute
    # in android_binary is deps, but need to double-check.
    native.filegroup(
        name = name + "_no_licenses",
        srcs = kwargs.get("data", []),
    )

    mf_name = name + "_manifest"
    manifest(
        name = mf_name,
        deps = [":" + name + "_no_licenses"],
    )

    # This uses the conditions tool to generate an approximation of a compliance report
    # to demonstrate how license data can be plumbed and made available at build time.
    native.genrule(
        name = "gen_" + name + "_manifest",
        srcs = [":" + mf_name],
        outs = ["licenses_manifest.txt"],
        cmd = "cat $(locations :%s) > $@" % mf_name,
    )

    # Swap out the :licenses dep for our new :licenses_w_licenses dep
    newdeps = []
    deps = kwargs.get("data", [])
    for dep in deps:
        if dep == ":licenses":
            newdeps.append(":licenses_w_licenses")
        else:
            newdeps.append(dep)
    kwargs["data"] = newdeps

    # Compile the executable with the user's originally supplied name, but with the new content.
    native.sh_binary(
        name = name,
        **kwargs
    )