Skip to content

Commit 7726bec

Browse files
A Googlercopybara-github
authored andcommitted
For optimized builds, use baseline profiles as startup profiles if no
startup profiles are explicitly provided. Guarded by ACL. PiperOrigin-RevId: 834965863 Change-Id: I4fa7447e31e32cda5adc10b4dc47d415e521e0b3
1 parent a11ce9d commit 7726bec

5 files changed

Lines changed: 45 additions & 0 deletions

File tree

rules/acls.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ load("//rules/acls:resource_translation_merging_rollout.bzl", "RESOURCE_TRANSLAT
7878
load("//rules/acls:shared_library_resource_linking.bzl", "SHARED_LIBRARY_RESOURCE_LINKING_ALLOWLIST")
7979
load("//rules/acls:stamp_signing.bzl", "STAMP_SIGNING_FALLBACK", "STAMP_SIGNING_ROLLOUT")
8080
load("//rules/acls:test_to_instrument_test_rollout.bzl", "TEST_TO_INSTRUMENT_TEST_FALLBACK", "TEST_TO_INSTRUMENT_TEST_ROLLOUT")
81+
load("//rules/acls:use_baseline_as_startup_profile.bzl", "USE_BASELINE_AS_STARTUP_PROFILE_FALLBACK", "USE_BASELINE_AS_STARTUP_PROFILE_ROLLOUT")
8182

8283
visibility(PROJECT_VISIBILITY)
8384

@@ -227,6 +228,9 @@ def _in_enable_exported_lint_checks(fqn):
227228
def _get_aapt2_feature_flags(_):
228229
return AAPT2_FEATURE_FLAGS
229230

231+
def _use_baseline_as_startup_profile(fqn):
232+
return matches(fqn, USE_BASELINE_AS_STARTUP_PROFILE_ROLLOUT_DICT) and not matches(fqn, USE_BASELINE_AS_STARTUP_PROFILE_FALLBACK_DICT)
233+
230234
def make_dict(lst):
231235
"""Do not use this method outside of acls directory."""
232236
return {t: True for t in lst}
@@ -297,6 +301,8 @@ STAMP_SIGNING_FALLBACK_DICT = make_dict(STAMP_SIGNING_FALLBACK)
297301
RESOURCE_TRANSLATION_MERGING_ROLLOUT_DICT = make_dict(RESOURCE_TRANSLATION_MERGING_ROLLOUT)
298302
RESOURCE_TRANSLATION_MERGING_FALLBACK_DICT = make_dict(RESOURCE_TRANSLATION_MERGING_FALLBACK)
299303
ENABLE_EXPORTED_LINT_CHECKS_DICT = make_dict(ENABLE_EXPORTED_LINT_CHECKS)
304+
USE_BASELINE_AS_STARTUP_PROFILE_ROLLOUT_DICT = make_dict(USE_BASELINE_AS_STARTUP_PROFILE_ROLLOUT)
305+
USE_BASELINE_AS_STARTUP_PROFILE_FALLBACK_DICT = make_dict(USE_BASELINE_AS_STARTUP_PROFILE_FALLBACK)
300306

301307
def matches(fqn, dct):
302308
# Labels with workspace names ("@workspace//pkg:target") are not supported.
@@ -396,6 +402,7 @@ acls = struct(
396402
in_resource_translation_merging_rollout = _in_resource_translation_merging_rollout,
397403
in_enable_exported_lint_checks = _in_enable_exported_lint_checks,
398404
get_aapt2_feature_flags = _get_aapt2_feature_flags,
405+
use_baseline_as_startup_profile = _use_baseline_as_startup_profile,
399406
)
400407

401408
# Visible for testing
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright 2025 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+
"""Targets that use baseline profiles as startup profiles if no startup profile is provided."""
15+
16+
load("//rules:visibility.bzl", "PROJECT_VISIBILITY")
17+
18+
visibility(PROJECT_VISIBILITY)
19+
20+
USE_BASELINE_AS_STARTUP_PROFILE_ROLLOUT = [
21+
"//test/rules/android_binary/baselineprofiles/use_baseline_as_startup_profiles:__subpackages__",
22+
]
23+
24+
USE_BASELINE_AS_STARTUP_PROFILE_FALLBACK = []

rules/android_binary/attrs.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ ATTRS = _attrs.replace(
222222
allow_single_file = True,
223223
cfg = android_platforms_transition,
224224
),
225+
_flags = attr.label(
226+
default = "//rules/flags",
227+
cfg = android_platforms_transition,
228+
),
225229
_bytecode_optimizer = attr.label(
226230
default = configuration_field(
227231
fragment = "java",

rules/android_binary/impl.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ load(
4545
"utils",
4646
)
4747
load("//rules:visibility.bzl", "PROJECT_VISIBILITY")
48+
load("//rules/flags:flags.bzl", _flags = "flags")
4849
load("@rules_java//java/common:java_common.bzl", "java_common")
4950
load("@rules_java//java/common:java_info.bzl", "JavaInfo")
5051
load("@rules_java//java/common:java_plugin_info.bzl", "JavaPluginInfo")
@@ -815,6 +816,9 @@ def _process_optimize(ctx, validation_ctx, deploy_ctx, packaged_resources_ctx, b
815816
if acls.in_baseline_profiles_optimizer_integration(str(ctx.label)) and bp_ctx.baseline_profile_output:
816817
startup_profile = bp_ctx.baseline_profile_output.startup_profile
817818
baseline_profile = bp_ctx.baseline_profile_output.baseline_profile
819+
if not startup_profile and (_flags.get(ctx).use_baseline_as_startup_profile or
820+
acls.use_baseline_as_startup_profile(str(ctx.label))):
821+
startup_profile = baseline_profile
818822

819823
enable_rewrite_resources_through_optimizer = enable_resource_shrinking and ctx.attr._rewrite_resources_through_optimizer
820824

rules/flags/flag_defs.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,9 @@ def define_flags():
9393
default = True,
9494
description = "Use Studio Deployer to install apks",
9595
)
96+
97+
flags.DEFINE_bool(
98+
name = "use_baseline_as_startup_profile",
99+
default = False,
100+
description = "For testing/validation only. Use baseline profiles as startup profiles in optimized builds.",
101+
)

0 commit comments

Comments
 (0)