blob: b047d7270fa7988f6252f278bf61267ff7f06462 [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
Morten Krogh-Jespersen47764032020-11-11 15:09:39 +010021import update_prebuilds_in_android
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +010022import utils
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +010023
Ian Zerny161ff742022-01-20 12:39:40 +010024GOLEM_BUILD_TARGETS = ['R8Lib', 'R8Retrace']
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +010025SHRINKERS = ['r8', 'r8-full', 'r8-nolib', 'r8-nolib-full']
26
27class AttrDict(dict):
28 def __getattr__(self, name):
29 return self.get(name, None)
30
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +010031# To generate the files for a new app, navigate to the app source folder and
32# run:
33# ./gradlew clean :app:assembleRelease -Dcom.android.tools.r8.dumpinputtodirectory=<path>
34# and store the dump and the apk.
35# If the app has instrumented tests, adding `testBuildType "release"` and
36# running:
37# ./gradlew assembleAndroidTest -Dcom.android.tools.r8.dumpinputtodirectory=<path>
38# will also generate dumps and apk for tests.
39
40class App(object):
41 def __init__(self, fields):
42 defaults = {
43 'id': None,
44 'name': None,
Christoffer Quist Adamsen53e29472020-11-13 10:53:06 +010045 'collections': [],
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +010046 'dump_app': None,
Morten Krogh-Jespersencd55f812020-11-04 09:13:31 +010047 'apk_app': None,
Morten Krogh-Jespersen571cfe72020-11-09 23:44:03 +010048 'dump_test': None,
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +010049 'apk_test': None,
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +010050 'skip': False,
51 'url': None, # url is not used but nice to have for updating apps
52 'revision': None,
53 'folder': None,
54 'skip_recompilation': False,
Morten Krogh-Jespersen43f3cea2020-11-12 17:09:51 +010055 'compiler_properties': [],
Morten Krogh-Jespersen86222742021-03-02 11:13:33 +010056 'internal': False,
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +010057 }
58 # This below does not work in python3
59 defaults.update(fields.items())
60 self.__dict__ = defaults
61
62
Christoffer Quist Adamsen53e29472020-11-13 10:53:06 +010063class AppCollection(object):
64 def __init__(self, fields):
65 defaults = {
66 'name': None
67 }
68 # This below does not work in python3
69 defaults.update(fields.items())
70 self.__dict__ = defaults
71
72
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +010073APPS = [
74 App({
Morten Krogh-Jespersen0f85fa52020-11-04 13:40:43 +010075 'id': 'com.numix.calculator',
76 'name': 'Calculator',
77 'dump_app': 'dump_app.zip',
78 'apk_app': 'app-release.apk',
79 # Compiling tests fail: Library class android.content.res.XmlResourceParser
80 # implements program class org.xmlpull.v1.XmlPullParser. Nothing to really
81 # do about that.
82 'id_test': 'com.numix.calculator.test',
83 'dump_test': 'dump_test.zip',
84 'apk_test': 'app-release-androidTest.apk',
85 'url': 'https://github.com/numixproject/android-suite/tree/master/Calculator',
86 'revision': 'f58e1b53f7278c9b675d5855842c6d8a44cccb1f',
87 'folder': 'android-suite-calculator',
88 }),
89 App({
Morten Krogh-Jespersen3f0d72f2020-11-04 15:49:17 +010090 'id': 'dev.dworks.apps.anexplorer.pro',
91 'name': 'AnExplorer',
92 'dump_app': 'dump_app.zip',
93 'apk_app': 'AnExplorer-googleMobileProRelease-4.0.3.apk',
94 'url': 'https://github.com/christofferqa/AnExplorer',
95 'revision': '365927477b8eab4052a1882d5e358057ae3dee4d',
96 'folder': 'anexplorer',
97 }),
98 App({
Morten Krogh-Jespersen09e2fda2020-11-04 16:43:25 +010099 'id': 'de.danoeh.antennapod',
100 'name': 'AntennaPod',
101 'dump_app': 'dump_app.zip',
102 'apk_app': 'app-free-release.apk',
103 # TODO(b/172452102): Tests and monkey do not work
104 'id_test': 'de.danoeh.antennapod.test',
105 'dump_test': 'dump_test.zip',
106 'apk_test': 'app-free-release-androidTest.apk',
107 'url': 'https://github.com/christofferqa/AntennaPod.git',
108 'revision': '77e94f4783a16abe9cc5b78dc2d2b2b1867d8c06',
109 'folder': 'antennapod',
Morten Krogh-Jespersen09e2fda2020-11-04 16:43:25 +0100110 }),
111 App({
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100112 'id': 'com.example.applymapping',
113 'name': 'applymapping',
114 'dump_app': 'dump_app.zip',
Morten Krogh-Jespersencd55f812020-11-04 09:13:31 +0100115 'apk_app': 'app-release.apk',
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100116 'id_test': 'com.example.applymapping.test',
117 'dump_test': 'dump_test.zip',
118 'apk_test': 'app-release-androidTest.apk',
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100119 'url': 'https://github.com/mkj-gram/applymapping',
120 'revision': 'e3ae14b8c16fa4718e5dea8f7ad00937701b3c48',
121 'folder': 'applymapping',
Morten Krogh-Jespersen90912fd2020-11-05 09:21:16 +0100122 }),
123 App({
Morten Krogh-Jespersena98a4222020-11-05 10:50:10 +0100124 'id': 'com.chanapps.four.activity',
125 'name': 'chanu',
126 'dump_app': 'dump_app.zip',
127 'apk_app': 'app-release.apk',
128 'url': 'https://github.com/mkj-gram/chanu.git',
129 'revision': '6e53458f167b6d78398da60c20fd0da01a232617',
130 'folder': 'chanu',
Morten Krogh-Jespersen43f3cea2020-11-12 17:09:51 +0100131 # The app depends on a class file that has access flags interface but
132 # not abstract
133 'compiler_properties': ['-Dcom.android.tools.r8.allowInvalidCfAccessFlags=true']
Morten Krogh-Jespersena98a4222020-11-05 10:50:10 +0100134 }),
Christoffer Quist Adamsen00840062021-12-16 12:24:13 +0100135 App({
136 'id': 'com.example.myapplication',
137 'name': 'empty-activity',
138 'dump_app': 'dump_app.zip',
139 'apk_app': 'app-release.apk',
140 'url': 'https://github.com/christofferqa/empty_android_activity.git',
141 'revision': '2d297ec3373dadb03cbae916b9feba4792563156',
142 'folder': 'empty-activity',
143 }),
144 App({
145 'id': 'com.example.emptycomposeactivity',
146 'name': 'empty-compose-activity',
147 'dump_app': 'dump_app.zip',
148 'apk_app': 'app-release.apk',
149 'url': 'https://github.com/christofferqa/empty_android_compose_activity.git',
150 'revision': '3c8111b8b7d6e9184049a07e2b96702d7b33d03e',
151 'folder': 'empty-compose-activity',
152 }),
Morten Krogh-Jespersend06ff012020-11-05 10:50:21 +0100153 # TODO(b/172539375): Monkey runner fails on recompilation.
154 App({
155 'id': 'com.google.firebase.example.fireeats',
156 'name': 'FriendlyEats',
157 'dump_app': 'dump_app.zip',
158 'apk_app': 'app-release-unsigned.apk',
159 'url': 'https://github.com/firebase/friendlyeats-android',
160 'revision': '7c6dd016fc31ea5ecb948d5166b8479efc3775cc',
161 'folder': 'friendlyeats',
162 }),
Morten Krogh-Jespersena98a4222020-11-05 10:50:10 +0100163 App({
Morten Krogh-Jespersen162b3452020-11-05 13:07:10 +0100164 'id': 'com.google.samples.apps.sunflower',
165 'name': 'Sunflower',
166 'dump_app': 'dump_app.zip',
167 'apk_app': 'app-debug.apk',
168 # TODO(b/172549283): Compiling tests fails
Morten Krogh-Jespersend0389c02020-11-09 23:42:51 +0100169 'id_test': 'com.google.samples.apps.sunflower.test',
Morten Krogh-Jespersen162b3452020-11-05 13:07:10 +0100170 'dump_test': 'dump_test.zip',
171 'apk_test': 'app-debug-androidTest.apk',
172 'url': 'https://github.com/android/sunflower',
173 'revision': '0c4c88fdad2a74791199dffd1a6559559b1dbd4a',
174 'folder': 'sunflower',
Morten Krogh-Jespersen162b3452020-11-05 13:07:10 +0100175 }),
Morten Krogh-Jespersenac9de3f2020-11-05 17:07:26 +0100176 # TODO(b/172565385): Monkey runner fails on recompilation
177 App({
178 'id': 'com.google.samples.apps.iosched',
179 'name': 'iosched',
180 'dump_app': 'dump_app.zip',
181 'apk_app': 'mobile-release.apk',
182 'url': 'https://github.com/christofferqa/iosched.git',
183 'revision': '581cbbe2253711775dbccb753cdb53e7e506cb02',
184 'folder': 'iosched',
185 }),
Morten Krogh-Jespersen162b3452020-11-05 13:07:10 +0100186 App({
Morten Krogh-Jespersend0389c02020-11-09 23:42:51 +0100187 'id': 'fr.neamar.kiss',
188 'name': 'KISS',
189 'dump_app': 'dump_app.zip',
190 'apk_app': 'app-release.apk',
191 # TODO(b/172569220): Running tests fails due to missing keep rules
192 'id_test': 'fr.neamar.kiss.test',
193 'dump_test': 'dump_test.zip',
194 'apk_test': 'app-release-androidTest.apk',
195 'url': 'https://github.com/Neamar/KISS',
196 'revision': '8ccffaadaf0d0b8fc4418ed2b4281a0935d3d971',
197 'folder': 'kiss',
198 }),
Morten Krogh-Jespersenaaacd722020-11-09 23:43:24 +0100199 # TODO(b/172577344): Monkey runner not working.
200 App({
201 'id': 'io.github.hidroh.materialistic',
202 'name': 'materialistic',
203 'dump_app': 'dump_app.zip',
204 'apk_app': 'app-release.apk',
205 'url': 'https://github.com/christofferqa/materialistic.git',
206 'revision': '2b2b2ee25ce9e672d5aab1dc90a354af1522b1d9',
207 'folder': 'materialistic',
208 }),
Morten Krogh-Jespersend0389c02020-11-09 23:42:51 +0100209 App({
Morten Krogh-Jespersen571cfe72020-11-09 23:44:03 +0100210 'id': 'com.avjindersinghsekhon.minimaltodo',
211 'name': 'MinimalTodo',
212 'dump_app': 'dump_app.zip',
213 'apk_app': 'app-release.apk',
214 'url': 'https://github.com/christofferqa/Minimal-Todo',
215 'revision': '9d8c73746762cd376b718858ec1e8783ca07ba7c',
216 'folder': 'minimal-todo',
217 }),
218 App({
Morten Krogh-Jespersen3e3781f2020-11-09 23:44:33 +0100219 'id': 'net.nurik.roman.muzei',
220 'name': 'muzei',
221 'dump_app': 'dump_app.zip',
222 'apk_app': 'muzei-release.apk',
223 'url': 'https://github.com/romannurik/muzei',
224 'revision': '9eac6e98aebeaf0ae40bdcd85f16dd2886551138',
225 'folder': 'muzei',
226 }),
Morten Krogh-Jespersenf8e77032020-11-09 23:44:58 +0100227 # TODO(b/172806281): Monkey runner does not work.
228 App({
229 'id': 'org.schabi.newpipe',
230 'name': 'NewPipe',
231 'dump_app': 'dump_app.zip',
232 'apk_app': 'app-release-unsigned.apk',
233 'url': 'https://github.com/TeamNewPipe/NewPipe',
234 'revision': 'f4435f90313281beece70c544032f784418d85fa',
235 'folder': 'newpipe',
Morten Krogh-Jespersenf8e77032020-11-09 23:44:58 +0100236 }),
Morten Krogh-Jespersendf700642020-11-09 23:45:25 +0100237 # TODO(b/172806808): Monkey runner does not work.
238 App({
239 'id': 'io.rover.app.debug',
240 'name': 'Rover',
241 'dump_app': 'dump_app.zip',
242 'apk_app': 'example-app-release-unsigned.apk',
243 'url': 'https://github.com/RoverPlatform/rover-android',
244 'revision': '94342117097770ea3ca2c6df6ab496a1a55c3ce7',
245 'folder': 'rover-android',
246 }),
Morten Krogh-Jespersen2f1be952020-11-09 23:45:48 +0100247 # TODO(b/172808159): Monkey runner does not work
248 App({
249 'id': 'com.google.android.apps.santatracker',
250 'name': 'SantaTracker',
251 'dump_app': 'dump_app.zip',
252 'apk_app': 'santa-tracker-release.apk',
253 'url': 'https://github.com/christofferqa/santa-tracker-android',
254 'revision': '8dee74be7d9ee33c69465a07088c53087d24a6dd',
255 'folder': 'santa-tracker',
256 }),
257 App({
Morten Krogh-Jespersen88cc0772020-11-09 23:46:13 +0100258 'id': 'org.thoughtcrime.securesms',
259 'name': 'Signal',
260 'dump_app': 'dump_app.zip',
261 'apk_app': 'Signal-Android-play-prod-universal-release-4.76.2.apk',
262 # TODO(b/172812839): Instrumentation test fails.
263 'id_test': 'org.thoughtcrime.securesms.test',
264 'dump_test': 'dump_test.zip',
265 'apk_test': 'Signal-Android-play-prod-release-androidTest.apk',
266 'url': 'https://github.com/signalapp/Signal-Android',
267 'revision': '91ca19f294362ccee2c2b43c247eba228e2b30a1',
268 'folder': 'signal-android',
269 }),
Morten Krogh-Jespersen733bcff2020-11-09 23:46:43 +0100270 # TODO(b/172815827): Monkey runner does not work
271 App({
272 'id': 'com.simplemobiletools.calendar.pro',
273 'name': 'Simple-Calendar',
274 'dump_app': 'dump_app.zip',
275 'apk_app': 'calendar-release.apk',
276 'url': 'https://github.com/SimpleMobileTools/Simple-Calendar',
277 'revision': '906209874d0a091c7fce5a57972472f272d6b068',
278 'folder': 'simple-calendar',
279 }),
Morten Krogh-Jespersen8a564d32020-11-09 23:47:07 +0100280 # TODO(b/172815534): Monkey runner does not work
281 App({
282 'id': 'com.simplemobiletools.camera.pro',
283 'name': 'Simple-Camera',
284 'dump_app': 'dump_app.zip',
285 'apk_app': 'camera-release.apk',
286 'url': 'https://github.com/SimpleMobileTools/Simple-Camera',
287 'revision': 'ebf9820c51e960912b3238287e30a131244fdee6',
288 'folder': 'simple-camera',
289 }),
Morten Krogh-Jespersen88cc0772020-11-09 23:46:13 +0100290 App({
Morten Krogh-Jespersen805e31a2020-11-09 23:47:32 +0100291 'id': 'com.simplemobiletools.filemanager.pro',
292 'name': 'Simple-File-Manager',
293 'dump_app': 'dump_app.zip',
294 'apk_app': 'file-manager-release.apk',
295 'url': 'https://github.com/SimpleMobileTools/Simple-File-Manager',
296 'revision': '2b7fa68ea251222cc40cf6d62ad1de260a6f54d9',
297 'folder': 'simple-file-manager',
298 }),
299 App({
Morten Krogh-Jespersendc29e992020-11-09 23:48:01 +0100300 'id': 'com.simplemobiletools.gallery.pro',
301 'name': 'Simple-Gallery',
302 'dump_app': 'dump_app.zip',
303 'apk_app': 'gallery-326-foss-release.apk',
304 'url': 'https://github.com/SimpleMobileTools/Simple-Gallery',
305 'revision': '564e56b20d33b28d0018c8087ec705beeb60785e',
306 'folder': 'simple-gallery',
Morten Krogh-Jespersendc29e992020-11-09 23:48:01 +0100307 }),
308 App({
Morten Krogh-Jespersen4a9657a2020-11-09 23:48:21 +0100309 'id': 'com.example.sqldelight.hockey',
310 'name': 'SQLDelight',
311 'dump_app': 'dump_app.zip',
312 'apk_app': 'android-release.apk',
313 'url': 'https://github.com/christofferqa/sqldelight',
314 'revision': '2e67a1126b6df05e4119d1e3a432fde51d76cdc8',
315 'folder': 'sqldelight',
316 }),
Morten Krogh-Jespersenf12a57e2020-11-09 23:48:53 +0100317 # TODO(b/172824096): Monkey runner does not work.
318 App({
319 'id': 'eu.kanade.tachiyomi',
320 'name': 'Tachiyomi',
321 'dump_app': 'dump_app.zip',
322 'apk_app': 'app-dev-release.apk',
323 'url': 'https://github.com/inorichi/tachiyomi',
324 'revision': '8aa6486bf76ab9a61a5494bee284b1a5e9180bf3',
325 'folder': 'tachiyomi',
326 }),
Morten Krogh-Jespersen58974522020-11-10 00:20:46 +0100327 # TODO(b/172862042): Monkey runner does not work.
328 App({
329 'id': 'app.tivi',
330 'name': 'Tivi',
331 'dump_app': 'dump_app.zip',
332 'apk_app': 'app-release.apk',
333 'url': 'https://github.com/chrisbanes/tivi',
Morten Krogh-Jespersen44e2f892020-12-17 09:18:35 +0100334 'revision': '5c6d9ed338885c59b1fc64050d92d056417bb4de',
Morten Krogh-Jespersen58974522020-11-10 00:20:46 +0100335 'folder': 'tivi',
Morten Krogh-Jespersen58974522020-11-10 00:20:46 +0100336 }),
Morten Krogh-Jespersen4a9657a2020-11-09 23:48:21 +0100337 App({
Morten Krogh-Jespersen9033d1a2020-11-09 23:49:23 +0100338 'id': 'com.keylesspalace.tusky',
339 'name': 'Tusky',
340 'dump_app': 'dump_app.zip',
341 'apk_app': 'app-blue-release.apk',
342 'url': 'https://github.com/tuskyapp/Tusky',
343 'revision': '814a9b8f9bacf8d26f712b06a0313a3534a2be95',
344 'folder': 'tusky',
Morten Krogh-Jespersen9033d1a2020-11-09 23:49:23 +0100345 }),
346 App({
Morten Krogh-Jespersen90912fd2020-11-05 09:21:16 +0100347 'id': 'org.wikipedia',
348 'name': 'Wikipedia',
349 'dump_app': 'dump_app.zip',
350 'apk_app': 'app-prod-release.apk',
351 'url': 'https://github.com/wikimedia/apps-android-wikipedia',
352 'revision': '0fa7cad843c66313be8e25790ef084cf1a1fa67e',
353 'folder': 'wikipedia',
354 }),
Christoffer Quist Adamsen53e29472020-11-13 10:53:06 +0100355 # TODO(b/173167253): Check if monkey testing works.
356 App({
357 'id': 'androidx.compose.samples.crane',
358 'name': 'compose-crane',
359 'collections': ['compose-samples'],
360 'dump_app': 'dump_app.zip',
361 'apk_app': 'app-release-unsigned.apk',
362 'url': 'https://github.com/android/compose-samples',
363 'revision': '779cf9e187b8ee2c6b620b2abb4524719b3f10f8',
364 'folder': 'android/compose-samples/crane',
365 }),
366 # TODO(b/173167253): Check if monkey testing works.
367 App({
368 'id': 'com.example.jetcaster',
369 'name': 'compose-jetcaster',
370 'collections': ['compose-samples'],
371 'dump_app': 'dump_app.zip',
372 'apk_app': 'app-release-unsigned.apk',
373 'url': 'https://github.com/android/compose-samples',
374 'revision': '779cf9e187b8ee2c6b620b2abb4524719b3f10f8',
375 'folder': 'android/compose-samples/jetcaster',
Christoffer Quist Adamsen53e29472020-11-13 10:53:06 +0100376 }),
377 # TODO(b/173167253): Check if monkey testing works.
378 App({
379 'id': 'com.example.compose.jetchat',
380 'name': 'compose-jetchat',
381 'collections': ['compose-samples'],
382 'dump_app': 'dump_app.zip',
383 'apk_app': 'app-release-unsigned.apk',
384 'url': 'https://github.com/android/compose-samples',
385 'revision': '779cf9e187b8ee2c6b620b2abb4524719b3f10f8',
386 'folder': 'android/compose-samples/jetchat',
Christoffer Quist Adamsen53e29472020-11-13 10:53:06 +0100387 }),
388 # TODO(b/173167253): Check if monkey testing works.
389 App({
390 'id': 'com.example.jetnews',
391 'name': 'compose-jetnews',
392 'collections': ['compose-samples'],
393 'dump_app': 'dump_app.zip',
394 'apk_app': 'app-release-unsigned.apk',
395 'url': 'https://github.com/android/compose-samples',
396 'revision': '779cf9e187b8ee2c6b620b2abb4524719b3f10f8',
397 'folder': 'android/compose-samples/jetnews',
Christoffer Quist Adamsen53e29472020-11-13 10:53:06 +0100398 }),
399 # TODO(b/173167253): Check if monkey testing works.
400 App({
401 'id': 'com.example.jetsnack',
402 'name': 'compose-jetsnack',
403 'collections': ['compose-samples'],
404 'dump_app': 'dump_app.zip',
405 'apk_app': 'app-release-unsigned.apk',
406 'url': 'https://github.com/android/compose-samples',
407 'revision': '779cf9e187b8ee2c6b620b2abb4524719b3f10f8',
408 'folder': 'android/compose-samples/jetsnack',
409 }),
410 # TODO(b/173167253): Check if monkey testing works.
411 App({
412 'id': 'com.example.compose.jetsurvey',
413 'name': 'compose-jetsurvey',
414 'collections': ['compose-samples'],
415 'dump_app': 'dump_app.zip',
416 'apk_app': 'app-release-unsigned.apk',
417 'url': 'https://github.com/android/compose-samples',
418 'revision': '779cf9e187b8ee2c6b620b2abb4524719b3f10f8',
419 'folder': 'android/compose-samples/jetsurvey',
420 }),
421 # TODO(b/173167253): Check if monkey testing works.
422 App({
423 'id': 'com.example.owl',
424 'name': 'compose-owl',
425 'collections': ['compose-samples'],
426 'dump_app': 'dump_app.zip',
427 'apk_app': 'app-release-unsigned.apk',
428 'url': 'https://github.com/android/compose-samples',
429 'revision': '779cf9e187b8ee2c6b620b2abb4524719b3f10f8',
430 'folder': 'android/compose-samples/owl',
431 }),
432 # TODO(b/173167253): Check if monkey testing works.
433 App({
434 'id': 'com.example.compose.rally',
435 'name': 'compose-rally',
436 'collections': ['compose-samples'],
437 'dump_app': 'dump_app.zip',
438 'apk_app': 'app-release-unsigned.apk',
439 'url': 'https://github.com/android/compose-samples',
440 'revision': '779cf9e187b8ee2c6b620b2abb4524719b3f10f8',
441 'folder': 'android/compose-samples/rally',
442 }),
Morten Krogh-Jespersen86222742021-03-02 11:13:33 +0100443 App({
444 'id': 'youtube_15_33',
445 'name': 'youtube_15_33',
446 'dump_app': 'dump.zip',
447 'apk_app': 'YouTubeRelease_unsigned.apk',
448 'folder': 'youtube_15_33',
449 'internal': True,
450 # TODO(b/181629268): Fix recompilation
451 'skip_recompilation': True,
452 })
Christoffer Quist Adamsen53e29472020-11-13 10:53:06 +0100453]
454
455
456APP_COLLECTIONS = [
457 AppCollection({
458 'name': 'compose-samples',
459 })
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100460]
461
Morten Krogh-Jespersenf8e77032020-11-09 23:44:58 +0100462
Morten Krogh-Jespersendfeb0e32020-11-04 14:55:55 +0100463def remove_print_lines(file):
464 with open(file) as f:
465 lines = f.readlines()
466 with open(file, 'w') as f:
467 for line in lines:
468 if '-printconfiguration' not in line:
469 f.write(line)
470
471
Rico Wind61a034f2021-03-16 09:37:11 +0100472def download_sha(app_sha, internal, quiet=False):
Morten Krogh-Jespersen86222742021-03-02 11:13:33 +0100473 if internal:
474 utils.DownloadFromX20(app_sha)
475 else:
Rico Wind59593922021-03-03 09:12:36 +0100476 utils.DownloadFromGoogleCloudStorage(app_sha, quiet=quiet)
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100477
478
Morten Krogh-Jespersencd55f812020-11-04 09:13:31 +0100479def is_logging_enabled_for(app, options):
480 if options.no_logging:
481 return False
482 if options.app_logging_filter and app.name not in options.app_logging_filter:
483 return False
484 return True
485
486
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100487def is_minified_r8(shrinker):
488 return '-nolib' not in shrinker
489
490
491def is_full_r8(shrinker):
Morten Krogh-Jespersen270d0932020-11-11 11:05:57 +0100492 return '-full' in shrinker
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100493
494
Morten Krogh-Jespersen51db2b02020-11-11 12:49:26 +0100495def version_is_built_jar(version):
Rico Wind1b52acf2021-03-21 12:36:55 +0100496 return version != 'main' and version != 'source'
Morten Krogh-Jespersen51db2b02020-11-11 12:49:26 +0100497
498
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100499def compute_size_of_dex_files_in_package(path):
500 dex_size = 0
501 z = zipfile.ZipFile(path, 'r')
502 for filename in z.namelist():
503 if filename.endswith('.dex'):
504 dex_size += z.getinfo(filename).file_size
505 return dex_size
506
507
508def dump_for_app(app_dir, app):
509 return os.path.join(app_dir, app.dump_app)
510
511
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100512def dump_test_for_app(app_dir, app):
513 return os.path.join(app_dir, app.dump_test)
514
515
Morten Krogh-Jespersen51db2b02020-11-11 12:49:26 +0100516def get_r8_jar(options, temp_dir, shrinker):
517 if (options.version == 'source'):
518 return None
519 return os.path.join(
520 temp_dir, 'r8lib.jar' if is_minified_r8(shrinker) else 'r8.jar')
521
522
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100523def get_results_for_app(app, options, temp_dir):
524 app_folder = app.folder if app.folder else app.name + "_" + app.revision
Rico Windb2ae4c62021-06-01 15:09:56 +0200525 # Golem extraction will extract to the basename under the benchmarks dir.
526 app_location = os.path.basename(app_folder) if options.golem else app_folder
Rico Winde32a9c72021-03-16 09:00:06 +0100527 opensource_basedir = (os.path.join('benchmarks', app.name) if options.golem
528 else utils.OPENSOURCE_DUMPS_DIR)
Rico Windb2ae4c62021-06-01 15:09:56 +0200529 app_dir = (os.path.join(utils.INTERNAL_DUMPS_DIR, app_location) if app.internal
530 else os.path.join(opensource_basedir, app_location))
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100531 if not os.path.exists(app_dir) and not options.golem:
532 # Download the app from google storage.
Rico Wind61a034f2021-03-16 09:37:11 +0100533 download_sha(app_dir + ".tar.gz.sha1", app.internal)
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100534
535 # Ensure that the dumps are in place
536 assert os.path.isfile(dump_for_app(app_dir, app)), "Could not find dump " \
537 "for app " + app.name
538
539 result = {}
540 result['status'] = 'success'
541 result_per_shrinker = build_app_with_shrinkers(
542 app, options, temp_dir, app_dir)
Christoffer Quist Adamsena9ff1cf2021-01-13 08:49:48 +0100543 for shrinker, shrinker_result in result_per_shrinker.items():
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100544 result[shrinker] = shrinker_result
545 return result
546
547
548def build_app_with_shrinkers(app, options, temp_dir, app_dir):
549 result_per_shrinker = {}
550 for shrinker in options.shrinker:
551 results = []
552 build_app_and_run_with_shrinker(
553 app, options, temp_dir, app_dir, shrinker, results)
554 result_per_shrinker[shrinker] = results
555 if len(options.apps) > 1:
556 print('')
557 log_results_for_app(app, result_per_shrinker, options)
558 print('')
559
560 return result_per_shrinker
561
562
563def is_last_build(index, compilation_steps):
564 return index == compilation_steps - 1
565
566
567def build_app_and_run_with_shrinker(app, options, temp_dir, app_dir, shrinker,
568 results):
569 print('[{}] Building {} with {}'.format(
570 datetime.now().strftime("%H:%M:%S"),
571 app.name,
572 shrinker))
573 print('To compile locally: '
Morten Krogh-Jespersenf098b422020-11-11 13:53:52 +0100574 'tools/run_on_app_dump.py --shrinker {} --r8-compilation-steps {} '
Christoffer Quist Adamsenf86fc0b2021-12-10 12:39:36 +0100575 '--app {} --minify {} --optimize {} --shrink {}'.format(
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100576 shrinker,
577 options.r8_compilation_steps,
Christoffer Quist Adamsenf86fc0b2021-12-10 12:39:36 +0100578 app.name,
579 options.minify,
580 options.optimize,
581 options.shrink))
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100582 print('HINT: use --shrinker r8-nolib --no-build if you have a local R8.jar')
583 recomp_jar = None
584 status = 'success'
Morten Krogh-Jespersenee89b3a2020-11-13 11:59:43 +0100585 if options.r8_compilation_steps < 1:
586 return
587 compilation_steps = 1 if app.skip_recompilation else options.r8_compilation_steps
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100588 for compilation_step in range(0, compilation_steps):
589 if status != 'success':
590 break
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100591 print('Compiling {} of {}'.format(compilation_step + 1, compilation_steps))
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100592 result = {}
593 try:
594 start = time.time()
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100595 (app_jar, mapping, new_recomp_jar) = \
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100596 build_app_with_shrinker(
597 app, options, temp_dir, app_dir, shrinker, compilation_step,
598 compilation_steps, recomp_jar)
599 end = time.time()
600 dex_size = compute_size_of_dex_files_in_package(app_jar)
601 result['build_status'] = 'success'
602 result['recompilation_status'] = 'success'
603 result['output_jar'] = app_jar
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100604 result['output_mapping'] = mapping
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100605 result['dex_size'] = dex_size
606 result['duration'] = int((end - start) * 1000) # Wall time
607 if (new_recomp_jar is None
608 and not is_last_build(compilation_step, compilation_steps)):
609 result['recompilation_status'] = 'failed'
610 warn('Failed to build {} with {}'.format(app.name, shrinker))
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100611 recomp_jar = new_recomp_jar
612 except Exception as e:
613 warn('Failed to build {} with {}'.format(app.name, shrinker))
614 if e:
615 print('Error: ' + str(e))
616 result['build_status'] = 'failed'
617 status = 'failed'
618
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100619 original_app_apk = os.path.join(app_dir, app.apk_app)
620 app_apk_destination = os.path.join(
621 temp_dir,"{}_{}.apk".format(app.id, compilation_step))
622
Morten Krogh-Jespersencd55f812020-11-04 09:13:31 +0100623 if result.get('build_status') == 'success' and options.monkey:
624 # Make a copy of the given APK, move the newly generated dex files into the
625 # copied APK, and then sign the APK.
Morten Krogh-Jespersencd55f812020-11-04 09:13:31 +0100626 apk_masseur.masseur(
627 original_app_apk, dex=app_jar, resources='META-INF/services/*',
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100628 out=app_apk_destination,
Morten Krogh-Jespersencd55f812020-11-04 09:13:31 +0100629 quiet=options.quiet, logging=is_logging_enabled_for(app, options),
630 keystore=options.keystore)
631
632 result['monkey_status'] = 'success' if adb.run_monkey(
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100633 app.id, options.emulator_id, app_apk_destination, options.monkey_events,
Morten Krogh-Jespersencd55f812020-11-04 09:13:31 +0100634 options.quiet, is_logging_enabled_for(app, options)) else 'failed'
635
Morten Krogh-Jespersen571cfe72020-11-09 23:44:03 +0100636 if (result.get('build_status') == 'success'
637 and options.run_tests and app.dump_test):
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100638 if not os.path.isfile(app_apk_destination):
639 apk_masseur.masseur(
640 original_app_apk, dex=app_jar, resources='META-INF/services/*',
641 out=app_apk_destination,
642 quiet=options.quiet, logging=is_logging_enabled_for(app, options),
643 keystore=options.keystore)
644
645 # Compile the tests with the mapping file.
646 test_jar = build_test_with_shrinker(
647 app, options, temp_dir, app_dir,shrinker, compilation_step,
648 result['output_mapping'])
Morten Krogh-Jespersen162b3452020-11-05 13:07:10 +0100649 if not test_jar:
650 result['instrumentation_test_status'] = 'compilation_failed'
651 else:
652 original_test_apk = os.path.join(app_dir, app.apk_test)
653 test_apk_destination = os.path.join(
654 temp_dir,"{}_{}.test.apk".format(app.id_test, compilation_step))
655 apk_masseur.masseur(
656 original_test_apk, dex=test_jar, resources='META-INF/services/*',
657 out=test_apk_destination,
658 quiet=options.quiet, logging=is_logging_enabled_for(app, options),
659 keystore=options.keystore)
660 result['instrumentation_test_status'] = 'success' if adb.run_instrumented(
661 app.id, app.id_test, options.emulator_id, app_apk_destination,
662 test_apk_destination, options.quiet,
663 is_logging_enabled_for(app, options)) else 'failed'
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100664
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100665 results.append(result)
Morten Krogh-Jespersencd55f812020-11-04 09:13:31 +0100666 if result.get('recompilation_status') != 'success':
667 break
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100668
Rico Wind33936d12021-04-07 08:04:14 +0200669def get_jdk_home(options, app):
Rico Winda74174d2021-04-07 09:10:35 +0200670 if options.golem:
Rico Wind33936d12021-04-07 08:04:14 +0200671 return os.path.join('benchmarks', app.name, 'linux')
672 return None
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100673
674def build_app_with_shrinker(app, options, temp_dir, app_dir, shrinker,
675 compilation_step_index, compilation_steps,
676 prev_recomp_jar):
Christoffer Quist Adamsenf86fc0b2021-12-10 12:39:36 +0100677 def config_file_consumer(file):
678 compiledump.clean_config(file, options)
679 remove_print_lines(file)
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100680 args = AttrDict({
681 'dump': dump_for_app(app_dir, app),
Morten Krogh-Jespersen51db2b02020-11-11 12:49:26 +0100682 'r8_jar': get_r8_jar(options, temp_dir, shrinker),
Christoffer Quist Adamsen2f48f3f2021-10-14 17:25:03 +0200683 'r8_flags': options.r8_flags,
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100684 'ea': False if options.disable_assertions else True,
Morten Krogh-Jespersen51db2b02020-11-11 12:49:26 +0100685 'version': options.version,
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100686 'compiler': 'r8full' if is_full_r8(shrinker) else 'r8',
687 'debug_agent': options.debug_agent,
688 'program_jar': prev_recomp_jar,
Morten Krogh-Jespersendfeb0e32020-11-04 14:55:55 +0100689 'nolib': not is_minified_r8(shrinker),
Christoffer Quist Adamsenf86fc0b2021-12-10 12:39:36 +0100690 'config_file_consumer': config_file_consumer,
Morten Krogh-Jespersen43f3cea2020-11-12 17:09:51 +0100691 'properties': app.compiler_properties,
Morten Krogh-Jespersend86a81b2020-11-13 12:33:26 +0100692 'disable_desugared_lib': False,
Morten Krogh-Jespersen6c702402021-06-21 12:29:48 +0200693 'print_times': options.print_times,
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100694 })
695
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100696 app_jar = os.path.join(
697 temp_dir, '{}_{}_{}_dex_out.jar'.format(
698 app.name, shrinker, compilation_step_index))
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100699 app_mapping = os.path.join(
700 temp_dir, '{}_{}_{}_dex_out.jar.map'.format(
701 app.name, shrinker, compilation_step_index))
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100702 recomp_jar = None
Rico Wind33936d12021-04-07 08:04:14 +0200703 jdkhome = get_jdk_home(options, app)
Morten Krogh-Jespersenc9d23462020-11-12 15:36:57 +0100704 with utils.TempDir() as compile_temp_dir:
Rico Wind33936d12021-04-07 08:04:14 +0200705 compile_result = compiledump.run1(compile_temp_dir, args, [], jdkhome)
Morten Krogh-Jespersenc9d23462020-11-12 15:36:57 +0100706 out_jar = os.path.join(compile_temp_dir, "out.jar")
707 out_mapping = os.path.join(compile_temp_dir, "out.jar.map")
708
709 if compile_result != 0 or not os.path.isfile(out_jar):
710 assert False, 'Compilation of {} failed'.format(dump_for_app(app_dir, app))
711 shutil.move(out_jar, app_jar)
712 shutil.move(out_mapping, app_mapping)
713
714 if compilation_step_index < compilation_steps - 1:
715 args['classfile'] = True
716 args['min_api'] = "10000"
Morten Krogh-Jespersend86a81b2020-11-13 12:33:26 +0100717 args['disable_desugared_lib'] = True
Rico Wind33936d12021-04-07 08:04:14 +0200718 compile_result = compiledump.run1(compile_temp_dir, args, [], jdkhome)
Morten Krogh-Jespersenc9d23462020-11-12 15:36:57 +0100719 if compile_result == 0:
720 recomp_jar = os.path.join(
721 temp_dir, '{}_{}_{}_cf_out.jar'.format(
722 app.name, shrinker, compilation_step_index))
723 shutil.move(out_jar, recomp_jar)
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100724
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100725 return (app_jar, app_mapping, recomp_jar)
726
Morten Krogh-Jespersen162b3452020-11-05 13:07:10 +0100727
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100728def build_test_with_shrinker(app, options, temp_dir, app_dir, shrinker,
729 compilation_step_index, mapping):
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100730
731 def rewrite_file(file):
Christoffer Quist Adamsenf86fc0b2021-12-10 12:39:36 +0100732 compiledump.clean_config(file, options)
Morten Krogh-Jespersendfeb0e32020-11-04 14:55:55 +0100733 remove_print_lines(file)
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100734 with open(file) as f:
735 lines = f.readlines()
736 with open(file, 'w') as f:
737 for line in lines:
738 if '-applymapping' not in line:
739 f.write(line + '\n')
740 f.write("-applymapping " + mapping + '\n')
741
742 args = AttrDict({
743 'dump': dump_test_for_app(app_dir, app),
Morten Krogh-Jespersen51db2b02020-11-11 12:49:26 +0100744 'r8_jar': get_r8_jar(options, temp_dir, shrinker),
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100745 'ea': False if options.disable_assertions else True,
Morten Krogh-Jespersen51db2b02020-11-11 12:49:26 +0100746 'version': options.version,
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100747 'compiler': 'r8full' if is_full_r8(shrinker) else 'r8',
748 'debug_agent': options.debug_agent,
749 'nolib': not is_minified_r8(shrinker),
750 # The config file will have an -applymapping reference to an old map.
751 # Update it to point to mapping file build in the compilation of the app.
Christoffer Quist Adamsenf86fc0b2021-12-10 12:39:36 +0100752 'config_file_consumer': rewrite_file,
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100753 })
754
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100755 test_jar = os.path.join(
756 temp_dir, '{}_{}_{}_test_out.jar'.format(
757 app.name, shrinker, compilation_step_index))
758
Morten Krogh-Jespersenc9d23462020-11-12 15:36:57 +0100759 with utils.TempDir() as compile_temp_dir:
Rico Wind33936d12021-04-07 08:04:14 +0200760 jdkhome = get_jdk_home(options, app)
761 compile_result = compiledump.run1(compile_temp_dir, args, [], jdkhome)
Morten Krogh-Jespersenc9d23462020-11-12 15:36:57 +0100762 out_jar = os.path.join(compile_temp_dir, "out.jar")
763 if compile_result != 0 or not os.path.isfile(out_jar):
764 return None
765 shutil.move(out_jar, test_jar)
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100766
767 return test_jar
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100768
769
770def log_results_for_apps(result_per_shrinker_per_app, options):
771 print('')
772 app_errors = 0
773 for (app, result_per_shrinker) in result_per_shrinker_per_app:
774 app_errors += (1 if log_results_for_app(app, result_per_shrinker, options)
775 else 0)
776 return app_errors
777
778
779def log_results_for_app(app, result_per_shrinker, options):
780 if options.print_dexsegments:
781 log_segments_for_app(app, result_per_shrinker, options)
782 return False
783 else:
784 return log_comparison_results_for_app(app, result_per_shrinker, options)
785
786
787def log_segments_for_app(app, result_per_shrinker, options):
788 for shrinker in SHRINKERS:
789 if shrinker not in result_per_shrinker:
790 continue
791 for result in result_per_shrinker.get(shrinker):
792 benchmark_name = '{}-{}'.format(options.print_dexsegments, app.name)
793 utils.print_dexsegments(benchmark_name, [result.get('output_jar')])
794 duration = result.get('duration')
795 print('%s-Total(RunTimeRaw): %s ms' % (benchmark_name, duration))
796 print('%s-Total(CodeSize): %s' % (benchmark_name, result.get('dex_size')))
797
798
799def percentage_diff_as_string(before, after):
800 if after < before:
801 return '-' + str(round((1.0 - after / before) * 100)) + '%'
802 else:
803 return '+' + str(round((after - before) / before * 100)) + '%'
804
805
806def log_comparison_results_for_app(app, result_per_shrinker, options):
807 print(app.name + ':')
808 app_error = False
809 if result_per_shrinker.get('status', 'success') != 'success':
810 error_message = result_per_shrinker.get('error_message')
811 print(' skipped ({})'.format(error_message))
812 return
813
814 proguard_result = result_per_shrinker.get('pg', {})
815 proguard_dex_size = float(proguard_result.get('dex_size', -1))
816
817 for shrinker in SHRINKERS:
818 if shrinker not in result_per_shrinker:
819 continue
820 compilation_index = 1
821 for result in result_per_shrinker.get(shrinker):
822 build_status = result.get('build_status')
823 if build_status != 'success' and build_status is not None:
824 app_error = True
825 warn(' {}-#{}: {}'.format(shrinker, compilation_index, build_status))
826 continue
827
Rico Wind59593922021-03-03 09:12:36 +0100828 if options.golem:
829 print('%s(RunTimeRaw): %s ms' % (app.name, result.get('duration')))
830 print('%s(CodeSize): %s' % (app.name, result.get('dex_size')))
831 continue
832
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100833 print(' {}-#{}:'.format(shrinker, compilation_index))
834 dex_size = result.get('dex_size')
835 msg = ' dex size: {}'.format(dex_size)
Rudi Horn82e7d222020-11-17 11:22:39 +0000836 if options.print_runtimeraw:
837 print(' run time raw: {} ms'.format(result.get('duration')))
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100838 if dex_size != proguard_dex_size and proguard_dex_size >= 0:
839 msg = '{} ({}, {})'.format(
840 msg, dex_size - proguard_dex_size,
841 percentage_diff_as_string(proguard_dex_size, dex_size))
842 success(msg) if dex_size < proguard_dex_size else warn(msg)
843 else:
844 print(msg)
845
Morten Krogh-Jespersencd55f812020-11-04 09:13:31 +0100846 if options.monkey:
847 monkey_status = result.get('monkey_status')
848 if monkey_status != 'success':
849 app_error = True
850 warn(' monkey: {}'.format(monkey_status))
851 else:
852 success(' monkey: {}'.format(monkey_status))
853
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100854 if options.run_tests and 'instrumentation_test_status' in result:
855 test_status = result.get('instrumentation_test_status')
856 if test_status != 'success':
857 warn(' instrumentation_tests: {}'.format(test_status))
858 else:
859 success(' instrumentation_tests: {}'.format(test_status))
860
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100861 recompilation_status = result.get('recompilation_status', '')
862 if recompilation_status == 'failed':
863 app_error = True
864 warn(' recompilation {}-#{}: failed'.format(shrinker,
865 compilation_index))
866 continue
867
868 compilation_index += 1
869
870 return app_error
871
872
873def parse_options(argv):
Morten Krogh-Jespersen6c702402021-06-21 12:29:48 +0200874 result = argparse.ArgumentParser(description = 'Run/compile dump artifacts.')
875 result.add_argument('--app',
876 help='What app to run on',
877 choices=[app.name for app in APPS],
878 action='append')
879 result.add_argument('--app-collection', '--app_collection',
880 help='What app collection to run',
881 choices=[collection.name for collection in
882 APP_COLLECTIONS],
883 action='append')
884 result.add_argument('--app-logging-filter', '--app_logging_filter',
885 help='The apps for which to turn on logging',
886 action='append')
887 result.add_argument('--bot',
888 help='Running on bot, use third_party dependency.',
889 default=False,
890 action='store_true')
891 result.add_argument('--generate-golem-config', '--generate_golem_config',
892 help='Generate a new config for golem.',
893 default=False,
894 action='store_true')
895 result.add_argument('--debug-agent',
896 help='Enable Java debug agent and suspend compilation '
897 '(default disabled)',
898 default=False,
899 action='store_true')
900 result.add_argument('--disable-assertions', '--disable_assertions',
901 help='Disable assertions when compiling',
902 default=False,
903 action='store_true')
904 result.add_argument('--emulator-id', '--emulator_id',
905 help='Id of the emulator to use',
906 default='emulator-5554')
907 result.add_argument('--golem',
908 help='Running on golem, do not download',
909 default=False,
910 action='store_true')
911 result.add_argument('--hash',
912 help='The commit of R8 to use')
913 result.add_argument('--internal',
914 help='Run internal apps if set, otherwise run opensource',
915 default=False,
916 action='store_true')
917 result.add_argument('--keystore',
918 help='Path to app.keystore',
919 default=os.path.join(utils.TOOLS_DIR, 'debug.keystore'))
920 result.add_argument('--keystore-password', '--keystore_password',
921 help='Password for app.keystore',
922 default='android')
Christoffer Quist Adamsenf86fc0b2021-12-10 12:39:36 +0100923 result.add_argument('--minify',
924 help='Force enable/disable minification' +
925 ' (defaults to app proguard config)',
926 choices=['default', 'force-enable', 'force-disable'],
927 default='default')
Morten Krogh-Jespersen6c702402021-06-21 12:29:48 +0200928 result.add_argument('--monkey',
929 help='Whether to install and run app(s) with monkey',
930 default=False,
931 action='store_true')
932 result.add_argument('--monkey-events', '--monkey_events',
933 help='Number of events that the monkey should trigger',
934 default=250,
935 type=int)
936 result.add_argument('--no-build', '--no_build',
937 help='Run without building first (only when using ToT)',
938 default=False,
939 action='store_true')
940 result.add_argument('--no-logging', '--no_logging',
941 help='Disable logging except for errors',
942 default=False,
943 action='store_true')
Christoffer Quist Adamsenf86fc0b2021-12-10 12:39:36 +0100944 result.add_argument('--optimize',
945 help='Force enable/disable optimizations' +
946 ' (defaults to app proguard config)',
947 choices=['default', 'force-enable', 'force-disable'],
948 default='default')
Morten Krogh-Jespersen6c702402021-06-21 12:29:48 +0200949 result.add_argument('--print-times',
950 help='Print timing information from r8',
951 default=False,
952 action='store_true')
953 result.add_argument('--print-dexsegments',
954 metavar='BENCHMARKNAME',
955 help='Print the sizes of individual dex segments as ' +
956 '\'<BENCHMARKNAME>-<APP>-<segment>(CodeSize): '
957 '<bytes>\'')
958 result.add_argument('--print-runtimeraw',
959 metavar='BENCHMARKNAME',
960 help='Print the line \'<BENCHMARKNAME>(RunTimeRaw):' +
961 ' <elapsed> ms\' at the end where <elapsed> is' +
962 ' the elapsed time in milliseconds.')
963 result.add_argument('--quiet',
964 help='Disable verbose logging',
965 default=False,
966 action='store_true')
967 result.add_argument('--r8-compilation-steps', '--r8_compilation_steps',
968 help='Number of times R8 should be run on each app',
969 default=2,
970 type=int)
Christoffer Quist Adamsen2f48f3f2021-10-14 17:25:03 +0200971 result.add_argument('--r8-flags', '--r8_flags',
972 help='Additional option(s) for the compiler.')
Morten Krogh-Jespersen6c702402021-06-21 12:29:48 +0200973 result.add_argument('--run-tests', '--run_tests',
974 help='Whether to run instrumentation tests',
975 default=False,
976 action='store_true')
Christoffer Quist Adamsenf86fc0b2021-12-10 12:39:36 +0100977 result.add_argument('--shrink',
978 help='Force enable/disable shrinking' +
979 ' (defaults to app proguard config)',
980 choices=['default', 'force-enable', 'force-disable'],
981 default='default')
Morten Krogh-Jespersen6c702402021-06-21 12:29:48 +0200982 result.add_argument('--sign-apks', '--sign_apks',
983 help='Whether the APKs should be signed',
984 default=False,
985 action='store_true')
986 result.add_argument('--shrinker',
987 help='The shrinkers to use (by default, all are run)',
988 action='append')
Ian Zernyf5a57ec2021-11-25 14:02:07 +0100989 result.add_argument('--temp',
990 help='A directory to use for temporaries and outputs.',
991 default=None)
Morten Krogh-Jespersen6c702402021-06-21 12:29:48 +0200992 result.add_argument('--version',
993 default='main',
994 help='The version of R8 to use (e.g., 1.4.51)')
995 (options, args) = result.parse_known_args(argv)
Christoffer Quist Adamsen53e29472020-11-13 10:53:06 +0100996
997 if options.app or options.app_collection:
998 if not options.app:
999 options.app = []
1000 if not options.app_collection:
1001 options.app_collection = []
1002 options.apps = [
1003 app
1004 for app in APPS
1005 if app.name in options.app
1006 or any(collection in options.app_collection
1007 for collection in app.collections)]
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +01001008 del options.app
Christoffer Quist Adamsen53e29472020-11-13 10:53:06 +01001009 del options.app_collection
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +01001010 else:
Morten Krogh-Jespersen86222742021-03-02 11:13:33 +01001011 options.apps = [app for app in APPS if app.internal == options.internal]
Christoffer Quist Adamsen53e29472020-11-13 10:53:06 +01001012
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +01001013 if options.app_logging_filter:
1014 for app_name in options.app_logging_filter:
1015 assert any(app.name == app_name for app in options.apps)
1016 if options.shrinker:
1017 for shrinker in options.shrinker:
Christoffer Quist Adamsena9ff1cf2021-01-13 08:49:48 +01001018 assert shrinker in SHRINKERS, (
1019 'Shrinker must be one of %s' % ', '.join(SHRINKERS))
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +01001020 else:
1021 options.shrinker = [shrinker for shrinker in SHRINKERS]
1022
Morten Krogh-Jespersen51db2b02020-11-11 12:49:26 +01001023 if options.hash or version_is_built_jar(options.version):
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +01001024 # No need to build R8 if a specific version should be used.
1025 options.no_build = True
1026 if 'r8-nolib' in options.shrinker:
1027 warn('Skipping shrinker r8-nolib because a specific version '
1028 + 'of r8 was specified')
1029 options.shrinker.remove('r8-nolib')
1030 if 'r8-nolib-full' in options.shrinker:
1031 warn('Skipping shrinker r8-nolib-full because a specific version '
1032 + 'of r8 was specified')
1033 options.shrinker.remove('r8-nolib-full')
1034 return (options, args)
1035
1036
Rico Wind59593922021-03-03 09:12:36 +01001037def print_indented(s, indent):
1038 print(' ' * indent + s)
1039
1040
1041def get_sha256(gz_file):
1042 with open(gz_file, 'rb') as f:
1043 bytes = f.read() # read entire file as bytes
1044 return hashlib.sha256(bytes).hexdigest();
1045
1046
1047def get_sha_from_file(sha_file):
1048 with open(sha_file, 'r') as f:
1049 return f.readlines()[0]
1050
1051
1052def print_golem_config(options):
1053 print('// AUTOGENERATED FILE from tools/run_on_app_dump.py in R8 repo')
Rico Windca91a952021-03-03 09:34:54 +01001054 print('part of r8_config;')
Rico Wind71ea2862021-03-03 10:17:59 +01001055 print('')
Rico Wind59593922021-03-03 09:12:36 +01001056 print('final Suite dumpsSuite = new Suite("OpenSourceAppDumps");')
1057 print('')
1058 print('createOpenSourceAppBenchmarks() {')
Rico Wind1abf5482021-03-03 13:55:43 +01001059 print_indented('final cpus = ["Lenovo M90"];', 2)
Christoffer Quist Adamsen8e9f7982021-12-10 13:10:05 +01001060 print_indented('final targetsCompat = ["R8"];', 2)
1061 print_indented('final targetsFull = ["R8-full-minify-optimize-shrink"];', 2)
Rico Wind61a034f2021-03-16 09:37:11 +01001062 # Avoid calculating this for every app
1063 jdk_gz = jdk.GetJdkHome() + '.tar.gz'
Ian Zerny161ff742022-01-20 12:39:40 +01001064 add_golem_resource(2, jdk_gz, 'openjdk')
Rico Wind59593922021-03-03 09:12:36 +01001065 for app in options.apps:
1066 if app.folder and not app.internal:
1067 indentation = 2;
1068 print_indented('{', indentation)
1069 indentation = 4
Rico Wind641e79a2021-03-04 09:12:07 +01001070 print_indented('final name = "%s";' % app.name, indentation)
1071 print_indented('final benchmark =', indentation)
Rico Wind59593922021-03-03 09:12:36 +01001072 print_indented(
Ian Zernyc013b702022-03-08 16:26:27 +01001073 'StandardBenchmark(name, [Metric.RunTimeRaw, Metric.CodeSize]);',
Rico Wind59593922021-03-03 09:12:36 +01001074 indentation + 4)
Rico Wind59593922021-03-03 09:12:36 +01001075 app_gz = os.path.join(utils.OPENSOURCE_DUMPS_DIR, app.folder + '.tar.gz')
Rico Wind5102b912021-06-02 07:50:49 +02001076 name = 'appResource'
1077 add_golem_resource(indentation, app_gz, name)
Christoffer Quist Adamsen8e9f7982021-12-10 13:10:05 +01001078 print_golem_config_target('Compat', 'r8', app, indentation)
1079 print_golem_config_target(
1080 'Full',
1081 'r8-full',
1082 app,
1083 indentation,
1084 minify='force-enable',
1085 optimize='force-enable',
1086 shrink='force-enable')
Rico Wind59593922021-03-03 09:12:36 +01001087 print_indented('dumpsSuite.addBenchmark(name);', indentation)
1088 indentation = 2
1089 print_indented('}', indentation)
1090 print('}')
1091
Christoffer Quist Adamsen8e9f7982021-12-10 13:10:05 +01001092def print_golem_config_target(
1093 target, shrinker, app, indentation,
1094 minify='default', optimize='default', shrink='default'):
1095 options="options" + target
1096 print_indented(
1097 'final %s = benchmark.addTargets(noImplementation, targets%s);'
1098 % (options, target),
1099 indentation)
1100 print_indented('%s.cpus = cpus;' % options, indentation)
1101 print_indented('%s.isScript = true;' % options, indentation)
1102 print_indented('%s.fromRevision = 9700;' % options, indentation);
1103 print_indented('%s.mainFile = "tools/run_on_app_dump.py "' % options,
1104 indentation)
1105 print_indented('"--golem --quiet --shrinker %s --app %s "'
1106 % (shrinker, app.name),
1107 indentation + 4)
1108 print_indented('"--minify %s --optimize %s --shrink %s";'
1109 % (minify, optimize, shrink),
1110 indentation + 4)
1111 print_indented('%s.resources.add(appResource);' % options, indentation)
1112 print_indented('%s.resources.add(openjdk);' % options, indentation)
1113
Rico Wind61a034f2021-03-16 09:37:11 +01001114def add_golem_resource(indentation, gz, name, sha256=None):
1115 sha = gz + '.sha1'
1116 if not sha256:
1117 # Golem uses a sha256 of the file in the cache, and you need to specify that.
1118 download_sha(sha, False, quiet=True)
1119 sha256 = get_sha256(gz)
1120 sha = get_sha_from_file(sha)
1121 print_indented('final %s = BenchmarkResource("",' % name, indentation)
Ian Zernyc013b702022-03-08 16:26:27 +01001122 print_indented('type: BenchmarkResourceType.storage,', indentation + 4)
Rico Wind61a034f2021-03-16 09:37:11 +01001123 print_indented('uri: "gs://r8-deps/%s",' % sha, indentation + 4)
Rico Wind5102b912021-06-02 07:50:49 +02001124 # Make dart formatter happy.
1125 if indentation > 2:
1126 print_indented('hash:', indentation + 4)
1127 print_indented('"%s",' % sha256, indentation + 8)
1128 else:
1129 print_indented('hash: "%s",' % sha256, indentation + 4)
Rico Wind61a034f2021-03-16 09:37:11 +01001130 print_indented('extract: "gz");', indentation + 4);
Rico Wind61a034f2021-03-16 09:37:11 +01001131
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +01001132def main(argv):
1133 (options, args) = parse_options(argv)
1134
1135 if options.bot:
1136 options.no_logging = True
1137 options.shrinker = ['r8', 'r8-full']
1138 print(options.shrinker)
1139
1140 if options.golem:
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +01001141 options.disable_assertions = True
1142 options.no_build = True
1143 options.r8_compilation_steps = 1
1144 options.quiet = True
1145 options.no_logging = True
1146
Rico Wind59593922021-03-03 09:12:36 +01001147 if options.generate_golem_config:
1148 print_golem_config(options)
1149 return 0
1150
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +01001151 with utils.TempDir() as temp_dir:
Ian Zernyf5a57ec2021-11-25 14:02:07 +01001152 if options.temp:
1153 temp_dir = options.temp
Ian Zerny7b500ba2022-02-03 16:41:17 +01001154 os.makedirs(temp_dir, exist_ok=True)
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +01001155 if options.hash:
1156 # Download r8-<hash>.jar from
1157 # https://storage.googleapis.com/r8-releases/raw/.
1158 target = 'r8-{}.jar'.format(options.hash)
1159 update_prebuilds_in_android.download_hash(
1160 temp_dir, 'com/android/tools/r8/' + options.hash, target)
1161 as_utils.MoveFile(
1162 os.path.join(temp_dir, target), os.path.join(temp_dir, 'r8lib.jar'),
1163 quiet=options.quiet)
Morten Krogh-Jespersen51db2b02020-11-11 12:49:26 +01001164 elif version_is_built_jar(options.version):
1165 # Download r8-<version>.jar from
1166 # https://storage.googleapis.com/r8-releases/raw/.
1167 target = 'r8-{}.jar'.format(options.version)
1168 update_prebuilds_in_android.download_version(
1169 temp_dir, 'com/android/tools/r8/' + options.version, target)
1170 as_utils.MoveFile(
1171 os.path.join(temp_dir, target), os.path.join(temp_dir, 'r8lib.jar'),
1172 quiet=options.quiet)
Rico Wind1b52acf2021-03-21 12:36:55 +01001173 elif options.version == 'main':
Ian Zerny161ff742022-01-20 12:39:40 +01001174 if not options.no_build:
Rico Windfc5ad672021-11-15 13:14:36 +01001175 gradle.RunGradle(['R8Retrace', 'r8', '-Pno_internal'])
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +01001176 build_r8lib = False
1177 for shrinker in options.shrinker:
1178 if is_minified_r8(shrinker):
1179 build_r8lib = True
1180 if build_r8lib:
1181 gradle.RunGradle(['r8lib', '-Pno_internal'])
1182 # Make a copy of r8.jar and r8lib.jar such that they stay the same for
1183 # the entire execution of this script.
1184 if 'r8-nolib' in options.shrinker or 'r8-nolib-full' in options.shrinker:
1185 assert os.path.isfile(utils.R8_JAR), 'Cannot build without r8.jar'
1186 shutil.copyfile(utils.R8_JAR, os.path.join(temp_dir, 'r8.jar'))
1187 if 'r8' in options.shrinker or 'r8-full' in options.shrinker:
1188 assert os.path.isfile(utils.R8LIB_JAR), 'Cannot build without r8lib.jar'
1189 shutil.copyfile(utils.R8LIB_JAR, os.path.join(temp_dir, 'r8lib.jar'))
1190
1191 result_per_shrinker_per_app = []
1192 for app in options.apps:
1193 if app.skip:
1194 continue
1195 result_per_shrinker_per_app.append(
1196 (app, get_results_for_app(app, options, temp_dir)))
Morten Krogh-Jespersenf098b422020-11-11 13:53:52 +01001197 errors = log_results_for_apps(result_per_shrinker_per_app, options)
1198 if errors > 0:
1199 dest = 'gs://r8-test-results/r8-libs/' + str(int(time.time()))
1200 utils.upload_file_to_cloud_storage(os.path.join(temp_dir, 'r8lib.jar'), dest)
1201 print('R8lib saved to %s' % dest)
1202 return errors
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +01001203
1204
1205def success(message):
1206 CGREEN = '\033[32m'
1207 CEND = '\033[0m'
1208 print(CGREEN + message + CEND)
1209
1210
1211def warn(message):
1212 CRED = '\033[91m'
1213 CEND = '\033[0m'
1214 print(CRED + message + CEND)
1215
1216
1217if __name__ == '__main__':
1218 sys.exit(main(sys.argv[1:]))