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