aboutsummaryrefslogtreecommitdiff
path: root/rules/java/merged_txts.bzl
blob: c786055f15af130d74f09a4a8ca380308cb4a955 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
Copyright (C) 2023 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

load("//build/bazel/rules/java:sdk_library.bzl", "JavaSdkLibraryInfo")

METALAVA_ARGS = [
    "-J--add-opens=java.base/java.util=ALL-UNNAMED",
    "--quiet",
    "--no-banner",
    "--format=v2",
]

def _get_inputs(ctx):
    inputs = []
    inputs.extend(ctx.files.base)
    from_deps = []
    if ctx.attr.scope == "public":
        from_deps = [d[JavaSdkLibraryInfo].public for d in ctx.attr.deps]
    elif ctx.attr.scope == "system":
        from_deps = [d[JavaSdkLibraryInfo].system for d in ctx.attr.deps]
    elif ctx.attr.scope == "module-lib":
        from_deps = [d[JavaSdkLibraryInfo].module_lib for d in ctx.attr.deps]
    elif ctx.attr.scope == "system-server":
        from_deps = [d[JavaSdkLibraryInfo].system_server for d in ctx.attr.deps]
    inputs.extend(from_deps)
    return depset(inputs)

def _get_output_name(ctx):
    output_name = "current.txt"
    if ctx.attr.scope != "public":
        output_name = ctx.attr.scope + "-" + output_name
    return output_name

def _merged_txts_impl(ctx):
    output = ctx.actions.declare_file(_get_output_name(ctx))
    inputs = _get_inputs(ctx)
    args = ctx.actions.args()
    args.add_all(METALAVA_ARGS)
    args.add_all(inputs)
    args.add("--api", output)
    ctx.actions.run(
        outputs = [output],
        inputs = inputs,
        executable = ctx.executable._metalava,
        arguments = [args],
    )
    return [DefaultInfo(files = depset([output]))]

merged_txts = rule(
    implementation = _merged_txts_impl,
    attrs = {
        "scope": attr.string(
            doc = "api scope",
        ),
        "base": attr.label(
            mandatory = True,
            allow_single_file = True,
            doc = "the target used to get the checked-in base current.txt",
        ),
        "deps": attr.label_list(
            mandatory = True,
            allow_empty = False,
            providers = [JavaSdkLibraryInfo],
        ),
        "_metalava": attr.label(
            default = "//tools/metalava/metalava:metalava",
            executable = True,
            cfg = "exec",
        ),
    },
)