• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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("android_app_certificate.bzl", "android_app_certificate_with_default_cert")
22load("android_app_keystore.bzl", "android_app_keystore")
23load("//build/bazel/rules/java:sdk_transition.bzl", "sdk_transition", "sdk_transition_attrs")
24
25# TODO(b/277801336): document these attributes.
26def _android_binary_helper(**attrs):
27    """ Duplicates the logic in top-level android_binary macro in
28        rules_android/rules/android_binary.bzl but uses
29        android_binary_aosp_internal_macro instead of android_binary_internal_macro.
30
31        https://docs.bazel.build/versions/master/be/android.html#android_binary
32
33        Args:
34          **attrs: Rule attributes
35    """
36    android_binary_aosp_internal_name = ":" + attrs["name"] + common.PACKAGED_RESOURCES_SUFFIX
37    android_binary_aosp_internal_macro(
38        **dict(
39            attrs,
40            name = android_binary_aosp_internal_name[1:],
41            visibility = ["//visibility:private"],
42        )
43    )
44
45    attrs.pop("$enable_manifest_merging", None)
46
47    native.android_binary(
48        application_resources = android_binary_aosp_internal_name,
49        **add_migration_tag(attrs)
50    )
51
52def android_binary(
53        name,
54        certificate = None,
55        certificate_name = None,
56        sdk_version = None,
57        java_version = None,
58        tags = [],
59        target_compatible_with = [],
60        visibility = None,
61        **kwargs):
62    """ android_binary macro wrapper that handles custom attrs needed in AOSP
63       Bazel macro to find and create a keystore to use for debug_signing_keys
64       with @rules_android android_binary.
65
66    This module emulates the Soong behavior which allows a developer to specify
67    a specific module name for the android_app_certificate or the name of a
68    .pem/.pk8 certificate/key pair in a directory specified by the
69    DefaultAppCertificate product variable. In either case, we convert the specified
70    .pem/.pk8 certificate/key pair to a JKS .keystore file before passing it to the
71    android_binary rule.
72
73    Arguments:
74        certificate: Bazel target
75        certificate_name: string, name of private key file in default certificate directory
76        **kwargs: map, additional args to pass to android_binary
77    """
78
79    if certificate and certificate_name:
80        fail("Cannot use both certificate_name and certificate attributes together. Use only one of them.")
81
82    debug_signing_keys = kwargs.pop("debug_signing_keys", [])
83
84    if certificate or certificate_name:
85        if certificate_name:
86            app_cert_name = name + "_app_certificate"
87            android_app_certificate_with_default_cert(
88                name = app_cert_name,
89                cert_name = certificate_name,
90            )
91            certificate = ":" + app_cert_name
92
93        app_keystore_name = name + "_keystore"
94        android_app_keystore(
95            name = app_keystore_name,
96            certificate = certificate,
97        )
98
99        debug_signing_keys.append(app_keystore_name)
100
101    bin_name = name + "_private"
102    _android_binary_helper(
103        name = bin_name,
104        debug_signing_keys = debug_signing_keys,
105        target_compatible_with = target_compatible_with,
106        tags = tags + ["manual"],
107        visibility = ["//visibility:private"],
108        **kwargs
109    )
110
111    android_binary_sdk_transition(
112        name = name,
113        sdk_version = sdk_version,
114        java_version = java_version,
115        exports = bin_name,
116        tags = tags,
117        target_compatible_with = target_compatible_with,
118        visibility = visibility,
119    )
120
121# The list of providers to forward was determined using cquery on one
122# of the example targets listed under EXAMPLE_WRAPPER_TARGETS at
123# //build/bazel/ci/target_lists.sh. It may not be exhaustive. A unit
124# test ensures that the wrapper's providers and the wrapped rule's do
125# match.
126def _android_binary_sdk_transition_impl(ctx):
127    return struct(
128        android = ctx.attr.exports[0].android,
129        JavaGenJarsProvider = ctx.attr.exports[0][JavaInfo].annotation_processing,
130        providers = [
131            ctx.attr.exports[0][AndroidIdlInfo],
132            ctx.attr.exports[0][InstrumentedFilesInfo],
133            ctx.attr.exports[0][DataBindingV2Info],
134            ctx.attr.exports[0][JavaInfo],
135            ctx.attr.exports[0][AndroidIdeInfo],
136            ctx.attr.exports[0][ApkInfo],
137            ctx.attr.exports[0][AndroidPreDexJarInfo],
138            ctx.attr.exports[0][AndroidFeatureFlagSet],
139            ctx.attr.exports[0][OutputGroupInfo],
140            ctx.attr.exports[0][DefaultInfo],
141        ],
142    )
143
144android_binary_sdk_transition = rule(
145    implementation = _android_binary_sdk_transition_impl,
146    attrs = sdk_transition_attrs,
147)
148