blob: c06e878a244073935c15a6d8d0cc2dfbb063f241 [file] [log] [blame]
Rico Wind3d369b42021-01-12 10:26:24 +01001#!/usr/bin/env python3
Christoffer Quist Adamsenf2e8db72020-11-07 14:09:49 +01002# Copyright (c) 2020, the R8 project authors. Please see the AUTHORS file
3# for details. All rights reserved. Use of this source code is governed by a
4# BSD-style license that can be found in the LICENSE file.
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +01005
Ian Zerny161ff742022-01-20 12:39:40 +01006import argparse
7import hashlib
8import os
9import shutil
10import sys
11import time
12import zipfile
13from datetime import datetime
14
Morten Krogh-Jespersencd55f812020-11-04 09:13:31 +010015import adb
16import apk_masseur
Christoffer Quist Adamsenf79f1a22021-01-12 16:24:34 +010017import as_utils
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +010018import compiledump
19import gradle
Rico Wind61a034f2021-03-16 09:37:11 +010020import jdk
Christoffer Quist Adamsenbe6661d2023-08-24 11:01:12 +020021import thread_utils
22from thread_utils import print_thread
Morten Krogh-Jespersen47764032020-11-11 15:09:39 +010023import update_prebuilds_in_android
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +010024import utils
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +010025
Ian Zernyed1f8512023-11-07 11:07:57 +010026GOLEM_BUILD_TARGETS = [utils.GRADLE_TASK_R8LIB]
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +010027SHRINKERS = ['r8', 'r8-full', 'r8-nolib', 'r8-nolib-full']
28
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020029
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +010030class AttrDict(dict):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020031
32 def __getattr__(self, name):
33 return self.get(name, None)
34
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +010035
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +010036# To generate the files for a new app, navigate to the app source folder and
37# run:
38# ./gradlew clean :app:assembleRelease -Dcom.android.tools.r8.dumpinputtodirectory=<path>
39# and store the dump and the apk.
40# If the app has instrumented tests, adding `testBuildType "release"` and
41# running:
42# ./gradlew assembleAndroidTest -Dcom.android.tools.r8.dumpinputtodirectory=<path>
43# will also generate dumps and apk for tests.
44
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020045
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +010046class App(object):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020047
48 def __init__(self, fields):
49 defaults = {
50 'id': None,
51 'name': None,
52 'collections': [],
53 'dump_app': None,
54 'apk_app': None,
55 'dump_test': None,
56 'apk_test': None,
57 'skip': False,
58 'url': None, # url is not used but nice to have for updating apps
59 'revision': None,
60 'folder': None,
61 'skip_recompilation': False,
62 'compiler_properties': [],
63 'internal': False,
64 'golem_duration': None,
65 }
66 # This below does not work in python3
67 defaults.update(fields.items())
68 self.__dict__ = defaults
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +010069
70
Christoffer Quist Adamsen53e29472020-11-13 10:53:06 +010071class AppCollection(object):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020072
73 def __init__(self, fields):
74 defaults = {'name': None}
75 # This below does not work in python3
76 defaults.update(fields.items())
77 self.__dict__ = defaults
Christoffer Quist Adamsen53e29472020-11-13 10:53:06 +010078
79
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +010080APPS = [
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020081 App({
82 'id':
83 'com.numix.calculator',
84 'name':
85 'Calculator',
86 'dump_app':
87 'dump_app.zip',
88 'apk_app':
89 'app-release.apk',
90 # Compiling tests fail: Library class android.content.res.XmlResourceParser
91 # implements program class org.xmlpull.v1.XmlPullParser. Nothing to really
92 # do about that.
93 'id_test':
94 'com.numix.calculator.test',
95 'dump_test':
96 'dump_test.zip',
97 'apk_test':
98 'app-release-androidTest.apk',
99 'url':
100 'https://github.com/numixproject/android-suite/tree/master/Calculator',
101 'revision':
102 'f58e1b53f7278c9b675d5855842c6d8a44cccb1f',
103 'folder':
104 'android-suite-calculator',
105 }),
106 App({
107 'id': 'dev.dworks.apps.anexplorer.pro',
108 'name': 'AnExplorer',
109 'dump_app': 'dump_app.zip',
110 'apk_app': 'AnExplorer-googleMobileProRelease-4.0.3.apk',
111 'url': 'https://github.com/christofferqa/AnExplorer',
112 'revision': '365927477b8eab4052a1882d5e358057ae3dee4d',
113 'folder': 'anexplorer',
114 }),
115 App({
116 'id': 'de.danoeh.antennapod',
117 'name': 'AntennaPod',
118 'dump_app': 'dump_app.zip',
119 'apk_app': 'app-free-release.apk',
120 # TODO(b/172452102): Tests and monkey do not work
121 'id_test': 'de.danoeh.antennapod.test',
122 'dump_test': 'dump_test.zip',
123 'apk_test': 'app-free-release-androidTest.apk',
124 'url': 'https://github.com/christofferqa/AntennaPod.git',
125 'revision': '77e94f4783a16abe9cc5b78dc2d2b2b1867d8c06',
126 'folder': 'antennapod',
127 }),
128 App({
129 'id': 'com.example.applymapping',
130 'name': 'applymapping',
131 'dump_app': 'dump_app.zip',
132 'apk_app': 'app-release.apk',
133 'id_test': 'com.example.applymapping.test',
134 'dump_test': 'dump_test.zip',
135 'apk_test': 'app-release-androidTest.apk',
136 'url': 'https://github.com/mkj-gram/applymapping',
137 'revision': 'e3ae14b8c16fa4718e5dea8f7ad00937701b3c48',
138 'folder': 'applymapping',
139 }),
140 App({
141 'id':
142 'com.chanapps.four.activity',
143 'name':
144 'chanu',
145 'dump_app':
146 'dump_app.zip',
147 'apk_app':
148 'app-release.apk',
149 'url':
150 'https://github.com/mkj-gram/chanu.git',
151 'revision':
152 '6e53458f167b6d78398da60c20fd0da01a232617',
153 'folder':
154 'chanu',
155 # The app depends on a class file that has access flags interface but
156 # not abstract
157 'compiler_properties': [
158 '-Dcom.android.tools.r8.allowInvalidCfAccessFlags=true'
159 ]
160 }),
161 App({
162 'id': 'com.example.myapplication',
163 'name': 'empty-activity',
164 'dump_app': 'dump_app.zip',
165 'apk_app': 'app-release.apk',
166 'url': 'https://github.com/christofferqa/empty_android_activity.git',
167 'revision': '2d297ec3373dadb03cbae916b9feba4792563156',
168 'folder': 'empty-activity',
169 }),
170 App({
171 'id':
172 'com.example.emptycomposeactivity',
173 'name':
174 'empty-compose-activity',
175 'dump_app':
176 'dump_app.zip',
177 'apk_app':
178 'app-release.apk',
179 'url':
180 'https://github.com/christofferqa/empty_android_compose_activity.git',
181 'revision':
182 '3c8111b8b7d6e9184049a07e2b96702d7b33d03e',
183 'folder':
184 'empty-compose-activity',
185 }),
186 # TODO(b/172539375): Monkey runner fails on recompilation.
187 App({
188 'id': 'com.google.firebase.example.fireeats',
189 'name': 'FriendlyEats',
190 'dump_app': 'dump_app.zip',
191 'apk_app': 'app-release-unsigned.apk',
192 'url': 'https://github.com/firebase/friendlyeats-android',
193 'revision': '7c6dd016fc31ea5ecb948d5166b8479efc3775cc',
194 'folder': 'friendlyeats',
195 }),
196 App({
197 'id': 'com.google.samples.apps.sunflower',
198 'name': 'Sunflower',
199 'dump_app': 'dump_app.zip',
200 'apk_app': 'app-debug.apk',
201 # TODO(b/172549283): Compiling tests fails
202 'id_test': 'com.google.samples.apps.sunflower.test',
203 'dump_test': 'dump_test.zip',
204 'apk_test': 'app-debug-androidTest.apk',
205 'url': 'https://github.com/android/sunflower',
206 'revision': '0c4c88fdad2a74791199dffd1a6559559b1dbd4a',
207 'folder': 'sunflower',
208 }),
209 # TODO(b/172565385): Monkey runner fails on recompilation
210 App({
211 'id': 'com.google.samples.apps.iosched',
212 'name': 'iosched',
213 'dump_app': 'dump_app.zip',
214 'apk_app': 'mobile-release.apk',
215 'url': 'https://github.com/christofferqa/iosched.git',
216 'revision': '581cbbe2253711775dbccb753cdb53e7e506cb02',
217 'folder': 'iosched',
218 }),
219 App({
220 'id': 'fr.neamar.kiss',
221 'name': 'KISS',
222 'dump_app': 'dump_app.zip',
223 'apk_app': 'app-release.apk',
224 # TODO(b/172569220): Running tests fails due to missing keep rules
225 'id_test': 'fr.neamar.kiss.test',
226 'dump_test': 'dump_test.zip',
227 'apk_test': 'app-release-androidTest.apk',
228 'url': 'https://github.com/Neamar/KISS',
229 'revision': '8ccffaadaf0d0b8fc4418ed2b4281a0935d3d971',
230 'folder': 'kiss',
231 }),
232 # TODO(b/172577344): Monkey runner not working.
233 App({
234 'id': 'io.github.hidroh.materialistic',
235 'name': 'materialistic',
236 'dump_app': 'dump_app.zip',
237 'apk_app': 'app-release.apk',
238 'url': 'https://github.com/christofferqa/materialistic.git',
239 'revision': '2b2b2ee25ce9e672d5aab1dc90a354af1522b1d9',
240 'folder': 'materialistic',
241 }),
242 App({
243 'id': 'com.avjindersinghsekhon.minimaltodo',
244 'name': 'MinimalTodo',
245 'dump_app': 'dump_app.zip',
246 'apk_app': 'app-release.apk',
247 'url': 'https://github.com/christofferqa/Minimal-Todo',
248 'revision': '9d8c73746762cd376b718858ec1e8783ca07ba7c',
249 'folder': 'minimal-todo',
250 }),
251 App({
252 'id': 'net.nurik.roman.muzei',
253 'name': 'muzei',
254 'dump_app': 'dump_app.zip',
255 'apk_app': 'muzei-release.apk',
256 'url': 'https://github.com/romannurik/muzei',
257 'revision': '9eac6e98aebeaf0ae40bdcd85f16dd2886551138',
258 'folder': 'muzei',
259 }),
260 # TODO(b/172806281): Monkey runner does not work.
261 App({
262 'id': 'org.schabi.newpipe',
263 'name': 'NewPipe',
264 'dump_app': 'dump_app.zip',
265 'apk_app': 'app-release-unsigned.apk',
266 'url': 'https://github.com/TeamNewPipe/NewPipe',
267 'revision': 'f4435f90313281beece70c544032f784418d85fa',
268 'folder': 'newpipe',
269 }),
270 # TODO(b/172806808): Monkey runner does not work.
271 App({
272 'id': 'io.rover.app.debug',
273 'name': 'Rover',
274 'dump_app': 'dump_app.zip',
275 'apk_app': 'example-app-release-unsigned.apk',
276 'url': 'https://github.com/RoverPlatform/rover-android',
277 'revision': '94342117097770ea3ca2c6df6ab496a1a55c3ce7',
278 'folder': 'rover-android',
279 }),
280 # TODO(b/172808159): Monkey runner does not work
281 App({
282 'id': 'com.google.android.apps.santatracker',
283 'name': 'SantaTracker',
284 'dump_app': 'dump_app.zip',
285 'apk_app': 'santa-tracker-release.apk',
286 'url': 'https://github.com/christofferqa/santa-tracker-android',
287 'revision': '8dee74be7d9ee33c69465a07088c53087d24a6dd',
288 'folder': 'santa-tracker',
289 }),
290 App({
291 'id': 'org.thoughtcrime.securesms',
292 'name': 'Signal',
293 'dump_app': 'dump_app.zip',
294 'apk_app': 'Signal-Android-play-prod-universal-release-4.76.2.apk',
295 # TODO(b/172812839): Instrumentation test fails.
296 'id_test': 'org.thoughtcrime.securesms.test',
297 'dump_test': 'dump_test.zip',
298 'apk_test': 'Signal-Android-play-prod-release-androidTest.apk',
299 'url': 'https://github.com/signalapp/Signal-Android',
300 'revision': '91ca19f294362ccee2c2b43c247eba228e2b30a1',
301 'folder': 'signal-android',
302 }),
303 # TODO(b/172815827): Monkey runner does not work
304 App({
305 'id': 'com.simplemobiletools.calendar.pro',
306 'name': 'Simple-Calendar',
307 'dump_app': 'dump_app.zip',
308 'apk_app': 'calendar-release.apk',
309 'url': 'https://github.com/SimpleMobileTools/Simple-Calendar',
310 'revision': '906209874d0a091c7fce5a57972472f272d6b068',
311 'folder': 'simple-calendar',
312 }),
313 # TODO(b/172815534): Monkey runner does not work
314 App({
315 'id': 'com.simplemobiletools.camera.pro',
316 'name': 'Simple-Camera',
317 'dump_app': 'dump_app.zip',
318 'apk_app': 'camera-release.apk',
319 'url': 'https://github.com/SimpleMobileTools/Simple-Camera',
320 'revision': 'ebf9820c51e960912b3238287e30a131244fdee6',
321 'folder': 'simple-camera',
322 }),
323 App({
324 'id': 'com.simplemobiletools.filemanager.pro',
325 'name': 'Simple-File-Manager',
326 'dump_app': 'dump_app.zip',
327 'apk_app': 'file-manager-release.apk',
328 'url': 'https://github.com/SimpleMobileTools/Simple-File-Manager',
329 'revision': '2b7fa68ea251222cc40cf6d62ad1de260a6f54d9',
330 'folder': 'simple-file-manager',
331 }),
332 App({
333 'id': 'com.simplemobiletools.gallery.pro',
334 'name': 'Simple-Gallery',
335 'dump_app': 'dump_app.zip',
336 'apk_app': 'gallery-326-foss-release.apk',
337 'url': 'https://github.com/SimpleMobileTools/Simple-Gallery',
338 'revision': '564e56b20d33b28d0018c8087ec705beeb60785e',
339 'folder': 'simple-gallery',
340 }),
341 App({
342 'id': 'com.example.sqldelight.hockey',
343 'name': 'SQLDelight',
344 'dump_app': 'dump_app.zip',
345 'apk_app': 'android-release.apk',
346 'url': 'https://github.com/christofferqa/sqldelight',
347 'revision': '2e67a1126b6df05e4119d1e3a432fde51d76cdc8',
348 'folder': 'sqldelight',
349 }),
350 # TODO(b/172824096): Monkey runner does not work.
351 App({
352 'id': 'eu.kanade.tachiyomi',
353 'name': 'Tachiyomi',
354 'dump_app': 'dump_app.zip',
355 'apk_app': 'app-dev-release.apk',
356 'url': 'https://github.com/inorichi/tachiyomi',
357 'revision': '8aa6486bf76ab9a61a5494bee284b1a5e9180bf3',
358 'folder': 'tachiyomi',
359 }),
360 # TODO(b/172862042): Monkey runner does not work.
361 App({
362 'id': 'app.tivi',
363 'name': 'Tivi',
364 'dump_app': 'dump_app.zip',
365 'apk_app': 'app-release.apk',
366 'url': 'https://github.com/chrisbanes/tivi',
367 'revision': '5c6d9ed338885c59b1fc64050d92d056417bb4de',
368 'folder': 'tivi',
369 'golem_duration': 300
370 }),
371 App({
372 'id': 'com.keylesspalace.tusky',
373 'name': 'Tusky',
374 'dump_app': 'dump_app.zip',
375 'apk_app': 'app-blue-release.apk',
376 'url': 'https://github.com/tuskyapp/Tusky',
377 'revision': '814a9b8f9bacf8d26f712b06a0313a3534a2be95',
378 'folder': 'tusky',
379 }),
380 App({
381 'id': 'org.wikipedia',
382 'name': 'Wikipedia',
383 'dump_app': 'dump_app.zip',
384 'apk_app': 'app-prod-release.apk',
385 'url': 'https://github.com/wikimedia/apps-android-wikipedia',
386 'revision': '0fa7cad843c66313be8e25790ef084cf1a1fa67e',
387 'folder': 'wikipedia',
388 }),
389 # TODO(b/173167253): Check if monkey testing works.
390 App({
391 'id': 'androidx.compose.samples.crane',
392 'name': 'compose-crane',
393 'collections': ['compose-samples'],
394 'dump_app': 'dump_app.zip',
395 'apk_app': 'app-release-unsigned.apk',
396 'url': 'https://github.com/android/compose-samples',
397 'revision': '779cf9e187b8ee2c6b620b2abb4524719b3f10f8',
398 'folder': 'android/compose-samples/crane',
399 'golem_duration': 240
400 }),
401 # TODO(b/173167253): Check if monkey testing works.
402 App({
403 'id': 'com.example.jetcaster',
404 'name': 'compose-jetcaster',
405 'collections': ['compose-samples'],
406 'dump_app': 'dump_app.zip',
407 'apk_app': 'app-release-unsigned.apk',
408 'url': 'https://github.com/android/compose-samples',
409 'revision': '779cf9e187b8ee2c6b620b2abb4524719b3f10f8',
410 'folder': 'android/compose-samples/jetcaster',
411 }),
412 # TODO(b/173167253): Check if monkey testing works.
413 App({
414 'id': 'com.example.compose.jetchat',
415 'name': 'compose-jetchat',
416 'collections': ['compose-samples'],
417 'dump_app': 'dump_app.zip',
418 'apk_app': 'app-release-unsigned.apk',
419 'url': 'https://github.com/android/compose-samples',
420 'revision': '779cf9e187b8ee2c6b620b2abb4524719b3f10f8',
421 'folder': 'android/compose-samples/jetchat',
422 }),
423 # TODO(b/173167253): Check if monkey testing works.
424 App({
425 'id': 'com.example.jetnews',
426 'name': 'compose-jetnews',
427 'collections': ['compose-samples'],
428 'dump_app': 'dump_app.zip',
429 'apk_app': 'app-release-unsigned.apk',
430 'url': 'https://github.com/android/compose-samples',
431 'revision': '779cf9e187b8ee2c6b620b2abb4524719b3f10f8',
432 'folder': 'android/compose-samples/jetnews',
433 }),
434 # TODO(b/173167253): Check if monkey testing works.
435 App({
436 'id': 'com.example.jetsnack',
437 'name': 'compose-jetsnack',
438 'collections': ['compose-samples'],
439 'dump_app': 'dump_app.zip',
440 'apk_app': 'app-release-unsigned.apk',
441 'url': 'https://github.com/android/compose-samples',
442 'revision': '779cf9e187b8ee2c6b620b2abb4524719b3f10f8',
443 'folder': 'android/compose-samples/jetsnack',
444 }),
445 # TODO(b/173167253): Check if monkey testing works.
446 App({
447 'id': 'com.example.compose.jetsurvey',
448 'name': 'compose-jetsurvey',
449 'collections': ['compose-samples'],
450 'dump_app': 'dump_app.zip',
451 'apk_app': 'app-release-unsigned.apk',
452 'url': 'https://github.com/android/compose-samples',
453 'revision': '779cf9e187b8ee2c6b620b2abb4524719b3f10f8',
454 'folder': 'android/compose-samples/jetsurvey',
455 }),
456 # TODO(b/173167253): Check if monkey testing works.
457 App({
458 'id': 'com.example.owl',
459 'name': 'compose-owl',
460 'collections': ['compose-samples'],
461 'dump_app': 'dump_app.zip',
462 'apk_app': 'app-release-unsigned.apk',
463 'url': 'https://github.com/android/compose-samples',
464 'revision': '779cf9e187b8ee2c6b620b2abb4524719b3f10f8',
465 'folder': 'android/compose-samples/owl',
466 }),
467 # TODO(b/173167253): Check if monkey testing works.
468 App({
469 'id': 'com.example.compose.rally',
470 'name': 'compose-rally',
471 'collections': ['compose-samples'],
472 'dump_app': 'dump_app.zip',
473 'apk_app': 'app-release-unsigned.apk',
474 'url': 'https://github.com/android/compose-samples',
475 'revision': '779cf9e187b8ee2c6b620b2abb4524719b3f10f8',
476 'folder': 'android/compose-samples/rally',
477 }),
Christoffer Quist Adamsen53e29472020-11-13 10:53:06 +0100478]
479
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200480APP_COLLECTIONS = [AppCollection({
Christoffer Quist Adamsen53e29472020-11-13 10:53:06 +0100481 'name': 'compose-samples',
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200482})]
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100483
Morten Krogh-Jespersenf8e77032020-11-09 23:44:58 +0100484
Morten Krogh-Jespersendfeb0e32020-11-04 14:55:55 +0100485def remove_print_lines(file):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200486 with open(file) as f:
487 lines = f.readlines()
488 with open(file, 'w') as f:
489 for line in lines:
490 if '-printconfiguration' not in line:
491 f.write(line)
Morten Krogh-Jespersendfeb0e32020-11-04 14:55:55 +0100492
493
Rico Wind61a034f2021-03-16 09:37:11 +0100494def download_sha(app_sha, internal, quiet=False):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200495 if internal:
496 utils.DownloadFromX20(app_sha)
497 else:
498 utils.DownloadFromGoogleCloudStorage(app_sha, quiet=quiet)
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100499
500
Morten Krogh-Jespersencd55f812020-11-04 09:13:31 +0100501def is_logging_enabled_for(app, options):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200502 if options.no_logging:
503 return False
504 if options.app_logging_filter and app.name not in options.app_logging_filter:
505 return False
506 return True
Morten Krogh-Jespersencd55f812020-11-04 09:13:31 +0100507
508
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100509def is_minified_r8(shrinker):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200510 return '-nolib' not in shrinker
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100511
512
513def is_full_r8(shrinker):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200514 return '-full' in shrinker
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100515
516
Morten Krogh-Jespersen51db2b02020-11-11 12:49:26 +0100517def version_is_built_jar(version):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200518 return version != 'main' and version != 'source'
Morten Krogh-Jespersen51db2b02020-11-11 12:49:26 +0100519
520
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100521def compute_size_of_dex_files_in_package(path):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200522 dex_size = 0
523 z = zipfile.ZipFile(path, 'r')
524 for filename in z.namelist():
525 if filename.endswith('.dex'):
526 dex_size += z.getinfo(filename).file_size
527 return dex_size
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100528
529
530def dump_for_app(app_dir, app):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200531 return os.path.join(app_dir, app.dump_app)
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100532
533
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100534def dump_test_for_app(app_dir, app):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200535 return os.path.join(app_dir, app.dump_test)
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100536
537
Morten Krogh-Jespersen51db2b02020-11-11 12:49:26 +0100538def get_r8_jar(options, temp_dir, shrinker):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200539 if (options.version == 'source'):
540 return None
541 jar = os.path.abspath(
542 os.path.join(temp_dir, '..',
543 'r8lib.jar' if is_minified_r8(shrinker) else 'r8.jar'))
544 return jar
Morten Krogh-Jespersen51db2b02020-11-11 12:49:26 +0100545
546
Christoffer Quist Adamsenbe6661d2023-08-24 11:01:12 +0200547def get_results_for_app(app, options, temp_dir, worker_id):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200548 app_folder = app.folder if app.folder else app.name + "_" + app.revision
549 # Golem extraction will extract to the basename under the benchmarks dir.
550 app_location = os.path.basename(app_folder) if options.golem else app_folder
551 opensource_basedir = (os.path.join('benchmarks', app.name)
552 if options.golem else utils.OPENSOURCE_DUMPS_DIR)
553 app_dir = (os.path.join(utils.INTERNAL_DUMPS_DIR, app_location) if
554 app.internal else os.path.join(opensource_basedir, app_location))
555 if not os.path.exists(app_dir) and not options.golem:
556 # Download the app from google storage.
557 download_sha(app_dir + ".tar.gz.sha1", app.internal)
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100558
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200559 # Ensure that the dumps are in place
560 assert os.path.isfile(dump_for_app(app_dir, app)), "Could not find dump " \
561 "for app " + app.name
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100562
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200563 result = {}
564 result['status'] = 'success'
565 result_per_shrinker = build_app_with_shrinkers(app,
566 options,
567 temp_dir,
568 app_dir,
569 worker_id=worker_id)
570 for shrinker, shrinker_result in result_per_shrinker.items():
571 result[shrinker] = shrinker_result
572 return result
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100573
574
Christoffer Quist Adamsenbe6661d2023-08-24 11:01:12 +0200575def build_app_with_shrinkers(app, options, temp_dir, app_dir, worker_id):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200576 result_per_shrinker = {}
577 for shrinker in options.shrinker:
578 results = []
579 build_app_and_run_with_shrinker(app,
580 options,
581 temp_dir,
582 app_dir,
583 shrinker,
584 results,
585 worker_id=worker_id)
586 result_per_shrinker[shrinker] = results
587 if len(options.apps) > 1:
588 print_thread('', worker_id)
589 log_results_for_app(app,
590 result_per_shrinker,
591 options,
592 worker_id=worker_id)
593 print_thread('', worker_id)
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100594
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200595 return result_per_shrinker
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100596
597
598def is_last_build(index, compilation_steps):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200599 return index == compilation_steps - 1
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100600
601
602def build_app_and_run_with_shrinker(app, options, temp_dir, app_dir, shrinker,
Christoffer Quist Adamsenbe6661d2023-08-24 11:01:12 +0200603 results, worker_id):
Christoffer Quist Adamsenbe6661d2023-08-24 11:01:12 +0200604 print_thread(
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200605 '[{}] Building {} with {}'.format(datetime.now().strftime("%H:%M:%S"),
606 app.name, shrinker), worker_id)
607 print_thread(
608 'To compile locally: '
609 'tools/run_on_app_dump.py --shrinker {} --r8-compilation-steps {} '
610 '--app {} --minify {} --optimize {} --shrink {}'.format(
611 shrinker, options.r8_compilation_steps, app.name, options.minify,
612 options.optimize, options.shrink), worker_id)
613 print_thread(
614 'HINT: use --shrinker r8-nolib --no-build if you have a local R8.jar',
Christoffer Quist Adamsenbe6661d2023-08-24 11:01:12 +0200615 worker_id)
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200616 recomp_jar = None
617 status = 'success'
618 if options.r8_compilation_steps < 1:
619 return
620 compilation_steps = 1 if app.skip_recompilation else options.r8_compilation_steps
621 for compilation_step in range(0, compilation_steps):
622 if status != 'success':
623 break
624 print_thread(
625 'Compiling {} of {}'.format(compilation_step + 1,
626 compilation_steps), worker_id)
627 result = {}
628 try:
629 start = time.time()
630 (app_jar, mapping, new_recomp_jar) = \
631 build_app_with_shrinker(
632 app, options, temp_dir, app_dir, shrinker, compilation_step,
633 compilation_steps, recomp_jar, worker_id=worker_id)
634 end = time.time()
635 dex_size = compute_size_of_dex_files_in_package(app_jar)
636 result['build_status'] = 'success'
637 result['recompilation_status'] = 'success'
638 result['output_jar'] = app_jar
639 result['output_mapping'] = mapping
640 result['dex_size'] = dex_size
641 result['duration'] = int((end - start) * 1000) # Wall time
642 if (new_recomp_jar is None and
643 not is_last_build(compilation_step, compilation_steps)):
644 result['recompilation_status'] = 'failed'
645 warn('Failed to build {} with {}'.format(app.name, shrinker))
646 recomp_jar = new_recomp_jar
647 except Exception as e:
648 warn('Failed to build {} with {}'.format(app.name, shrinker))
649 if e:
650 print_thread('Error: ' + str(e), worker_id)
651 result['build_status'] = 'failed'
652 status = 'failed'
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100653
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200654 original_app_apk = os.path.join(app_dir, app.apk_app)
655 app_apk_destination = os.path.join(
656 temp_dir, "{}_{}.apk".format(app.id, compilation_step))
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100657
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200658 if result.get('build_status') == 'success' and options.monkey:
659 # Make a copy of the given APK, move the newly generated dex files into the
660 # copied APK, and then sign the APK.
661 apk_masseur.masseur(original_app_apk,
662 dex=app_jar,
663 resources='META-INF/services/*',
664 out=app_apk_destination,
665 quiet=options.quiet,
666 logging=is_logging_enabled_for(app, options),
667 keystore=options.keystore)
Morten Krogh-Jespersencd55f812020-11-04 09:13:31 +0100668
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200669 result['monkey_status'] = 'success' if adb.run_monkey(
670 app.id, options.emulator_id, app_apk_destination,
671 options.monkey_events, options.quiet,
672 is_logging_enabled_for(app, options)) else 'failed'
Morten Krogh-Jespersencd55f812020-11-04 09:13:31 +0100673
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200674 if (result.get('build_status') == 'success' and options.run_tests and
675 app.dump_test):
676 if not os.path.isfile(app_apk_destination):
677 apk_masseur.masseur(original_app_apk,
678 dex=app_jar,
679 resources='META-INF/services/*',
680 out=app_apk_destination,
681 quiet=options.quiet,
682 logging=is_logging_enabled_for(
683 app, options),
684 keystore=options.keystore)
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100685
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200686 # Compile the tests with the mapping file.
687 test_jar = build_test_with_shrinker(app, options, temp_dir, app_dir,
688 shrinker, compilation_step,
689 result['output_mapping'])
690 if not test_jar:
691 result['instrumentation_test_status'] = 'compilation_failed'
692 else:
693 original_test_apk = os.path.join(app_dir, app.apk_test)
694 test_apk_destination = os.path.join(
695 temp_dir, "{}_{}.test.apk".format(app.id_test,
696 compilation_step))
697 apk_masseur.masseur(original_test_apk,
698 dex=test_jar,
699 resources='META-INF/services/*',
700 out=test_apk_destination,
701 quiet=options.quiet,
702 logging=is_logging_enabled_for(
703 app, options),
704 keystore=options.keystore)
705 result[
706 'instrumentation_test_status'] = 'success' if adb.run_instrumented(
707 app.id, app.id_test, options.emulator_id,
708 app_apk_destination,
709 test_apk_destination, options.quiet,
710 is_logging_enabled_for(app, options)) else 'failed'
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100711
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200712 results.append(result)
713 if result.get('recompilation_status') != 'success':
714 break
715
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100716
Rico Wind33936d12021-04-07 08:04:14 +0200717def get_jdk_home(options, app):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200718 if options.golem:
719 return os.path.join('benchmarks', app.name, 'linux')
720 return None
721
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100722
723def build_app_with_shrinker(app, options, temp_dir, app_dir, shrinker,
724 compilation_step_index, compilation_steps,
Christoffer Quist Adamsenbe6661d2023-08-24 11:01:12 +0200725 prev_recomp_jar, worker_id):
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100726
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200727 def config_files_consumer(files):
728 for file in files:
729 compiledump.clean_config(file, options)
730 remove_print_lines(file)
Morten Krogh-Jespersenc9d23462020-11-12 15:36:57 +0100731
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200732 args = AttrDict({
733 'dump': dump_for_app(app_dir, app),
734 'r8_jar': get_r8_jar(options, temp_dir, shrinker),
735 'r8_flags': options.r8_flags,
736 'disable_assertions': options.disable_assertions,
737 'version': options.version,
738 'compiler': 'r8full' if is_full_r8(shrinker) else 'r8',
739 'debug_agent': options.debug_agent,
740 'program_jar': prev_recomp_jar,
741 'nolib': not is_minified_r8(shrinker),
742 'config_files_consumer': config_files_consumer,
743 'properties': app.compiler_properties,
744 'disable_desugared_lib': False,
745 'print_times': options.print_times,
746 })
Morten Krogh-Jespersenc9d23462020-11-12 15:36:57 +0100747
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200748 app_jar = os.path.join(
749 temp_dir, '{}_{}_{}_dex_out.jar'.format(app.name, shrinker,
750 compilation_step_index))
751 app_mapping = os.path.join(
752 temp_dir, '{}_{}_{}_dex_out.jar.map'.format(app.name, shrinker,
753 compilation_step_index))
754 recomp_jar = None
755 jdkhome = get_jdk_home(options, app)
756 with utils.TempDir() as compile_temp_dir:
757 compile_result = compiledump.run1(compile_temp_dir,
758 args, [],
759 jdkhome,
760 worker_id=worker_id)
761 out_jar = os.path.join(compile_temp_dir, "out.jar")
762 out_mapping = os.path.join(compile_temp_dir, "out.jar.map")
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100763
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200764 if compile_result != 0 or not os.path.isfile(out_jar):
765 assert False, 'Compilation of {} failed'.format(
766 dump_for_app(app_dir, app))
767 shutil.move(out_jar, app_jar)
768 shutil.move(out_mapping, app_mapping)
769
770 if compilation_step_index < compilation_steps - 1:
771 args['classfile'] = True
772 args['min_api'] = "10000"
773 args['disable_desugared_lib'] = True
774 compile_result = compiledump.run1(compile_temp_dir, args, [],
775 jdkhome)
776 if compile_result == 0:
777 recomp_jar = os.path.join(
778 temp_dir,
779 '{}_{}_{}_cf_out.jar'.format(app.name, shrinker,
780 compilation_step_index))
781 shutil.move(out_jar, recomp_jar)
782
783 return (app_jar, app_mapping, recomp_jar)
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100784
Morten Krogh-Jespersen162b3452020-11-05 13:07:10 +0100785
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100786def build_test_with_shrinker(app, options, temp_dir, app_dir, shrinker,
787 compilation_step_index, mapping):
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100788
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200789 def rewrite_files(files):
790 add_applymapping = True
791 for file in files:
792 compiledump.clean_config(file, options)
793 remove_print_lines(file)
794 with open(file) as f:
795 lines = f.readlines()
796 with open(file, 'w') as f:
797 for line in lines:
798 if '-applymapping' not in line:
799 f.write(line + '\n')
800 if add_applymapping:
801 f.write("-applymapping " + mapping + '\n')
802 add_applymapping = False
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100803
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200804 args = AttrDict({
805 'dump': dump_test_for_app(app_dir, app),
806 'r8_jar': get_r8_jar(options, temp_dir, shrinker),
807 'disable_assertions': options.disable_assertions,
808 'version': options.version,
809 'compiler': 'r8full' if is_full_r8(shrinker) else 'r8',
810 'debug_agent': options.debug_agent,
811 'nolib': not is_minified_r8(shrinker),
812 # The config file will have an -applymapping reference to an old map.
813 # Update it to point to mapping file build in the compilation of the app.
814 'config_files_consumer': rewrite_files,
815 })
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100816
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200817 test_jar = os.path.join(
818 temp_dir, '{}_{}_{}_test_out.jar'.format(app.name, shrinker,
819 compilation_step_index))
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100820
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200821 with utils.TempDir() as compile_temp_dir:
822 jdkhome = get_jdk_home(options, app)
823 compile_result = compiledump.run1(compile_temp_dir, args, [], jdkhome)
824 out_jar = os.path.join(compile_temp_dir, "out.jar")
825 if compile_result != 0 or not os.path.isfile(out_jar):
826 return None
827 shutil.move(out_jar, test_jar)
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100828
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200829 return test_jar
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100830
831
832def log_results_for_apps(result_per_shrinker_per_app, options):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200833 print('')
834 app_errors = 0
835 for (app, result_per_shrinker) in result_per_shrinker_per_app:
836 app_errors += (1 if log_results_for_app(app, result_per_shrinker,
837 options) else 0)
838 return app_errors
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100839
840
Christoffer Quist Adamsenbe6661d2023-08-24 11:01:12 +0200841def log_results_for_app(app, result_per_shrinker, options, worker_id=None):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200842 if options.print_dexsegments:
843 log_segments_for_app(app,
844 result_per_shrinker,
845 options,
846 worker_id=worker_id)
847 return False
848 else:
849 return log_comparison_results_for_app(app,
850 result_per_shrinker,
851 options,
852 worker_id=worker_id)
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100853
854
Christoffer Quist Adamsenbe6661d2023-08-24 11:01:12 +0200855def log_segments_for_app(app, result_per_shrinker, options, worker_id):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200856 for shrinker in SHRINKERS:
857 if shrinker not in result_per_shrinker:
858 continue
859 for result in result_per_shrinker.get(shrinker):
860 benchmark_name = '{}-{}'.format(options.print_dexsegments, app.name)
861 utils.print_dexsegments(benchmark_name, [result.get('output_jar')],
862 worker_id=worker_id)
863 duration = result.get('duration')
864 print_thread(
865 '%s-Total(RunTimeRaw): %s ms' % (benchmark_name, duration),
866 worker_id)
867 print_thread(
868 '%s-Total(CodeSize): %s' %
869 (benchmark_name, result.get('dex_size')), worker_id)
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100870
871
872def percentage_diff_as_string(before, after):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200873 if after < before:
874 return '-' + str(round((1.0 - after / before) * 100)) + '%'
875 else:
876 return '+' + str(round((after - before) / before * 100)) + '%'
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100877
878
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200879def log_comparison_results_for_app(app, result_per_shrinker, options,
880 worker_id):
881 print_thread(app.name + ':', worker_id)
882 app_error = False
883 if result_per_shrinker.get('status', 'success') != 'success':
884 error_message = result_per_shrinker.get('error_message')
885 print_thread(' skipped ({})'.format(error_message), worker_id)
886 return
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100887
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200888 proguard_result = result_per_shrinker.get('pg', {})
889 proguard_dex_size = float(proguard_result.get('dex_size', -1))
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100890
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200891 for shrinker in SHRINKERS:
892 if shrinker not in result_per_shrinker:
893 continue
894 compilation_index = 1
895 for result in result_per_shrinker.get(shrinker):
896 build_status = result.get('build_status')
897 if build_status != 'success' and build_status is not None:
898 app_error = True
899 warn(' {}-#{}: {}'.format(shrinker, compilation_index,
900 build_status))
901 continue
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100902
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200903 if options.golem:
904 print_thread(
905 '%s(RunTimeRaw): %s ms' %
906 (app.name, result.get('duration')), worker_id)
907 print_thread(
908 '%s(CodeSize): %s' % (app.name, result.get('dex_size')),
909 worker_id)
910 continue
Rico Wind59593922021-03-03 09:12:36 +0100911
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200912 print_thread(' {}-#{}:'.format(shrinker, compilation_index),
913 worker_id)
914 dex_size = result.get('dex_size')
915 msg = ' dex size: {}'.format(dex_size)
916 if options.print_runtimeraw:
917 print_thread(
918 ' run time raw: {} ms'.format(result.get('duration')),
919 worker_id)
920 if dex_size != proguard_dex_size and proguard_dex_size >= 0:
921 msg = '{} ({}, {})'.format(
922 msg, dex_size - proguard_dex_size,
923 percentage_diff_as_string(proguard_dex_size, dex_size))
924 success(msg) if dex_size < proguard_dex_size else warn(msg)
925 else:
926 print_thread(msg, worker_id)
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100927
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200928 if options.monkey:
929 monkey_status = result.get('monkey_status')
930 if monkey_status != 'success':
931 app_error = True
932 warn(' monkey: {}'.format(monkey_status))
933 else:
934 success(' monkey: {}'.format(monkey_status))
Morten Krogh-Jespersencd55f812020-11-04 09:13:31 +0100935
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200936 if options.run_tests and 'instrumentation_test_status' in result:
937 test_status = result.get('instrumentation_test_status')
938 if test_status != 'success':
939 warn(' instrumentation_tests: {}'.format(test_status))
940 else:
941 success(' instrumentation_tests: {}'.format(test_status))
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100942
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200943 recompilation_status = result.get('recompilation_status', '')
944 if recompilation_status == 'failed':
945 app_error = True
946 warn(' recompilation {}-#{}: failed'.format(
947 shrinker, compilation_index))
948 continue
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100949
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200950 compilation_index += 1
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100951
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200952 return app_error
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100953
954
955def parse_options(argv):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200956 result = argparse.ArgumentParser(description='Run/compile dump artifacts.')
957 result.add_argument('--app',
958 help='What app to run on',
959 choices=[app.name for app in APPS],
960 action='append')
961 result.add_argument(
962 '--app-collection',
963 '--app_collection',
964 help='What app collection to run',
965 choices=[collection.name for collection in APP_COLLECTIONS],
966 action='append')
967 result.add_argument('--app-logging-filter',
968 '--app_logging_filter',
969 help='The apps for which to turn on logging',
970 action='append')
971 result.add_argument('--bot',
972 help='Running on bot, use third_party dependency.',
973 default=False,
974 action='store_true')
975 result.add_argument('--generate-golem-config',
976 '--generate_golem_config',
977 help='Generate a new config for golem.',
978 default=False,
979 action='store_true')
980 result.add_argument('--debug-agent',
981 help='Enable Java debug agent and suspend compilation '
982 '(default disabled)',
983 default=False,
984 action='store_true')
985 result.add_argument(
986 '--disable-assertions',
987 '--disable_assertions',
988 '-da',
989 help='Disable Java assertions when running the compiler '
990 '(default enabled)',
991 default=False,
992 action='store_true')
993 result.add_argument('--emulator-id',
994 '--emulator_id',
995 help='Id of the emulator to use',
996 default='emulator-5554')
997 result.add_argument('--golem',
998 help='Running on golem, do not download',
999 default=False,
1000 action='store_true')
1001 result.add_argument('--hash', help='The commit of R8 to use')
1002 result.add_argument(
1003 '--internal',
1004 help='Run internal apps if set, otherwise run opensource',
1005 default=False,
1006 action='store_true')
1007 result.add_argument('--keystore',
1008 help='Path to app.keystore',
1009 default=os.path.join(utils.TOOLS_DIR, 'debug.keystore'))
1010 result.add_argument('--keystore-password',
1011 '--keystore_password',
1012 help='Password for app.keystore',
1013 default='android')
1014 result.add_argument('--minify',
1015 help='Force enable/disable minification' +
1016 ' (defaults to app proguard config)',
1017 choices=['default', 'force-enable', 'force-disable'],
1018 default='default')
1019 result.add_argument('--monkey',
1020 help='Whether to install and run app(s) with monkey',
1021 default=False,
1022 action='store_true')
1023 result.add_argument('--monkey-events',
1024 '--monkey_events',
1025 help='Number of events that the monkey should trigger',
1026 default=250,
1027 type=int)
1028 result.add_argument('--no-build',
1029 '--no_build',
1030 help='Run without building first (only when using ToT)',
1031 default=False,
1032 action='store_true')
1033 result.add_argument('--no-logging',
1034 '--no_logging',
1035 help='Disable logging except for errors',
1036 default=False,
1037 action='store_true')
1038 result.add_argument('--optimize',
1039 help='Force enable/disable optimizations' +
1040 ' (defaults to app proguard config)',
1041 choices=['default', 'force-enable', 'force-disable'],
1042 default='default')
1043 result.add_argument('--print-times',
1044 help='Print timing information from r8',
1045 default=False,
1046 action='store_true')
1047 result.add_argument('--print-dexsegments',
1048 metavar='BENCHMARKNAME',
1049 help='Print the sizes of individual dex segments as ' +
1050 '\'<BENCHMARKNAME>-<APP>-<segment>(CodeSize): '
1051 '<bytes>\'')
1052 result.add_argument('--print-runtimeraw',
1053 metavar='BENCHMARKNAME',
1054 help='Print the line \'<BENCHMARKNAME>(RunTimeRaw):' +
1055 ' <elapsed> ms\' at the end where <elapsed> is' +
1056 ' the elapsed time in milliseconds.')
1057 result.add_argument('--quiet',
1058 help='Disable verbose logging',
1059 default=False,
1060 action='store_true')
1061 result.add_argument('--r8-compilation-steps',
1062 '--r8_compilation_steps',
1063 help='Number of times R8 should be run on each app',
1064 default=2,
1065 type=int)
1066 result.add_argument('--r8-flags',
1067 '--r8_flags',
1068 help='Additional option(s) for the compiler.')
1069 result.add_argument('--run-tests',
1070 '--run_tests',
1071 help='Whether to run instrumentation tests',
1072 default=False,
1073 action='store_true')
1074 result.add_argument('--shrink',
1075 help='Force enable/disable shrinking' +
1076 ' (defaults to app proguard config)',
1077 choices=['default', 'force-enable', 'force-disable'],
1078 default='default')
1079 result.add_argument('--sign-apks',
1080 '--sign_apks',
1081 help='Whether the APKs should be signed',
1082 default=False,
1083 action='store_true')
1084 result.add_argument('--shrinker',
1085 help='The shrinkers to use (by default, all are run)',
1086 action='append')
1087 result.add_argument('--temp',
1088 help='A directory to use for temporaries and outputs.',
1089 default=None)
1090 result.add_argument('--version',
1091 default='main',
1092 help='The version of R8 to use (e.g., 1.4.51)')
1093 result.add_argument('--workers',
1094 help='Number of workers to use',
1095 default=1,
1096 type=int)
1097 (options, args) = result.parse_known_args(argv)
Christoffer Quist Adamsen53e29472020-11-13 10:53:06 +01001098
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001099 if options.app or options.app_collection:
1100 if not options.app:
1101 options.app = []
1102 if not options.app_collection:
1103 options.app_collection = []
1104 options.apps = [
1105 app for app in APPS if app.name in options.app or any(
1106 collection in options.app_collection
1107 for collection in app.collections)
1108 ]
1109 del options.app
1110 del options.app_collection
1111 else:
1112 options.apps = [app for app in APPS if app.internal == options.internal]
Christoffer Quist Adamsen53e29472020-11-13 10:53:06 +01001113
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001114 if options.app_logging_filter:
1115 for app_name in options.app_logging_filter:
1116 assert any(app.name == app_name for app in options.apps)
1117 if options.shrinker:
1118 for shrinker in options.shrinker:
1119 assert shrinker in SHRINKERS, ('Shrinker must be one of %s' %
1120 ', '.join(SHRINKERS))
1121 else:
1122 options.shrinker = [shrinker for shrinker in SHRINKERS]
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +01001123
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001124 if options.hash or version_is_built_jar(options.version):
1125 # No need to build R8 if a specific version should be used.
1126 options.no_build = True
1127 if 'r8-nolib' in options.shrinker:
1128 warn('Skipping shrinker r8-nolib because a specific version ' +
1129 'of r8 was specified')
1130 options.shrinker.remove('r8-nolib')
1131 if 'r8-nolib-full' in options.shrinker:
1132 warn('Skipping shrinker r8-nolib-full because a specific version ' +
1133 'of r8 was specified')
1134 options.shrinker.remove('r8-nolib-full')
1135 return (options, args)
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +01001136
1137
Rico Wind59593922021-03-03 09:12:36 +01001138def print_indented(s, indent):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001139 print(' ' * indent + s)
Rico Wind59593922021-03-03 09:12:36 +01001140
1141
1142def get_sha256(gz_file):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001143 with open(gz_file, 'rb') as f:
1144 bytes = f.read() # read entire file as bytes
1145 return hashlib.sha256(bytes).hexdigest()
Rico Wind59593922021-03-03 09:12:36 +01001146
1147
1148def get_sha_from_file(sha_file):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001149 with open(sha_file, 'r') as f:
1150 return f.readlines()[0]
Rico Wind59593922021-03-03 09:12:36 +01001151
1152
1153def print_golem_config(options):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001154 print('// AUTOGENERATED FILE from tools/run_on_app_dump.py in R8 repo')
1155 print('part of r8_config;')
1156 print('')
1157 print('final Suite dumpsSuite = Suite("OpenSourceAppDumps");')
1158 print('')
1159 print('createOpenSourceAppBenchmarks() {')
1160 print_indented('final cpus = ["Lenovo M90"];', 2)
1161 print_indented('final targetsCompat = ["R8"];', 2)
1162 print_indented('final targetsFull = ["R8-full-minify-optimize-shrink"];', 2)
1163 # Avoid calculating this for every app
1164 jdk_gz = jdk.GetJdkHome() + '.tar.gz'
1165 add_golem_resource(2, jdk_gz, 'openjdk')
1166 for app in options.apps:
1167 if app.folder and not app.internal:
1168 indentation = 2
1169 print_indented('{', indentation)
1170 indentation = 4
1171 print_indented('final name = "%s";' % app.name, indentation)
1172 print_indented('final benchmark =', indentation)
1173 print_indented(
1174 'StandardBenchmark(name, [Metric.RunTimeRaw, Metric.CodeSize]);',
1175 indentation + 4)
1176 if app.golem_duration != None:
1177 print_indented(
1178 'final timeout = const Duration(seconds: %s);' %
1179 app.golem_duration, indentation)
1180 print_indented(
1181 'ExecutionManagement.addTimeoutConstraint'
1182 '(timeout, benchmark: benchmark);', indentation)
1183 app_gz = os.path.join(utils.OPENSOURCE_DUMPS_DIR,
1184 app.folder + '.tar.gz')
1185 name = 'appResource'
1186 add_golem_resource(indentation, app_gz, name)
1187 print_golem_config_target('Compat', 'r8', app, indentation)
1188 print_golem_config_target('Full',
1189 'r8-full',
1190 app,
1191 indentation,
1192 minify='force-enable',
1193 optimize='force-enable',
1194 shrink='force-enable')
1195 print_indented('dumpsSuite.addBenchmark(name);', indentation)
1196 indentation = 2
1197 print_indented('}', indentation)
1198 print('}')
Rico Wind59593922021-03-03 09:12:36 +01001199
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001200
1201def print_golem_config_target(target,
1202 shrinker,
1203 app,
1204 indentation,
1205 minify='default',
1206 optimize='default',
1207 shrink='default'):
1208 options = "options" + target
1209 print_indented(
1210 'final %s = benchmark.addTargets(noImplementation, targets%s);' %
1211 (options, target), indentation)
1212 print_indented('%s.cpus = cpus;' % options, indentation)
1213 print_indented('%s.isScript = true;' % options, indentation)
1214 print_indented('%s.fromRevision = 9700;' % options, indentation)
1215 print_indented('%s.mainFile = "tools/run_on_app_dump.py "' % options,
1216 indentation)
1217 print_indented(
1218 '"--golem --disable-assertions --quiet --shrinker %s --app %s "' %
1219 (shrinker, app.name), indentation + 4)
1220 print_indented(
1221 '"--minify %s --optimize %s --shrink %s";' % (minify, optimize, shrink),
1222 indentation + 4)
1223 print_indented('%s.resources.add(appResource);' % options, indentation)
1224 print_indented('%s.resources.add(openjdk);' % options, indentation)
1225
Christoffer Quist Adamsen8e9f7982021-12-10 13:10:05 +01001226
Rico Wind61a034f2021-03-16 09:37:11 +01001227def add_golem_resource(indentation, gz, name, sha256=None):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001228 sha = gz + '.sha1'
1229 if not sha256:
1230 # Golem uses a sha256 of the file in the cache, and you need to specify that.
1231 download_sha(sha, False, quiet=True)
1232 sha256 = get_sha256(gz)
1233 sha = get_sha_from_file(sha)
1234 print_indented('final %s = BenchmarkResource("",' % name, indentation)
1235 print_indented('type: BenchmarkResourceType.storage,', indentation + 4)
1236 print_indented('uri: "gs://r8-deps/%s",' % sha, indentation + 4)
1237 # Make dart formatter happy.
1238 if indentation > 2:
1239 print_indented('hash:', indentation + 4)
1240 print_indented('"%s",' % sha256, indentation + 8)
1241 else:
1242 print_indented('hash: "%s",' % sha256, indentation + 4)
1243 print_indented('extract: "gz");', indentation + 4)
1244
Rico Wind61a034f2021-03-16 09:37:11 +01001245
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +01001246def main(argv):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001247 (options, args) = parse_options(argv)
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +01001248
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001249 if options.bot:
1250 options.no_logging = True
1251 options.shrinker = ['r8', 'r8-full']
1252 print(options.shrinker)
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +01001253
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001254 if options.golem:
1255 options.disable_assertions = True
1256 options.no_build = True
1257 options.r8_compilation_steps = 1
1258 options.quiet = True
1259 options.no_logging = True
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +01001260
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001261 if options.generate_golem_config:
1262 print_golem_config(options)
1263 return 0
Rico Wind59593922021-03-03 09:12:36 +01001264
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001265 with utils.TempDir() as temp_dir:
1266 if options.temp:
1267 temp_dir = options.temp
1268 os.makedirs(temp_dir, exist_ok=True)
1269 if options.hash:
1270 # Download r8-<hash>.jar from
1271 # https://storage.googleapis.com/r8-releases/raw/.
1272 target = 'r8-{}.jar'.format(options.hash)
1273 update_prebuilds_in_android.download_hash(
1274 temp_dir, 'com/android/tools/r8/' + options.hash, target)
1275 as_utils.MoveFile(os.path.join(temp_dir, target),
1276 os.path.join(temp_dir, 'r8lib.jar'),
1277 quiet=options.quiet)
1278 elif version_is_built_jar(options.version):
1279 # Download r8-<version>.jar from
1280 # https://storage.googleapis.com/r8-releases/raw/.
1281 target = 'r8-{}.jar'.format(options.version)
1282 update_prebuilds_in_android.download_version(
1283 temp_dir, 'com/android/tools/r8/' + options.version, target)
1284 as_utils.MoveFile(os.path.join(temp_dir, target),
1285 os.path.join(temp_dir, 'r8lib.jar'),
1286 quiet=options.quiet)
1287 elif options.version == 'main':
1288 if not options.no_build:
1289 gradle.RunGradle([
Ian Zernyed1f8512023-11-07 11:07:57 +01001290 utils.GRADLE_TASK_R8,
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001291 '-Pno_internal'
1292 ])
1293 build_r8lib = False
1294 for shrinker in options.shrinker:
1295 if is_minified_r8(shrinker):
1296 build_r8lib = True
1297 if build_r8lib:
1298 gradle.RunGradle([utils.GRADLE_TASK_R8LIB, '-Pno_internal'])
1299 # Make a copy of r8.jar and r8lib.jar such that they stay the same for
1300 # the entire execution of this script.
1301 if 'r8-nolib' in options.shrinker or 'r8-nolib-full' in options.shrinker:
1302 assert os.path.isfile(
1303 utils.R8_JAR), 'Cannot build without r8.jar'
1304 shutil.copyfile(utils.R8_JAR, os.path.join(temp_dir, 'r8.jar'))
1305 if 'r8' in options.shrinker or 'r8-full' in options.shrinker:
1306 assert os.path.isfile(
1307 utils.R8LIB_JAR), 'Cannot build without r8lib.jar'
1308 shutil.copyfile(utils.R8LIB_JAR,
1309 os.path.join(temp_dir, 'r8lib.jar'))
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +01001310
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001311 jobs = []
1312 result_per_shrinker_per_app = []
1313 for app in options.apps:
1314 if app.skip:
1315 continue
1316 result = {}
1317 result_per_shrinker_per_app.append((app, result))
1318 jobs.append(create_job(app, options, result, temp_dir))
1319 thread_utils.run_in_parallel(jobs,
1320 number_of_workers=options.workers,
1321 stop_on_first_failure=False)
1322 errors = log_results_for_apps(result_per_shrinker_per_app, options)
1323 if errors > 0:
1324 dest = 'gs://r8-test-results/r8-libs/' + str(int(time.time()))
1325 utils.upload_file_to_cloud_storage(
1326 os.path.join(temp_dir, 'r8lib.jar'), dest)
1327 print('R8lib saved to %s' % dest)
1328 return errors
1329
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +01001330
Christoffer Quist Adamsenbe6661d2023-08-24 11:01:12 +02001331def create_job(app, options, result, temp_dir):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001332 return lambda worker_id: run_job(app, options, result, temp_dir, worker_id)
1333
Christoffer Quist Adamsenbe6661d2023-08-24 11:01:12 +02001334
1335def run_job(app, options, result, temp_dir, worker_id):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001336 job_temp_dir = os.path.join(temp_dir, str(worker_id or 0))
1337 os.makedirs(job_temp_dir, exist_ok=True)
1338 result.update(get_results_for_app(app, options, job_temp_dir, worker_id))
1339 return 0
1340
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +01001341
1342def success(message):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001343 CGREEN = '\033[32m'
1344 CEND = '\033[0m'
1345 print(CGREEN + message + CEND)
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +01001346
1347
1348def warn(message):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001349 CRED = '\033[91m'
1350 CEND = '\033[0m'
1351 print(CRED + message + CEND)
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +01001352
1353
1354if __name__ == '__main__':
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001355 sys.exit(main(sys.argv[1:]))