1# Copyright 2018 The Bazel Authors. All rights reserved. 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"""Bazel common library for the Android rules.""" 16 17load(":java.bzl", _java = "java") 18load(":utils.bzl", "get_android_sdk", "get_android_toolchain", _log = "log") 19 20# TODO(ostonge): Remove once kotlin/jvm_library.internal.bzl 21# is updated and released to use the java.resolve_package function 22def _java_package(label, custom_package): 23 return _java.resolve_package_from_label(label, custom_package) 24 25# Validates that the packages listed under "deps" all have the given constraint. If a package 26# does not have this attribute, an error is generated. 27def _validate_constraints(targets, constraint): 28 for target in targets: 29 if JavaInfo in target: 30 if constraint not in java_common.get_constraints(target[JavaInfo]): 31 _log.error("%s: does not have constraint '%s'" % (target.label, constraint)) 32 33TARGET_DNE = "Target '%s' does not exist or is a file and is not allowed." 34 35def _check_rule(targets): 36 _validate_constraints(targets, "android") 37 38def _get_java_toolchain(ctx): 39 if not hasattr(ctx.attr, "_java_toolchain"): 40 _log.error("Missing _java_toolchain attr") 41 return ctx.attr._java_toolchain 42 43def _get_host_javabase(ctx): 44 if not hasattr(ctx.attr, "_host_javabase"): 45 _log.error("Missing _host_javabase attr") 46 return ctx.attr._host_javabase 47 48def _sign_apk(ctx, unsigned_apk, signed_apk, keystore = None, signing_keys = [], signing_lineage = None): 49 """Signs an apk. Usage of keystore is deprecated. Prefer using signing_keys.""" 50 inputs = [unsigned_apk] 51 signer_args = ctx.actions.args() 52 signer_args.add("sign") 53 54 if signing_keys: 55 inputs.extend(signing_keys) 56 for i, key in enumerate(signing_keys): 57 if i > 0: 58 signer_args.add("--next-signer") 59 signer_args.add("--ks") 60 signer_args.add(key.path) 61 signer_args.add("--ks-pass") 62 signer_args.add("pass:android") 63 if signing_lineage: 64 inputs.append(signing_lineage) 65 signer_args.add("--lineage", signing_lineage.path) 66 elif keystore: 67 inputs.append(keystore) 68 signer_args.add("--ks", keystore.path) 69 signer_args.add("--ks-pass", "pass:android") 70 71 signer_args.add("--v1-signing-enabled", ctx.fragments.android.apk_signing_method_v1) 72 signer_args.add("--v1-signer-name", "CERT") 73 signer_args.add("--v2-signing-enabled", ctx.fragments.android.apk_signing_method_v2) 74 signer_args.add("--out", signed_apk.path) 75 signer_args.add(unsigned_apk.path) 76 ctx.actions.run( 77 executable = get_android_sdk(ctx).apk_signer, 78 inputs = inputs, 79 outputs = [signed_apk], 80 arguments = [signer_args], 81 mnemonic = "ApkSignerTool", 82 progress_message = "Signing APK for %s" % unsigned_apk.path, 83 ) 84 return signed_apk 85 86def _filter_zip(ctx, in_zip, out_zip, filters = []): 87 """Creates a copy of a zip file with files that match filters.""" 88 args = ctx.actions.args() 89 args.add("-q") 90 args.add(in_zip.path) 91 args.add_all(filters) 92 args.add("--copy") 93 args.add("--out") 94 args.add(out_zip.path) 95 ctx.actions.run( 96 executable = get_android_toolchain(ctx).zip_tool.files_to_run, 97 arguments = [args], 98 inputs = [in_zip], 99 outputs = [out_zip], 100 mnemonic = "FilterZip", 101 progress_message = "Filtering %s" % in_zip.short_path, 102 ) 103 104common = struct( 105 check_rule = _check_rule, 106 get_host_javabase = _get_host_javabase, 107 get_java_toolchain = _get_java_toolchain, 108 filter_zip = _filter_zip, 109 java_package = _java_package, 110 sign_apk = _sign_apk, 111) 112