Skip to content

Commit

Permalink
Switch minSdkVersion floor check with a constant instead of using the…
Browse files Browse the repository at this point in the history
… ACL system.

I put the value in its own bzl file for now. I intend to put other minSdkVersion related utilities in the same bzl file in future changelists.

PiperOrigin-RevId: 605662834
Change-Id: I3392d80a1cbac3dd67924b4d41b833ba17a177ee
  • Loading branch information
ajsinclair authored and copybara-github committed Feb 9, 2024
1 parent 4d537b6 commit 417ca4c
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 23 deletions.
6 changes: 6 additions & 0 deletions rules/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,9 @@ bzl_library(
"//rules/flags:bzl",
],
)

bzl_library(
name = "min_sdk_version_bzl",
srcs = ["min_sdk_version.bzl"],
visibility = ["//:__subpackages__"],
)
1 change: 0 additions & 1 deletion rules/aar_import/impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,6 @@ def impl(ctx):
manifest_ctx = _resources.bump_min_sdk(
ctx,
manifest = android_manifest,
floor = _acls.get_min_sdk_floor(str(ctx.label)),
enforce_min_sdk_floor_tool = _get_android_toolchain(ctx).enforce_min_sdk_floor_tool.files_to_run,
)

Expand Down
9 changes: 0 additions & 9 deletions rules/acls.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ load("//rules/acls:dex2oat_opts.bzl", "CAN_USE_DEX2OAT_OPTIONS")
load("//rules/acls:install_apps_in_data.bzl", "INSTALL_APPS_IN_DATA")
load("//rules/acls:lint_registry_rollout.bzl", "LINT_REGISTRY_FALLBACK", "LINT_REGISTRY_ROLLOUT")
load("//rules/acls:local_test_multi_proto.bzl", "LOCAL_TEST_MULTI_PROTO_PKG")
load("//rules/acls:min_sdk_floors.bzl", "MIN_SDK_FLOORS")
load(
"//rules/acls:partial_jetification_targets.bzl",
"PARTIAL_JETIFICATION_TARGETS_FALLBACK",
Expand All @@ -76,12 +75,6 @@ load("//rules/acls:r8.bzl", "USE_R8")
load("//rules/acls:shared_library_resource_linking.bzl", "SHARED_LIBRARY_RESOURCE_LINKING_ALLOWLIST")
load("//rules/acls:test_to_instrument_test_rollout.bzl", "TEST_TO_INSTRUMENT_TEST_FALLBACK", "TEST_TO_INSTRUMENT_TEST_ROLLOUT")

def _get_min_sdk_floor(fqn):
for minsdk, package_dict in MIN_SDK_FLOORS_DICT.items():
if matches(fqn, package_dict):
return minsdk
fail("No matching min_sdk_floor for %s" % fqn)

def _in_aar_import_deps_checker(fqn):
return not matches(fqn, AAR_IMPORT_DEPS_CHECKER_FALLBACK_DICT) and matches(fqn, AAR_IMPORT_DEPS_CHECKER_ROLLOUT_DICT)

Expand Down Expand Up @@ -272,7 +265,6 @@ ANDROID_INSTRUMENTATION_TEST_PREBUILT_TEST_APK_FALLBACK_DICT = make_dict(ANDROID
BASELINE_PROFILES_ROLLOUT_DICT = make_dict(BASELINE_PROFILES_ROLLOUT)
BASELINE_PROFILES_OPTIMIZER_INTEGRATION_DICT = make_dict(BASELINE_PROFILES_OPTIMIZER_INTEGRATION)
BASELINE_PROFILES_OPTIMIZER_INTEGRATION_FALLBACK_DICT = make_dict(BASELINE_PROFILES_OPTIMIZER_INTEGRATION_FALLBACK)
MIN_SDK_FLOORS_DICT = make_min_sdk_dict(MIN_SDK_FLOORS)
ANDROID_APK_TO_BUNDLE_FEATURES_DICT = make_dict(ANDROID_APK_TO_BUNDLE_FEATURES)
ANDROID_LIBRARY_USE_AOSP_AIDL_COMPILER_ALLOWLIST_DICT = make_dict(ANDROID_LIBRARY_USE_AOSP_AIDL_COMPILER_ALLOWLIST)
DATABINDING_ALLOWED_DICT = make_dict(DATABINDING_ALLOWED)
Expand Down Expand Up @@ -337,7 +329,6 @@ def matches(fqn, dct):
acls = struct(
get_android_archive_duplicate_class_allowlist = _get_android_archive_duplicate_class_allowlist,
get_android_archive_exposed_package_allowlist = _get_android_archive_exposed_package_allowlist,
get_min_sdk_floor = _get_min_sdk_floor,
in_aar_import_deps_checker = _in_aar_import_deps_checker,
in_aar_import_explicit_exports_manifest = _in_aar_import_explicit_exports_manifest,
in_aar_import_exports_r_java = _in_aar_import_exports_r_java,
Expand Down
2 changes: 0 additions & 2 deletions rules/android_binary_internal/impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def _process_manifest(ctx, **unused_ctxs):
ctx,
manifest = ctx.file.manifest,
manifest_values = ctx.attr.manifest_values,
floor = acls.get_min_sdk_floor(str(ctx.label)),
enforce_min_sdk_floor_tool = get_android_toolchain(ctx).enforce_min_sdk_floor_tool.files_to_run,
)

Expand Down Expand Up @@ -113,7 +112,6 @@ def _validate_manifest(ctx, packaged_resources_ctx, **unused_ctxs):
min_sdk_validation_output = _resources.validate_min_sdk(
ctx,
manifest = packaged_resources_ctx.processed_manifest,
floor = acls.get_min_sdk_floor(str(ctx.label)),
enforce_min_sdk_floor_tool = get_android_toolchain(ctx).enforce_min_sdk_floor_tool.files_to_run,
)
if min_sdk_validation_output:
Expand Down
1 change: 0 additions & 1 deletion rules/android_library/impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ def _process_manifest(ctx, **unused_ctxs):
manifest_ctx = _resources.bump_min_sdk(
ctx,
manifest = ctx.file.manifest,
floor = acls.get_min_sdk_floor(str(ctx.label)),
enforce_min_sdk_floor_tool = get_android_toolchain(ctx).enforce_min_sdk_floor_tool.files_to_run,
)

Expand Down
5 changes: 1 addition & 4 deletions rules/android_local_test/impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

"""Bazel rule for Android local test."""

load("//rules:acls.bzl", "acls")
load("//rules:attrs.bzl", "attrs")
load("//rules:common.bzl", "common")
load("//rules:java.bzl", "java")
Expand Down Expand Up @@ -49,7 +48,7 @@ DEFAULT_JIT_FLAGS = ["-XX:+TieredCompilation", "-XX:TieredStopAtLevel=1"]
# the best across all P% layers from profiling.
DEFAULT_GC_FLAGS = ["-Xmx8g"]

# disable class loading by default for faster classloading and consistent enviroment across
# disable class loading by default for faster classloading and consistent environment across
# local and remote execution
DEFAULT_VERIFY_FLAGS = ["-Xverify:none"]

Expand All @@ -61,7 +60,6 @@ def _process_manifest(ctx, java_package, **_unused_sub_ctxs):
manifest_values = resources.process_manifest_values(
ctx,
ctx.attr.manifest_values,
acls.get_min_sdk_floor(str(ctx.label)),
)
if ctx.file.manifest == None:
# No manifest provided, generate one
Expand All @@ -78,7 +76,6 @@ def _process_manifest(ctx, java_package, **_unused_sub_ctxs):
ctx,
manifest = ctx.file.manifest,
manifest_values = ctx.attr.manifest_values,
floor = acls.get_min_sdk_floor(str(ctx.label)),
enforce_min_sdk_floor_tool = get_android_toolchain(ctx).enforce_min_sdk_floor_tool.files_to_run,
)

Expand Down
26 changes: 26 additions & 0 deletions rules/min_sdk_version.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 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.

""" Module for handling minSdkVersion configuration.
This module holds the current minimum minSdkVersion supported by the Android Rules. Additionally
it holds utilities for handling minSdkVersion propagation.
"""

_DEPOT_FLOOR = 19

min_sdk_version = struct(
DEPOT_FLOOR = _DEPOT_FLOOR,
)
13 changes: 7 additions & 6 deletions rules/resources.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Bazel Android Resources."""

load("//rules:acls.bzl", "acls")
load("//rules:min_sdk_version.bzl", _min_sdk_version = "min_sdk_version")
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load(":attrs.bzl", _attrs = "attrs")
load(":busybox.bzl", _busybox = "busybox")
Expand Down Expand Up @@ -199,7 +200,7 @@ def _generate_dummy_manifest(
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="%s">""" % (java_package or "com.default")

min_sdk_version = max(min_sdk_version, acls.get_min_sdk_floor(str(ctx.label)))
min_sdk_version = max(min_sdk_version, _min_sdk_version.DEPOT_FLOOR)
content = content + """
<uses-sdk android:minSdkVersion="%s" />""" % min_sdk_version

Expand Down Expand Up @@ -1110,7 +1111,7 @@ def _validate_resources(resource_files = None):
if res_type not in _RESOURCE_FOLDER_TYPES:
fail(_INCORRECT_RESOURCE_LAYOUT_ERROR % resource_file)

def _process_manifest_values(ctx, manifest_values, min_sdk_floor):
def _process_manifest_values(ctx, manifest_values, min_sdk_floor = _min_sdk_version.DEPOT_FLOOR):
expanded_manifest_values = utils.expand_make_vars(ctx, manifest_values)

# We cannot bump non-integer minSdkVersion values. These are used for pre-release versions of Android.
Expand All @@ -1124,7 +1125,7 @@ def _bump_min_sdk(
ctx,
manifest = None,
manifest_values = None,
floor = None,
floor = _min_sdk_version.DEPOT_FLOOR,
enforce_min_sdk_floor_tool = None):
"""Bumps the min SDK attribute of AndroidManifest to the floor.
Expand Down Expand Up @@ -1237,8 +1238,8 @@ def _set_default_min_sdk(
def _validate_min_sdk(
ctx,
manifest,
floor,
enforce_min_sdk_floor_tool):
floor = _min_sdk_version.DEPOT_FLOOR,
enforce_min_sdk_floor_tool = None):
"""Validates that the min SDK attribute of AndroidManifest is at least at the floor.
Args:
Expand Down Expand Up @@ -1528,7 +1529,7 @@ def _process_starlark(
ctx,
out_manifest = generated_manifest,
java_package = java_package if java_package else ctx.label.package.replace("/", "."),
min_sdk_version = acls.get_min_sdk_floor(str(ctx.label)),
min_sdk_version = _min_sdk_version.DEPOT_FLOOR,
)
r_txt = ctx.actions.declare_file(
"_migrated/" + ctx.label.name + "_symbols/R.txt",
Expand Down

0 comments on commit 417ca4c

Please sign in to comment.