Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 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 |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 11 | import sys |
Tamas Kenez | f2ee2a3 | 2017-06-21 10:30:20 +0200 | [diff] [blame] | 12 | import time |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 13 | |
Søren Gjesse | 5ecb04a | 2017-06-13 09:44:32 +0200 | [diff] [blame] | 14 | import gmail_data |
Ian Zerny | 877c186 | 2017-07-06 11:12:26 +0200 | [diff] [blame] | 15 | import gmscore_data |
Rico Wind | 1f4172c | 2018-09-06 16:29:03 +0200 | [diff] [blame] | 16 | import golem |
Christoffer Quist Adamsen | a2a5877 | 2018-10-03 09:47:46 +0200 | [diff] [blame] | 17 | import nest_data |
Mathias Rav | dd6a6de | 2018-05-18 10:18:33 +0200 | [diff] [blame] | 18 | import toolhelper |
Ian Zerny | 877c186 | 2017-07-06 11:12:26 +0200 | [diff] [blame] | 19 | import utils |
| 20 | import youtube_data |
Rico Wind | 86bfc83 | 2018-09-18 07:48:21 +0200 | [diff] [blame] | 21 | import chrome_data |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 22 | |
| 23 | TYPES = ['dex', 'deploy', 'proguarded'] |
Christoffer Quist Adamsen | a2a5877 | 2018-10-03 09:47:46 +0200 | [diff] [blame] | 24 | APPS = ['gmscore', 'nest', 'youtube', 'gmail', 'chrome'] |
Tamas Kenez | 63a51d0 | 2019-01-07 15:53:02 +0100 | [diff] [blame] | 25 | COMPILERS = ['d8', 'r8'] |
| 26 | COMPILER_BUILDS = ['full', 'lib'] |
| 27 | |
Rico Wind | 5fdec15 | 2018-12-17 09:16:14 +0100 | [diff] [blame] | 28 | # We use this magic exit code to signal that the program OOM'ed |
| 29 | OOM_EXIT_CODE = 42 |
Jinseong Jeon | 158a3f1 | 2019-02-08 01:40:59 -0800 | [diff] [blame] | 30 | # According to Popen.returncode doc: |
| 31 | # A negative value -N indicates that the child was terminated by signal N. |
| 32 | TIMEOUT_KILL_CODE = -9 |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 33 | |
Tamas Kenez | 5b1c585 | 2017-07-21 13:38:33 +0200 | [diff] [blame] | 34 | def ParseOptions(argv): |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 35 | result = optparse.OptionParser() |
Søren Gjesse | 932881f | 2017-06-13 10:43:36 +0200 | [diff] [blame] | 36 | result.add_option('--compiler', |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 37 | help='The compiler to use', |
Tamas Kenez | 5b1c585 | 2017-07-21 13:38:33 +0200 | [diff] [blame] | 38 | choices=COMPILERS) |
Tamas Kenez | 63a51d0 | 2019-01-07 15:53:02 +0100 | [diff] [blame] | 39 | result.add_option('--compiler-build', |
| 40 | help='Compiler build to use', |
| 41 | choices=COMPILER_BUILDS, |
| 42 | default='lib') |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 43 | result.add_option('--app', |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 44 | help='What app to run on', |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 45 | choices=APPS) |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 46 | result.add_option('--run-all', |
| 47 | help='Compile all possible combinations', |
| 48 | default=False, |
| 49 | action='store_true') |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 50 | result.add_option('--type', |
Tamas Kenez | 3fdaabd | 2017-06-15 13:05:12 +0200 | [diff] [blame] | 51 | help='Default for R8: deploy, for D8: proguarded', |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 52 | choices=TYPES) |
| 53 | result.add_option('--out', |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 54 | help='Where to place the output', |
Rico Wind | e9485ba | 2018-10-01 07:04:16 +0200 | [diff] [blame] | 55 | default=utils.BUILD) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 56 | result.add_option('--no-build', |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 57 | help='Run without building first', |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 58 | default=False, |
| 59 | action='store_true') |
Rico Wind | 5fdec15 | 2018-12-17 09:16:14 +0100 | [diff] [blame] | 60 | result.add_option('--find-min-xmx', |
| 61 | help='Find the minimum amount of memory we can run in', |
| 62 | default=False, |
| 63 | action='store_true') |
Jinseong Jeon | 158a3f1 | 2019-02-08 01:40:59 -0800 | [diff] [blame] | 64 | result.add_option('--timeout', |
| 65 | type=int, |
| 66 | default=0, |
| 67 | help='Set timeout instead of waiting for OOM.') |
Rico Wind | 1f4172c | 2018-09-06 16:29:03 +0200 | [diff] [blame] | 68 | result.add_option('--golem', |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 69 | help='Running on golem, do not build or download', |
Rico Wind | 1f4172c | 2018-09-06 16:29:03 +0200 | [diff] [blame] | 70 | default=False, |
| 71 | action='store_true') |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 72 | result.add_option('--ignore-java-version', |
| 73 | help='Do not check java version', |
| 74 | default=False, |
| 75 | action='store_true') |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 76 | result.add_option('--no-libraries', |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 77 | help='Do not pass in libraries, even if they exist in conf', |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 78 | default=False, |
| 79 | action='store_true') |
| 80 | result.add_option('--no-debug', |
| 81 | help='Run without debug asserts.', |
| 82 | default=False, |
| 83 | action='store_true') |
| 84 | result.add_option('--version', |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 85 | help='The version of the app to run') |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 86 | result.add_option('-k', |
| 87 | help='Override the default ProGuard keep rules') |
Tamas Kenez | 139acc1 | 2017-06-14 17:14:58 +0200 | [diff] [blame] | 88 | result.add_option('--compiler-flags', |
| 89 | help='Additional option(s) for the compiler. ' + |
| 90 | 'If passing several options use a quoted string.') |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 91 | result.add_option('--r8-flags', |
Tamas Kenez | 139acc1 | 2017-06-14 17:14:58 +0200 | [diff] [blame] | 92 | help='Additional option(s) for the compiler. ' + |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 93 | 'Same as --compiler-flags, keeping it for backward' |
| 94 | ' compatibility. ' + |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 95 | 'If passing several options use a quoted string.') |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 96 | # TODO(tamaskenez) remove track-memory-to-file as soon as we updated golem |
| 97 | # to use --print-memoryuse instead |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 98 | result.add_option('--track-memory-to-file', |
| 99 | help='Track how much memory the jvm is using while ' + |
| 100 | ' compiling. Output to the specified file.') |
| 101 | result.add_option('--profile', |
| 102 | help='Profile R8 run.', |
| 103 | default=False, |
| 104 | action='store_true') |
| 105 | result.add_option('--dump-args-file', |
| 106 | help='Dump a file with the arguments for the specified ' + |
| 107 | 'configuration. For use as a @<file> argument to perform ' + |
| 108 | 'the run.') |
Tamas Kenez | f2ee2a3 | 2017-06-21 10:30:20 +0200 | [diff] [blame] | 109 | result.add_option('--print-runtimeraw', |
| 110 | metavar='BENCHMARKNAME', |
Tamas Kenez | 02bff03 | 2017-07-18 12:13:58 +0200 | [diff] [blame] | 111 | help='Print the line \'<BENCHMARKNAME>(RunTimeRaw):' + |
| 112 | ' <elapsed> ms\' at the end where <elapsed> is' + |
| 113 | ' the elapsed time in milliseconds.') |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 114 | result.add_option('--print-memoryuse', |
| 115 | metavar='BENCHMARKNAME', |
Tamas Kenez | 02bff03 | 2017-07-18 12:13:58 +0200 | [diff] [blame] | 116 | help='Print the line \'<BENCHMARKNAME>(MemoryUse):' + |
| 117 | ' <mem>\' at the end where <mem> is the peak' + |
| 118 | ' peak resident set size (VmHWM) in bytes.') |
| 119 | result.add_option('--print-dexsegments', |
| 120 | metavar='BENCHMARKNAME', |
| 121 | help='Print the sizes of individual dex segments as ' + |
| 122 | '\'<BENCHMARKNAME>-<segment>(CodeSize): <bytes>\'') |
Tamas Kenez | 5b1c585 | 2017-07-21 13:38:33 +0200 | [diff] [blame] | 123 | return result.parse_args(argv) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 124 | |
Man Cao | 29b9ef1 | 2019-03-25 11:19:35 -0700 | [diff] [blame] | 125 | # Most apps have -printmapping, -printseeds, -printusage and |
| 126 | # -printconfiguration in the Proguard configuration. However we don't |
| 127 | # want to write these files in the locations specified. |
| 128 | # Instead generate an auxiliary Proguard configuration placing these |
| 129 | # output files together with the dex output. |
Søren Gjesse | 3a5aed9 | 2017-06-14 15:36:02 +0200 | [diff] [blame] | 130 | def GenerateAdditionalProguardConfiguration(temp, outdir): |
| 131 | name = "output.config" |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 132 | with open(os.path.join(temp, name), 'w') as f: |
| 133 | f.write('-printmapping ' + os.path.join(outdir, 'proguard.map') + "\n") |
| 134 | f.write('-printseeds ' + os.path.join(outdir, 'proguard.seeds') + "\n") |
Søren Gjesse | 5945958 | 2018-04-06 10:12:45 +0200 | [diff] [blame] | 135 | f.write('-printusage ' + os.path.join(outdir, 'proguard.usage') + "\n") |
Man Cao | 29b9ef1 | 2019-03-25 11:19:35 -0700 | [diff] [blame] | 136 | f.write('-printconfiguration ' + os.path.join(outdir, 'proguard.config') + "\n") |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 137 | return os.path.abspath(f.name) |
Søren Gjesse | 3a5aed9 | 2017-06-14 15:36:02 +0200 | [diff] [blame] | 138 | |
Rico Wind | 3f9302b | 2018-09-21 08:53:09 +0200 | [diff] [blame] | 139 | # Please add bug number for disabled permutations and please explicitly |
| 140 | # do Bug: #BUG in the commit message of disabling to ensure re-enabling |
| 141 | DISABLED_PERMUTATIONS = [ |
Ian Zerny | d459b5f | 2018-10-03 06:46:21 +0200 | [diff] [blame] | 142 | # (app, version, type), e.g., ('gmail', '180826.15', 'deploy'), |
Rico Wind | ed78833 | 2018-12-14 10:39:20 +0100 | [diff] [blame] | 143 | ('youtube', '13.37', 'deploy'), # b/120977564 |
Rico Wind | 3f9302b | 2018-09-21 08:53:09 +0200 | [diff] [blame] | 144 | ] |
| 145 | |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 146 | def get_permutations(): |
| 147 | data_providers = { |
| 148 | 'gmscore': gmscore_data, |
Christoffer Quist Adamsen | a2a5877 | 2018-10-03 09:47:46 +0200 | [diff] [blame] | 149 | 'nest': nest_data, |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 150 | 'youtube': youtube_data, |
| 151 | 'chrome': chrome_data, |
| 152 | 'gmail': gmail_data |
| 153 | } |
| 154 | # Check to ensure that we add all variants here. |
| 155 | assert len(APPS) == len(data_providers) |
| 156 | for app, data in data_providers.iteritems(): |
| 157 | for version in data.VERSIONS: |
| 158 | for type in data.VERSIONS[version]: |
Rico Wind | 3f9302b | 2018-09-21 08:53:09 +0200 | [diff] [blame] | 159 | if (app, version, type) not in DISABLED_PERMUTATIONS: |
Tamas Kenez | a730a7e | 2018-12-10 15:02:28 +0100 | [diff] [blame] | 160 | for use_r8lib in [False, True]: |
| 161 | yield app, version, type, use_r8lib |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 162 | |
| 163 | def run_all(options, args): |
| 164 | # Args will be destroyed |
| 165 | assert len(args) == 0 |
Tamas Kenez | a730a7e | 2018-12-10 15:02:28 +0100 | [diff] [blame] | 166 | for name, version, type, use_r8lib in get_permutations(): |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 167 | compiler = 'r8' if type == 'deploy' else 'd8' |
Tamas Kenez | 63a51d0 | 2019-01-07 15:53:02 +0100 | [diff] [blame] | 168 | compiler_build = 'lib' if use_r8lib else 'full' |
| 169 | print('Executing %s/%s with %s %s %s' % (compiler, compiler_build, name, |
| 170 | version, type)) |
Tamas Kenez | a730a7e | 2018-12-10 15:02:28 +0100 | [diff] [blame] | 171 | |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 172 | fixed_options = copy.copy(options) |
| 173 | fixed_options.app = name |
| 174 | fixed_options.version = version |
| 175 | fixed_options.compiler = compiler |
Tamas Kenez | 63a51d0 | 2019-01-07 15:53:02 +0100 | [diff] [blame] | 176 | fixed_options.compiler_build = compiler_build |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 177 | fixed_options.type = type |
| 178 | exit_code = run_with_options(fixed_options, []) |
| 179 | if exit_code != 0: |
Tamas Kenez | 63a51d0 | 2019-01-07 15:53:02 +0100 | [diff] [blame] | 180 | print('Failed %s %s %s with %s/%s' % (name, version, type, compiler, |
| 181 | compiler_build)) |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 182 | exit(exit_code) |
| 183 | |
Rico Wind | 5fdec15 | 2018-12-17 09:16:14 +0100 | [diff] [blame] | 184 | def find_min_xmx(options, args): |
| 185 | # Args will be destroyed |
| 186 | assert len(args) == 0 |
| 187 | # If we can run in 128 MB then we are good (which we can for small examples |
| 188 | # or D8 on medium sized examples) |
Jinseong Jeon | 158a3f1 | 2019-02-08 01:40:59 -0800 | [diff] [blame] | 189 | not_working = 128 if options.compiler == 'd8' else 1024 |
Rico Wind | 5fdec15 | 2018-12-17 09:16:14 +0100 | [diff] [blame] | 190 | working = 1024 * 8 |
| 191 | exit_code = 0 |
| 192 | while working - not_working > 32: |
| 193 | next_candidate = working - ((working - not_working)/2) |
| 194 | print('working: %s, non_working: %s, next_candidate: %s' % |
| 195 | (working, not_working, next_candidate)) |
| 196 | extra_args = ['-Xmx%sM' % next_candidate] |
| 197 | new_options = copy.copy(options) |
| 198 | t0 = time.time() |
| 199 | exit_code = run_with_options(options, [], extra_args) |
| 200 | t1 = time.time() |
| 201 | print('Running took: %s ms' % (1000.0 * (t1 - t0))) |
Jinseong Jeon | 158a3f1 | 2019-02-08 01:40:59 -0800 | [diff] [blame] | 202 | if exit_code != 0: |
| 203 | if exit_code not in [OOM_EXIT_CODE, TIMEOUT_KILL_CODE]: |
| 204 | print('Non OOM/Timeout error executing, exiting') |
| 205 | return 2 |
Rico Wind | 5fdec15 | 2018-12-17 09:16:14 +0100 | [diff] [blame] | 206 | if exit_code == 0: |
| 207 | working = next_candidate |
Jinseong Jeon | 158a3f1 | 2019-02-08 01:40:59 -0800 | [diff] [blame] | 208 | elif exit_code == TIMEOUT_KILL_CODE: |
| 209 | print('Timeout. Continue to the next candidate.') |
| 210 | not_working = next_candidate |
Rico Wind | 5fdec15 | 2018-12-17 09:16:14 +0100 | [diff] [blame] | 211 | else: |
| 212 | assert exit_code == OOM_EXIT_CODE |
| 213 | not_working = next_candidate |
| 214 | |
| 215 | assert working - not_working <= 32 |
| 216 | print('Found range: %s - %s' % (not_working, working)) |
| 217 | return 0 |
| 218 | |
Tamas Kenez | 5b1c585 | 2017-07-21 13:38:33 +0200 | [diff] [blame] | 219 | def main(argv): |
Tamas Kenez | 5b1c585 | 2017-07-21 13:38:33 +0200 | [diff] [blame] | 220 | (options, args) = ParseOptions(argv) |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 221 | if options.run_all: |
| 222 | return run_all(options, args) |
Rico Wind | 5fdec15 | 2018-12-17 09:16:14 +0100 | [diff] [blame] | 223 | if options.find_min_xmx: |
| 224 | return find_min_xmx(options, args) |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 225 | return run_with_options(options, args) |
| 226 | |
Tamas Kenez | 63a51d0 | 2019-01-07 15:53:02 +0100 | [diff] [blame] | 227 | def run_with_options(options, args, extra_args=None): |
| 228 | if extra_args is None: |
| 229 | extra_args = [] |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 230 | app_provided_pg_conf = False; |
Rico Wind | add0813 | 2018-12-14 14:17:15 +0100 | [diff] [blame] | 231 | # todo(121018500): remove when memory is under control |
Rico Wind | 20602b7 | 2019-01-09 09:17:13 +0100 | [diff] [blame] | 232 | if not any('-Xmx' in arg for arg in extra_args): |
| 233 | extra_args.append('-Xmx8G') |
Rico Wind | 1f4172c | 2018-09-06 16:29:03 +0200 | [diff] [blame] | 234 | if options.golem: |
| 235 | golem.link_third_party() |
Rico Wind | 9bd3f5f | 2018-10-01 10:53:18 +0200 | [diff] [blame] | 236 | options.out = os.getcwd() |
Rico Wind | afdbbfd | 2019-02-22 09:32:07 +0100 | [diff] [blame] | 237 | if not options.ignore_java_version: |
| 238 | utils.check_java_version() |
| 239 | |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 240 | outdir = options.out |
| 241 | data = None |
| 242 | if options.app == 'gmscore': |
| 243 | options.version = options.version or 'v9' |
| 244 | data = gmscore_data |
Christoffer Quist Adamsen | a2a5877 | 2018-10-03 09:47:46 +0200 | [diff] [blame] | 245 | elif options.app == 'nest': |
| 246 | options.version = options.version or '20180926' |
| 247 | data = nest_data |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 248 | elif options.app == 'youtube': |
Søren Gjesse | cc33fb4 | 2017-06-09 10:25:08 +0200 | [diff] [blame] | 249 | options.version = options.version or '12.22' |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 250 | data = youtube_data |
Rico Wind | 86bfc83 | 2018-09-18 07:48:21 +0200 | [diff] [blame] | 251 | elif options.app == 'chrome': |
| 252 | options.version = options.version or 'default' |
| 253 | data = chrome_data |
Søren Gjesse | 5ecb04a | 2017-06-13 09:44:32 +0200 | [diff] [blame] | 254 | elif options.app == 'gmail': |
| 255 | options.version = options.version or '170604.16' |
| 256 | data = gmail_data |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 257 | else: |
Tamas Kenez | 5b1c585 | 2017-07-21 13:38:33 +0200 | [diff] [blame] | 258 | raise Exception("You need to specify '--app={}'".format('|'.join(APPS))) |
| 259 | |
| 260 | if options.compiler not in COMPILERS: |
| 261 | raise Exception("You need to specify '--compiler={}'" |
| 262 | .format('|'.join(COMPILERS))) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 263 | |
Tamas Kenez | 63a51d0 | 2019-01-07 15:53:02 +0100 | [diff] [blame] | 264 | if options.compiler_build not in COMPILER_BUILDS: |
| 265 | raise Exception("You need to specify '--compiler-build={}'" |
| 266 | .format('|'.join(COMPILER_BUILDS))) |
| 267 | |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 268 | if not options.version in data.VERSIONS.keys(): |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 269 | print('No version {} for application {}' |
| 270 | .format(options.version, options.app)) |
| 271 | print('Valid versions are {}'.format(data.VERSIONS.keys())) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 272 | return 1 |
| 273 | |
| 274 | version = data.VERSIONS[options.version] |
| 275 | |
Tamas Kenez | 3fdaabd | 2017-06-15 13:05:12 +0200 | [diff] [blame] | 276 | if not options.type: |
Tamas Kenez | 63a51d0 | 2019-01-07 15:53:02 +0100 | [diff] [blame] | 277 | options.type = 'deploy' if options.compiler == 'r8' \ |
Tamas Kenez | 3fdaabd | 2017-06-15 13:05:12 +0200 | [diff] [blame] | 278 | else 'proguarded' |
| 279 | |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 280 | if options.type not in version: |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 281 | print('No type {} for version {}'.format(options.type, options.version)) |
| 282 | print('Valid types are {}'.format(version.keys())) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 283 | return 1 |
| 284 | values = version[options.type] |
| 285 | inputs = None |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 286 | # For R8 'deploy' the JAR is located using the Proguard configuration |
Christoffer Quist Adamsen | a2a5877 | 2018-10-03 09:47:46 +0200 | [diff] [blame] | 287 | # -injars option. For chrome and nest we don't have the injars in the |
| 288 | # proguard files. |
Tamas Kenez | 63a51d0 | 2019-01-07 15:53:02 +0100 | [diff] [blame] | 289 | if 'inputs' in values and (options.compiler != 'r8' |
Rico Wind | 86bfc83 | 2018-09-18 07:48:21 +0200 | [diff] [blame] | 290 | or options.type != 'deploy' |
Christoffer Quist Adamsen | a2a5877 | 2018-10-03 09:47:46 +0200 | [diff] [blame] | 291 | or options.app == 'chrome' |
| 292 | or options.app == 'nest'): |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 293 | inputs = values['inputs'] |
| 294 | |
| 295 | args.extend(['--output', outdir]) |
Ian Zerny | 877c186 | 2017-07-06 11:12:26 +0200 | [diff] [blame] | 296 | if 'min-api' in values: |
| 297 | args.extend(['--min-api', values['min-api']]) |
Søren Gjesse | 932881f | 2017-06-13 10:43:36 +0200 | [diff] [blame] | 298 | |
Søren Gjesse | 8ae55eb | 2018-09-28 11:11:36 +0200 | [diff] [blame] | 299 | if 'main-dex-list' in values: |
| 300 | args.extend(['--main-dex-list', values['main-dex-list']]) |
| 301 | |
Tamas Kenez | 63a51d0 | 2019-01-07 15:53:02 +0100 | [diff] [blame] | 302 | if options.compiler == 'r8': |
Søren Gjesse | 932881f | 2017-06-13 10:43:36 +0200 | [diff] [blame] | 303 | if 'pgconf' in values and not options.k: |
Søren Gjesse | 7464530 | 2019-04-12 12:53:50 +0000 | [diff] [blame] | 304 | for pgconf in values['pgconf']: |
| 305 | args.extend(['--pg-conf', pgconf]) |
| 306 | app_provided_pg_conf = True |
Søren Gjesse | 932881f | 2017-06-13 10:43:36 +0200 | [diff] [blame] | 307 | if options.k: |
| 308 | args.extend(['--pg-conf', options.k]) |
Søren Gjesse | c801ecc | 2017-08-03 13:40:06 +0200 | [diff] [blame] | 309 | if 'maindexrules' in values: |
| 310 | for rules in values['maindexrules']: |
| 311 | args.extend(['--main-dex-rules', rules]) |
Rico Wind | 79e4eb5 | 2018-12-13 13:00:49 +0100 | [diff] [blame] | 312 | if 'allow-type-errors' in values: |
| 313 | extra_args.append('-Dcom.android.tools.r8.allowTypeErrors=1') |
Søren Gjesse | 932881f | 2017-06-13 10:43:36 +0200 | [diff] [blame] | 314 | |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 315 | if not options.no_libraries and 'libraries' in values: |
| 316 | for lib in values['libraries']: |
| 317 | args.extend(['--lib', lib]) |
| 318 | |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 319 | if not outdir.endswith('.zip') and not outdir.endswith('.jar') \ |
| 320 | and not os.path.exists(outdir): |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 321 | os.makedirs(outdir) |
| 322 | |
Søren Gjesse | 8ae55eb | 2018-09-28 11:11:36 +0200 | [diff] [blame] | 323 | # Additional flags for the compiler from the configuration file. |
| 324 | if 'flags' in values: |
| 325 | args.extend(values['flags'].split(' ')) |
Tamas Kenez | 63a51d0 | 2019-01-07 15:53:02 +0100 | [diff] [blame] | 326 | if options.compiler == 'r8': |
Søren Gjesse | 932881f | 2017-06-13 10:43:36 +0200 | [diff] [blame] | 327 | if 'r8-flags' in values: |
| 328 | args.extend(values['r8-flags'].split(' ')) |
Tamas Kenez | 139acc1 | 2017-06-14 17:14:58 +0200 | [diff] [blame] | 329 | |
Søren Gjesse | 8ae55eb | 2018-09-28 11:11:36 +0200 | [diff] [blame] | 330 | # Additional flags for the compiler from the command line. |
Tamas Kenez | 139acc1 | 2017-06-14 17:14:58 +0200 | [diff] [blame] | 331 | if options.compiler_flags: |
| 332 | args.extend(options.compiler_flags.split(' ')) |
| 333 | if options.r8_flags: |
| 334 | args.extend(options.r8_flags.split(' ')) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 335 | |
| 336 | if inputs: |
| 337 | args.extend(inputs) |
| 338 | |
Tamas Kenez | f2ee2a3 | 2017-06-21 10:30:20 +0200 | [diff] [blame] | 339 | t0 = time.time() |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 340 | if options.dump_args_file: |
| 341 | with open(options.dump_args_file, 'w') as args_file: |
| 342 | args_file.writelines([arg + os.linesep for arg in args]) |
| 343 | else: |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 344 | with utils.TempDir() as temp: |
| 345 | if options.print_memoryuse and not options.track_memory_to_file: |
| 346 | options.track_memory_to_file = os.path.join(temp, |
| 347 | utils.MEMORY_USE_TMP_FILE) |
Tamas Kenez | 63a51d0 | 2019-01-07 15:53:02 +0100 | [diff] [blame] | 348 | if options.compiler == 'r8' and app_provided_pg_conf: |
Mathias Rav | dd6a6de | 2018-05-18 10:18:33 +0200 | [diff] [blame] | 349 | # Ensure that output of -printmapping and -printseeds go to the output |
| 350 | # location and not where the app Proguard configuration places them. |
| 351 | if outdir.endswith('.zip') or outdir.endswith('.jar'): |
| 352 | pg_outdir = os.path.dirname(outdir) |
| 353 | else: |
| 354 | pg_outdir = outdir |
| 355 | additional_pg_conf = GenerateAdditionalProguardConfiguration( |
| 356 | temp, os.path.abspath(pg_outdir)) |
| 357 | args.extend(['--pg-conf', additional_pg_conf]) |
Rico Wind | 1f4172c | 2018-09-06 16:29:03 +0200 | [diff] [blame] | 358 | build = not options.no_build and not options.golem |
Rico Wind | 5fdec15 | 2018-12-17 09:16:14 +0100 | [diff] [blame] | 359 | stderr_path = os.path.join(temp, 'stderr') |
| 360 | with open(stderr_path, 'w') as stderr: |
Tamas Kenez | 63a51d0 | 2019-01-07 15:53:02 +0100 | [diff] [blame] | 361 | if options.compiler_build == 'full': |
| 362 | tool = options.compiler |
| 363 | else: |
| 364 | assert(options.compiler_build == 'lib') |
| 365 | tool = 'r8lib-' + options.compiler |
| 366 | exit_code = toolhelper.run(tool, args, |
Rico Wind | 5fdec15 | 2018-12-17 09:16:14 +0100 | [diff] [blame] | 367 | build=build, |
| 368 | debug=not options.no_debug, |
| 369 | profile=options.profile, |
| 370 | track_memory_file=options.track_memory_to_file, |
Jinseong Jeon | 158a3f1 | 2019-02-08 01:40:59 -0800 | [diff] [blame] | 371 | extra_args=extra_args, |
| 372 | stderr=stderr, |
| 373 | timeout=options.timeout) |
Christoffer Quist Adamsen | 21c6660 | 2018-08-09 16:22:54 +0200 | [diff] [blame] | 374 | if exit_code != 0: |
Rico Wind | 5fdec15 | 2018-12-17 09:16:14 +0100 | [diff] [blame] | 375 | with open(stderr_path) as stderr: |
| 376 | stderr_text = stderr.read() |
| 377 | print(stderr_text) |
| 378 | if 'java.lang.OutOfMemoryError' in stderr_text: |
| 379 | print('Failure was OOM') |
| 380 | return OOM_EXIT_CODE |
| 381 | return exit_code |
Christoffer Quist Adamsen | 21c6660 | 2018-08-09 16:22:54 +0200 | [diff] [blame] | 382 | |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 383 | if options.print_memoryuse: |
| 384 | print('{}(MemoryUse): {}' |
| 385 | .format(options.print_memoryuse, |
| 386 | utils.grep_memoryuse(options.track_memory_to_file))) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 387 | |
Tamas Kenez | f2ee2a3 | 2017-06-21 10:30:20 +0200 | [diff] [blame] | 388 | if options.print_runtimeraw: |
| 389 | print('{}(RunTimeRaw): {} ms' |
| 390 | .format(options.print_runtimeraw, 1000.0 * (time.time() - t0))) |
| 391 | |
Tamas Kenez | 02bff03 | 2017-07-18 12:13:58 +0200 | [diff] [blame] | 392 | if options.print_dexsegments: |
| 393 | dex_files = glob(os.path.join(outdir, '*.dex')) |
| 394 | utils.print_dexsegments(options.print_dexsegments, dex_files) |
Rico Wind | b57bbc1 | 2018-09-20 19:23:32 +0200 | [diff] [blame] | 395 | return 0 |
Tamas Kenez | 02bff03 | 2017-07-18 12:13:58 +0200 | [diff] [blame] | 396 | |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 397 | if __name__ == '__main__': |
Tamas Kenez | 5b1c585 | 2017-07-21 13:38:33 +0200 | [diff] [blame] | 398 | sys.exit(main(sys.argv[1:])) |