1# Copyright (C) 2022 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 15load("@rules_android//rules:common.bzl", "common") 16load("@rules_android//rules:migration_tag_DONOTUSE.bzl", "add_migration_tag") 17load( 18 "//build/bazel/rules/android/android_binary_aosp_internal:rule.bzl", 19 "android_binary_aosp_internal_macro", 20) 21load("//build/bazel/rules/java:sdk_transition.bzl", "sdk_transition_attrs") 22load(":debug_signing_key.bzl", "debug_signing_key") 23 24# TODO(b/277801336): document these attributes. 25def _android_binary_helper(**attrs): 26 """ Duplicates the logic in top-level android_binary macro in 27 rules_android/rules/android_binary.bzl but uses 28 android_binary_aosp_internal_macro instead of android_binary_internal_macro. 29 30 https://docs.bazel.build/versions/master/be/android.html#android_binary 31 32 Args: 33 **attrs: Rule attributes 34 """ 35 android_binary_aosp_internal_name = ":" + attrs["name"] + common.PACKAGED_RESOURCES_SUFFIX 36 android_binary_aosp_internal_macro( 37 **dict( 38 attrs, 39 name = android_binary_aosp_internal_name[1:], 40 visibility = ["//visibility:private"], 41 ) 42 ) 43 44 # The following attributes are unknown the native android_binary rule and must be removed 45 # prior to instantiating it. 46 attrs.pop("$enable_manifest_merging", None) 47 attrs["proguard_specs"] = [] 48 attrs.pop("sdk_version") 49 if "updatable" in attrs: 50 attrs.pop("updatable") 51 52 native.android_binary( 53 application_resources = android_binary_aosp_internal_name, 54 **add_migration_tag(attrs) 55 ) 56 57def android_binary( 58 name, 59 certificate = None, 60 certificate_name = None, 61 sdk_version = None, 62 errorprone_force_enable = None, 63 javacopts = [], 64 java_version = None, 65 optimize = True, 66 tags = [], 67 target_compatible_with = [], 68 testonly = False, 69 visibility = None, 70 **kwargs): 71 """ android_binary macro wrapper that handles custom attrs needed in AOSP 72 Bazel macro to find and create a keystore to use for debug_signing_keys 73 with @rules_android android_binary. 74 75 This module emulates the Soong behavior which allows a developer to specify 76 a specific module name for the android_app_certificate or the name of a 77 .pem/.pk8 certificate/key pair in a directory specified by the 78 DefaultAppCertificate product variable. In either case, we convert the specified 79 .pem/.pk8 certificate/key pair to a JKS .keystore file before passing it to the 80 android_binary rule. 81 82 Arguments: 83 certificate: Bazel target 84 certificate_name: string, name of private key file in default certificate directory 85 errorprone_force_enable: set this to true to always run Error Prone 86 on this target (overriding the value of environment variable 87 RUN_ERROR_PRONE). Error Prone can be force disabled for an individual 88 module by adding the "-XepDisableAllChecks" flag to javacopts 89 **kwargs: map, additional args to pass to android_binary 90 91 """ 92 93 opts = javacopts 94 if errorprone_force_enable == None: 95 # TODO (b/227504307) temporarily disable errorprone until environment variable is handled 96 opts = opts + ["-XepDisableAllChecks"] 97 98 debug_signing_keys = kwargs.pop("debug_signing_keys", []) 99 debug_signing_keys.extend(debug_signing_key(name, certificate, certificate_name)) 100 101 if optimize: 102 kwargs["proguard_specs"] = [ 103 "//build/make/core:global_proguard_flags", 104 ] + kwargs.get("proguard_specs", []) 105 106 bin_name = name + "_private" 107 _android_binary_helper( 108 name = bin_name, 109 debug_signing_keys = debug_signing_keys, 110 javacopts = opts, 111 target_compatible_with = target_compatible_with, 112 tags = tags + ["manual"], 113 testonly = testonly, 114 visibility = ["//visibility:private"], 115 sdk_version = sdk_version, 116 **kwargs 117 ) 118 119 android_binary_sdk_transition( 120 name = name, 121 sdk_version = sdk_version, 122 java_version = java_version, 123 exports = bin_name, 124 tags = tags, 125 target_compatible_with = target_compatible_with, 126 testonly = testonly, 127 visibility = visibility, 128 ) 129 130# The list of providers to forward was determined using cquery on one 131# of the example targets listed under EXAMPLE_WRAPPER_TARGETS at 132# //build/bazel/ci/target_lists.sh. It may not be exhaustive. A unit 133# test ensures that the wrapper's providers and the wrapped rule's do 134# match. 135def _android_binary_sdk_transition_impl(ctx): 136 return struct( 137 android = ctx.attr.exports[0].android, 138 providers = [ 139 ctx.attr.exports[0][AndroidIdlInfo], 140 ctx.attr.exports[0][InstrumentedFilesInfo], 141 ctx.attr.exports[0][DataBindingV2Info], 142 ctx.attr.exports[0][JavaInfo], 143 ctx.attr.exports[0][AndroidIdeInfo], 144 ctx.attr.exports[0][ApkInfo], 145 ctx.attr.exports[0][AndroidPreDexJarInfo], 146 ctx.attr.exports[0][AndroidFeatureFlagSet], 147 ctx.attr.exports[0][OutputGroupInfo], 148 ctx.attr.exports[0][DefaultInfo], 149 ], 150 ) 151 152android_binary_sdk_transition = rule( 153 implementation = _android_binary_sdk_transition_impl, 154 attrs = sdk_transition_attrs, 155) 156