xref: /aosp_15_r20/build/bazel/rules/java/merged_txts.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1"""
2Copyright (C) 2023 The Android Open Source Project
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15"""
16
17load("//build/bazel/rules/java:sdk_library.bzl", "JavaSdkLibraryInfo")
18
19METALAVA_ARGS = [
20    "--quiet",
21    "--format=v2",
22]
23
24def _get_inputs(ctx):
25    inputs = []
26    inputs.extend(ctx.files.base)
27    from_deps = []
28    if ctx.attr.scope == "public":
29        from_deps = [d[JavaSdkLibraryInfo].public for d in ctx.attr.deps]
30    elif ctx.attr.scope == "system":
31        from_deps = [d[JavaSdkLibraryInfo].system for d in ctx.attr.deps]
32    elif ctx.attr.scope == "module-lib":
33        from_deps = [d[JavaSdkLibraryInfo].module_lib for d in ctx.attr.deps]
34    elif ctx.attr.scope == "system-server":
35        from_deps = [d[JavaSdkLibraryInfo].system_server for d in ctx.attr.deps]
36    inputs.extend(from_deps)
37    return depset(inputs)
38
39def _get_output_name(ctx):
40    output_name = "current.txt"
41    if ctx.attr.scope != "public":
42        output_name = ctx.attr.scope + "-" + output_name
43    return output_name
44
45def _merged_txts_impl(ctx):
46    output = ctx.actions.declare_file(_get_output_name(ctx))
47    inputs = _get_inputs(ctx)
48    args = ctx.actions.args()
49    args.add_all(METALAVA_ARGS)
50    args.add_all(inputs)
51    args.add("--api", output)
52    ctx.actions.run(
53        outputs = [output],
54        inputs = inputs,
55        executable = ctx.executable._metalava,
56        arguments = [args],
57    )
58    return [DefaultInfo(files = depset([output]))]
59
60merged_txts = rule(
61    implementation = _merged_txts_impl,
62    attrs = {
63        "scope": attr.string(
64            doc = "api scope",
65        ),
66        "base": attr.label(
67            mandatory = True,
68            allow_single_file = True,
69            doc = "the target used to get the checked-in base current.txt",
70        ),
71        "deps": attr.label_list(
72            mandatory = True,
73            allow_empty = False,
74            providers = [JavaSdkLibraryInfo],
75        ),
76        "_metalava": attr.label(
77            default = "//tools/metalava/metalava:metalava",
78            executable = True,
79            cfg = "exec",
80        ),
81    },
82)
83