1*9e965d6fSRomain Jobredeaux# Copyright 2018 The Bazel Authors. All rights reserved. 2*9e965d6fSRomain Jobredeaux# 3*9e965d6fSRomain Jobredeaux# Licensed under the Apache License, Version 2.0 (the "License"); 4*9e965d6fSRomain Jobredeaux# you may not use this file except in compliance with the License. 5*9e965d6fSRomain Jobredeaux# You may obtain a copy of the License at 6*9e965d6fSRomain Jobredeaux# 7*9e965d6fSRomain Jobredeaux# http://www.apache.org/licenses/LICENSE-2.0 8*9e965d6fSRomain Jobredeaux# 9*9e965d6fSRomain Jobredeaux# Unless required by applicable law or agreed to in writing, software 10*9e965d6fSRomain Jobredeaux# distributed under the License is distributed on an "AS IS" BASIS, 11*9e965d6fSRomain Jobredeaux# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*9e965d6fSRomain Jobredeaux# See the License for the specific language governing permissions and 13*9e965d6fSRomain Jobredeaux# limitations under the License. 14*9e965d6fSRomain Jobredeaux 15*9e965d6fSRomain Jobredeaux"""Bazel common library for the Android rules.""" 16*9e965d6fSRomain Jobredeaux 17*9e965d6fSRomain Jobredeauxload(":utils.bzl", "ANDROID_TOOLCHAIN_TYPE", "get_android_toolchain", _log = "log") 18*9e965d6fSRomain Jobredeauxload("//rules/android_common:reexport_android_common.bzl", _native_android_common = "native_android_common") 19*9e965d6fSRomain Jobredeaux 20*9e965d6fSRomain Jobredeaux# Suffix attached to the Starlark portion of android_binary target 21*9e965d6fSRomain Jobredeaux_PACKAGED_RESOURCES_SUFFIX = "_RESOURCES_DO_NOT_USE" 22*9e965d6fSRomain Jobredeaux 23*9e965d6fSRomain Jobredeaux# Validates that the packages listed under "deps" all have the given constraint. If a package 24*9e965d6fSRomain Jobredeaux# does not have this attribute, an error is generated. 25*9e965d6fSRomain Jobredeauxdef _validate_constraints(targets, constraint): 26*9e965d6fSRomain Jobredeaux for target in targets: 27*9e965d6fSRomain Jobredeaux if JavaInfo in target: 28*9e965d6fSRomain Jobredeaux if constraint not in java_common.get_constraints(target[JavaInfo]): 29*9e965d6fSRomain Jobredeaux _log.error("%s: does not have constraint '%s'" % (target.label, constraint)) 30*9e965d6fSRomain Jobredeaux 31*9e965d6fSRomain JobredeauxTARGET_DNE = "Target '%s' does not exist or is a file and is not allowed." 32*9e965d6fSRomain Jobredeaux 33*9e965d6fSRomain Jobredeauxdef _check_rule(targets): 34*9e965d6fSRomain Jobredeaux _validate_constraints(targets, "android") 35*9e965d6fSRomain Jobredeaux 36*9e965d6fSRomain Jobredeauxdef _get_java_toolchain(ctx): 37*9e965d6fSRomain Jobredeaux if not hasattr(ctx.attr, "_java_toolchain"): 38*9e965d6fSRomain Jobredeaux _log.error("Missing _java_toolchain attr") 39*9e965d6fSRomain Jobredeaux return ctx.attr._java_toolchain 40*9e965d6fSRomain Jobredeaux 41*9e965d6fSRomain Jobredeauxdef _get_host_javabase(ctx): 42*9e965d6fSRomain Jobredeaux if not hasattr(ctx.attr, "_host_javabase"): 43*9e965d6fSRomain Jobredeaux _log.error("Missing _host_javabase attr") 44*9e965d6fSRomain Jobredeaux return ctx.attr._host_javabase 45*9e965d6fSRomain Jobredeaux 46*9e965d6fSRomain Jobredeauxdef _filter_zip_include(ctx, in_zip, out_zip, filters = []): 47*9e965d6fSRomain Jobredeaux """Creates a copy of a zip file with files that match filters.""" 48*9e965d6fSRomain Jobredeaux args = ctx.actions.args() 49*9e965d6fSRomain Jobredeaux args.add("-q") 50*9e965d6fSRomain Jobredeaux args.add(in_zip.path) 51*9e965d6fSRomain Jobredeaux args.add_all(filters) 52*9e965d6fSRomain Jobredeaux args.add("--copy") 53*9e965d6fSRomain Jobredeaux args.add("--out") 54*9e965d6fSRomain Jobredeaux args.add(out_zip.path) 55*9e965d6fSRomain Jobredeaux ctx.actions.run( 56*9e965d6fSRomain Jobredeaux executable = get_android_toolchain(ctx).zip_tool.files_to_run, 57*9e965d6fSRomain Jobredeaux arguments = [args], 58*9e965d6fSRomain Jobredeaux inputs = [in_zip], 59*9e965d6fSRomain Jobredeaux outputs = [out_zip], 60*9e965d6fSRomain Jobredeaux mnemonic = "FilterZipInclude", 61*9e965d6fSRomain Jobredeaux progress_message = "Filtering %s" % in_zip.short_path, 62*9e965d6fSRomain Jobredeaux toolchain = ANDROID_TOOLCHAIN_TYPE, 63*9e965d6fSRomain Jobredeaux ) 64*9e965d6fSRomain Jobredeaux 65*9e965d6fSRomain Jobredeauxdef _filter_zip_exclude( 66*9e965d6fSRomain Jobredeaux ctx, 67*9e965d6fSRomain Jobredeaux output = None, 68*9e965d6fSRomain Jobredeaux input = None, 69*9e965d6fSRomain Jobredeaux filter_zips = [], 70*9e965d6fSRomain Jobredeaux filter_types = [], 71*9e965d6fSRomain Jobredeaux filters = [], 72*9e965d6fSRomain Jobredeaux check_hash_mismatch = False, 73*9e965d6fSRomain Jobredeaux compression_mode = "DONT_CARE"): 74*9e965d6fSRomain Jobredeaux """Filter out entries from a zip file based on the filter types and filter zips. 75*9e965d6fSRomain Jobredeaux 76*9e965d6fSRomain Jobredeaux Args: 77*9e965d6fSRomain Jobredeaux ctx: The Context. 78*9e965d6fSRomain Jobredeaux output: File. The output zip. 79*9e965d6fSRomain Jobredeaux input: File. The input zip. 80*9e965d6fSRomain Jobredeaux filter_zips: List of Files. The zips used as filters. Contents in these files will be omitted from the output zip. 81*9e965d6fSRomain Jobredeaux filter_types: List of strings. Only contents in the filter Zip files with these extensions will be filtered out. 82*9e965d6fSRomain Jobredeaux filters: List of strings. The regex to the set of filters to always check for and remove. 83*9e965d6fSRomain Jobredeaux check_hash_mismatch: Boolean. Whether to enable checking of hash mismatches for files with the same name. 84*9e965d6fSRomain Jobredeaux compression_mode: String. The compression mode for the output zip. There are 3 modes: 85*9e965d6fSRomain Jobredeaux * FORCE_DEFLATE: Force the output zip to be compressed. 86*9e965d6fSRomain Jobredeaux * FORCE_STORED: Force the output zip to be stored. 87*9e965d6fSRomain Jobredeaux * DONT_CARE: The output zip will have the same compression mode with the input zip. 88*9e965d6fSRomain Jobredeaux """ 89*9e965d6fSRomain Jobredeaux args = ctx.actions.args() 90*9e965d6fSRomain Jobredeaux 91*9e965d6fSRomain Jobredeaux args.add("--inputZip", input.path) 92*9e965d6fSRomain Jobredeaux args.add("--outputZip", output.path) 93*9e965d6fSRomain Jobredeaux 94*9e965d6fSRomain Jobredeaux if filter_zips: 95*9e965d6fSRomain Jobredeaux args.add("--filterZips", ",".join([z.path for z in filter_zips])) 96*9e965d6fSRomain Jobredeaux if filter_types: 97*9e965d6fSRomain Jobredeaux args.add("--filterTypes", ",".join(filter_types)) 98*9e965d6fSRomain Jobredeaux if filters: 99*9e965d6fSRomain Jobredeaux args.add("--explicitFilters", ",".join(filters)) 100*9e965d6fSRomain Jobredeaux 101*9e965d6fSRomain Jobredeaux if check_hash_mismatch: 102*9e965d6fSRomain Jobredeaux args.add("--checkHashMismatch", "ERROR") 103*9e965d6fSRomain Jobredeaux else: 104*9e965d6fSRomain Jobredeaux args.add("--checkHashMismatch", "IGNORE") 105*9e965d6fSRomain Jobredeaux 106*9e965d6fSRomain Jobredeaux args.add("--outputMode", compression_mode) 107*9e965d6fSRomain Jobredeaux 108*9e965d6fSRomain Jobredeaux ctx.actions.run( 109*9e965d6fSRomain Jobredeaux executable = get_android_toolchain(ctx).zip_filter.files_to_run, 110*9e965d6fSRomain Jobredeaux arguments = [args], 111*9e965d6fSRomain Jobredeaux inputs = [input] + filter_zips, 112*9e965d6fSRomain Jobredeaux outputs = [output], 113*9e965d6fSRomain Jobredeaux mnemonic = "FilterZipExclude", 114*9e965d6fSRomain Jobredeaux progress_message = "Filtering %s" % input.short_path, 115*9e965d6fSRomain Jobredeaux toolchain = ANDROID_TOOLCHAIN_TYPE, 116*9e965d6fSRomain Jobredeaux ) 117*9e965d6fSRomain Jobredeaux 118*9e965d6fSRomain Jobredeauxdef _create_signer_properties(ctx, oldest_key): 119*9e965d6fSRomain Jobredeaux properties = ctx.actions.declare_file("%s/keystore.properties" % ctx.label.name) 120*9e965d6fSRomain Jobredeaux ctx.actions.expand_template( 121*9e965d6fSRomain Jobredeaux template = ctx.file._bundle_keystore_properties, 122*9e965d6fSRomain Jobredeaux output = properties, 123*9e965d6fSRomain Jobredeaux substitutions = {"%oldest_key%": oldest_key.short_path}, 124*9e965d6fSRomain Jobredeaux ) 125*9e965d6fSRomain Jobredeaux return properties 126*9e965d6fSRomain Jobredeaux 127*9e965d6fSRomain Jobredeauxcommon = struct( 128*9e965d6fSRomain Jobredeaux PACKAGED_RESOURCES_SUFFIX = _PACKAGED_RESOURCES_SUFFIX, 129*9e965d6fSRomain Jobredeaux check_rule = _check_rule, 130*9e965d6fSRomain Jobredeaux create_signer_properties = _create_signer_properties, 131*9e965d6fSRomain Jobredeaux get_host_javabase = _get_host_javabase, 132*9e965d6fSRomain Jobredeaux get_java_toolchain = _get_java_toolchain, 133*9e965d6fSRomain Jobredeaux filter_zip_include = _filter_zip_include, 134*9e965d6fSRomain Jobredeaux filter_zip_exclude = _filter_zip_exclude, 135*9e965d6fSRomain Jobredeaux) 136*9e965d6fSRomain Jobredeaux 137*9e965d6fSRomain Jobredeauxandroid_common = _native_android_common 138