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(":utils.bzl", "ANDROID_TOOLCHAIN_TYPE", "get_android_toolchain", _log = "log") 18load("//rules/android_common:reexport_android_common.bzl", _native_android_common = "native_android_common") 19 20# Suffix attached to the Starlark portion of android_binary target 21_PACKAGED_RESOURCES_SUFFIX = "_RESOURCES_DO_NOT_USE" 22 23# Validates that the packages listed under "deps" all have the given constraint. If a package 24# does not have this attribute, an error is generated. 25def _validate_constraints(targets, constraint): 26 for target in targets: 27 if JavaInfo in target: 28 if constraint not in java_common.get_constraints(target[JavaInfo]): 29 _log.error("%s: does not have constraint '%s'" % (target.label, constraint)) 30 31TARGET_DNE = "Target '%s' does not exist or is a file and is not allowed." 32 33def _check_rule(targets): 34 _validate_constraints(targets, "android") 35 36def _get_java_toolchain(ctx): 37 if not hasattr(ctx.attr, "_java_toolchain"): 38 _log.error("Missing _java_toolchain attr") 39 return ctx.attr._java_toolchain 40 41def _get_host_javabase(ctx): 42 if not hasattr(ctx.attr, "_host_javabase"): 43 _log.error("Missing _host_javabase attr") 44 return ctx.attr._host_javabase 45 46def _filter_zip_include(ctx, in_zip, out_zip, filters = []): 47 """Creates a copy of a zip file with files that match filters.""" 48 args = ctx.actions.args() 49 args.add("-q") 50 args.add(in_zip.path) 51 args.add_all(filters) 52 args.add("--copy") 53 args.add("--out") 54 args.add(out_zip.path) 55 ctx.actions.run( 56 executable = get_android_toolchain(ctx).zip_tool.files_to_run, 57 arguments = [args], 58 inputs = [in_zip], 59 outputs = [out_zip], 60 mnemonic = "FilterZipInclude", 61 progress_message = "Filtering %s" % in_zip.short_path, 62 toolchain = ANDROID_TOOLCHAIN_TYPE, 63 ) 64 65def _filter_zip_exclude( 66 ctx, 67 output = None, 68 input = None, 69 filter_zips = [], 70 filter_types = [], 71 filters = [], 72 check_hash_mismatch = False, 73 compression_mode = "DONT_CARE"): 74 """Filter out entries from a zip file based on the filter types and filter zips. 75 76 Args: 77 ctx: The Context. 78 output: File. The output zip. 79 input: File. The input zip. 80 filter_zips: List of Files. The zips used as filters. Contents in these files will be omitted from the output zip. 81 filter_types: List of strings. Only contents in the filter Zip files with these extensions will be filtered out. 82 filters: List of strings. The regex to the set of filters to always check for and remove. 83 check_hash_mismatch: Boolean. Whether to enable checking of hash mismatches for files with the same name. 84 compression_mode: String. The compression mode for the output zip. There are 3 modes: 85 * FORCE_DEFLATE: Force the output zip to be compressed. 86 * FORCE_STORED: Force the output zip to be stored. 87 * DONT_CARE: The output zip will have the same compression mode with the input zip. 88 """ 89 args = ctx.actions.args() 90 91 args.add("--inputZip", input.path) 92 args.add("--outputZip", output.path) 93 94 if filter_zips: 95 args.add("--filterZips", ",".join([z.path for z in filter_zips])) 96 if filter_types: 97 args.add("--filterTypes", ",".join(filter_types)) 98 if filters: 99 args.add("--explicitFilters", ",".join(filters)) 100 101 if check_hash_mismatch: 102 args.add("--checkHashMismatch", "ERROR") 103 else: 104 args.add("--checkHashMismatch", "IGNORE") 105 106 args.add("--outputMode", compression_mode) 107 108 ctx.actions.run( 109 executable = get_android_toolchain(ctx).zip_filter.files_to_run, 110 arguments = [args], 111 inputs = [input] + filter_zips, 112 outputs = [output], 113 mnemonic = "FilterZipExclude", 114 progress_message = "Filtering %s" % input.short_path, 115 toolchain = ANDROID_TOOLCHAIN_TYPE, 116 ) 117 118def _create_signer_properties(ctx, oldest_key): 119 properties = ctx.actions.declare_file("%s/keystore.properties" % ctx.label.name) 120 ctx.actions.expand_template( 121 template = ctx.file._bundle_keystore_properties, 122 output = properties, 123 substitutions = {"%oldest_key%": oldest_key.short_path}, 124 ) 125 return properties 126 127common = struct( 128 PACKAGED_RESOURCES_SUFFIX = _PACKAGED_RESOURCES_SUFFIX, 129 check_rule = _check_rule, 130 create_signer_properties = _create_signer_properties, 131 get_host_javabase = _get_host_javabase, 132 get_java_toolchain = _get_java_toolchain, 133 filter_zip_include = _filter_zip_include, 134 filter_zip_exclude = _filter_zip_exclude, 135) 136 137android_common = _native_android_common 138