1# Copyright 2020 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 Android Proguard library for the Android rules.""" 16 17_ProguardContextInfo = provider( 18 doc = "Contains data from processing Proguard specs.", 19 fields = dict( 20 proguard_configs = "The direct proguard configs", 21 transitive_proguard_configs = 22 "The proguard configs within the transitive closure of the target", 23 providers = "The list of all providers to propagate.", 24 ), 25) 26 27def _validate_proguard_spec( 28 ctx, 29 out_validated_proguard_spec, 30 proguard_spec, 31 proguard_allowlister): 32 args = ctx.actions.args() 33 args.add("--path", proguard_spec) 34 args.add("--output", out_validated_proguard_spec) 35 36 ctx.actions.run( 37 executable = proguard_allowlister, 38 arguments = [args], 39 inputs = [proguard_spec], 40 outputs = [out_validated_proguard_spec], 41 mnemonic = "ValidateProguard", 42 progress_message = ( 43 "Validating proguard configuration %s" % proguard_spec.short_path 44 ), 45 ) 46 47def _process( 48 ctx, 49 proguard_configs = [], 50 proguard_spec_providers = [], 51 proguard_allowlister = None): 52 """Processes Proguard Specs 53 54 Args: 55 ctx: The context. 56 proguard_configs: sequence of Files. A list of proguard config files to be 57 processed. Optional. 58 proguard_spec_providers: sequence of ProguardSpecProvider providers. A 59 list of providers from the dependencies, exports, plugins, 60 exported_plugins, etc. Optional. 61 proguard_allowlister: The proguard_allowlister exeutable provider. 62 63 Returns: 64 A _ProguardContextInfo provider. 65 """ 66 67 # TODO(djwhang): Look to see if this can be just a validation action and the 68 # proguard_spec provided by the rule can be propagated. 69 validated_proguard_configs = [] 70 for proguard_spec in proguard_configs: 71 validated_proguard_spec = ctx.actions.declare_file( 72 "validated_proguard/%s/%s_valid" % 73 (ctx.label.name, proguard_spec.path), 74 ) 75 _validate_proguard_spec( 76 ctx, 77 validated_proguard_spec, 78 proguard_spec, 79 proguard_allowlister, 80 ) 81 validated_proguard_configs.append(validated_proguard_spec) 82 83 transitive_validated_proguard_configs = [] 84 for info in proguard_spec_providers: 85 transitive_validated_proguard_configs.append(info.specs) 86 87 transitive_proguard_configs = depset( 88 validated_proguard_configs, 89 transitive = transitive_validated_proguard_configs, 90 order = "preorder", 91 ) 92 return _ProguardContextInfo( 93 proguard_configs = proguard_configs, 94 transitive_proguard_configs = transitive_proguard_configs, 95 providers = [ 96 ProguardSpecProvider(transitive_proguard_configs), 97 # TODO(b/152659272): Remove this once the android_archive rule is 98 # able to process a transitive closure of deps to produce an aar. 99 AndroidProguardInfo(proguard_configs), 100 ], 101 ) 102 103proguard = struct( 104 process = _process, 105) 106 107testing = struct( 108 validate_proguard_spec = _validate_proguard_spec, 109 ProguardContextInfo = _ProguardContextInfo, 110) 111