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"""android_library rule.""" 16 17load("//build/bazel/rules/java:sdk_transition.bzl", "sdk_transition", "sdk_transition_attrs") 18load( 19 "//build/bazel/rules/android/android_library_aosp_internal:rule.bzl", 20 "android_library_aosp_internal_macro", 21) 22load("@rules_android//rules:providers.bzl", "StarlarkAndroidResourcesInfo") 23 24# TODO(b/277801336): document these attributes. 25def android_library( 26 name, 27 sdk_version = None, 28 java_version = None, 29 tags = [], 30 target_compatible_with = [], 31 visibility = None, 32 **attrs): 33 """ android_library macro wrapper that handles custom attrs needed in AOSP 34 35 Args: 36 name: the wrapper rule name. 37 sdk_version: string representing which sdk_version to build against. See 38 //build/bazel/rules/common/sdk_version.bzl for formatting and semantics. 39 java_version: string representing which version of java the java code in this rule should be 40 built with. 41 tags, target_compatible_with and visibility have Bazel's traditional semantics. 42 **attrs: Rule attributes 43 """ 44 lib_name = name + "_private" 45 android_library_aosp_internal_macro( 46 name = lib_name, 47 tags = tags + ["manual"], 48 target_compatible_with = target_compatible_with, 49 visibility = ["//visibility:private"], 50 **attrs 51 ) 52 53 android_library_sdk_transition( 54 aar = name + ".aar", 55 name = name, 56 sdk_version = sdk_version, 57 java_version = java_version, 58 exports = lib_name, 59 tags = tags, 60 target_compatible_with = target_compatible_with, 61 visibility = visibility, 62 ) 63 64# The list of providers to forward was determined using cquery on one 65# of the example targets listed under EXAMPLE_WRAPPER_TARGETS at 66# //build/bazel/ci/target_lists.sh. It may not be exhaustive. A unit 67# test ensures that the wrapper's providers and the wrapped rule's do 68# match. 69def _android_library_sdk_transition_impl(ctx): 70 ctx.actions.symlink( 71 output = ctx.outputs.aar, 72 target_file = ctx.attr.exports[0][AndroidIdeInfo].aar, 73 ) 74 75 providers = [] 76 if AndroidLibraryAarInfo in ctx.attr.exports[0]: 77 providers.append(ctx.attr.exports[0][AndroidLibraryAarInfo]) 78 return struct( 79 android = ctx.attr.exports[0].android, 80 java = ctx.attr.exports[0].java, 81 providers = providers + [ 82 ctx.attr.exports[0][StarlarkAndroidResourcesInfo], 83 ctx.attr.exports[0][AndroidLibraryResourceClassJarProvider], 84 ctx.attr.exports[0][AndroidIdlInfo], 85 ctx.attr.exports[0][DataBindingV2Info], 86 ctx.attr.exports[0][JavaInfo], 87 ctx.attr.exports[0][ProguardSpecProvider], 88 ctx.attr.exports[0][AndroidProguardInfo], 89 ctx.attr.exports[0][AndroidNativeLibsInfo], 90 ctx.attr.exports[0][AndroidCcLinkParamsInfo], 91 ctx.attr.exports[0][AndroidIdeInfo], 92 ctx.attr.exports[0][InstrumentedFilesInfo], 93 ctx.attr.exports[0][Actions], 94 ctx.attr.exports[0][OutputGroupInfo], 95 ctx.attr.exports[0][DefaultInfo], 96 ], 97 ) 98 99android_library_sdk_transition = rule( 100 implementation = _android_library_sdk_transition_impl, 101 attrs = sdk_transition_attrs | {"aar": attr.output()}, 102 provides = [ 103 AndroidCcLinkParamsInfo, 104 AndroidIdeInfo, 105 AndroidIdlInfo, 106 AndroidLibraryResourceClassJarProvider, 107 AndroidNativeLibsInfo, 108 JavaInfo, 109 ], 110) 111