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