xref: /aosp_15_r20/external/bazelbuild-rules_android/mobile_install/adapters/java_import.bzl (revision 9e965d6fece27a77de5377433c2f7e6999b8cc0b)
1# Copyright 2018 The Bazel Authors. All rights reserved.
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"""Rule adapter for java_import."""
15
16load(":adapters/base.bzl", "make_adapter")
17load(
18    ":providers.bzl",
19    "MIAndroidDexInfo",
20    "MIJavaResourcesInfo",
21    "providers",
22)
23load(":transform.bzl", "dex", "extract_jar_resources")
24load(":utils.bzl", "utils")
25
26def _aspect_attrs():
27    """Attrs of the rule requiring traversal by the aspect."""
28    return ["deps", "exports"]
29
30def _adapt(target, ctx):
31    """Adapts the rule and target data.
32
33    Args:
34      target: The target.
35      ctx: The context.
36
37    Returns:
38      A list of providers.
39    """
40    if ctx.rule.attr.neverlink:
41        return []
42
43    return [
44        providers.make_mi_android_dex_info(
45            dex_shards = dex(
46                ctx,
47                target[JavaInfo].runtime_output_jars,
48                target[JavaInfo].transitive_compile_time_jars,
49                create_file = utils.declare_file,
50            ),
51            deps = providers.collect(
52                MIAndroidDexInfo,
53                ctx.rule.attr.deps,
54                ctx.rule.attr.exports,
55            ),
56        ),
57        providers.make_mi_java_resources_info(
58            java_resources = extract_jar_resources(
59                ctx,
60                target[JavaInfo].runtime_output_jars,
61                create_file = utils.declare_file,
62            ),
63            deps = providers.collect(
64                MIJavaResourcesInfo,
65                ctx.rule.attr.deps,
66                ctx.rule.attr.exports,
67            ),
68        ),
69    ]
70
71java_import = make_adapter(_aspect_attrs, _adapt)
72