blob: 31cecfc58e13639af817c57782583db92011b9c1 [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
Mathias Ravdd6a6de2018-05-18 10:18:33 +020018import toolhelper
Ian Zerny877c1862017-07-06 11:12:26 +020019import utils
20import youtube_data
Rico Wind86bfc832018-09-18 07:48:21 +020021import chrome_data
Mads Ager418d1ca2017-05-22 09:35:49 +020022
23TYPES = ['dex', 'deploy', 'proguarded']
Christoffer Quist Adamsena2a58772018-10-03 09:47:46 +020024APPS = ['gmscore', 'nest', 'youtube', 'gmail', 'chrome']
Tamas Keneza730a7e2018-12-10 15:02:28 +010025COMPILERS = ['d8', 'r8', 'r8lib-r8', 'r8lib-d8']
26R8_COMPILERS = ['r8', 'r8lib-r8']
Rico Wind5fdec152018-12-17 09:16:14 +010027# We use this magic exit code to signal that the program OOM'ed
28OOM_EXIT_CODE = 42
Mads Ager418d1ca2017-05-22 09:35:49 +020029
Tamas Kenez5b1c5852017-07-21 13:38:33 +020030def ParseOptions(argv):
Mads Ager418d1ca2017-05-22 09:35:49 +020031 result = optparse.OptionParser()
Søren Gjesse932881f2017-06-13 10:43:36 +020032 result.add_option('--compiler',
Rico Windb57bbc12018-09-20 19:23:32 +020033 help='The compiler to use',
Tamas Kenez5b1c5852017-07-21 13:38:33 +020034 choices=COMPILERS)
Mads Ager418d1ca2017-05-22 09:35:49 +020035 result.add_option('--app',
Rico Windb57bbc12018-09-20 19:23:32 +020036 help='What app to run on',
Mads Ager418d1ca2017-05-22 09:35:49 +020037 choices=APPS)
Rico Windb57bbc12018-09-20 19:23:32 +020038 result.add_option('--run-all',
39 help='Compile all possible combinations',
40 default=False,
41 action='store_true')
Mads Ager418d1ca2017-05-22 09:35:49 +020042 result.add_option('--type',
Tamas Kenez3fdaabd2017-06-15 13:05:12 +020043 help='Default for R8: deploy, for D8: proguarded',
Mads Ager418d1ca2017-05-22 09:35:49 +020044 choices=TYPES)
45 result.add_option('--out',
Rico Windb57bbc12018-09-20 19:23:32 +020046 help='Where to place the output',
Rico Winde9485ba2018-10-01 07:04:16 +020047 default=utils.BUILD)
Mads Ager418d1ca2017-05-22 09:35:49 +020048 result.add_option('--no-build',
Rico Windb57bbc12018-09-20 19:23:32 +020049 help='Run without building first',
Mads Ager418d1ca2017-05-22 09:35:49 +020050 default=False,
51 action='store_true')
Rico Wind5fdec152018-12-17 09:16:14 +010052 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 Wind1f4172c2018-09-06 16:29:03 +020056 result.add_option('--golem',
Rico Windb57bbc12018-09-20 19:23:32 +020057 help='Running on golem, do not build or download',
Rico Wind1f4172c2018-09-06 16:29:03 +020058 default=False,
59 action='store_true')
Rico Wind139eece2018-09-25 09:42:09 +020060 result.add_option('--ignore-java-version',
61 help='Do not check java version',
62 default=False,
63 action='store_true')
Mads Ager418d1ca2017-05-22 09:35:49 +020064 result.add_option('--no-libraries',
Rico Windb57bbc12018-09-20 19:23:32 +020065 help='Do not pass in libraries, even if they exist in conf',
Mads Ager418d1ca2017-05-22 09:35:49 +020066 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 Windb57bbc12018-09-20 19:23:32 +020073 help='The version of the app to run')
Mads Ager418d1ca2017-05-22 09:35:49 +020074 result.add_option('-k',
75 help='Override the default ProGuard keep rules')
Tamas Kenez139acc12017-06-14 17:14:58 +020076 result.add_option('--compiler-flags',
77 help='Additional option(s) for the compiler. ' +
78 'If passing several options use a quoted string.')
Mads Ager418d1ca2017-05-22 09:35:49 +020079 result.add_option('--r8-flags',
Tamas Kenez139acc12017-06-14 17:14:58 +020080 help='Additional option(s) for the compiler. ' +
Tamas Kenezfc34cd82017-07-13 12:43:57 +020081 'Same as --compiler-flags, keeping it for backward'
82 ' compatibility. ' +
Mads Ager418d1ca2017-05-22 09:35:49 +020083 'If passing several options use a quoted string.')
Tamas Kenezfc34cd82017-07-13 12:43:57 +020084 # TODO(tamaskenez) remove track-memory-to-file as soon as we updated golem
85 # to use --print-memoryuse instead
Mads Ager418d1ca2017-05-22 09:35:49 +020086 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 Kenezf2ee2a32017-06-21 10:30:20 +020097 result.add_option('--print-runtimeraw',
98 metavar='BENCHMARKNAME',
Tamas Kenez02bff032017-07-18 12:13:58 +020099 help='Print the line \'<BENCHMARKNAME>(RunTimeRaw):' +
100 ' <elapsed> ms\' at the end where <elapsed> is' +
101 ' the elapsed time in milliseconds.')
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200102 result.add_option('--print-memoryuse',
103 metavar='BENCHMARKNAME',
Tamas Kenez02bff032017-07-18 12:13:58 +0200104 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 Kenez5b1c5852017-07-21 13:38:33 +0200111 return result.parse_args(argv)
Mads Ager418d1ca2017-05-22 09:35:49 +0200112
Søren Gjesse59459582018-04-06 10:12:45 +0200113# 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 Gjesse3a5aed92017-06-14 15:36:02 +0200118def GenerateAdditionalProguardConfiguration(temp, outdir):
119 name = "output.config"
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200120 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 Gjesse59459582018-04-06 10:12:45 +0200123 f.write('-printusage ' + os.path.join(outdir, 'proguard.usage') + "\n")
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200124 return os.path.abspath(f.name)
Søren Gjesse3a5aed92017-06-14 15:36:02 +0200125
Rico Wind3f9302b2018-09-21 08:53:09 +0200126# Please add bug number for disabled permutations and please explicitly
127# do Bug: #BUG in the commit message of disabling to ensure re-enabling
128DISABLED_PERMUTATIONS = [
Ian Zernyd459b5f2018-10-03 06:46:21 +0200129 # (app, version, type), e.g., ('gmail', '180826.15', 'deploy'),
Rico Winded788332018-12-14 10:39:20 +0100130 ('youtube', '13.37', 'deploy'), # b/120977564
Rico Wind3f9302b2018-09-21 08:53:09 +0200131]
132
Rico Windb57bbc12018-09-20 19:23:32 +0200133def get_permutations():
134 data_providers = {
135 'gmscore': gmscore_data,
Christoffer Quist Adamsena2a58772018-10-03 09:47:46 +0200136 'nest': nest_data,
Rico Windb57bbc12018-09-20 19:23:32 +0200137 '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 Wind3f9302b2018-09-21 08:53:09 +0200146 if (app, version, type) not in DISABLED_PERMUTATIONS:
Tamas Keneza730a7e2018-12-10 15:02:28 +0100147 for use_r8lib in [False, True]:
148 yield app, version, type, use_r8lib
Rico Windb57bbc12018-09-20 19:23:32 +0200149
150def run_all(options, args):
151 # Args will be destroyed
152 assert len(args) == 0
Tamas Keneza730a7e2018-12-10 15:02:28 +0100153 for name, version, type, use_r8lib in get_permutations():
Rico Windb57bbc12018-09-20 19:23:32 +0200154 compiler = 'r8' if type == 'deploy' else 'd8'
Tamas Keneza730a7e2018-12-10 15:02:28 +0100155 if use_r8lib:
156 compiler = 'r8lib-' + compiler
157 print('Executing %s with %s %s %s' % (compiler, name, version, type))
158
Rico Windb57bbc12018-09-20 19:23:32 +0200159 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 Wind5fdec152018-12-17 09:16:14 +0100169def 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 Kenez5b1c5852017-07-21 13:38:33 +0200200def main(argv):
Tamas Kenez5b1c5852017-07-21 13:38:33 +0200201 (options, args) = ParseOptions(argv)
Rico Wind139eece2018-09-25 09:42:09 +0200202 if not options.ignore_java_version:
203 utils.check_java_version()
204
Rico Windb57bbc12018-09-20 19:23:32 +0200205 if options.run_all:
206 return run_all(options, args)
Rico Wind5fdec152018-12-17 09:16:14 +0100207 if options.find_min_xmx:
208 return find_min_xmx(options, args)
Rico Windb57bbc12018-09-20 19:23:32 +0200209 return run_with_options(options, args)
210
Rico Wind5fdec152018-12-17 09:16:14 +0100211def run_with_options(options, args, extra_args=[]):
Rico Windb57bbc12018-09-20 19:23:32 +0200212 app_provided_pg_conf = False;
Rico Windadd08132018-12-14 14:17:15 +0100213 # todo(121018500): remove when memory is under control
Rico Wind38817bc2018-12-17 07:30:42 +0100214 extra_args.append('-Xmx8G')
Rico Wind1f4172c2018-09-06 16:29:03 +0200215 if options.golem:
216 golem.link_third_party()
Rico Wind9bd3f5f2018-10-01 10:53:18 +0200217 options.out = os.getcwd()
Mads Ager418d1ca2017-05-22 09:35:49 +0200218 outdir = options.out
219 data = None
220 if options.app == 'gmscore':
221 options.version = options.version or 'v9'
222 data = gmscore_data
Christoffer Quist Adamsena2a58772018-10-03 09:47:46 +0200223 elif options.app == 'nest':
224 options.version = options.version or '20180926'
225 data = nest_data
Mads Ager418d1ca2017-05-22 09:35:49 +0200226 elif options.app == 'youtube':
Søren Gjessecc33fb42017-06-09 10:25:08 +0200227 options.version = options.version or '12.22'
Mads Ager418d1ca2017-05-22 09:35:49 +0200228 data = youtube_data
Rico Wind86bfc832018-09-18 07:48:21 +0200229 elif options.app == 'chrome':
230 options.version = options.version or 'default'
231 data = chrome_data
Søren Gjesse5ecb04a2017-06-13 09:44:32 +0200232 elif options.app == 'gmail':
233 options.version = options.version or '170604.16'
234 data = gmail_data
Mads Ager418d1ca2017-05-22 09:35:49 +0200235 else:
Tamas Kenez5b1c5852017-07-21 13:38:33 +0200236 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 Ager418d1ca2017-05-22 09:35:49 +0200241
242 if not options.version in data.VERSIONS.keys():
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200243 print('No version {} for application {}'
244 .format(options.version, options.app))
245 print('Valid versions are {}'.format(data.VERSIONS.keys()))
Mads Ager418d1ca2017-05-22 09:35:49 +0200246 return 1
247
248 version = data.VERSIONS[options.version]
249
Tamas Kenez3fdaabd2017-06-15 13:05:12 +0200250 if not options.type:
Tamas Keneza730a7e2018-12-10 15:02:28 +0100251 options.type = 'deploy' if options.compiler in R8_COMPILERS \
Tamas Kenez3fdaabd2017-06-15 13:05:12 +0200252 else 'proguarded'
253
Mads Ager418d1ca2017-05-22 09:35:49 +0200254 if options.type not in version:
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200255 print('No type {} for version {}'.format(options.type, options.version))
256 print('Valid types are {}'.format(version.keys()))
Mads Ager418d1ca2017-05-22 09:35:49 +0200257 return 1
258 values = version[options.type]
259 inputs = None
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200260 # For R8 'deploy' the JAR is located using the Proguard configuration
Christoffer Quist Adamsena2a58772018-10-03 09:47:46 +0200261 # -injars option. For chrome and nest we don't have the injars in the
262 # proguard files.
Tamas Keneza730a7e2018-12-10 15:02:28 +0100263 if 'inputs' in values and (options.compiler not in R8_COMPILERS
Rico Wind86bfc832018-09-18 07:48:21 +0200264 or options.type != 'deploy'
Christoffer Quist Adamsena2a58772018-10-03 09:47:46 +0200265 or options.app == 'chrome'
266 or options.app == 'nest'):
Mads Ager418d1ca2017-05-22 09:35:49 +0200267 inputs = values['inputs']
268
269 args.extend(['--output', outdir])
Ian Zerny877c1862017-07-06 11:12:26 +0200270 if 'min-api' in values:
271 args.extend(['--min-api', values['min-api']])
Søren Gjesse932881f2017-06-13 10:43:36 +0200272
Søren Gjesse8ae55eb2018-09-28 11:11:36 +0200273 if 'main-dex-list' in values:
274 args.extend(['--main-dex-list', values['main-dex-list']])
275
Tamas Keneza730a7e2018-12-10 15:02:28 +0100276 if options.compiler in R8_COMPILERS:
Søren Gjesse932881f2017-06-13 10:43:36 +0200277 if 'pgconf' in values and not options.k:
278 for pgconf in values['pgconf']:
279 args.extend(['--pg-conf', pgconf])
Søren Gjesseda2ac8d2017-06-22 14:14:36 +0200280 app_provided_pg_conf = True
Søren Gjesse932881f2017-06-13 10:43:36 +0200281 if options.k:
282 args.extend(['--pg-conf', options.k])
Søren Gjessec801ecc2017-08-03 13:40:06 +0200283 if 'maindexrules' in values:
284 for rules in values['maindexrules']:
285 args.extend(['--main-dex-rules', rules])
Rico Wind79e4eb52018-12-13 13:00:49 +0100286 if 'allow-type-errors' in values:
287 extra_args.append('-Dcom.android.tools.r8.allowTypeErrors=1')
Søren Gjesse932881f2017-06-13 10:43:36 +0200288
Mads Ager418d1ca2017-05-22 09:35:49 +0200289 if not options.no_libraries and 'libraries' in values:
290 for lib in values['libraries']:
291 args.extend(['--lib', lib])
292
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200293 if not outdir.endswith('.zip') and not outdir.endswith('.jar') \
294 and not os.path.exists(outdir):
Mads Ager418d1ca2017-05-22 09:35:49 +0200295 os.makedirs(outdir)
296
Søren Gjesse8ae55eb2018-09-28 11:11:36 +0200297 # Additional flags for the compiler from the configuration file.
298 if 'flags' in values:
299 args.extend(values['flags'].split(' '))
Tamas Keneza730a7e2018-12-10 15:02:28 +0100300 if options.compiler in R8_COMPILERS:
Søren Gjesse932881f2017-06-13 10:43:36 +0200301 if 'r8-flags' in values:
302 args.extend(values['r8-flags'].split(' '))
Tamas Kenez139acc12017-06-14 17:14:58 +0200303
Søren Gjesse8ae55eb2018-09-28 11:11:36 +0200304 # Additional flags for the compiler from the command line.
Tamas Kenez139acc12017-06-14 17:14:58 +0200305 if options.compiler_flags:
306 args.extend(options.compiler_flags.split(' '))
307 if options.r8_flags:
308 args.extend(options.r8_flags.split(' '))
Mads Ager418d1ca2017-05-22 09:35:49 +0200309
310 if inputs:
311 args.extend(inputs)
312
Tamas Kenezf2ee2a32017-06-21 10:30:20 +0200313 t0 = time.time()
Mads Ager418d1ca2017-05-22 09:35:49 +0200314 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 Kenezfc34cd82017-07-13 12:43:57 +0200318 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 Keneza730a7e2018-12-10 15:02:28 +0100322 if options.compiler in R8_COMPILERS and app_provided_pg_conf:
Mathias Ravdd6a6de2018-05-18 10:18:33 +0200323 # 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 Wind1f4172c2018-09-06 16:29:03 +0200332 build = not options.no_build and not options.golem
Rico Wind5fdec152018-12-17 09:16:14 +0100333 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 Adamsen21c66602018-08-09 16:22:54 +0200341 if exit_code != 0:
Rico Wind5fdec152018-12-17 09:16:14 +0100342 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 Adamsen21c66602018-08-09 16:22:54 +0200349
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200350 if options.print_memoryuse:
351 print('{}(MemoryUse): {}'
352 .format(options.print_memoryuse,
353 utils.grep_memoryuse(options.track_memory_to_file)))
Mads Ager418d1ca2017-05-22 09:35:49 +0200354
Tamas Kenezf2ee2a32017-06-21 10:30:20 +0200355 if options.print_runtimeraw:
356 print('{}(RunTimeRaw): {} ms'
357 .format(options.print_runtimeraw, 1000.0 * (time.time() - t0)))
358
Tamas Kenez02bff032017-07-18 12:13:58 +0200359 if options.print_dexsegments:
360 dex_files = glob(os.path.join(outdir, '*.dex'))
361 utils.print_dexsegments(options.print_dexsegments, dex_files)
Rico Windb57bbc12018-09-20 19:23:32 +0200362 return 0
Tamas Kenez02bff032017-07-18 12:13:58 +0200363
Mads Ager418d1ca2017-05-22 09:35:49 +0200364if __name__ == '__main__':
Tamas Kenez5b1c5852017-07-21 13:38:33 +0200365 sys.exit(main(sys.argv[1:]))