xref: /aosp_15_r20/build/bazel/rules/android/aar_import.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
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 aar_import for bp2build. """
16
17load("//build/bazel/rules/android/aar_import_aosp_internal:rule.bzl", _aar_import = "aar_import")
18load("//build/bazel/rules/java:sdk_transition.bzl", "sdk_transition_attrs")
19load("@rules_android//rules:providers.bzl", "StarlarkAndroidResourcesInfo")
20
21# TODO(b/277801336): document these attributes.
22def aar_import(
23        name = "",
24        aar = [],
25        sdk_version = None,
26        deps = [],
27        tags = [],
28        target_compatible_with = [],
29        visibility = None,
30        **kwargs):
31    lib_name = name + "_private"
32    _aar_import(
33        name = lib_name,
34        aar = aar,
35        deps = deps,
36        tags = tags + ["manual"],
37        target_compatible_with = target_compatible_with,
38        visibility = ["//visibility:private"],
39        **kwargs
40    )
41
42    aar_import_sdk_transition(
43        name = name,
44        sdk_version = sdk_version,
45        java_version = None,
46        exports = lib_name,
47        tags = tags,
48        target_compatible_with = target_compatible_with,
49        visibility = visibility,
50    )
51
52# The list of providers to forward was determined using cquery on one
53# of the example targets listed under EXAMPLE_WRAPPER_TARGETS at
54# //build/bazel/ci/target_lists.sh. It may not be exhaustive. A unit
55# test ensures that the wrapper's providers and the wrapped rule's do
56# match.
57def _aar_import_sdk_transition_impl(ctx):
58    return [
59        ctx.attr.exports[0][AndroidLibraryResourceClassJarProvider],
60        ctx.attr.exports[0][JavaInfo],
61        ctx.attr.exports[0][AndroidNativeLibsInfo],
62        ctx.attr.exports[0][ProguardSpecProvider],
63        ctx.attr.exports[0][AndroidIdeInfo],
64        ctx.attr.exports[0][DefaultInfo],
65        ctx.attr.exports[0][StarlarkAndroidResourcesInfo],
66    ]
67
68aar_import_sdk_transition = rule(
69    implementation = _aar_import_sdk_transition_impl,
70    attrs = sdk_transition_attrs,
71    provides = [
72        AndroidIdeInfo,
73        AndroidLibraryResourceClassJarProvider,
74        AndroidNativeLibsInfo,
75        JavaInfo,
76    ],
77)
78