1""" 2Copyright (C) 2022 The Android Open Source Project 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15""" 16 17load("@bazel_skylib//lib:paths.bzl", "paths") 18load("@rules_android//rules:rules.bzl", _android_binary = "android_binary") 19load("@soong_injection//product_config:product_variables.bzl", "product_vars") 20 21load("android_app_certificate.bzl", "android_app_certificate") 22load("android_app_keystore.bzl", "android_app_keystore") 23 24 25def _default_cert_prod_var(): 26 return product_vars["DefaultAppCertificate"] 27 28def _default_app_certificate_package(): 29 default_cert = _default_cert_prod_var() 30 if default_cert: 31 return "//" + paths.dirname(default_cert) 32 # if product variable is not set, default to Soong default: 33 return "//build/make/target/product/security" 34 35def _default_app_certificate(): 36 default_cert = _default_cert_prod_var() 37 if default_cert: 38 return default_cert 39 return _default_app_certificate_package() + ":testkey" 40 41def _android_app_certificate_with_default_cert(name, cert_name): 42 43 if cert_name: 44 # if a specific certificate name is given, check the default directory 45 # for that certificate 46 certificate = _default_app_certificate_package() + ":" + cert_name 47 else: 48 certificate = _default_app_certificate() 49 50 android_app_certificate( 51 name = name, 52 certificate = certificate, 53 ) 54 55def android_binary( 56 name, 57 certificate = None, 58 certificate_name = None, 59 **kwargs): 60 """Bazel macro to find and create a keystore to use for debug_signing_keys 61 with @rules_android android_binary. 62 63 This module emulates the Soong behavior which allows a developer to specify 64 a specific module name for the android_app_certificate or the name of a 65 .pem/.pk8 certificate/key pair in a directory specified by the 66 DefaultAppCertificate product variable. In either case, we convert the specified 67 .pem/.pk8 certificate/key pair to a JKS .keystore file before passing it to the 68 android_binary rule. 69 70 Arguments: 71 certificate: Bazel target 72 certificate_name: string, name of private key file in default certificate directory 73 **kwargs: map, additional args to pass to android_binary 74 """ 75 76 if certificate and certificate_name: 77 fail("Cannot use both certificate_name and certificate attributes together. Use only one of them.") 78 79 debug_signing_keys = kwargs.pop("debug_signing_keys", []) 80 81 if certificate or certificate_name: 82 if certificate_name: 83 app_cert_name = name + "_app_certificate" 84 _android_app_certificate_with_default_cert(app_cert_name, certificate_name) 85 certificate = ":" + app_cert_name 86 87 app_keystore_name = name + "_keystore" 88 android_app_keystore( 89 name = app_keystore_name, 90 certificate = certificate 91 ) 92 93 debug_signing_keys.append(app_keystore_name) 94 95 _android_binary( 96 name = name, 97 debug_signing_keys = debug_signing_keys, 98 **kwargs 99 ) 100