• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Resource Packaging for Android Rules."""
16
17load(":attrs.bzl", "ATTRS")
18load(":impl.bzl", "impl")
19load(
20    "@rules_android//rules:attrs.bzl",
21    _attrs = "attrs",
22)
23
24_DEFAULT_ALLOWED_ATTRS = ["name", "visibility", "tags", "testonly", "transitive_configs", "$enable_manifest_merging"]
25
26_DEFAULT_PROVIDES = [AndroidApplicationResourceInfo, OutputGroupInfo]
27
28# TODO(b/167721629): Rename android_packaged_resources to android_binary_internal.
29def make_rule(
30        attrs = ATTRS,
31        implementation = impl,
32        provides = _DEFAULT_PROVIDES):
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
40    Returns:
41      A rule.
42    """
43    return rule(
44        attrs = attrs,
45        implementation = implementation,
46        provides = provides,
47        toolchains = ["@rules_android//toolchains/android:toolchain_type"],
48        _skylark_testable = True,
49        fragments = [
50            "android",
51            "java",
52        ],
53    )
54
55_android_packaged_resources = make_rule()
56
57def sanitize_attrs(attrs, allowed_attrs = ATTRS.keys()):
58    """Sanitizes the attributes.
59
60    The android_packaged_resources has a subset of the android_binary attributes, but is
61    called from the android_binary macro with the same full set of attributes. This removes
62    any unnecessary attributes.
63
64    Args:
65      attrs: A dict. The attributes for the android_packaged_resources rule.
66      allowed_attrs: The list of attribute keys to keep.
67
68    Returns:
69      A dictionary containing valid attributes.
70    """
71    for attr_name in attrs.keys():
72        if attr_name not in allowed_attrs and attr_name not in _DEFAULT_ALLOWED_ATTRS:
73            attrs.pop(attr_name, None)
74
75        # Some teams set this to a boolean/None which works for the native attribute but breaks
76        # the Starlark attribute.
77        if attr_name == "shrink_resources":
78            if attrs[attr_name] == None:
79                attrs.pop(attr_name, None)
80            else:
81                attrs[attr_name] = _attrs.tristate.normalize(attrs[attr_name])
82
83    return attrs
84
85def android_packaged_resources_macro(**attrs):
86    """android_packaged_resources rule.
87
88    Args:
89      **attrs: Rule attributes
90    """
91    _android_packaged_resources(**sanitize_attrs(attrs))
92