From 559a915f4211ee570da97d606bd6912b110fb737 Mon Sep 17 00:00:00 2001 From: Andrew Sinclair Date: Thu, 10 Oct 2024 14:54:14 -0700 Subject: [PATCH] Use an aspect to validate the transitive deps of an android_feature_module. This is prework before we filter out all dexes to ensure users aren't confused. PiperOrigin-RevId: 684587452 Change-Id: Ie0b783725730ee17075120c52dc61f0ca0e60d40 --- ...droid_feature_module_validation_aspect.bzl | 42 +++++++++++++++++++ rules/android_application/attrs.bzl | 3 +- 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 rules/android_application/android_feature_module_validation_aspect.bzl diff --git a/rules/android_application/android_feature_module_validation_aspect.bzl b/rules/android_application/android_feature_module_validation_aspect.bzl new file mode 100644 index 000000000..f0ca1a507 --- /dev/null +++ b/rules/android_application/android_feature_module_validation_aspect.bzl @@ -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.", +) diff --git a/rules/android_application/attrs.bzl b/rules/android_application/attrs.bzl index 0ba9c4bda..51d71700c 100644 --- a/rules/android_application/attrs.bzl +++ b/rules/android_application/attrs.bzl @@ -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) @@ -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"],