xref: /aosp_15_r20/build/bazel/rules/java/java_aidl_library.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1# Copyright (C) 2022 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
15load("//build/bazel/rules/aidl:aidl_library.bzl", "AidlGenInfo", "aidl_file_utils")
16load("//build/bazel/rules/java:sdk_transition.bzl", "sdk_transition")
17
18JavaAidlAspectInfo = provider("JavaAidlAspectInfo", fields = ["jars"])
19
20def _java_aidl_gen_aspect_impl(target, ctx):
21    aidl_gen_java_files = aidl_file_utils.generate_aidl_bindings(ctx, "java", target[AidlGenInfo])
22    java_deps = [
23        d[JavaInfo]
24        for d in ctx.rule.attr.deps
25    ]
26    out_jar = ctx.actions.declare_file(target.label.name + "-aidl-gen.jar")
27    java_info = java_common.compile(
28        ctx,
29        source_files = aidl_gen_java_files,
30        deps = java_deps,
31        output = out_jar,
32        java_toolchain = ctx.toolchains["@bazel_tools//tools/jdk:toolchain_type"].java,
33    )
34
35    return [
36        java_info,
37        JavaAidlAspectInfo(
38            jars = depset([out_jar]),
39        ),
40    ]
41
42_java_aidl_gen_aspect = aspect(
43    implementation = _java_aidl_gen_aspect_impl,
44    attr_aspects = ["deps"],
45    attrs = {
46        "_aidl_tool": attr.label(
47            allow_files = True,
48            executable = True,
49            cfg = "exec",
50            default = Label("//system/tools/aidl"),
51        ),
52    },
53    toolchains = ["@bazel_tools//tools/jdk:toolchain_type"],
54    fragments = ["java"],
55    provides = [JavaInfo, JavaAidlAspectInfo],
56)
57
58def _java_aidl_library_rule_impl(ctx):
59    java_info = java_common.merge([d[JavaInfo] for d in ctx.attr.deps])
60    runtime_jars = depset(transitive = [dep[JavaAidlAspectInfo].jars for dep in ctx.attr.deps])
61    transitive_runtime_jars = depset(transitive = [java_info.transitive_runtime_jars])
62
63    return [
64        java_info,
65        DefaultInfo(
66            files = runtime_jars,
67            runfiles = ctx.runfiles(transitive_files = transitive_runtime_jars),
68        ),
69        OutputGroupInfo(default = depset()),
70    ]
71
72java_aidl_library = rule(
73    implementation = _java_aidl_library_rule_impl,
74    attrs = {
75        # This attribute's name lets the DexArchiveAspect propagate
76        # through it. It should be changed carefully.
77        "deps": attr.label_list(
78            providers = [AidlGenInfo],
79            aspects = [_java_aidl_gen_aspect],
80            cfg = sdk_transition,
81        ),
82        "java_version": attr.string(),
83        "sdk_version": attr.string(
84            default = "system_current",
85        ),
86        "_allowlist_function_transition": attr.label(
87            default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
88        ),
89    },
90    provides = [JavaInfo],
91)
92