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