Morten Krogh-Jespersen | 45abed9 | 2021-01-18 22:07:41 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 2 | # Copyright (c) 2017, 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. |
| 5 | |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 6 | from __future__ import print_function |
Tamas Kenez | 02bff03 | 2017-07-18 12:13:58 +0200 | [diff] [blame] | 7 | from glob import glob |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 8 | import copy |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 9 | import optparse |
| 10 | import os |
Søren Gjesse | 80e0048 | 2021-04-06 13:17:35 +0200 | [diff] [blame] | 11 | import shutil |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 12 | import sys |
Tamas Kenez | f2ee2a3 | 2017-06-21 10:30:20 +0200 | [diff] [blame] | 13 | import time |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 14 | |
Christoffer Quist Adamsen | 870fa46 | 2020-12-15 10:50:54 +0100 | [diff] [blame] | 15 | import archive |
Morten Krogh-Jespersen | 89e1901 | 2021-10-05 09:55:01 +0200 | [diff] [blame] | 16 | import gradle |
Søren Gjesse | 5ecb04a | 2017-06-13 09:44:32 +0200 | [diff] [blame] | 17 | import gmail_data |
Christoffer Quist Adamsen | a2a5877 | 2018-10-03 09:47:46 +0200 | [diff] [blame] | 18 | import nest_data |
Søren Gjesse | 889e09d | 2019-11-07 16:33:51 +0100 | [diff] [blame] | 19 | from sanitize_libraries import SanitizeLibraries, SanitizeLibrariesInPgconf |
Christoffer Quist Adamsen | 65ef298 | 2023-08-24 08:45:39 +0200 | [diff] [blame] | 20 | import thread_utils |
| 21 | from thread_utils import print_thread |
Mathias Rav | dd6a6de | 2018-05-18 10:18:33 +0200 | [diff] [blame] | 22 | import toolhelper |
Christoffer Quist Adamsen | 870fa46 | 2020-12-15 10:50:54 +0100 | [diff] [blame] | 23 | import update_prebuilds_in_android |
Ian Zerny | 877c186 | 2017-07-06 11:12:26 +0200 | [diff] [blame] | 24 | import utils |
| 25 | import youtube_data |
Rico Wind | 86bfc83 | 2018-09-18 07:48:21 +0200 | [diff] [blame] | 26 | import chrome_data |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 27 | |
| 28 | TYPES = ['dex', 'deploy', 'proguarded'] |
Christoffer Quist Adamsen | ca2cc7d | 2023-08-24 09:14:43 +0200 | [diff] [blame] | 29 | APPS = ['nest', 'youtube', 'gmail', 'chrome'] |
Tamas Kenez | 63a51d0 | 2019-01-07 15:53:02 +0100 | [diff] [blame] | 30 | COMPILERS = ['d8', 'r8'] |
| 31 | COMPILER_BUILDS = ['full', 'lib'] |
| 32 | |
Rico Wind | 5fdec15 | 2018-12-17 09:16:14 +0100 | [diff] [blame] | 33 | # We use this magic exit code to signal that the program OOM'ed |
| 34 | OOM_EXIT_CODE = 42 |
Jinseong Jeon | 158a3f1 | 2019-02-08 01:40:59 -0800 | [diff] [blame] | 35 | # According to Popen.returncode doc: |
| 36 | # A negative value -N indicates that the child was terminated by signal N. |
| 37 | TIMEOUT_KILL_CODE = -9 |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 38 | |
Morten Krogh-Jespersen | 0981b72 | 2019-10-09 10:00:33 +0200 | [diff] [blame] | 39 | # Log file names |
| 40 | FIND_MIN_XMX_FILE = 'find_min_xmx_results' |
| 41 | FIND_MIN_XMX_DIR = 'find_min_xmx' |
| 42 | |
Tamas Kenez | 5b1c585 | 2017-07-21 13:38:33 +0200 | [diff] [blame] | 43 | def ParseOptions(argv): |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 44 | result = optparse.OptionParser() |
Søren Gjesse | 932881f | 2017-06-13 10:43:36 +0200 | [diff] [blame] | 45 | result.add_option('--compiler', |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 46 | help='The compiler to use', |
Tamas Kenez | 5b1c585 | 2017-07-21 13:38:33 +0200 | [diff] [blame] | 47 | choices=COMPILERS) |
Tamas Kenez | 63a51d0 | 2019-01-07 15:53:02 +0100 | [diff] [blame] | 48 | result.add_option('--compiler-build', |
| 49 | help='Compiler build to use', |
| 50 | choices=COMPILER_BUILDS, |
| 51 | default='lib') |
Christoffer Quist Adamsen | 65ef298 | 2023-08-24 08:45:39 +0200 | [diff] [blame] | 52 | result.add_option('--no-fail-fast', |
| 53 | help='Whether run_on_app.py should report all failures ' |
| 54 | 'and not just the first one', |
| 55 | default=False, |
| 56 | action='store_true') |
Christoffer Quist Adamsen | 870fa46 | 2020-12-15 10:50:54 +0100 | [diff] [blame] | 57 | result.add_option('--hash', |
| 58 | help='The version of D8/R8 to use') |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 59 | result.add_option('--app', |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 60 | help='What app to run on', |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 61 | choices=APPS) |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 62 | result.add_option('--run-all', |
| 63 | help='Compile all possible combinations', |
| 64 | default=False, |
| 65 | action='store_true') |
Morten Krogh-Jespersen | b7e1a18 | 2019-10-09 13:04:54 +0200 | [diff] [blame] | 66 | result.add_option('--expect-oom', |
| 67 | help='Expect that compilation will fail with an OOM', |
| 68 | default=False, |
| 69 | action='store_true') |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 70 | result.add_option('--type', |
Tamas Kenez | 3fdaabd | 2017-06-15 13:05:12 +0200 | [diff] [blame] | 71 | help='Default for R8: deploy, for D8: proguarded', |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 72 | choices=TYPES) |
| 73 | result.add_option('--out', |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 74 | help='Where to place the output', |
Rico Wind | e9485ba | 2018-10-01 07:04:16 +0200 | [diff] [blame] | 75 | default=utils.BUILD) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 76 | result.add_option('--no-build', |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 77 | help='Run without building first', |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 78 | default=False, |
| 79 | action='store_true') |
Morten Krogh-Jespersen | 322c2f1 | 2019-10-08 10:41:21 +0200 | [diff] [blame] | 80 | result.add_option('--max-memory', |
| 81 | help='The maximum memory in MB to run with', |
| 82 | type='int') |
Rico Wind | 5fdec15 | 2018-12-17 09:16:14 +0100 | [diff] [blame] | 83 | result.add_option('--find-min-xmx', |
| 84 | help='Find the minimum amount of memory we can run in', |
| 85 | default=False, |
| 86 | action='store_true') |
Morten Krogh-Jespersen | 04c7c30 | 2019-10-01 15:04:33 +0200 | [diff] [blame] | 87 | result.add_option('--find-min-xmx-min-memory', |
| 88 | help='Setting the minimum memory baseline to run in', |
| 89 | type='int') |
| 90 | result.add_option('--find-min-xmx-max-memory', |
| 91 | help='Setting the maximum memory baseline to run in', |
| 92 | type='int') |
| 93 | result.add_option('--find-min-xmx-range-size', |
| 94 | help='Setting the size of the acceptable memory range', |
| 95 | type='int', |
| 96 | default=32) |
Morten Krogh-Jespersen | 0981b72 | 2019-10-09 10:00:33 +0200 | [diff] [blame] | 97 | result.add_option('--find-min-xmx-archive', |
| 98 | help='Archive find-min-xmx results on GCS', |
| 99 | default=False, |
| 100 | action='store_true') |
Christoffer Quist Adamsen | 3bbb50e | 2020-05-19 15:00:00 +0200 | [diff] [blame] | 101 | result.add_option('--no-extra-pgconf', '--no_extra_pgconf', |
| 102 | help='Build without the following extra rules: ' + |
| 103 | '-printconfiguration, -printmapping, -printseeds, ' + |
| 104 | '-printusage', |
| 105 | default=False, |
| 106 | action='store_true') |
Jinseong Jeon | 158a3f1 | 2019-02-08 01:40:59 -0800 | [diff] [blame] | 107 | result.add_option('--timeout', |
Morten Krogh-Jespersen | 0981b72 | 2019-10-09 10:00:33 +0200 | [diff] [blame] | 108 | type='int', |
Jinseong Jeon | 158a3f1 | 2019-02-08 01:40:59 -0800 | [diff] [blame] | 109 | default=0, |
| 110 | help='Set timeout instead of waiting for OOM.') |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 111 | result.add_option('--ignore-java-version', |
| 112 | help='Do not check java version', |
| 113 | default=False, |
| 114 | action='store_true') |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 115 | result.add_option('--no-libraries', |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 116 | help='Do not pass in libraries, even if they exist in conf', |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 117 | default=False, |
| 118 | action='store_true') |
| 119 | result.add_option('--no-debug', |
| 120 | help='Run without debug asserts.', |
| 121 | default=False, |
| 122 | action='store_true') |
Ian Zerny | 016d6f6 | 2020-03-19 08:58:21 +0100 | [diff] [blame] | 123 | result.add_option('--debug-agent', |
| 124 | help='Run with debug agent.', |
| 125 | default=False, |
| 126 | action='store_true') |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 127 | result.add_option('--version', |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 128 | help='The version of the app to run') |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 129 | result.add_option('-k', |
| 130 | help='Override the default ProGuard keep rules') |
Tamas Kenez | 139acc1 | 2017-06-14 17:14:58 +0200 | [diff] [blame] | 131 | result.add_option('--compiler-flags', |
| 132 | help='Additional option(s) for the compiler. ' + |
| 133 | 'If passing several options use a quoted string.') |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 134 | result.add_option('--r8-flags', |
Tamas Kenez | 139acc1 | 2017-06-14 17:14:58 +0200 | [diff] [blame] | 135 | help='Additional option(s) for the compiler. ' + |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 136 | 'Same as --compiler-flags, keeping it for backward' |
| 137 | ' compatibility. ' + |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 138 | 'If passing several options use a quoted string.') |
| 139 | result.add_option('--track-memory-to-file', |
| 140 | help='Track how much memory the jvm is using while ' + |
| 141 | ' compiling. Output to the specified file.') |
| 142 | result.add_option('--profile', |
| 143 | help='Profile R8 run.', |
| 144 | default=False, |
| 145 | action='store_true') |
| 146 | result.add_option('--dump-args-file', |
| 147 | help='Dump a file with the arguments for the specified ' + |
| 148 | 'configuration. For use as a @<file> argument to perform ' + |
| 149 | 'the run.') |
Tamas Kenez | f2ee2a3 | 2017-06-21 10:30:20 +0200 | [diff] [blame] | 150 | result.add_option('--print-runtimeraw', |
| 151 | metavar='BENCHMARKNAME', |
Tamas Kenez | 02bff03 | 2017-07-18 12:13:58 +0200 | [diff] [blame] | 152 | help='Print the line \'<BENCHMARKNAME>(RunTimeRaw):' + |
| 153 | ' <elapsed> ms\' at the end where <elapsed> is' + |
| 154 | ' the elapsed time in milliseconds.') |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 155 | result.add_option('--print-memoryuse', |
| 156 | metavar='BENCHMARKNAME', |
Tamas Kenez | 02bff03 | 2017-07-18 12:13:58 +0200 | [diff] [blame] | 157 | help='Print the line \'<BENCHMARKNAME>(MemoryUse):' + |
| 158 | ' <mem>\' at the end where <mem> is the peak' + |
| 159 | ' peak resident set size (VmHWM) in bytes.') |
| 160 | result.add_option('--print-dexsegments', |
| 161 | metavar='BENCHMARKNAME', |
| 162 | help='Print the sizes of individual dex segments as ' + |
| 163 | '\'<BENCHMARKNAME>-<segment>(CodeSize): <bytes>\'') |
Morten Krogh-Jespersen | ae9557c | 2019-10-23 15:14:02 +0200 | [diff] [blame] | 164 | result.add_option('--track-time-in-memory', |
| 165 | help='Plot the times taken from memory starting point to ' |
| 166 | 'end-point with defined memory increment', |
| 167 | default=False, |
| 168 | action='store_true') |
| 169 | result.add_option('--track-time-in-memory-max', |
| 170 | help='Setting the maximum memory baseline to run in', |
| 171 | type='int') |
| 172 | result.add_option('--track-time-in-memory-min', |
| 173 | help='Setting the minimum memory baseline to run in', |
| 174 | type='int') |
| 175 | result.add_option('--track-time-in-memory-increment', |
| 176 | help='Setting the increment', |
| 177 | type='int', |
| 178 | default=32) |
Søren Gjesse | 04a9433 | 2020-01-27 15:35:42 +0100 | [diff] [blame] | 179 | result.add_option('--print-times', |
| 180 | help='Include timing', |
| 181 | default=False, |
| 182 | action='store_true') |
Søren Gjesse | 943389f | 2020-03-13 10:40:25 +0100 | [diff] [blame] | 183 | result.add_option('--cpu-list', |
| 184 | help='Run under \'taskset\' with these CPUs. See ' |
| 185 | 'the \'taskset\' -c option for the format') |
Christoffer Quist Adamsen | d61d2a3 | 2020-06-02 07:41:10 +0200 | [diff] [blame] | 186 | result.add_option('--quiet', |
| 187 | help='Disable compiler logging', |
| 188 | default=False, |
| 189 | action='store_true') |
Christoffer Quist Adamsen | 65ef298 | 2023-08-24 08:45:39 +0200 | [diff] [blame] | 190 | result.add_option('--workers', |
| 191 | help='Number of workers to use', |
| 192 | default=1, |
| 193 | type=int) |
Christoffer Quist Adamsen | 870fa46 | 2020-12-15 10:50:54 +0100 | [diff] [blame] | 194 | (options, args) = result.parse_args(argv) |
| 195 | assert not options.hash or options.no_build, ( |
| 196 | 'Argument --no-build is required when using --hash') |
| 197 | assert not options.hash or options.compiler_build == 'full', ( |
| 198 | 'Compiler build lib not yet supported with --hash') |
| 199 | return (options, args) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 200 | |
Man Cao | 29b9ef1 | 2019-03-25 11:19:35 -0700 | [diff] [blame] | 201 | # Most apps have -printmapping, -printseeds, -printusage and |
| 202 | # -printconfiguration in the Proguard configuration. However we don't |
| 203 | # want to write these files in the locations specified. |
| 204 | # Instead generate an auxiliary Proguard configuration placing these |
| 205 | # output files together with the dex output. |
Søren Gjesse | 3a5aed9 | 2017-06-14 15:36:02 +0200 | [diff] [blame] | 206 | def GenerateAdditionalProguardConfiguration(temp, outdir): |
| 207 | name = "output.config" |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 208 | with open(os.path.join(temp, name), 'w') as f: |
| 209 | f.write('-printmapping ' + os.path.join(outdir, 'proguard.map') + "\n") |
| 210 | f.write('-printseeds ' + os.path.join(outdir, 'proguard.seeds') + "\n") |
Søren Gjesse | 5945958 | 2018-04-06 10:12:45 +0200 | [diff] [blame] | 211 | f.write('-printusage ' + os.path.join(outdir, 'proguard.usage') + "\n") |
Man Cao | 29b9ef1 | 2019-03-25 11:19:35 -0700 | [diff] [blame] | 212 | f.write('-printconfiguration ' + os.path.join(outdir, 'proguard.config') + "\n") |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 213 | return os.path.abspath(f.name) |
Søren Gjesse | 3a5aed9 | 2017-06-14 15:36:02 +0200 | [diff] [blame] | 214 | |
Rico Wind | 3f9302b | 2018-09-21 08:53:09 +0200 | [diff] [blame] | 215 | # Please add bug number for disabled permutations and please explicitly |
| 216 | # do Bug: #BUG in the commit message of disabling to ensure re-enabling |
| 217 | DISABLED_PERMUTATIONS = [ |
Christoffer Quist Adamsen | 130394c | 2021-01-19 08:55:57 +0100 | [diff] [blame] | 218 | # (app, version, type), e.g., ('gmail', '180826.15', 'deploy') |
Rico Wind | 3f9302b | 2018-09-21 08:53:09 +0200 | [diff] [blame] | 219 | ] |
| 220 | |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 221 | def get_permutations(): |
| 222 | data_providers = { |
Christoffer Quist Adamsen | a2a5877 | 2018-10-03 09:47:46 +0200 | [diff] [blame] | 223 | 'nest': nest_data, |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 224 | 'youtube': youtube_data, |
| 225 | 'chrome': chrome_data, |
Morten Krogh-Jespersen | 04c7c30 | 2019-10-01 15:04:33 +0200 | [diff] [blame] | 226 | 'gmail': gmail_data, |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 227 | } |
| 228 | # Check to ensure that we add all variants here. |
| 229 | assert len(APPS) == len(data_providers) |
Christoffer Quist Adamsen | 147342c | 2021-01-20 11:50:17 +0100 | [diff] [blame] | 230 | for app, data in data_providers.items(): |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 231 | for version in data.VERSIONS: |
| 232 | for type in data.VERSIONS[version]: |
Rico Wind | 3f9302b | 2018-09-21 08:53:09 +0200 | [diff] [blame] | 233 | if (app, version, type) not in DISABLED_PERMUTATIONS: |
Rico Wind | 6ddf2ce | 2023-08-23 08:46:29 +0200 | [diff] [blame] | 234 | # Only run with R8 lib to reduce cycle times. |
| 235 | for use_r8lib in [True]: |
Tamas Kenez | a730a7e | 2018-12-10 15:02:28 +0100 | [diff] [blame] | 236 | yield app, version, type, use_r8lib |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 237 | |
| 238 | def run_all(options, args): |
Christoffer Quist Adamsen | 65ef298 | 2023-08-24 08:45:39 +0200 | [diff] [blame] | 239 | # Build first so that each job won't. |
| 240 | if should_build(options): |
| 241 | gradle.RunGradle(['r8lib']) |
| 242 | options.no_build = True |
| 243 | assert not should_build(options) |
| 244 | |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 245 | # Args will be destroyed |
| 246 | assert len(args) == 0 |
Christoffer Quist Adamsen | 65ef298 | 2023-08-24 08:45:39 +0200 | [diff] [blame] | 247 | jobs = [] |
Tamas Kenez | a730a7e | 2018-12-10 15:02:28 +0100 | [diff] [blame] | 248 | for name, version, type, use_r8lib in get_permutations(): |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 249 | compiler = 'r8' if type == 'deploy' else 'd8' |
Tamas Kenez | 63a51d0 | 2019-01-07 15:53:02 +0100 | [diff] [blame] | 250 | compiler_build = 'lib' if use_r8lib else 'full' |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 251 | fixed_options = copy.copy(options) |
| 252 | fixed_options.app = name |
| 253 | fixed_options.version = version |
| 254 | fixed_options.compiler = compiler |
Tamas Kenez | 63a51d0 | 2019-01-07 15:53:02 +0100 | [diff] [blame] | 255 | fixed_options.compiler_build = compiler_build |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 256 | fixed_options.type = type |
Christoffer Quist Adamsen | 65ef298 | 2023-08-24 08:45:39 +0200 | [diff] [blame] | 257 | jobs.append( |
| 258 | create_job( |
| 259 | compiler, compiler_build, name, fixed_options, type, version)) |
| 260 | exit_code = thread_utils.run_in_parallel( |
| 261 | jobs, |
| 262 | number_of_workers=options.workers, |
| 263 | stop_on_first_failure=not options.no_fail_fast) |
| 264 | exit(exit_code) |
| 265 | |
| 266 | def create_job(compiler, compiler_build, name, options, type, version): |
| 267 | return lambda worker_id: run_job( |
| 268 | compiler, compiler_build, name, options, type, version, worker_id) |
| 269 | |
| 270 | def run_job( |
| 271 | compiler, compiler_build, name, options, type, version, worker_id): |
| 272 | print_thread( |
| 273 | 'Executing %s/%s with %s %s %s' |
| 274 | % (compiler, compiler_build, name, version, type), |
| 275 | worker_id) |
| 276 | if worker_id is not None: |
| 277 | options.out = os.path.join(options.out, str(worker_id)) |
| 278 | os.makedirs(options.out, exist_ok=True) |
| 279 | exit_code = run_with_options(options, [], worker_id=worker_id) |
| 280 | if exit_code: |
| 281 | print_thread( |
| 282 | 'Failed %s %s %s with %s/%s' |
| 283 | % (name, version, type, compiler, compiler_build), |
| 284 | worker_id) |
| 285 | return exit_code |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 286 | |
Rico Wind | 5fdec15 | 2018-12-17 09:16:14 +0100 | [diff] [blame] | 287 | def find_min_xmx(options, args): |
| 288 | # Args will be destroyed |
| 289 | assert len(args) == 0 |
| 290 | # If we can run in 128 MB then we are good (which we can for small examples |
| 291 | # or D8 on medium sized examples) |
Morten Krogh-Jespersen | 04c7c30 | 2019-10-01 15:04:33 +0200 | [diff] [blame] | 292 | if options.find_min_xmx_min_memory: |
| 293 | not_working = options.find_min_xmx_min_memory |
| 294 | elif options.compiler == 'd8': |
| 295 | not_working = 128 |
| 296 | else: |
| 297 | not_working = 1024 |
| 298 | if options.find_min_xmx_max_memory: |
| 299 | working = options.find_min_xmx_max_memory |
| 300 | else: |
| 301 | working = 1024 * 8 |
Rico Wind | 5fdec15 | 2018-12-17 09:16:14 +0100 | [diff] [blame] | 302 | exit_code = 0 |
Morten Krogh-Jespersen | 7043284 | 2021-01-19 17:07:58 +0100 | [diff] [blame] | 303 | range = int(options.find_min_xmx_range_size) |
Morten Krogh-Jespersen | 04c7c30 | 2019-10-01 15:04:33 +0200 | [diff] [blame] | 304 | while working - not_working > range: |
Morten Krogh-Jespersen | 7043284 | 2021-01-19 17:07:58 +0100 | [diff] [blame] | 305 | next_candidate = int(working - ((working - not_working)/2)) |
Rico Wind | 5fdec15 | 2018-12-17 09:16:14 +0100 | [diff] [blame] | 306 | print('working: %s, non_working: %s, next_candidate: %s' % |
| 307 | (working, not_working, next_candidate)) |
| 308 | extra_args = ['-Xmx%sM' % next_candidate] |
Rico Wind | 5fdec15 | 2018-12-17 09:16:14 +0100 | [diff] [blame] | 309 | t0 = time.time() |
| 310 | exit_code = run_with_options(options, [], extra_args) |
| 311 | t1 = time.time() |
| 312 | print('Running took: %s ms' % (1000.0 * (t1 - t0))) |
Jinseong Jeon | 158a3f1 | 2019-02-08 01:40:59 -0800 | [diff] [blame] | 313 | if exit_code != 0: |
| 314 | if exit_code not in [OOM_EXIT_CODE, TIMEOUT_KILL_CODE]: |
| 315 | print('Non OOM/Timeout error executing, exiting') |
| 316 | return 2 |
Rico Wind | 5fdec15 | 2018-12-17 09:16:14 +0100 | [diff] [blame] | 317 | if exit_code == 0: |
| 318 | working = next_candidate |
Jinseong Jeon | 158a3f1 | 2019-02-08 01:40:59 -0800 | [diff] [blame] | 319 | elif exit_code == TIMEOUT_KILL_CODE: |
| 320 | print('Timeout. Continue to the next candidate.') |
| 321 | not_working = next_candidate |
Rico Wind | 5fdec15 | 2018-12-17 09:16:14 +0100 | [diff] [blame] | 322 | else: |
| 323 | assert exit_code == OOM_EXIT_CODE |
| 324 | not_working = next_candidate |
| 325 | |
Morten Krogh-Jespersen | 04c7c30 | 2019-10-01 15:04:33 +0200 | [diff] [blame] | 326 | assert working - not_working <= range |
Morten Krogh-Jespersen | 0981b72 | 2019-10-09 10:00:33 +0200 | [diff] [blame] | 327 | found_range = 'Found range: %s - %s' % (not_working, working) |
| 328 | print(found_range) |
| 329 | |
| 330 | if options.find_min_xmx_archive: |
| 331 | sha = utils.get_HEAD_sha1() |
| 332 | (version, _) = get_version_and_data(options) |
| 333 | destination = os.path.join( |
| 334 | utils.R8_TEST_RESULTS_BUCKET, |
| 335 | FIND_MIN_XMX_DIR, |
| 336 | sha, |
| 337 | options.compiler, |
| 338 | options.compiler_build, |
| 339 | options.app, |
| 340 | version, |
| 341 | get_type(options)) |
| 342 | gs_destination = 'gs://%s' % destination |
| 343 | utils.archive_value(FIND_MIN_XMX_FILE, gs_destination, found_range + '\n') |
| 344 | |
Rico Wind | 5fdec15 | 2018-12-17 09:16:14 +0100 | [diff] [blame] | 345 | return 0 |
| 346 | |
Morten Krogh-Jespersen | f241230 | 2019-10-22 10:18:04 +0200 | [diff] [blame] | 347 | def print_min_xmx_ranges_for_hash(hash, compiler, compiler_build): |
| 348 | app_directory = os.path.join( |
| 349 | utils.R8_TEST_RESULTS_BUCKET, |
| 350 | FIND_MIN_XMX_DIR, |
| 351 | hash, |
| 352 | compiler, |
| 353 | compiler_build) |
| 354 | gs_base = 'gs://%s' % app_directory |
| 355 | for app in utils.ls_files_on_cloud_storage(gs_base).strip().split('\n'): |
| 356 | for version in utils.ls_files_on_cloud_storage(app).strip().split('\n'): |
| 357 | for type in utils.ls_files_on_cloud_storage(version).strip().split('\n'): |
| 358 | gs_location = '%s%s' % (type, FIND_MIN_XMX_FILE) |
| 359 | value = utils.cat_file_on_cloud_storage(gs_location, ignore_errors=True) |
| 360 | print('%s\n' % value) |
| 361 | |
Morten Krogh-Jespersen | ae9557c | 2019-10-23 15:14:02 +0200 | [diff] [blame] | 362 | def track_time_in_memory(options, args): |
| 363 | # Args will be destroyed |
| 364 | assert len(args) == 0 |
| 365 | if not options.track_time_in_memory_min: |
| 366 | raise Exception( |
| 367 | 'You have to specify --track_time_in_memory_min when running with ' |
| 368 | '--track-time-in-memory') |
| 369 | if not options.track_time_in_memory_max: |
| 370 | raise Exception( |
| 371 | 'You have to specify --track_time_in_memory_max when running with ' |
| 372 | '--track-time-in-memory') |
| 373 | if not options.track_time_in_memory_increment: |
| 374 | raise Exception( |
| 375 | 'You have to specify --track_time_in_memory_increment when running ' |
| 376 | 'with --track-time-in-memory') |
| 377 | current = options.track_time_in_memory_min |
| 378 | print('Memory (KB)\tTime (ms)') |
| 379 | with utils.TempDir() as temp: |
| 380 | stdout = os.path.join(temp, 'stdout') |
| 381 | stdout_fd = open(stdout, 'w') |
| 382 | while current <= options.track_time_in_memory_max: |
| 383 | extra_args = ['-Xmx%sM' % current] |
| 384 | t0 = time.time() |
| 385 | exit_code = run_with_options(options, [], extra_args, stdout_fd, quiet=True) |
| 386 | t1 = time.time() |
| 387 | total = (1000.0 * (t1 - t0)) if exit_code == 0 else -1 |
| 388 | print('%s\t%s' % (current, total)) |
| 389 | current += options.track_time_in_memory_increment |
| 390 | |
| 391 | return 0 |
| 392 | |
Tamas Kenez | 5b1c585 | 2017-07-21 13:38:33 +0200 | [diff] [blame] | 393 | def main(argv): |
Tamas Kenez | 5b1c585 | 2017-07-21 13:38:33 +0200 | [diff] [blame] | 394 | (options, args) = ParseOptions(argv) |
Morten Krogh-Jespersen | b7e1a18 | 2019-10-09 13:04:54 +0200 | [diff] [blame] | 395 | if options.expect_oom and not options.max_memory: |
| 396 | raise Exception( |
| 397 | 'You should only use --expect-oom if also specifying --max-memory') |
| 398 | if options.expect_oom and options.timeout: |
| 399 | raise Exception( |
| 400 | 'You should not use --timeout when also specifying --expect-oom') |
Morten Krogh-Jespersen | ae9557c | 2019-10-23 15:14:02 +0200 | [diff] [blame] | 401 | if options.find_min_xmx and options.track_time_in_memory: |
| 402 | raise Exception( |
| 403 | 'You cannot both find the min xmx and track time at the same time') |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 404 | if options.run_all: |
| 405 | return run_all(options, args) |
Rico Wind | 5fdec15 | 2018-12-17 09:16:14 +0100 | [diff] [blame] | 406 | if options.find_min_xmx: |
| 407 | return find_min_xmx(options, args) |
Morten Krogh-Jespersen | ae9557c | 2019-10-23 15:14:02 +0200 | [diff] [blame] | 408 | if options.track_time_in_memory: |
| 409 | return track_time_in_memory(options, args) |
Christoffer Quist Adamsen | d61d2a3 | 2020-06-02 07:41:10 +0200 | [diff] [blame] | 410 | exit_code = run_with_options(options, args, quiet=options.quiet) |
Morten Krogh-Jespersen | b7e1a18 | 2019-10-09 13:04:54 +0200 | [diff] [blame] | 411 | if options.expect_oom: |
| 412 | exit_code = 0 if exit_code == OOM_EXIT_CODE else 1 |
| 413 | return exit_code |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 414 | |
Morten Krogh-Jespersen | 0981b72 | 2019-10-09 10:00:33 +0200 | [diff] [blame] | 415 | def get_version_and_data(options): |
Christoffer Quist Adamsen | ca2cc7d | 2023-08-24 09:14:43 +0200 | [diff] [blame] | 416 | if options.app == 'nest': |
Morten Krogh-Jespersen | 0981b72 | 2019-10-09 10:00:33 +0200 | [diff] [blame] | 417 | version = options.version or '20180926' |
| 418 | data = nest_data |
| 419 | elif options.app == 'youtube': |
Christoffer Quist Adamsen | 4380de3 | 2021-04-23 06:24:31 +0200 | [diff] [blame] | 420 | version = options.version or youtube_data.LATEST_VERSION |
Morten Krogh-Jespersen | 0981b72 | 2019-10-09 10:00:33 +0200 | [diff] [blame] | 421 | data = youtube_data |
| 422 | elif options.app == 'chrome': |
| 423 | version = options.version or '180917' |
| 424 | data = chrome_data |
| 425 | elif options.app == 'gmail': |
| 426 | version = options.version or '170604.16' |
| 427 | data = gmail_data |
Morten Krogh-Jespersen | 0981b72 | 2019-10-09 10:00:33 +0200 | [diff] [blame] | 428 | else: |
| 429 | raise Exception("You need to specify '--app={}'".format('|'.join(APPS))) |
| 430 | return version, data |
| 431 | |
| 432 | def get_type(options): |
| 433 | if not options.type: |
| 434 | return 'deploy' if options.compiler == 'r8' else 'proguarded' |
| 435 | return options.type |
| 436 | |
Søren Gjesse | 94793ca | 2019-11-08 13:53:08 +0100 | [diff] [blame] | 437 | def has_injars_and_libraryjars(pgconfs): |
| 438 | # Check if there are -injars and -libraryjars in the configuration. |
| 439 | has_injars = False |
| 440 | has_libraryjars = False |
| 441 | for pgconf in pgconfs: |
| 442 | pgconf_dirname = os.path.abspath(os.path.dirname(pgconf)) |
| 443 | with open(pgconf) as pgconf_file: |
| 444 | for line in pgconf_file: |
| 445 | trimmed = line.strip() |
| 446 | if trimmed.startswith('-injars'): |
| 447 | has_injars = True |
| 448 | elif trimmed.startswith('-libraryjars'): |
| 449 | has_libraryjars = True |
| 450 | if has_injars and has_libraryjars: |
| 451 | return True |
| 452 | return False |
| 453 | |
Søren Gjesse | 889e09d | 2019-11-07 16:33:51 +0100 | [diff] [blame] | 454 | def check_no_injars_and_no_libraryjars(pgconfs): |
| 455 | # Ensure that there are no -injars or -libraryjars in the configuration. |
| 456 | for pgconf in pgconfs: |
| 457 | pgconf_dirname = os.path.abspath(os.path.dirname(pgconf)) |
| 458 | with open(pgconf) as pgconf_file: |
| 459 | for line in pgconf_file: |
| 460 | trimmed = line.strip() |
| 461 | if trimmed.startswith('-injars'): |
| 462 | raise Exception("Unexpected -injars found in " + pgconf) |
| 463 | elif trimmed.startswith('-libraryjars'): |
| 464 | raise Exception("Unexpected -libraryjars found in " + pgconf) |
| 465 | |
Søren Gjesse | 80e0048 | 2021-04-06 13:17:35 +0200 | [diff] [blame] | 466 | def should_build(options): |
Morten Krogh-Jespersen | 254805e | 2022-06-03 09:32:42 +0200 | [diff] [blame] | 467 | return not options.no_build |
Søren Gjesse | 80e0048 | 2021-04-06 13:17:35 +0200 | [diff] [blame] | 468 | |
Christoffer Quist Adamsen | 7ff1409 | 2021-04-15 11:48:12 +0200 | [diff] [blame] | 469 | def build_desugared_library_dex( |
| 470 | options, |
| 471 | quiet, |
| 472 | temp, |
| 473 | android_java8_libs, |
| 474 | desugared_lib_pg_conf, |
| 475 | inputs, |
| 476 | outdir): |
| 477 | if not inputs: |
| 478 | raise Exception( |
| 479 | "If 'android_java8_libs' is specified the inputs must be explicit" |
| 480 | + "(not defined using '-injars' in Proguard configuration files)") |
Søren Gjesse | 80e0048 | 2021-04-06 13:17:35 +0200 | [diff] [blame] | 481 | if outdir.endswith('.zip') or outdir.endswith('.jar'): |
Christoffer Quist Adamsen | 7ff1409 | 2021-04-15 11:48:12 +0200 | [diff] [blame] | 482 | raise Exception( |
| 483 | "If 'android_java8_libs' is specified the output must be a directory") |
Søren Gjesse | 80e0048 | 2021-04-06 13:17:35 +0200 | [diff] [blame] | 484 | |
| 485 | jar = None |
| 486 | main = None |
| 487 | if options.hash: |
| 488 | jar = os.path.join(utils.LIBS, 'r8-' + options.hash + '.jar') |
Christoffer Quist Adamsen | 7ff1409 | 2021-04-15 11:48:12 +0200 | [diff] [blame] | 489 | main = 'com.android.tools.r8.R8' |
Søren Gjesse | 80e0048 | 2021-04-06 13:17:35 +0200 | [diff] [blame] | 490 | |
Christoffer Quist Adamsen | 7ff1409 | 2021-04-15 11:48:12 +0200 | [diff] [blame] | 491 | # Determine the l8 tool. |
Søren Gjesse | 80e0048 | 2021-04-06 13:17:35 +0200 | [diff] [blame] | 492 | assert(options.compiler_build in ['full', 'lib']) |
| 493 | lib_prefix = 'r8lib-' if options.compiler_build == 'lib' else '' |
Christoffer Quist Adamsen | 7ff1409 | 2021-04-15 11:48:12 +0200 | [diff] [blame] | 494 | tool = lib_prefix + 'l8' |
Søren Gjesse | 80e0048 | 2021-04-06 13:17:35 +0200 | [diff] [blame] | 495 | |
Christoffer Quist Adamsen | 7ff1409 | 2021-04-15 11:48:12 +0200 | [diff] [blame] | 496 | # Prepare out directory. |
Søren Gjesse | 80e0048 | 2021-04-06 13:17:35 +0200 | [diff] [blame] | 497 | android_java8_libs_output = os.path.join(temp, 'android_java8_libs') |
| 498 | os.makedirs(android_java8_libs_output) |
Christoffer Quist Adamsen | 7ff1409 | 2021-04-15 11:48:12 +0200 | [diff] [blame] | 499 | |
| 500 | # Prepare arguments for L8. |
Søren Gjesse | 80e0048 | 2021-04-06 13:17:35 +0200 | [diff] [blame] | 501 | args = [ |
Christoffer Quist Adamsen | 7ff1409 | 2021-04-15 11:48:12 +0200 | [diff] [blame] | 502 | '--desugared-lib', android_java8_libs['config'], |
| 503 | '--lib', android_java8_libs['library'], |
Søren Gjesse | 80e0048 | 2021-04-06 13:17:35 +0200 | [diff] [blame] | 504 | '--output', android_java8_libs_output, |
Christoffer Quist Adamsen | 7ff1409 | 2021-04-15 11:48:12 +0200 | [diff] [blame] | 505 | '--pg-conf', desugared_lib_pg_conf, |
| 506 | '--release', |
Søren Gjesse | 80e0048 | 2021-04-06 13:17:35 +0200 | [diff] [blame] | 507 | ] |
Christoffer Quist Adamsen | 7ff1409 | 2021-04-15 11:48:12 +0200 | [diff] [blame] | 508 | if 'pgconf' in android_java8_libs: |
| 509 | for pgconf in android_java8_libs['pgconf']: |
| 510 | args.extend(['--pg-conf', pgconf]) |
| 511 | args.extend(android_java8_libs['program']) |
| 512 | |
| 513 | # Run L8. |
| 514 | exit_code = toolhelper.run( |
| 515 | tool, args, |
Søren Gjesse | 80e0048 | 2021-04-06 13:17:35 +0200 | [diff] [blame] | 516 | build=should_build(options), |
| 517 | debug=not options.no_debug, |
| 518 | quiet=quiet, |
| 519 | jar=jar, |
| 520 | main=main) |
Christoffer Quist Adamsen | 7ff1409 | 2021-04-15 11:48:12 +0200 | [diff] [blame] | 521 | |
Søren Gjesse | 80e0048 | 2021-04-06 13:17:35 +0200 | [diff] [blame] | 522 | # Copy the desugared library DEX to the output. |
Christoffer Quist Adamsen | 7ff1409 | 2021-04-15 11:48:12 +0200 | [diff] [blame] | 523 | dex_file_name = ( |
| 524 | 'classes' + str(len(glob(os.path.join(outdir, '*.dex'))) + 1) + '.dex') |
Søren Gjesse | 80e0048 | 2021-04-06 13:17:35 +0200 | [diff] [blame] | 525 | shutil.copyfile( |
| 526 | os.path.join(android_java8_libs_output, 'classes.dex'), |
| 527 | os.path.join(outdir, dex_file_name)) |
| 528 | |
Christoffer Quist Adamsen | 65ef298 | 2023-08-24 08:45:39 +0200 | [diff] [blame] | 529 | def run_with_options( |
| 530 | options, args, extra_args=None, stdout=None, quiet=False, worker_id=None): |
Morten Krogh-Jespersen | 94a9b10 | 2019-11-01 09:45:44 +0000 | [diff] [blame] | 531 | if extra_args is None: |
| 532 | extra_args = [] |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 533 | app_provided_pg_conf = False; |
Rico Wind | add0813 | 2018-12-14 14:17:15 +0100 | [diff] [blame] | 534 | # todo(121018500): remove when memory is under control |
Rico Wind | 20602b7 | 2019-01-09 09:17:13 +0100 | [diff] [blame] | 535 | if not any('-Xmx' in arg for arg in extra_args): |
Morten Krogh-Jespersen | 322c2f1 | 2019-10-08 10:41:21 +0200 | [diff] [blame] | 536 | if options.max_memory: |
| 537 | extra_args.append('-Xmx%sM' % options.max_memory) |
| 538 | else: |
| 539 | extra_args.append('-Xmx8G') |
Rico Wind | afdbbfd | 2019-02-22 09:32:07 +0100 | [diff] [blame] | 540 | if not options.ignore_java_version: |
| 541 | utils.check_java_version() |
| 542 | |
Søren Gjesse | 04a9433 | 2020-01-27 15:35:42 +0100 | [diff] [blame] | 543 | if options.print_times: |
| 544 | extra_args.append('-Dcom.android.tools.r8.printtimes=1') |
| 545 | |
Morten Krogh-Jespersen | a565f01 | 2021-07-13 13:27:41 +0200 | [diff] [blame] | 546 | if not options.no_debug: |
| 547 | extra_args.append('-Dcom.android.tools.r8.enableTestAssertions=1') |
| 548 | |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 549 | outdir = options.out |
Morten Krogh-Jespersen | 0981b72 | 2019-10-09 10:00:33 +0200 | [diff] [blame] | 550 | (version_id, data) = get_version_and_data(options) |
| 551 | |
Tamas Kenez | 5b1c585 | 2017-07-21 13:38:33 +0200 | [diff] [blame] | 552 | if options.compiler not in COMPILERS: |
| 553 | raise Exception("You need to specify '--compiler={}'" |
| 554 | .format('|'.join(COMPILERS))) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 555 | |
Tamas Kenez | 63a51d0 | 2019-01-07 15:53:02 +0100 | [diff] [blame] | 556 | if options.compiler_build not in COMPILER_BUILDS: |
| 557 | raise Exception("You need to specify '--compiler-build={}'" |
| 558 | .format('|'.join(COMPILER_BUILDS))) |
| 559 | |
Morten Krogh-Jespersen | 0981b72 | 2019-10-09 10:00:33 +0200 | [diff] [blame] | 560 | if not version_id in data.VERSIONS.keys(): |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 561 | print('No version {} for application {}' |
Morten Krogh-Jespersen | 0981b72 | 2019-10-09 10:00:33 +0200 | [diff] [blame] | 562 | .format(version_id, options.app)) |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 563 | print('Valid versions are {}'.format(data.VERSIONS.keys())) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 564 | return 1 |
| 565 | |
Morten Krogh-Jespersen | 0981b72 | 2019-10-09 10:00:33 +0200 | [diff] [blame] | 566 | version = data.VERSIONS[version_id] |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 567 | |
Morten Krogh-Jespersen | 0981b72 | 2019-10-09 10:00:33 +0200 | [diff] [blame] | 568 | type = get_type(options) |
Tamas Kenez | 3fdaabd | 2017-06-15 13:05:12 +0200 | [diff] [blame] | 569 | |
Morten Krogh-Jespersen | 0981b72 | 2019-10-09 10:00:33 +0200 | [diff] [blame] | 570 | if type not in version: |
| 571 | print('No type {} for version {}'.format(type, version)) |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 572 | print('Valid types are {}'.format(version.keys())) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 573 | return 1 |
Morten Krogh-Jespersen | 0981b72 | 2019-10-09 10:00:33 +0200 | [diff] [blame] | 574 | values = version[type] |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 575 | |
| 576 | args.extend(['--output', outdir]) |
Ian Zerny | 877c186 | 2017-07-06 11:12:26 +0200 | [diff] [blame] | 577 | if 'min-api' in values: |
| 578 | args.extend(['--min-api', values['min-api']]) |
Søren Gjesse | 932881f | 2017-06-13 10:43:36 +0200 | [diff] [blame] | 579 | |
Søren Gjesse | 8ae55eb | 2018-09-28 11:11:36 +0200 | [diff] [blame] | 580 | if 'main-dex-list' in values: |
| 581 | args.extend(['--main-dex-list', values['main-dex-list']]) |
| 582 | |
Christoffer Quist Adamsen | e727302 | 2021-04-12 07:42:07 +0200 | [diff] [blame] | 583 | inputs = values['inputs'] |
Søren Gjesse | 94793ca | 2019-11-08 13:53:08 +0100 | [diff] [blame] | 584 | libraries = values['libraries'] if 'libraries' in values else [] |
Christoffer Quist Adamsen | e727302 | 2021-04-12 07:42:07 +0200 | [diff] [blame] | 585 | |
Tamas Kenez | 63a51d0 | 2019-01-07 15:53:02 +0100 | [diff] [blame] | 586 | if options.compiler == 'r8': |
Søren Gjesse | 932881f | 2017-06-13 10:43:36 +0200 | [diff] [blame] | 587 | if 'pgconf' in values and not options.k: |
Christoffer Quist Adamsen | 65ef298 | 2023-08-24 08:45:39 +0200 | [diff] [blame] | 588 | sanitized_lib_path = os.path.join( |
| 589 | os.path.abspath(outdir), 'sanitized_lib.jar') |
Søren Gjesse | 94793ca | 2019-11-08 13:53:08 +0100 | [diff] [blame] | 590 | if has_injars_and_libraryjars(values['pgconf']): |
Søren Gjesse | 889e09d | 2019-11-07 16:33:51 +0100 | [diff] [blame] | 591 | sanitized_pgconf_path = os.path.join( |
| 592 | os.path.abspath(outdir), 'sanitized.config') |
| 593 | SanitizeLibrariesInPgconf( |
| 594 | sanitized_lib_path, sanitized_pgconf_path, values['pgconf']) |
Søren Gjesse | 94793ca | 2019-11-08 13:53:08 +0100 | [diff] [blame] | 595 | libraries = [sanitized_lib_path] |
Søren Gjesse | 889e09d | 2019-11-07 16:33:51 +0100 | [diff] [blame] | 596 | args.extend(['--pg-conf', sanitized_pgconf_path]) |
Christoffer Quist Adamsen | e727302 | 2021-04-12 07:42:07 +0200 | [diff] [blame] | 597 | inputs = [] |
Søren Gjesse | 94793ca | 2019-11-08 13:53:08 +0100 | [diff] [blame] | 598 | else: |
| 599 | # -injars without -libraryjars or vice versa is not supported. |
| 600 | check_no_injars_and_no_libraryjars(values['pgconf']) |
| 601 | for pgconf in values['pgconf']: |
| 602 | args.extend(['--pg-conf', pgconf]) |
| 603 | if 'sanitize_libraries' in values and values['sanitize_libraries']: |
Søren Gjesse | 94793ca | 2019-11-08 13:53:08 +0100 | [diff] [blame] | 604 | SanitizeLibraries( |
| 605 | sanitized_lib_path, values['libraries'], values['inputs']) |
| 606 | libraries = [sanitized_lib_path] |
Søren Gjesse | cbeae78 | 2019-05-21 14:14:25 +0200 | [diff] [blame] | 607 | app_provided_pg_conf = True |
Rico Wind | 51f0bb2 | 2021-09-09 09:57:42 +0200 | [diff] [blame] | 608 | if 'pgconf_extra' in values: |
| 609 | extra_conf = os.path.join(os.path.abspath(outdir), 'pgconf_extra') |
| 610 | with open(extra_conf, 'w') as extra_f: |
| 611 | extra_f.write(values['pgconf_extra']) |
| 612 | args.extend(['--pg-conf', extra_conf]) |
Søren Gjesse | 932881f | 2017-06-13 10:43:36 +0200 | [diff] [blame] | 613 | if options.k: |
| 614 | args.extend(['--pg-conf', options.k]) |
Søren Gjesse | c801ecc | 2017-08-03 13:40:06 +0200 | [diff] [blame] | 615 | if 'maindexrules' in values: |
| 616 | for rules in values['maindexrules']: |
| 617 | args.extend(['--main-dex-rules', rules]) |
Rico Wind | 79e4eb5 | 2018-12-13 13:00:49 +0100 | [diff] [blame] | 618 | if 'allow-type-errors' in values: |
| 619 | extra_args.append('-Dcom.android.tools.r8.allowTypeErrors=1') |
Christoffer Quist Adamsen | 57254f0 | 2020-05-18 15:43:58 +0200 | [diff] [blame] | 620 | extra_args.append( |
| 621 | '-Dcom.android.tools.r8.disallowClassInlinerGracefulExit=1') |
Christoffer Quist Adamsen | 62fcc15 | 2022-06-03 14:01:35 +0200 | [diff] [blame] | 622 | if 'system-properties' in values: |
| 623 | for system_property in values['system-properties']: |
| 624 | extra_args.append(system_property) |
Søren Gjesse | 932881f | 2017-06-13 10:43:36 +0200 | [diff] [blame] | 625 | |
Ian Zerny | 016d6f6 | 2020-03-19 08:58:21 +0100 | [diff] [blame] | 626 | if options.debug_agent: |
| 627 | if not options.compiler_build == 'full': |
| 628 | print('WARNING: Running debugging agent on r8lib is questionable...') |
| 629 | extra_args.append( |
| 630 | '-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005') |
| 631 | |
Søren Gjesse | 94793ca | 2019-11-08 13:53:08 +0100 | [diff] [blame] | 632 | if not options.no_libraries: |
| 633 | for lib in libraries: |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 634 | args.extend(['--lib', lib]) |
| 635 | |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 636 | if not outdir.endswith('.zip') and not outdir.endswith('.jar') \ |
| 637 | and not os.path.exists(outdir): |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 638 | os.makedirs(outdir) |
| 639 | |
Christoffer Quist Adamsen | 870fa46 | 2020-12-15 10:50:54 +0100 | [diff] [blame] | 640 | if options.hash: |
| 641 | # Download r8-<hash>.jar from |
| 642 | # https://storage.googleapis.com/r8-releases/raw/<hash>/. |
| 643 | download_path = archive.GetUploadDestination(options.hash, 'r8.jar', True) |
| 644 | assert utils.file_exists_on_cloud_storage(download_path), ( |
| 645 | 'Could not find r8.jar file from provided hash: %s' % options.hash) |
| 646 | destination = os.path.join(utils.LIBS, 'r8-' + options.hash + '.jar') |
| 647 | utils.download_file_from_cloud_storage( |
| 648 | download_path, destination, quiet=quiet) |
| 649 | |
Søren Gjesse | 8ae55eb | 2018-09-28 11:11:36 +0200 | [diff] [blame] | 650 | # Additional flags for the compiler from the configuration file. |
| 651 | if 'flags' in values: |
| 652 | args.extend(values['flags'].split(' ')) |
Tamas Kenez | 63a51d0 | 2019-01-07 15:53:02 +0100 | [diff] [blame] | 653 | if options.compiler == 'r8': |
Søren Gjesse | 932881f | 2017-06-13 10:43:36 +0200 | [diff] [blame] | 654 | if 'r8-flags' in values: |
| 655 | args.extend(values['r8-flags'].split(' ')) |
Tamas Kenez | 139acc1 | 2017-06-14 17:14:58 +0200 | [diff] [blame] | 656 | |
Søren Gjesse | 8ae55eb | 2018-09-28 11:11:36 +0200 | [diff] [blame] | 657 | # Additional flags for the compiler from the command line. |
Tamas Kenez | 139acc1 | 2017-06-14 17:14:58 +0200 | [diff] [blame] | 658 | if options.compiler_flags: |
| 659 | args.extend(options.compiler_flags.split(' ')) |
| 660 | if options.r8_flags: |
| 661 | args.extend(options.r8_flags.split(' ')) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 662 | |
Christoffer Quist Adamsen | 287c186 | 2020-05-20 15:51:12 +0200 | [diff] [blame] | 663 | # Feature jars. |
| 664 | features = values['features'] if 'features' in values else [] |
| 665 | for i, feature in enumerate(features, start=1): |
| 666 | feature_out = os.path.join(outdir, 'feature-%d.zip' % i) |
| 667 | for feature_jar in feature['inputs']: |
| 668 | args.extend(['--feature', feature_jar, feature_out]) |
| 669 | |
Søren Gjesse | 94793ca | 2019-11-08 13:53:08 +0100 | [diff] [blame] | 670 | args.extend(inputs) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 671 | |
Morten Krogh-Jespersen | 89e1901 | 2021-10-05 09:55:01 +0200 | [diff] [blame] | 672 | t0 = None |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 673 | if options.dump_args_file: |
| 674 | with open(options.dump_args_file, 'w') as args_file: |
| 675 | args_file.writelines([arg + os.linesep for arg in args]) |
| 676 | else: |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 677 | with utils.TempDir() as temp: |
| 678 | if options.print_memoryuse and not options.track_memory_to_file: |
| 679 | options.track_memory_to_file = os.path.join(temp, |
| 680 | utils.MEMORY_USE_TMP_FILE) |
Tamas Kenez | 63a51d0 | 2019-01-07 15:53:02 +0100 | [diff] [blame] | 681 | if options.compiler == 'r8' and app_provided_pg_conf: |
Mathias Rav | dd6a6de | 2018-05-18 10:18:33 +0200 | [diff] [blame] | 682 | # Ensure that output of -printmapping and -printseeds go to the output |
| 683 | # location and not where the app Proguard configuration places them. |
| 684 | if outdir.endswith('.zip') or outdir.endswith('.jar'): |
| 685 | pg_outdir = os.path.dirname(outdir) |
| 686 | else: |
| 687 | pg_outdir = outdir |
Christoffer Quist Adamsen | 3bbb50e | 2020-05-19 15:00:00 +0200 | [diff] [blame] | 688 | if not options.no_extra_pgconf: |
| 689 | additional_pg_conf = GenerateAdditionalProguardConfiguration( |
| 690 | temp, os.path.abspath(pg_outdir)) |
| 691 | args.extend(['--pg-conf', additional_pg_conf]) |
Christoffer Quist Adamsen | 7ff1409 | 2021-04-15 11:48:12 +0200 | [diff] [blame] | 692 | |
| 693 | android_java8_libs = values.get('android_java8_libs') |
| 694 | if android_java8_libs: |
| 695 | desugared_lib_pg_conf = os.path.join( |
| 696 | temp, 'desugared-lib-pg-conf.txt') |
| 697 | args.extend(['--desugared-lib', android_java8_libs['config']]) |
| 698 | args.extend( |
| 699 | ['--desugared-lib-pg-conf-output', desugared_lib_pg_conf]) |
| 700 | |
Rico Wind | 5fdec15 | 2018-12-17 09:16:14 +0100 | [diff] [blame] | 701 | stderr_path = os.path.join(temp, 'stderr') |
| 702 | with open(stderr_path, 'w') as stderr: |
Christoffer Quist Adamsen | 870fa46 | 2020-12-15 10:50:54 +0100 | [diff] [blame] | 703 | jar = None |
| 704 | main = None |
Tamas Kenez | 63a51d0 | 2019-01-07 15:53:02 +0100 | [diff] [blame] | 705 | if options.compiler_build == 'full': |
| 706 | tool = options.compiler |
| 707 | else: |
| 708 | assert(options.compiler_build == 'lib') |
| 709 | tool = 'r8lib-' + options.compiler |
Christoffer Quist Adamsen | 870fa46 | 2020-12-15 10:50:54 +0100 | [diff] [blame] | 710 | if options.hash: |
| 711 | jar = os.path.join(utils.LIBS, 'r8-' + options.hash + '.jar') |
| 712 | main = 'com.android.tools.r8.' + options.compiler.upper() |
Morten Krogh-Jespersen | 89e1901 | 2021-10-05 09:55:01 +0200 | [diff] [blame] | 713 | if should_build(options): |
| 714 | gradle.RunGradle(['r8lib' if tool.startswith('r8lib') else 'r8']) |
| 715 | t0 = time.time() |
Tamas Kenez | 63a51d0 | 2019-01-07 15:53:02 +0100 | [diff] [blame] | 716 | exit_code = toolhelper.run(tool, args, |
Morten Krogh-Jespersen | 89e1901 | 2021-10-05 09:55:01 +0200 | [diff] [blame] | 717 | build=False, |
Rico Wind | 5fdec15 | 2018-12-17 09:16:14 +0100 | [diff] [blame] | 718 | debug=not options.no_debug, |
| 719 | profile=options.profile, |
| 720 | track_memory_file=options.track_memory_to_file, |
Jinseong Jeon | 158a3f1 | 2019-02-08 01:40:59 -0800 | [diff] [blame] | 721 | extra_args=extra_args, |
Morten Krogh-Jespersen | ae9557c | 2019-10-23 15:14:02 +0200 | [diff] [blame] | 722 | stdout=stdout, |
Jinseong Jeon | 158a3f1 | 2019-02-08 01:40:59 -0800 | [diff] [blame] | 723 | stderr=stderr, |
Morten Krogh-Jespersen | ae9557c | 2019-10-23 15:14:02 +0200 | [diff] [blame] | 724 | timeout=options.timeout, |
Søren Gjesse | 943389f | 2020-03-13 10:40:25 +0100 | [diff] [blame] | 725 | quiet=quiet, |
Morten Krogh-Jespersen | 0e4d7e2 | 2020-03-14 00:56:32 +0100 | [diff] [blame] | 726 | cmd_prefix=[ |
Christoffer Quist Adamsen | 870fa46 | 2020-12-15 10:50:54 +0100 | [diff] [blame] | 727 | 'taskset', '-c', options.cpu_list] if options.cpu_list else [], |
| 728 | jar=jar, |
Christoffer Quist Adamsen | 65ef298 | 2023-08-24 08:45:39 +0200 | [diff] [blame] | 729 | main=main, |
| 730 | worker_id=worker_id) |
Christoffer Quist Adamsen | 21c6660 | 2018-08-09 16:22:54 +0200 | [diff] [blame] | 731 | if exit_code != 0: |
Rico Wind | 5fdec15 | 2018-12-17 09:16:14 +0100 | [diff] [blame] | 732 | with open(stderr_path) as stderr: |
| 733 | stderr_text = stderr.read() |
Morten Krogh-Jespersen | ae9557c | 2019-10-23 15:14:02 +0200 | [diff] [blame] | 734 | if not quiet: |
| 735 | print(stderr_text) |
Rico Wind | 5fdec15 | 2018-12-17 09:16:14 +0100 | [diff] [blame] | 736 | if 'java.lang.OutOfMemoryError' in stderr_text: |
Morten Krogh-Jespersen | ae9557c | 2019-10-23 15:14:02 +0200 | [diff] [blame] | 737 | if not quiet: |
| 738 | print('Failure was OOM') |
Rico Wind | 5fdec15 | 2018-12-17 09:16:14 +0100 | [diff] [blame] | 739 | return OOM_EXIT_CODE |
| 740 | return exit_code |
Christoffer Quist Adamsen | 21c6660 | 2018-08-09 16:22:54 +0200 | [diff] [blame] | 741 | |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 742 | if options.print_memoryuse: |
| 743 | print('{}(MemoryUse): {}' |
| 744 | .format(options.print_memoryuse, |
| 745 | utils.grep_memoryuse(options.track_memory_to_file))) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 746 | |
Christoffer Quist Adamsen | 7ff1409 | 2021-04-15 11:48:12 +0200 | [diff] [blame] | 747 | if android_java8_libs: |
| 748 | build_desugared_library_dex( |
| 749 | options, quiet, temp, android_java8_libs, |
| 750 | desugared_lib_pg_conf, inputs, outdir) |
Søren Gjesse | 80e0048 | 2021-04-06 13:17:35 +0200 | [diff] [blame] | 751 | |
| 752 | |
Tamas Kenez | f2ee2a3 | 2017-06-21 10:30:20 +0200 | [diff] [blame] | 753 | if options.print_runtimeraw: |
| 754 | print('{}(RunTimeRaw): {} ms' |
| 755 | .format(options.print_runtimeraw, 1000.0 * (time.time() - t0))) |
| 756 | |
Tamas Kenez | 02bff03 | 2017-07-18 12:13:58 +0200 | [diff] [blame] | 757 | if options.print_dexsegments: |
| 758 | dex_files = glob(os.path.join(outdir, '*.dex')) |
| 759 | utils.print_dexsegments(options.print_dexsegments, dex_files) |
Morten Krogh-Jespersen | 254805e | 2022-06-03 09:32:42 +0200 | [diff] [blame] | 760 | print('{}-Total(CodeSize): {}'.format( |
| 761 | options.print_dexsegments, compute_size_of_dex_files(dex_files))) |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 762 | return 0 |
Tamas Kenez | 02bff03 | 2017-07-18 12:13:58 +0200 | [diff] [blame] | 763 | |
Morten Krogh-Jespersen | f4a8f76 | 2021-12-22 12:31:21 +0100 | [diff] [blame] | 764 | def compute_size_of_dex_files(dex_files): |
| 765 | dex_size = 0 |
| 766 | for dex_file in dex_files: |
| 767 | dex_size += os.path.getsize(dex_file) |
| 768 | return dex_size |
| 769 | |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 770 | if __name__ == '__main__': |
Tamas Kenez | 5b1c585 | 2017-07-21 13:38:33 +0200 | [diff] [blame] | 771 | sys.exit(main(sys.argv[1:])) |