Skip to content

Commit

Permalink
Use an aspect to validate the transitive deps of an android_feature_m…
Browse files Browse the repository at this point in the history
…odule.

This is prework before we filter out all dexes to ensure users aren't confused.

PiperOrigin-RevId: 684587452
Change-Id: Ie0b783725730ee17075120c52dc61f0ca0e60d40
  • Loading branch information
ajsinclair authored and copybara-github committed Oct 10, 2024
1 parent c0f424e commit 559a915
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2024 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.

"""Aspect to validate the transitive dependencies of an android_feature_module."""
_SRCS_DISALLOWED_EXTENSIONS = ["java", "kt", "srcjar"]

_DISALLOWED_RULE_TYPES = ["java_import", "aar_import"]

def _has_disallowed_srcs(srcs):
if not srcs:
return False
for src in srcs:
if src.extension in _SRCS_DISALLOWED_EXTENSIONS:
return True
return False

def _impl(target, ctx):
if ctx.rule.kind in _DISALLOWED_RULE_TYPES:
fail("android_feature_module cannot transitively depend on {} rules".format(ctx.rule.kind))
srcs = getattr(ctx.rule.files, "srcs", [])
if _has_disallowed_srcs(srcs):
fail("android_feature_module cannot transitively depend on Java/Kotlin sources and {} has Java/Kotlin sources".format(target.label))
if getattr(ctx.rule.attr, "resource_files", False):
fail("android_feature_module cannot transitively depend on resource_files and {} has resource_files".format(target.label))
return []

android_feature_module_validation_aspect = aspect(
implementation = _impl,
attr_aspects = ["deps", "exports"],
doc = "An aspect that validates the dependencies of an android_feature_module.",
)
3 changes: 2 additions & 1 deletion rules/android_application/attrs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ load(
_attrs = "attrs",
)
load("//rules:visibility.bzl", "PROJECT_VISIBILITY")
load(":android_feature_module_validation_aspect.bzl", "android_feature_module_validation_aspect")

visibility(PROJECT_VISIBILITY)

Expand Down Expand Up @@ -102,7 +103,7 @@ ANDROID_APPLICATION_ATTRS = _attrs.add(
)

ANDROID_FEATURE_MODULE_ATTRS = dict(
binary = attr.label(),
binary = attr.label(aspects = [android_feature_module_validation_aspect]),
feature_name = attr.string(),
library = attr.label(
allow_rules = ["android_library"],
Expand Down

0 comments on commit 559a915

Please sign in to comment.