xref: /aosp_15_r20/external/bazelbuild-rules_android/rules/android_neverlink_aspect.bzl (revision 9e965d6fece27a77de5377433c2f7e6999b8cc0b)
1# Copyright 2023 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
15"""Aspect to collect neverlink libraries in the transitive closure.
16
17Used for determining the -libraryjars argument for Proguard. The compile-time classpath is
18unsufficient here as those are ijars.
19"""
20
21load("//rules:acls.bzl", "acls")
22load(
23    "//rules:utils.bzl",
24    "utils",
25)
26
27StarlarkAndroidNeverlinkInfo = provider(
28    doc = "Contains all neverlink libraries in the transitive closure.",
29    fields = {
30        "transitive_neverlink_libraries": "Depset of transitive neverlink jars",
31    },
32)
33
34_ATTRS = ["deps", "exports", "runtime_deps", "binary_under_test", "$instrumentation_test_runner"]
35
36def _android_neverlink_aspect_impl(target, ctx):
37    if not acls.in_android_binary_starlark_dex_desugar_proguard(str(ctx.label)):
38        return []
39
40    # Only run on Android targets
41    if "android" not in getattr(ctx.rule.attr, "constraints", "") and not ctx.rule.kind.startswith("android_"):
42        return []
43
44    deps = []
45    for attr in _ATTRS:
46        if type(getattr(ctx.rule.attr, attr, None)) == "list":
47            deps.extend(getattr(ctx.rule.attr, attr))
48
49    direct_runtime_jars = depset(
50        target[JavaInfo].runtime_output_jars,
51        transitive = [target[AndroidLibraryResourceClassJarProvider].jars] if AndroidLibraryResourceClassJarProvider in target else [],
52    )
53
54    neverlink_libs = _collect_transitive_neverlink_libs(ctx, deps, direct_runtime_jars)
55
56    return [StarlarkAndroidNeverlinkInfo(transitive_neverlink_libraries = neverlink_libs)]
57
58def _collect_transitive_neverlink_libs(ctx, deps, runtime_jars):
59    neverlink_runtime_jars = []
60    for provider in utils.collect_providers(StarlarkAndroidNeverlinkInfo, deps):
61        neverlink_runtime_jars.append(provider.transitive_neverlink_libraries)
62
63    if getattr(ctx.rule.attr, "neverlink", False):
64        neverlink_runtime_jars.append(runtime_jars)
65        for java_info in utils.collect_providers(JavaInfo, deps):
66            neverlink_runtime_jars.append(java_info.transitive_runtime_jars)
67
68    return depset([], transitive = neverlink_runtime_jars)
69
70android_neverlink_aspect = aspect(
71    implementation = _android_neverlink_aspect_impl,
72    attr_aspects = _ATTRS,
73)
74