blob: 107a9e3e0fdd7a10d0a811b4c85098a900f7c0a2 [file] [log] [blame]
# Copyright 2020 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Starlark Android Binary for Android Rules."""
load(
"//rules:attrs.bzl",
_attrs = "attrs",
)
load(":attrs.bzl", "ATTRS")
load(":impl.bzl", "impl")
_DEFAULT_ALLOWED_ATTRS = ["name", "visibility", "tags", "testonly", "transitive_configs", "$enable_manifest_merging", "features", "exec_properties"]
_DEFAULT_PROVIDES = [AndroidApplicationResourceInfo, OutputGroupInfo]
def make_rule(
attrs = ATTRS,
implementation = impl,
provides = _DEFAULT_PROVIDES,
additional_toolchains = []):
"""Makes the rule.
Args:
attrs: A dict. The attributes for the rule.
implementation: A function. The rule's implementation method.
provides: A list. The providers that the rule must provide.
additional_toolchains: A list. Additional toolchains passed to pass to rule(toolchains).
Returns:
A rule.
"""
return rule(
attrs = attrs,
implementation = implementation,
provides = provides,
toolchains = [
"//toolchains/android:toolchain_type",
"//toolchains/android_sdk:toolchain_type",
"@bazel_tools//tools/jdk:toolchain_type",
] + additional_toolchains,
_skylark_testable = True,
fragments = [
"android",
"bazel_android", # NOTE: Only exists for Bazel
"java",
"cpp",
],
cfg = config_common.config_feature_flag_transition("feature_flags"),
)
android_binary_internal = make_rule()
def sanitize_attrs(attrs, allowed_attrs = ATTRS.keys()):
"""Sanitizes the attributes.
The android_binary_internal has a subset of the android_binary attributes, but is
called from the android_binary macro with the same full set of attributes. This removes
any unnecessary attributes.
Args:
attrs: A dict. The attributes for the android_binary_internal rule.
allowed_attrs: The list of attribute keys to keep.
Returns:
A dictionary containing valid attributes.
"""
for attr_name in list(attrs.keys()):
if attr_name not in allowed_attrs and attr_name not in _DEFAULT_ALLOWED_ATTRS:
attrs.pop(attr_name, None)
elif attr_name == "shrink_resources":
if attrs[attr_name] == None:
attrs.pop(attr_name, None)
else:
# Some teams set this to a boolean/None which works for the native attribute but breaks
# the Starlark attribute.
attrs[attr_name] = _attrs.tristate.normalize(attrs[attr_name])
return attrs
def android_binary_internal_macro(**attrs):
"""android_binary_internal rule.
Args:
**attrs: Rule attributes
"""
# Required for ACLs check in _outputs(), since the callback can't access the native module.
attrs["$package_name"] = native.package_name()
# _package_name is an allowed attribute but is also private. We need to allow
# the $ form of the attribute to stop the sanitize function from removing it.
android_binary_internal(
**sanitize_attrs(
attrs,
allowed_attrs = list(ATTRS.keys()) + ["$package_name", "$rewrite_resources_through_optimizer"],
)
)