blob: d93e3c056ff00836db24d8c66b7676c50a96e05d [file] [log] [blame]
Mads Ager418d1ca2017-05-22 09:35:49 +02001#!/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 Kenezfc34cd82017-07-13 12:43:57 +02006from __future__ import print_function
Tamas Kenez02bff032017-07-18 12:13:58 +02007from glob import glob
Rico Windb57bbc12018-09-20 19:23:32 +02008import copy
Mads Ager418d1ca2017-05-22 09:35:49 +02009import optparse
10import os
Mads Ager418d1ca2017-05-22 09:35:49 +020011import sys
Tamas Kenezf2ee2a32017-06-21 10:30:20 +020012import time
Mads Ager418d1ca2017-05-22 09:35:49 +020013
Søren Gjesse5ecb04a2017-06-13 09:44:32 +020014import gmail_data
Ian Zerny877c1862017-07-06 11:12:26 +020015import gmscore_data
Rico Wind1f4172c2018-09-06 16:29:03 +020016import golem
Christoffer Quist Adamsena2a58772018-10-03 09:47:46 +020017import nest_data
Søren Gjessecbeae782019-05-21 14:14:25 +020018from sanitize_libraries import SanitizeLibraries
Mathias Ravdd6a6de2018-05-18 10:18:33 +020019import toolhelper
Ian Zerny877c1862017-07-06 11:12:26 +020020import utils
21import youtube_data
Rico Wind86bfc832018-09-18 07:48:21 +020022import chrome_data
Morten Krogh-Jespersen04c7c302019-10-01 15:04:33 +020023import r8_data
Morten Krogh-Jespersencd6712c2019-10-09 13:09:47 +020024import iosched_data
Mads Ager418d1ca2017-05-22 09:35:49 +020025
26TYPES = ['dex', 'deploy', 'proguarded']
Morten Krogh-Jespersencd6712c2019-10-09 13:09:47 +020027APPS = ['gmscore', 'nest', 'youtube', 'gmail', 'chrome', 'r8', 'iosched']
Tamas Kenez63a51d02019-01-07 15:53:02 +010028COMPILERS = ['d8', 'r8']
29COMPILER_BUILDS = ['full', 'lib']
30
Rico Wind5fdec152018-12-17 09:16:14 +010031# We use this magic exit code to signal that the program OOM'ed
32OOM_EXIT_CODE = 42
Jinseong Jeon158a3f12019-02-08 01:40:59 -080033# According to Popen.returncode doc:
34# A negative value -N indicates that the child was terminated by signal N.
35TIMEOUT_KILL_CODE = -9
Mads Ager418d1ca2017-05-22 09:35:49 +020036
Morten Krogh-Jespersen0981b722019-10-09 10:00:33 +020037# Log file names
38FIND_MIN_XMX_FILE = 'find_min_xmx_results'
39FIND_MIN_XMX_DIR = 'find_min_xmx'
40
Tamas Kenez5b1c5852017-07-21 13:38:33 +020041def ParseOptions(argv):
Mads Ager418d1ca2017-05-22 09:35:49 +020042 result = optparse.OptionParser()
Søren Gjesse932881f2017-06-13 10:43:36 +020043 result.add_option('--compiler',
Rico Windb57bbc12018-09-20 19:23:32 +020044 help='The compiler to use',
Tamas Kenez5b1c5852017-07-21 13:38:33 +020045 choices=COMPILERS)
Tamas Kenez63a51d02019-01-07 15:53:02 +010046 result.add_option('--compiler-build',
47 help='Compiler build to use',
48 choices=COMPILER_BUILDS,
49 default='lib')
Mads Ager418d1ca2017-05-22 09:35:49 +020050 result.add_option('--app',
Rico Windb57bbc12018-09-20 19:23:32 +020051 help='What app to run on',
Mads Ager418d1ca2017-05-22 09:35:49 +020052 choices=APPS)
Rico Windb57bbc12018-09-20 19:23:32 +020053 result.add_option('--run-all',
54 help='Compile all possible combinations',
55 default=False,
56 action='store_true')
Morten Krogh-Jespersenb7e1a182019-10-09 13:04:54 +020057 result.add_option('--expect-oom',
58 help='Expect that compilation will fail with an OOM',
59 default=False,
60 action='store_true')
Mads Ager418d1ca2017-05-22 09:35:49 +020061 result.add_option('--type',
Tamas Kenez3fdaabd2017-06-15 13:05:12 +020062 help='Default for R8: deploy, for D8: proguarded',
Mads Ager418d1ca2017-05-22 09:35:49 +020063 choices=TYPES)
64 result.add_option('--out',
Rico Windb57bbc12018-09-20 19:23:32 +020065 help='Where to place the output',
Rico Winde9485ba2018-10-01 07:04:16 +020066 default=utils.BUILD)
Mads Ager418d1ca2017-05-22 09:35:49 +020067 result.add_option('--no-build',
Rico Windb57bbc12018-09-20 19:23:32 +020068 help='Run without building first',
Mads Ager418d1ca2017-05-22 09:35:49 +020069 default=False,
70 action='store_true')
Morten Krogh-Jespersen322c2f12019-10-08 10:41:21 +020071 result.add_option('--max-memory',
72 help='The maximum memory in MB to run with',
73 type='int')
Rico Wind5fdec152018-12-17 09:16:14 +010074 result.add_option('--find-min-xmx',
75 help='Find the minimum amount of memory we can run in',
76 default=False,
77 action='store_true')
Morten Krogh-Jespersen04c7c302019-10-01 15:04:33 +020078 result.add_option('--find-min-xmx-min-memory',
79 help='Setting the minimum memory baseline to run in',
80 type='int')
81 result.add_option('--find-min-xmx-max-memory',
82 help='Setting the maximum memory baseline to run in',
83 type='int')
84 result.add_option('--find-min-xmx-range-size',
85 help='Setting the size of the acceptable memory range',
86 type='int',
87 default=32)
Morten Krogh-Jespersen0981b722019-10-09 10:00:33 +020088 result.add_option('--find-min-xmx-archive',
89 help='Archive find-min-xmx results on GCS',
90 default=False,
91 action='store_true')
Jinseong Jeon158a3f12019-02-08 01:40:59 -080092 result.add_option('--timeout',
Morten Krogh-Jespersen0981b722019-10-09 10:00:33 +020093 type='int',
Jinseong Jeon158a3f12019-02-08 01:40:59 -080094 default=0,
95 help='Set timeout instead of waiting for OOM.')
Rico Wind1f4172c2018-09-06 16:29:03 +020096 result.add_option('--golem',
Rico Windb57bbc12018-09-20 19:23:32 +020097 help='Running on golem, do not build or download',
Rico Wind1f4172c2018-09-06 16:29:03 +020098 default=False,
99 action='store_true')
Rico Wind139eece2018-09-25 09:42:09 +0200100 result.add_option('--ignore-java-version',
101 help='Do not check java version',
102 default=False,
103 action='store_true')
Mads Ager418d1ca2017-05-22 09:35:49 +0200104 result.add_option('--no-libraries',
Rico Windb57bbc12018-09-20 19:23:32 +0200105 help='Do not pass in libraries, even if they exist in conf',
Mads Ager418d1ca2017-05-22 09:35:49 +0200106 default=False,
107 action='store_true')
108 result.add_option('--no-debug',
109 help='Run without debug asserts.',
110 default=False,
111 action='store_true')
112 result.add_option('--version',
Rico Windb57bbc12018-09-20 19:23:32 +0200113 help='The version of the app to run')
Mads Ager418d1ca2017-05-22 09:35:49 +0200114 result.add_option('-k',
115 help='Override the default ProGuard keep rules')
Tamas Kenez139acc12017-06-14 17:14:58 +0200116 result.add_option('--compiler-flags',
117 help='Additional option(s) for the compiler. ' +
118 'If passing several options use a quoted string.')
Mads Ager418d1ca2017-05-22 09:35:49 +0200119 result.add_option('--r8-flags',
Tamas Kenez139acc12017-06-14 17:14:58 +0200120 help='Additional option(s) for the compiler. ' +
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200121 'Same as --compiler-flags, keeping it for backward'
122 ' compatibility. ' +
Mads Ager418d1ca2017-05-22 09:35:49 +0200123 'If passing several options use a quoted string.')
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200124 # TODO(tamaskenez) remove track-memory-to-file as soon as we updated golem
125 # to use --print-memoryuse instead
Mads Ager418d1ca2017-05-22 09:35:49 +0200126 result.add_option('--track-memory-to-file',
127 help='Track how much memory the jvm is using while ' +
128 ' compiling. Output to the specified file.')
129 result.add_option('--profile',
130 help='Profile R8 run.',
131 default=False,
132 action='store_true')
133 result.add_option('--dump-args-file',
134 help='Dump a file with the arguments for the specified ' +
135 'configuration. For use as a @<file> argument to perform ' +
136 'the run.')
Tamas Kenezf2ee2a32017-06-21 10:30:20 +0200137 result.add_option('--print-runtimeraw',
138 metavar='BENCHMARKNAME',
Tamas Kenez02bff032017-07-18 12:13:58 +0200139 help='Print the line \'<BENCHMARKNAME>(RunTimeRaw):' +
140 ' <elapsed> ms\' at the end where <elapsed> is' +
141 ' the elapsed time in milliseconds.')
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200142 result.add_option('--print-memoryuse',
143 metavar='BENCHMARKNAME',
Tamas Kenez02bff032017-07-18 12:13:58 +0200144 help='Print the line \'<BENCHMARKNAME>(MemoryUse):' +
145 ' <mem>\' at the end where <mem> is the peak' +
146 ' peak resident set size (VmHWM) in bytes.')
147 result.add_option('--print-dexsegments',
148 metavar='BENCHMARKNAME',
149 help='Print the sizes of individual dex segments as ' +
150 '\'<BENCHMARKNAME>-<segment>(CodeSize): <bytes>\'')
Morten Krogh-Jespersenae9557c2019-10-23 15:14:02 +0200151 result.add_option('--track-time-in-memory',
152 help='Plot the times taken from memory starting point to '
153 'end-point with defined memory increment',
154 default=False,
155 action='store_true')
156 result.add_option('--track-time-in-memory-max',
157 help='Setting the maximum memory baseline to run in',
158 type='int')
159 result.add_option('--track-time-in-memory-min',
160 help='Setting the minimum memory baseline to run in',
161 type='int')
162 result.add_option('--track-time-in-memory-increment',
163 help='Setting the increment',
164 type='int',
165 default=32)
Morten Krogh-Jespersenb7e1a182019-10-09 13:04:54 +0200166
Tamas Kenez5b1c5852017-07-21 13:38:33 +0200167 return result.parse_args(argv)
Mads Ager418d1ca2017-05-22 09:35:49 +0200168
Man Cao29b9ef12019-03-25 11:19:35 -0700169# Most apps have -printmapping, -printseeds, -printusage and
170# -printconfiguration in the Proguard configuration. However we don't
171# want to write these files in the locations specified.
172# Instead generate an auxiliary Proguard configuration placing these
173# output files together with the dex output.
Søren Gjesse3a5aed92017-06-14 15:36:02 +0200174def GenerateAdditionalProguardConfiguration(temp, outdir):
175 name = "output.config"
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200176 with open(os.path.join(temp, name), 'w') as f:
177 f.write('-printmapping ' + os.path.join(outdir, 'proguard.map') + "\n")
178 f.write('-printseeds ' + os.path.join(outdir, 'proguard.seeds') + "\n")
Søren Gjesse59459582018-04-06 10:12:45 +0200179 f.write('-printusage ' + os.path.join(outdir, 'proguard.usage') + "\n")
Man Cao29b9ef12019-03-25 11:19:35 -0700180 f.write('-printconfiguration ' + os.path.join(outdir, 'proguard.config') + "\n")
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200181 return os.path.abspath(f.name)
Søren Gjesse3a5aed92017-06-14 15:36:02 +0200182
Rico Wind3f9302b2018-09-21 08:53:09 +0200183# Please add bug number for disabled permutations and please explicitly
184# do Bug: #BUG in the commit message of disabling to ensure re-enabling
185DISABLED_PERMUTATIONS = [
Ian Zernyd459b5f2018-10-03 06:46:21 +0200186 # (app, version, type), e.g., ('gmail', '180826.15', 'deploy'),
Rico Winded788332018-12-14 10:39:20 +0100187 ('youtube', '13.37', 'deploy'), # b/120977564
Rico Wind3f9302b2018-09-21 08:53:09 +0200188]
189
Rico Windb57bbc12018-09-20 19:23:32 +0200190def get_permutations():
191 data_providers = {
192 'gmscore': gmscore_data,
Christoffer Quist Adamsena2a58772018-10-03 09:47:46 +0200193 'nest': nest_data,
Rico Windb57bbc12018-09-20 19:23:32 +0200194 'youtube': youtube_data,
195 'chrome': chrome_data,
Morten Krogh-Jespersen04c7c302019-10-01 15:04:33 +0200196 'gmail': gmail_data,
Morten Krogh-Jespersencd6712c2019-10-09 13:09:47 +0200197 'r8': r8_data,
198 'iosched': iosched_data,
Rico Windb57bbc12018-09-20 19:23:32 +0200199 }
200 # Check to ensure that we add all variants here.
201 assert len(APPS) == len(data_providers)
202 for app, data in data_providers.iteritems():
203 for version in data.VERSIONS:
204 for type in data.VERSIONS[version]:
Rico Wind3f9302b2018-09-21 08:53:09 +0200205 if (app, version, type) not in DISABLED_PERMUTATIONS:
Tamas Keneza730a7e2018-12-10 15:02:28 +0100206 for use_r8lib in [False, True]:
207 yield app, version, type, use_r8lib
Rico Windb57bbc12018-09-20 19:23:32 +0200208
209def run_all(options, args):
210 # Args will be destroyed
211 assert len(args) == 0
Tamas Keneza730a7e2018-12-10 15:02:28 +0100212 for name, version, type, use_r8lib in get_permutations():
Rico Windb57bbc12018-09-20 19:23:32 +0200213 compiler = 'r8' if type == 'deploy' else 'd8'
Tamas Kenez63a51d02019-01-07 15:53:02 +0100214 compiler_build = 'lib' if use_r8lib else 'full'
215 print('Executing %s/%s with %s %s %s' % (compiler, compiler_build, name,
216 version, type))
Tamas Keneza730a7e2018-12-10 15:02:28 +0100217
Rico Windb57bbc12018-09-20 19:23:32 +0200218 fixed_options = copy.copy(options)
219 fixed_options.app = name
220 fixed_options.version = version
221 fixed_options.compiler = compiler
Tamas Kenez63a51d02019-01-07 15:53:02 +0100222 fixed_options.compiler_build = compiler_build
Rico Windb57bbc12018-09-20 19:23:32 +0200223 fixed_options.type = type
224 exit_code = run_with_options(fixed_options, [])
225 if exit_code != 0:
Tamas Kenez63a51d02019-01-07 15:53:02 +0100226 print('Failed %s %s %s with %s/%s' % (name, version, type, compiler,
227 compiler_build))
Rico Windb57bbc12018-09-20 19:23:32 +0200228 exit(exit_code)
229
Rico Wind5fdec152018-12-17 09:16:14 +0100230def find_min_xmx(options, args):
231 # Args will be destroyed
232 assert len(args) == 0
233 # If we can run in 128 MB then we are good (which we can for small examples
234 # or D8 on medium sized examples)
Morten Krogh-Jespersen04c7c302019-10-01 15:04:33 +0200235 if options.find_min_xmx_min_memory:
236 not_working = options.find_min_xmx_min_memory
237 elif options.compiler == 'd8':
238 not_working = 128
239 else:
240 not_working = 1024
241 if options.find_min_xmx_max_memory:
242 working = options.find_min_xmx_max_memory
243 else:
244 working = 1024 * 8
Rico Wind5fdec152018-12-17 09:16:14 +0100245 exit_code = 0
Morten Krogh-Jespersen04c7c302019-10-01 15:04:33 +0200246 range = options.find_min_xmx_range_size
247 while working - not_working > range:
Rico Wind5fdec152018-12-17 09:16:14 +0100248 next_candidate = working - ((working - not_working)/2)
249 print('working: %s, non_working: %s, next_candidate: %s' %
250 (working, not_working, next_candidate))
251 extra_args = ['-Xmx%sM' % next_candidate]
Rico Wind5fdec152018-12-17 09:16:14 +0100252 t0 = time.time()
253 exit_code = run_with_options(options, [], extra_args)
254 t1 = time.time()
255 print('Running took: %s ms' % (1000.0 * (t1 - t0)))
Jinseong Jeon158a3f12019-02-08 01:40:59 -0800256 if exit_code != 0:
257 if exit_code not in [OOM_EXIT_CODE, TIMEOUT_KILL_CODE]:
258 print('Non OOM/Timeout error executing, exiting')
259 return 2
Rico Wind5fdec152018-12-17 09:16:14 +0100260 if exit_code == 0:
261 working = next_candidate
Jinseong Jeon158a3f12019-02-08 01:40:59 -0800262 elif exit_code == TIMEOUT_KILL_CODE:
263 print('Timeout. Continue to the next candidate.')
264 not_working = next_candidate
Rico Wind5fdec152018-12-17 09:16:14 +0100265 else:
266 assert exit_code == OOM_EXIT_CODE
267 not_working = next_candidate
268
Morten Krogh-Jespersen04c7c302019-10-01 15:04:33 +0200269 assert working - not_working <= range
Morten Krogh-Jespersen0981b722019-10-09 10:00:33 +0200270 found_range = 'Found range: %s - %s' % (not_working, working)
271 print(found_range)
272
273 if options.find_min_xmx_archive:
274 sha = utils.get_HEAD_sha1()
275 (version, _) = get_version_and_data(options)
276 destination = os.path.join(
277 utils.R8_TEST_RESULTS_BUCKET,
278 FIND_MIN_XMX_DIR,
279 sha,
280 options.compiler,
281 options.compiler_build,
282 options.app,
283 version,
284 get_type(options))
285 gs_destination = 'gs://%s' % destination
286 utils.archive_value(FIND_MIN_XMX_FILE, gs_destination, found_range + '\n')
287
Rico Wind5fdec152018-12-17 09:16:14 +0100288 return 0
289
Morten Krogh-Jespersenf2412302019-10-22 10:18:04 +0200290def print_min_xmx_ranges_for_hash(hash, compiler, compiler_build):
291 app_directory = os.path.join(
292 utils.R8_TEST_RESULTS_BUCKET,
293 FIND_MIN_XMX_DIR,
294 hash,
295 compiler,
296 compiler_build)
297 gs_base = 'gs://%s' % app_directory
298 for app in utils.ls_files_on_cloud_storage(gs_base).strip().split('\n'):
299 for version in utils.ls_files_on_cloud_storage(app).strip().split('\n'):
300 for type in utils.ls_files_on_cloud_storage(version).strip().split('\n'):
301 gs_location = '%s%s' % (type, FIND_MIN_XMX_FILE)
302 value = utils.cat_file_on_cloud_storage(gs_location, ignore_errors=True)
303 print('%s\n' % value)
304
Morten Krogh-Jespersenae9557c2019-10-23 15:14:02 +0200305def track_time_in_memory(options, args):
306 # Args will be destroyed
307 assert len(args) == 0
308 if not options.track_time_in_memory_min:
309 raise Exception(
310 'You have to specify --track_time_in_memory_min when running with '
311 '--track-time-in-memory')
312 if not options.track_time_in_memory_max:
313 raise Exception(
314 'You have to specify --track_time_in_memory_max when running with '
315 '--track-time-in-memory')
316 if not options.track_time_in_memory_increment:
317 raise Exception(
318 'You have to specify --track_time_in_memory_increment when running '
319 'with --track-time-in-memory')
320 current = options.track_time_in_memory_min
321 print('Memory (KB)\tTime (ms)')
322 with utils.TempDir() as temp:
323 stdout = os.path.join(temp, 'stdout')
324 stdout_fd = open(stdout, 'w')
325 while current <= options.track_time_in_memory_max:
326 extra_args = ['-Xmx%sM' % current]
327 t0 = time.time()
328 exit_code = run_with_options(options, [], extra_args, stdout_fd, quiet=True)
329 t1 = time.time()
330 total = (1000.0 * (t1 - t0)) if exit_code == 0 else -1
331 print('%s\t%s' % (current, total))
332 current += options.track_time_in_memory_increment
333
334 return 0
335
Tamas Kenez5b1c5852017-07-21 13:38:33 +0200336def main(argv):
Tamas Kenez5b1c5852017-07-21 13:38:33 +0200337 (options, args) = ParseOptions(argv)
Morten Krogh-Jespersenb7e1a182019-10-09 13:04:54 +0200338 if options.expect_oom and not options.max_memory:
339 raise Exception(
340 'You should only use --expect-oom if also specifying --max-memory')
341 if options.expect_oom and options.timeout:
342 raise Exception(
343 'You should not use --timeout when also specifying --expect-oom')
Morten Krogh-Jespersenae9557c2019-10-23 15:14:02 +0200344 if options.find_min_xmx and options.track_time_in_memory:
345 raise Exception(
346 'You cannot both find the min xmx and track time at the same time')
Rico Windb57bbc12018-09-20 19:23:32 +0200347 if options.run_all:
348 return run_all(options, args)
Rico Wind5fdec152018-12-17 09:16:14 +0100349 if options.find_min_xmx:
350 return find_min_xmx(options, args)
Morten Krogh-Jespersenae9557c2019-10-23 15:14:02 +0200351 if options.track_time_in_memory:
352 return track_time_in_memory(options, args)
Morten Krogh-Jespersenb7e1a182019-10-09 13:04:54 +0200353 exit_code = run_with_options(options, args)
354 if options.expect_oom:
355 exit_code = 0 if exit_code == OOM_EXIT_CODE else 1
356 return exit_code
Rico Windb57bbc12018-09-20 19:23:32 +0200357
Morten Krogh-Jespersen0981b722019-10-09 10:00:33 +0200358def get_version_and_data(options):
359 if options.app == 'gmscore':
360 version = options.version or 'v9'
361 data = gmscore_data
362 elif options.app == 'nest':
363 version = options.version or '20180926'
364 data = nest_data
365 elif options.app == 'youtube':
366 version = options.version or '12.22'
367 data = youtube_data
368 elif options.app == 'chrome':
369 version = options.version or '180917'
370 data = chrome_data
371 elif options.app == 'gmail':
372 version = options.version or '170604.16'
373 data = gmail_data
374 elif options.app == 'r8':
375 version = options.version or 'cf'
376 data = r8_data
Morten Krogh-Jespersencd6712c2019-10-09 13:09:47 +0200377 elif options.app == 'iosched':
378 version = options.version or '2019'
379 data = iosched_data
Morten Krogh-Jespersen0981b722019-10-09 10:00:33 +0200380 else:
381 raise Exception("You need to specify '--app={}'".format('|'.join(APPS)))
382 return version, data
383
384def get_type(options):
385 if not options.type:
386 return 'deploy' if options.compiler == 'r8' else 'proguarded'
387 return options.type
388
Morten Krogh-Jespersenae9557c2019-10-23 15:14:02 +0200389def run_with_options(options, args, extra_args=None, stdout=None, quiet=False):
Tamas Kenez63a51d02019-01-07 15:53:02 +0100390 if extra_args is None:
391 extra_args = []
Rico Windb57bbc12018-09-20 19:23:32 +0200392 app_provided_pg_conf = False;
Rico Windadd08132018-12-14 14:17:15 +0100393 # todo(121018500): remove when memory is under control
Rico Wind20602b72019-01-09 09:17:13 +0100394 if not any('-Xmx' in arg for arg in extra_args):
Morten Krogh-Jespersen322c2f12019-10-08 10:41:21 +0200395 if options.max_memory:
396 extra_args.append('-Xmx%sM' % options.max_memory)
397 else:
398 extra_args.append('-Xmx8G')
Rico Wind1f4172c2018-09-06 16:29:03 +0200399 if options.golem:
400 golem.link_third_party()
Rico Wind9bd3f5f2018-10-01 10:53:18 +0200401 options.out = os.getcwd()
Rico Windafdbbfd2019-02-22 09:32:07 +0100402 if not options.ignore_java_version:
403 utils.check_java_version()
404
Mads Ager418d1ca2017-05-22 09:35:49 +0200405 outdir = options.out
Morten Krogh-Jespersen0981b722019-10-09 10:00:33 +0200406 (version_id, data) = get_version_and_data(options)
407
Tamas Kenez5b1c5852017-07-21 13:38:33 +0200408 if options.compiler not in COMPILERS:
409 raise Exception("You need to specify '--compiler={}'"
410 .format('|'.join(COMPILERS)))
Mads Ager418d1ca2017-05-22 09:35:49 +0200411
Tamas Kenez63a51d02019-01-07 15:53:02 +0100412 if options.compiler_build not in COMPILER_BUILDS:
413 raise Exception("You need to specify '--compiler-build={}'"
414 .format('|'.join(COMPILER_BUILDS)))
415
Morten Krogh-Jespersen0981b722019-10-09 10:00:33 +0200416 if not version_id in data.VERSIONS.keys():
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200417 print('No version {} for application {}'
Morten Krogh-Jespersen0981b722019-10-09 10:00:33 +0200418 .format(version_id, options.app))
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200419 print('Valid versions are {}'.format(data.VERSIONS.keys()))
Mads Ager418d1ca2017-05-22 09:35:49 +0200420 return 1
421
Morten Krogh-Jespersen0981b722019-10-09 10:00:33 +0200422 version = data.VERSIONS[version_id]
Mads Ager418d1ca2017-05-22 09:35:49 +0200423
Morten Krogh-Jespersen0981b722019-10-09 10:00:33 +0200424 type = get_type(options)
Tamas Kenez3fdaabd2017-06-15 13:05:12 +0200425
Morten Krogh-Jespersen0981b722019-10-09 10:00:33 +0200426 if type not in version:
427 print('No type {} for version {}'.format(type, version))
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200428 print('Valid types are {}'.format(version.keys()))
Mads Ager418d1ca2017-05-22 09:35:49 +0200429 return 1
Morten Krogh-Jespersen0981b722019-10-09 10:00:33 +0200430 values = version[type]
Mads Ager418d1ca2017-05-22 09:35:49 +0200431 inputs = None
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200432 # For R8 'deploy' the JAR is located using the Proguard configuration
Christoffer Quist Adamsena2a58772018-10-03 09:47:46 +0200433 # -injars option. For chrome and nest we don't have the injars in the
434 # proguard files.
Tamas Kenez63a51d02019-01-07 15:53:02 +0100435 if 'inputs' in values and (options.compiler != 'r8'
Morten Krogh-Jespersen0981b722019-10-09 10:00:33 +0200436 or type != 'deploy'
Christoffer Quist Adamsena2a58772018-10-03 09:47:46 +0200437 or options.app == 'chrome'
Morten Krogh-Jespersen04c7c302019-10-01 15:04:33 +0200438 or options.app == 'nest'
Morten Krogh-Jespersencd6712c2019-10-09 13:09:47 +0200439 or options.app == 'r8'
440 or options.app == 'iosched'):
Mads Ager418d1ca2017-05-22 09:35:49 +0200441 inputs = values['inputs']
442
443 args.extend(['--output', outdir])
Ian Zerny877c1862017-07-06 11:12:26 +0200444 if 'min-api' in values:
445 args.extend(['--min-api', values['min-api']])
Søren Gjesse932881f2017-06-13 10:43:36 +0200446
Søren Gjesse8ae55eb2018-09-28 11:11:36 +0200447 if 'main-dex-list' in values:
448 args.extend(['--main-dex-list', values['main-dex-list']])
449
Tamas Kenez63a51d02019-01-07 15:53:02 +0100450 if options.compiler == 'r8':
Søren Gjesse932881f2017-06-13 10:43:36 +0200451 if 'pgconf' in values and not options.k:
Søren Gjessecbeae782019-05-21 14:14:25 +0200452 sanitized_lib_path = os.path.join(
453 os.path.abspath(outdir), 'sanitized_lib.jar')
454 sanitized_pgconf_path = os.path.join(
455 os.path.abspath(outdir), 'sanitized.config')
456 SanitizeLibraries(
457 sanitized_lib_path, sanitized_pgconf_path, values['pgconf'])
458 args.extend(['--pg-conf', sanitized_pgconf_path])
459 app_provided_pg_conf = True
Søren Gjesse932881f2017-06-13 10:43:36 +0200460 if options.k:
461 args.extend(['--pg-conf', options.k])
Søren Gjessec801ecc2017-08-03 13:40:06 +0200462 if 'maindexrules' in values:
463 for rules in values['maindexrules']:
464 args.extend(['--main-dex-rules', rules])
Rico Wind79e4eb52018-12-13 13:00:49 +0100465 if 'allow-type-errors' in values:
466 extra_args.append('-Dcom.android.tools.r8.allowTypeErrors=1')
Christoffer Quist Adamsen955fa632019-06-03 14:55:49 +0200467 if 'proto-shrinking' in values:
Christoffer Quist Adamsend79b6a72019-09-16 17:03:05 +0200468 extra_args.append('-Dcom.android.tools.r8.fieldBitAccessAnalysis=1')
Christoffer Quist Adamsen955fa632019-06-03 14:55:49 +0200469 extra_args.append('-Dcom.android.tools.r8.generatedExtensionRegistryShrinking=1')
Christoffer Quist Adamsene32e3912019-08-23 15:11:38 +0200470 extra_args.append('-Dcom.android.tools.r8.generatedMessageLiteShrinking=1')
Christoffer Quist Adamsend79b6a72019-09-16 17:03:05 +0200471 extra_args.append('-Dcom.android.tools.r8.stringSwitchConversion=1')
Søren Gjesse932881f2017-06-13 10:43:36 +0200472
Mads Ager418d1ca2017-05-22 09:35:49 +0200473 if not options.no_libraries and 'libraries' in values:
474 for lib in values['libraries']:
475 args.extend(['--lib', lib])
476
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200477 if not outdir.endswith('.zip') and not outdir.endswith('.jar') \
478 and not os.path.exists(outdir):
Mads Ager418d1ca2017-05-22 09:35:49 +0200479 os.makedirs(outdir)
480
Søren Gjesse8ae55eb2018-09-28 11:11:36 +0200481 # Additional flags for the compiler from the configuration file.
482 if 'flags' in values:
483 args.extend(values['flags'].split(' '))
Tamas Kenez63a51d02019-01-07 15:53:02 +0100484 if options.compiler == 'r8':
Søren Gjesse932881f2017-06-13 10:43:36 +0200485 if 'r8-flags' in values:
486 args.extend(values['r8-flags'].split(' '))
Tamas Kenez139acc12017-06-14 17:14:58 +0200487
Søren Gjesse8ae55eb2018-09-28 11:11:36 +0200488 # Additional flags for the compiler from the command line.
Tamas Kenez139acc12017-06-14 17:14:58 +0200489 if options.compiler_flags:
490 args.extend(options.compiler_flags.split(' '))
491 if options.r8_flags:
492 args.extend(options.r8_flags.split(' '))
Mads Ager418d1ca2017-05-22 09:35:49 +0200493
494 if inputs:
495 args.extend(inputs)
496
Tamas Kenezf2ee2a32017-06-21 10:30:20 +0200497 t0 = time.time()
Mads Ager418d1ca2017-05-22 09:35:49 +0200498 if options.dump_args_file:
499 with open(options.dump_args_file, 'w') as args_file:
500 args_file.writelines([arg + os.linesep for arg in args])
501 else:
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200502 with utils.TempDir() as temp:
503 if options.print_memoryuse and not options.track_memory_to_file:
504 options.track_memory_to_file = os.path.join(temp,
505 utils.MEMORY_USE_TMP_FILE)
Tamas Kenez63a51d02019-01-07 15:53:02 +0100506 if options.compiler == 'r8' and app_provided_pg_conf:
Mathias Ravdd6a6de2018-05-18 10:18:33 +0200507 # Ensure that output of -printmapping and -printseeds go to the output
508 # location and not where the app Proguard configuration places them.
509 if outdir.endswith('.zip') or outdir.endswith('.jar'):
510 pg_outdir = os.path.dirname(outdir)
511 else:
512 pg_outdir = outdir
513 additional_pg_conf = GenerateAdditionalProguardConfiguration(
514 temp, os.path.abspath(pg_outdir))
515 args.extend(['--pg-conf', additional_pg_conf])
Rico Wind1f4172c2018-09-06 16:29:03 +0200516 build = not options.no_build and not options.golem
Rico Wind5fdec152018-12-17 09:16:14 +0100517 stderr_path = os.path.join(temp, 'stderr')
518 with open(stderr_path, 'w') as stderr:
Tamas Kenez63a51d02019-01-07 15:53:02 +0100519 if options.compiler_build == 'full':
520 tool = options.compiler
521 else:
522 assert(options.compiler_build == 'lib')
523 tool = 'r8lib-' + options.compiler
524 exit_code = toolhelper.run(tool, args,
Rico Wind5fdec152018-12-17 09:16:14 +0100525 build=build,
526 debug=not options.no_debug,
527 profile=options.profile,
528 track_memory_file=options.track_memory_to_file,
Jinseong Jeon158a3f12019-02-08 01:40:59 -0800529 extra_args=extra_args,
Morten Krogh-Jespersenae9557c2019-10-23 15:14:02 +0200530 stdout=stdout,
Jinseong Jeon158a3f12019-02-08 01:40:59 -0800531 stderr=stderr,
Morten Krogh-Jespersenae9557c2019-10-23 15:14:02 +0200532 timeout=options.timeout,
533 quiet=quiet)
Christoffer Quist Adamsen21c66602018-08-09 16:22:54 +0200534 if exit_code != 0:
Rico Wind5fdec152018-12-17 09:16:14 +0100535 with open(stderr_path) as stderr:
536 stderr_text = stderr.read()
Morten Krogh-Jespersenae9557c2019-10-23 15:14:02 +0200537 if not quiet:
538 print(stderr_text)
Rico Wind5fdec152018-12-17 09:16:14 +0100539 if 'java.lang.OutOfMemoryError' in stderr_text:
Morten Krogh-Jespersenae9557c2019-10-23 15:14:02 +0200540 if not quiet:
541 print('Failure was OOM')
Rico Wind5fdec152018-12-17 09:16:14 +0100542 return OOM_EXIT_CODE
543 return exit_code
Christoffer Quist Adamsen21c66602018-08-09 16:22:54 +0200544
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200545 if options.print_memoryuse:
546 print('{}(MemoryUse): {}'
547 .format(options.print_memoryuse,
548 utils.grep_memoryuse(options.track_memory_to_file)))
Mads Ager418d1ca2017-05-22 09:35:49 +0200549
Tamas Kenezf2ee2a32017-06-21 10:30:20 +0200550 if options.print_runtimeraw:
551 print('{}(RunTimeRaw): {} ms'
552 .format(options.print_runtimeraw, 1000.0 * (time.time() - t0)))
553
Tamas Kenez02bff032017-07-18 12:13:58 +0200554 if options.print_dexsegments:
555 dex_files = glob(os.path.join(outdir, '*.dex'))
556 utils.print_dexsegments(options.print_dexsegments, dex_files)
Rico Windb57bbc12018-09-20 19:23:32 +0200557 return 0
Tamas Kenez02bff032017-07-18 12:13:58 +0200558
Mads Ager418d1ca2017-05-22 09:35:49 +0200559if __name__ == '__main__':
Tamas Kenez5b1c5852017-07-21 13:38:33 +0200560 sys.exit(main(sys.argv[1:]))