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"""Starlark Android Binary for Android Rules.""" 16 17load(":attrs.bzl", "ATTRS") 18load(":impl.bzl", "impl") 19load( 20 "//rules:attrs.bzl", 21 _attrs = "attrs", 22) 23 24_DEFAULT_ALLOWED_ATTRS = ["name", "visibility", "tags", "testonly", "transitive_configs", "$enable_manifest_merging", "features", "exec_properties"] 25 26_DEFAULT_PROVIDES = [AndroidApplicationResourceInfo, OutputGroupInfo] 27 28def make_rule( 29 attrs = ATTRS, 30 implementation = impl, 31 provides = _DEFAULT_PROVIDES, 32 additional_toolchains = []): 33 """Makes the rule. 34 35 Args: 36 attrs: A dict. The attributes for the rule. 37 implementation: A function. The rule's implementation method. 38 provides: A list. The providers that the rule must provide. 39 additional_toolchains: A list. Additional toolchains passed to pass to rule(toolchains). 40 41 Returns: 42 A rule. 43 """ 44 return rule( 45 attrs = attrs, 46 implementation = implementation, 47 provides = provides, 48 toolchains = [ 49 "//toolchains/android:toolchain_type", 50 "//toolchains/android_sdk:toolchain_type", 51 "@bazel_tools//tools/jdk:toolchain_type", 52 ] + additional_toolchains, 53 _skylark_testable = True, 54 fragments = [ 55 "android", 56 "java", 57 "cpp", 58 ], 59 ) 60 61android_binary_internal = make_rule() 62 63def sanitize_attrs(attrs, allowed_attrs = ATTRS.keys()): 64 """Sanitizes the attributes. 65 66 The android_binary_internal has a subset of the android_binary attributes, but is 67 called from the android_binary macro with the same full set of attributes. This removes 68 any unnecessary attributes. 69 70 Args: 71 attrs: A dict. The attributes for the android_binary_internal rule. 72 allowed_attrs: The list of attribute keys to keep. 73 74 Returns: 75 A dictionary containing valid attributes. 76 """ 77 for attr_name in list(attrs.keys()): 78 if attr_name not in allowed_attrs and attr_name not in _DEFAULT_ALLOWED_ATTRS: 79 attrs.pop(attr_name, None) 80 81 # Some teams set this to a boolean/None which works for the native attribute but breaks 82 # the Starlark attribute. 83 if attr_name == "shrink_resources": 84 if attrs[attr_name] == None: 85 attrs.pop(attr_name, None) 86 else: 87 attrs[attr_name] = _attrs.tristate.normalize(attrs[attr_name]) 88 89 return attrs 90 91def android_binary_internal_macro(**attrs): 92 """android_binary_internal rule. 93 94 Args: 95 **attrs: Rule attributes 96 """ 97 android_binary_internal(**sanitize_attrs(attrs)) 98