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"""Implementation.""" 16 17load("@rules_android//rules:acls.bzl", "acls") 18load("@rules_android//rules:java.bzl", "java") 19load( 20 "@rules_android//rules:processing_pipeline.bzl", 21 "ProviderInfo", 22 "processing_pipeline", 23) 24load("@rules_android//rules:resources.bzl", _resources = "resources") 25load("@rules_android//rules:utils.bzl", "compilation_mode", "get_android_toolchain", "utils") 26 27def _process_resources(ctx, java_package, **unused_ctxs): 28 packaged_resources_ctx = _resources.package( 29 ctx, 30 assets = ctx.files.assets, 31 assets_dir = ctx.attr.assets_dir, 32 resource_files = ctx.files.resource_files, 33 manifest = ctx.file.manifest, 34 manifest_values = utils.expand_make_vars(ctx, ctx.attr.manifest_values), 35 resource_configs = ctx.attr.resource_configuration_filters, 36 densities = ctx.attr.densities, 37 nocompress_extensions = ctx.attr.nocompress_extensions, 38 java_package = java_package, 39 compilation_mode = compilation_mode.get(ctx), 40 shrink_resources = ctx.attr.shrink_resources, 41 use_android_resource_shrinking = ctx.fragments.android.use_android_resource_shrinking, 42 use_android_resource_cycle_shrinking = ctx.fragments.android.use_android_resource_cycle_shrinking, 43 use_legacy_manifest_merger = use_legacy_manifest_merger(ctx), 44 should_throw_on_conflict = not acls.in_allow_resource_conflicts(str(ctx.label)), 45 enable_data_binding = ctx.attr.enable_data_binding, 46 enable_manifest_merging = ctx.attr._enable_manifest_merging, 47 deps = ctx.attr.deps, 48 instruments = ctx.attr.instruments, 49 aapt = get_android_toolchain(ctx).aapt2.files_to_run, 50 android_jar = ctx.attr._android_sdk[AndroidSdkInfo].android_jar, 51 legacy_merger = ctx.attr._android_manifest_merge_tool.files_to_run, 52 xsltproc = ctx.attr._xsltproc_tool.files_to_run, 53 instrument_xslt = ctx.file._add_g3itr_xslt, 54 busybox = get_android_toolchain(ctx).android_resources_busybox.files_to_run, 55 host_javabase = ctx.attr._host_javabase, 56 ) 57 return ProviderInfo( 58 name = "packaged_resources_ctx", 59 value = packaged_resources_ctx, 60 ) 61 62def use_legacy_manifest_merger(ctx): 63 """Whether legacy manifest merging is enabled. 64 65 Args: 66 ctx: The context. 67 68 Returns: 69 Boolean indicating whether legacy manifest merging is enabled. 70 """ 71 manifest_merger = ctx.attr.manifest_merger 72 android_manifest_merger = ctx.fragments.android.manifest_merger 73 74 if android_manifest_merger == "force_android": 75 return False 76 if manifest_merger == "auto": 77 manifest_merger = android_manifest_merger 78 79 return manifest_merger == "legacy" 80 81def finalize(ctx, providers, validation_outputs, **unused_ctxs): 82 providers.append( 83 OutputGroupInfo( 84 _validation = depset(validation_outputs), 85 ), 86 ) 87 return providers 88 89# Order dependent, as providers will not be available to downstream processors 90# that may depend on the provider. Iteration order for a dictionary is based on 91# insertion. 92PROCESSORS = dict( 93 ResourceProcessor = _process_resources, 94) 95 96_PROCESSING_PIPELINE = processing_pipeline.make_processing_pipeline( 97 processors = PROCESSORS, 98 finalize = finalize, 99) 100 101def impl(ctx): 102 """The rule implementation. 103 104 Args: 105 ctx: The context. 106 107 Returns: 108 A list of providers. 109 """ 110 java_package = java.resolve_package_from_label(ctx.label, ctx.attr.custom_package) 111 return processing_pipeline.run(ctx, java_package, _PROCESSING_PIPELINE) 112