1# Copyright (C) 2023 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Macro wrapping the java_aconfig_library for bp2build. """
16
17load("@bazel_skylib//lib:paths.bzl", "paths")
18load("//build/bazel/rules/aconfig:aconfig_declarations.bzl", "AconfigDeclarationsInfo")
19load("//build/bazel/rules/java:sdk_transition.bzl", "sdk_transition")
20
21def _java_aconfig_library_impl(ctx):
22    gen_dir_str = paths.join(ctx.label.name, "gen")
23
24    aconfig_declarations = ctx.attr.aconfig_declarations[AconfigDeclarationsInfo]
25    gen_srcjar = ctx.actions.declare_file(paths.join(gen_dir_str, ctx.label.name + ".srcjar"))
26
27    # TODO(b/301457407): find a solution for declare_directory.
28    gen_srcjar_tmp = ctx.actions.declare_directory("tmp", sibling = gen_srcjar)
29
30    intermediate_path = aconfig_declarations.intermediate_path
31
32    mode = "production"
33    if ctx.attr.test:
34        mode = "test"
35
36    args = ctx.actions.args()
37    args.add("create-java-lib")
38    args.add_all(["--mode", mode])
39    args.add_all(["--cache", intermediate_path])
40    args.add_all(["--out", gen_srcjar_tmp.path])
41
42    ctx.actions.run(
43        inputs = [intermediate_path],
44        executable = ctx.executable._aconfig,
45        outputs = [gen_srcjar_tmp],
46        arguments = [args],
47        tools = [
48            ctx.executable._aconfig,
49        ],
50        mnemonic = "AconfigCreateJavaLib",
51    )
52
53    args = ctx.actions.args()
54    args.add("-write_if_changed")
55    args.add("-jar")
56    args.add("-o", gen_srcjar)
57    args.add("-C", gen_srcjar_tmp.path)
58    args.add("-D", gen_srcjar_tmp.path)
59    args.add("-symlinks=false")
60
61    ctx.actions.run(
62        executable = ctx.executable._soong_zip,
63        inputs = [gen_srcjar_tmp],
64        outputs = [gen_srcjar],
65        arguments = [args],
66        tools = [
67            ctx.executable._soong_zip,
68        ],
69        mnemonic = "AconfigZipJavaLib",
70    )
71
72    out_file = ctx.actions.declare_file(ctx.label.name + ".jar")
73    java_info = java_common.compile(
74        ctx,
75        source_jars = [gen_srcjar],
76        deps = [d[JavaInfo] for d in ctx.attr.libs],
77        output = out_file,
78        java_toolchain = ctx.toolchains["@bazel_tools//tools/jdk:toolchain_type"].java,
79    )
80
81    return [
82        java_info,
83        DefaultInfo(
84            files = depset([out_file]),
85            runfiles = ctx.runfiles(
86                transitive_files = depset(
87                    transitive = [java_info.transitive_runtime_jars],
88                ),
89            ),
90        ),
91        OutputGroupInfo(
92            srcjar = depset([gen_srcjar]),
93        ),
94    ]
95
96_java_aconfig_library = rule(
97    implementation = _java_aconfig_library_impl,
98    cfg = sdk_transition,
99    attrs = {
100        "aconfig_declarations": attr.label(
101            providers = [AconfigDeclarationsInfo],
102            mandatory = True,
103        ),
104        "libs": attr.label_list(
105            providers = [JavaInfo],
106        ),
107        "test": attr.bool(default = False),
108        "java_version": attr.string(),
109        "sdk_version": attr.string(),
110        "_aconfig": attr.label(
111            allow_single_file = True,
112            executable = True,
113            cfg = "exec",
114            default = Label("//build/make/tools/aconfig:aconfig"),
115        ),
116        "_soong_zip": attr.label(
117            executable = True,
118            cfg = "exec",
119            allow_single_file = True,
120            default = Label("//build/soong/zip/cmd:soong_zip"),
121        ),
122        "_allowlist_function_transition": attr.label(
123            default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
124        ),
125    },
126    toolchains = ["@bazel_tools//tools/jdk:toolchain_type"],
127    fragments = ["java"],
128    provides = [JavaInfo],
129)
130
131def java_aconfig_library(
132        name,
133        aconfig_declarations,
134        test = False,
135        sdk_version = "system_current",
136        java_version = None,
137        visibility = None,
138        libs = [],
139        tags = [],
140        target_compatible_with = []):
141    combined_libs = [
142        "//frameworks/libs/modules-utils/java:aconfig-annotations-lib",
143        "//tools/platform-compat/java/android/compat/annotation:unsupportedappusage",
144    ] + libs
145    _java_aconfig_library(
146        name = name,
147        aconfig_declarations = aconfig_declarations,
148        libs = combined_libs,
149        test = test,
150        sdk_version = sdk_version,
151        java_version = java_version,
152        visibility = visibility,
153        tags = tags,
154        target_compatible_with = target_compatible_with,
155    )
156
157    native.filegroup(
158        name = name + ".generated_srcjars",
159        srcs = [name],
160        output_group = "srcjar",
161        visibility = visibility,
162        tags = tags,
163    )
164