Skip to content

Commit b502428

Browse files
A Googlercopybara-github
authored andcommitted
Add obfuscated-hrp primary.prof as a buildable file.
PiperOrigin-RevId: 843395626 Change-Id: I07a789d8e77da500c2783eeecd27b46dbe8ddb96
1 parent 9365ddf commit b502428

4 files changed

Lines changed: 94 additions & 21 deletions

File tree

providers/providers.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ ArtProfileInfo = provider(
289289
art_profile_zip = "Zip file containing the baseline profile and metadata, intended to be merged into APK.",
290290
baseline_profile = "Generated baseline.prof file",
291291
baseline_profile_metadata = "Generated baseline.profm file",
292+
primary_profile = "Generated primary.prof file",
292293
),
293294
)
294295

rules/android_binary/impl.bzl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def _validate_manifest(ctx, packaged_resources_ctx, manifest_ctx, **unused_ctxs)
152152
),
153153
)
154154

155-
def _process_native_libs(ctx, **_unusued_ctxs):
155+
def _process_native_libs(ctx, **_unused_ctxs):
156156
providers = []
157157
native_libs_info = _process_native_deps(
158158
ctx,
@@ -710,6 +710,7 @@ def _process_baseline_profiles(ctx, validation_ctx, deploy_ctx, **_unused_ctxs):
710710
def _process_art_profile(ctx, validation_ctx, bp_ctx, dex_ctx, optimize_ctx, **_unused_ctxs):
711711
providers = []
712712
art_profile_info = None
713+
713714
if ctx.attr.generate_art_profile and not validation_ctx.use_r8:
714715
merged_baseline_profile = bp_ctx.baseline_profile_output.baseline_profile
715716
merged_baseline_profile_rewritten = \
@@ -731,12 +732,23 @@ def _process_art_profile(ctx, validation_ctx, bp_ctx, dex_ctx, optimize_ctx, **_
731732
ctx,
732733
final_classes_dex = dex_ctx.dex_info.final_classes_dex_zip,
733734
merged_profile = merged_baseline_profile,
735+
output_primary_profile = ctx.outputs.primary_profile,
734736
proguard_output_map = proguard_output_map,
735737
profgen = get_android_toolchain(ctx).profgen.files_to_run,
736738
toolchain_type = ANDROID_TOOLCHAIN_TYPE,
737739
)
738740
providers.append(art_profile_info)
739741

742+
if ctx.attr.generate_art_profile and not art_profile_info:
743+
# There are a lot of ways baseline profiles could fail, and thus art profile generation also
744+
# fails. For example, if the baseline profile is not included as a dependency, if the
745+
# baseline profile is empty, or if you're attempting to use R8.
746+
ctx.actions.run_shell(
747+
mnemonic = "FailedGeneratingArtProfile",
748+
outputs = [ctx.outputs.primary_profile],
749+
command = "echo \"Unable to generate art profile, ensure baseline profiles are included as dependencies or disable generate_art_profile\"; exit 1;",
750+
)
751+
740752
return ProviderInfo(
741753
name = "ap_ctx",
742754
value = struct(

rules/android_binary/rule.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ _DEFAULT_ALLOWED_ATTRS = ["name", "visibility", "tags", "testonly", "transitive_
3131

3232
_DEFAULT_PROVIDES = [ApkInfo, JavaInfo]
3333

34-
def _outputs(name, proguard_generate_mapping, _package_name, _generate_proguard_outputs):
34+
def _outputs(name, proguard_generate_mapping, _package_name, _generate_proguard_outputs, generate_art_profile):
3535
label = "//" + _package_name + ":" + name
3636

3737
outputs = dict(
@@ -50,6 +50,10 @@ def _outputs(name, proguard_generate_mapping, _package_name, _generate_proguard_
5050
outputs["proguard_config"] = "%{name}_proguard.config"
5151
if proguard_generate_mapping:
5252
outputs["proguard_map"] = "%{name}_proguard.map"
53+
54+
if generate_art_profile:
55+
outputs["primary_profile"] = "%{name}_primary.prof"
56+
5357
return outputs
5458

5559
def make_rule(

rules/baseline_profiles.bzl

Lines changed: 75 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def _process_art_profile(
104104
ctx,
105105
final_classes_dex,
106106
merged_profile,
107+
output_primary_profile,
107108
proguard_output_map = None,
108109
profgen = None,
109110
toolchain_type = None):
@@ -124,27 +125,17 @@ def _process_art_profile(
124125
ArtProfileInfo containing the generated profile, the metadata, and the combined zip file.
125126
"""
126127

127-
# Profgen
128128
output_profile = _get_profile_artifact(ctx, "baseline.prof")
129129
output_profile_meta = _get_profile_artifact(ctx, "baseline.profm")
130-
profgen_inputs = [final_classes_dex, merged_profile]
131-
profgen_args = ctx.actions.args()
132-
profgen_args.add("bin", merged_profile)
133-
profgen_args.add("--apk", final_classes_dex)
134-
profgen_args.add("--output", output_profile)
135-
profgen_args.add("--output-meta", output_profile_meta)
136-
if proguard_output_map:
137-
profgen_args.add("--map", proguard_output_map)
138-
profgen_inputs.append(proguard_output_map)
139-
ctx.actions.run(
140-
mnemonic = "GenerateARTProfile",
141-
executable = profgen,
142-
progress_message = "Generating Android P-R ART profile for %{label} APK",
143-
arguments = [profgen_args],
144-
inputs = profgen_inputs,
145-
outputs = [output_profile, output_profile_meta],
146-
use_default_shell_env = True,
147-
toolchain = toolchain_type,
130+
_generate_profile(
131+
ctx,
132+
output_profile,
133+
output_profile_meta,
134+
final_classes_dex,
135+
merged_profile,
136+
proguard_output_map,
137+
profgen,
138+
toolchain_type,
148139
)
149140

150141
# Zip ART profiles
@@ -161,10 +152,21 @@ def _process_art_profile(
161152
resources = [output_profile, output_profile_meta],
162153
java_toolchain = _common.get_java_toolchain(ctx),
163154
)
155+
156+
_dump_profile(
157+
ctx,
158+
output_primary_profile,
159+
output_profile,
160+
final_classes_dex,
161+
profgen,
162+
toolchain_type,
163+
)
164+
164165
return ArtProfileInfo(
165166
art_profile_zip = output_profile_zip,
166167
baseline_profile = output_profile,
167168
baseline_profile_metadata = output_profile_meta,
169+
primary_profile = output_primary_profile,
168170
)
169171

170172
def _get_profile_dir(ctx):
@@ -216,6 +218,60 @@ def _expand_wildcards(
216218
toolchain = toolchain_type,
217219
)
218220

221+
def _generate_profile(
222+
ctx,
223+
output_profile,
224+
output_profile_meta,
225+
final_classes_dex,
226+
merged_profile,
227+
proguard_output_map = None,
228+
profgen = None,
229+
toolchain_type = None):
230+
profgen_inputs = [final_classes_dex, merged_profile]
231+
args = ctx.actions.args()
232+
args.add("bin", merged_profile)
233+
args.add("--apk", final_classes_dex)
234+
args.add("--output", output_profile)
235+
args.add("--output-meta", output_profile_meta)
236+
if proguard_output_map:
237+
args.add("--map", proguard_output_map)
238+
profgen_inputs.append(proguard_output_map)
239+
240+
ctx.actions.run(
241+
mnemonic = "GenerateARTProfile",
242+
executable = profgen,
243+
progress_message = "Generating Android P-R ART profile for %{label} APK",
244+
arguments = [args],
245+
inputs = profgen_inputs,
246+
outputs = [output_profile, output_profile_meta],
247+
use_default_shell_env = True,
248+
toolchain = toolchain_type,
249+
)
250+
251+
def _dump_profile(
252+
ctx,
253+
output,
254+
binary_profile,
255+
final_classes_dex,
256+
profgen = None,
257+
toolchain_type = None):
258+
args = ctx.actions.args()
259+
args.add("dumpProfile")
260+
args.add("--profile", binary_profile)
261+
args.add("--apk", final_classes_dex)
262+
args.add("--output", output)
263+
264+
ctx.actions.run(
265+
mnemonic = "DumpProfile",
266+
executable = profgen,
267+
progress_message = "Dumping Obfuscated-HRP profile for %{label} APK",
268+
arguments = [args],
269+
inputs = [binary_profile, final_classes_dex],
270+
outputs = [output],
271+
use_default_shell_env = True,
272+
toolchain = toolchain_type,
273+
)
274+
219275
baseline_profiles = struct(
220276
expand_wildcards = _expand_wildcards,
221277
get_profile_artifact = _get_profile_artifact,

0 commit comments

Comments
 (0)