blob: e5ab66ce504baca1135579409908315a79f1b972 [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']
Mads Ager418d1ca2017-05-22 09:35:49 +020027
Tamas Kenez5b1c5852017-07-21 13:38:33 +020028def ParseOptions(argv):
Mads Ager418d1ca2017-05-22 09:35:49 +020029 result = optparse.OptionParser()
Søren Gjesse932881f2017-06-13 10:43:36 +020030 result.add_option('--compiler',
Rico Windb57bbc12018-09-20 19:23:32 +020031 help='The compiler to use',
Tamas Kenez5b1c5852017-07-21 13:38:33 +020032 choices=COMPILERS)
Mads Ager418d1ca2017-05-22 09:35:49 +020033 result.add_option('--app',
Rico Windb57bbc12018-09-20 19:23:32 +020034 help='What app to run on',
Mads Ager418d1ca2017-05-22 09:35:49 +020035 choices=APPS)
Rico Windb57bbc12018-09-20 19:23:32 +020036 result.add_option('--run-all',
37 help='Compile all possible combinations',
38 default=False,
39 action='store_true')
Mads Ager418d1ca2017-05-22 09:35:49 +020040 result.add_option('--type',
Tamas Kenez3fdaabd2017-06-15 13:05:12 +020041 help='Default for R8: deploy, for D8: proguarded',
Mads Ager418d1ca2017-05-22 09:35:49 +020042 choices=TYPES)
43 result.add_option('--out',
Rico Windb57bbc12018-09-20 19:23:32 +020044 help='Where to place the output',
Rico Winde9485ba2018-10-01 07:04:16 +020045 default=utils.BUILD)
Mads Ager418d1ca2017-05-22 09:35:49 +020046 result.add_option('--no-build',
Rico Windb57bbc12018-09-20 19:23:32 +020047 help='Run without building first',
Mads Ager418d1ca2017-05-22 09:35:49 +020048 default=False,
49 action='store_true')
Rico Wind1f4172c2018-09-06 16:29:03 +020050 result.add_option('--golem',
Rico Windb57bbc12018-09-20 19:23:32 +020051 help='Running on golem, do not build or download',
Rico Wind1f4172c2018-09-06 16:29:03 +020052 default=False,
53 action='store_true')
Rico Wind139eece2018-09-25 09:42:09 +020054 result.add_option('--ignore-java-version',
55 help='Do not check java version',
56 default=False,
57 action='store_true')
Mads Ager418d1ca2017-05-22 09:35:49 +020058 result.add_option('--no-libraries',
Rico Windb57bbc12018-09-20 19:23:32 +020059 help='Do not pass in libraries, even if they exist in conf',
Mads Ager418d1ca2017-05-22 09:35:49 +020060 default=False,
61 action='store_true')
62 result.add_option('--no-debug',
63 help='Run without debug asserts.',
64 default=False,
65 action='store_true')
66 result.add_option('--version',
Rico Windb57bbc12018-09-20 19:23:32 +020067 help='The version of the app to run')
Mads Ager418d1ca2017-05-22 09:35:49 +020068 result.add_option('-k',
69 help='Override the default ProGuard keep rules')
Tamas Kenez139acc12017-06-14 17:14:58 +020070 result.add_option('--compiler-flags',
71 help='Additional option(s) for the compiler. ' +
72 'If passing several options use a quoted string.')
Mads Ager418d1ca2017-05-22 09:35:49 +020073 result.add_option('--r8-flags',
Tamas Kenez139acc12017-06-14 17:14:58 +020074 help='Additional option(s) for the compiler. ' +
Tamas Kenezfc34cd82017-07-13 12:43:57 +020075 'Same as --compiler-flags, keeping it for backward'
76 ' compatibility. ' +
Mads Ager418d1ca2017-05-22 09:35:49 +020077 'If passing several options use a quoted string.')
Tamas Kenezfc34cd82017-07-13 12:43:57 +020078 # TODO(tamaskenez) remove track-memory-to-file as soon as we updated golem
79 # to use --print-memoryuse instead
Mads Ager418d1ca2017-05-22 09:35:49 +020080 result.add_option('--track-memory-to-file',
81 help='Track how much memory the jvm is using while ' +
82 ' compiling. Output to the specified file.')
83 result.add_option('--profile',
84 help='Profile R8 run.',
85 default=False,
86 action='store_true')
87 result.add_option('--dump-args-file',
88 help='Dump a file with the arguments for the specified ' +
89 'configuration. For use as a @<file> argument to perform ' +
90 'the run.')
Tamas Kenezf2ee2a32017-06-21 10:30:20 +020091 result.add_option('--print-runtimeraw',
92 metavar='BENCHMARKNAME',
Tamas Kenez02bff032017-07-18 12:13:58 +020093 help='Print the line \'<BENCHMARKNAME>(RunTimeRaw):' +
94 ' <elapsed> ms\' at the end where <elapsed> is' +
95 ' the elapsed time in milliseconds.')
Tamas Kenezfc34cd82017-07-13 12:43:57 +020096 result.add_option('--print-memoryuse',
97 metavar='BENCHMARKNAME',
Tamas Kenez02bff032017-07-18 12:13:58 +020098 help='Print the line \'<BENCHMARKNAME>(MemoryUse):' +
99 ' <mem>\' at the end where <mem> is the peak' +
100 ' peak resident set size (VmHWM) in bytes.')
101 result.add_option('--print-dexsegments',
102 metavar='BENCHMARKNAME',
103 help='Print the sizes of individual dex segments as ' +
104 '\'<BENCHMARKNAME>-<segment>(CodeSize): <bytes>\'')
Tamas Kenez5b1c5852017-07-21 13:38:33 +0200105 return result.parse_args(argv)
Mads Ager418d1ca2017-05-22 09:35:49 +0200106
Søren Gjesse59459582018-04-06 10:12:45 +0200107# Most apps have the -printmapping, -printseeds and -printusage in the
108# Proguard configuration. However we don't want to write these files
109# in the locations specified. Instead generate an auxiliary Proguard
110# configuration placing these two output files together with the dex
111# output.
Søren Gjesse3a5aed92017-06-14 15:36:02 +0200112def GenerateAdditionalProguardConfiguration(temp, outdir):
113 name = "output.config"
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200114 with open(os.path.join(temp, name), 'w') as f:
115 f.write('-printmapping ' + os.path.join(outdir, 'proguard.map') + "\n")
116 f.write('-printseeds ' + os.path.join(outdir, 'proguard.seeds') + "\n")
Søren Gjesse59459582018-04-06 10:12:45 +0200117 f.write('-printusage ' + os.path.join(outdir, 'proguard.usage') + "\n")
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200118 return os.path.abspath(f.name)
Søren Gjesse3a5aed92017-06-14 15:36:02 +0200119
Rico Wind3f9302b2018-09-21 08:53:09 +0200120# Please add bug number for disabled permutations and please explicitly
121# do Bug: #BUG in the commit message of disabling to ensure re-enabling
122DISABLED_PERMUTATIONS = [
Ian Zernyd459b5f2018-10-03 06:46:21 +0200123 # (app, version, type), e.g., ('gmail', '180826.15', 'deploy'),
Rico Wind3f9302b2018-09-21 08:53:09 +0200124]
125
Rico Windb57bbc12018-09-20 19:23:32 +0200126def get_permutations():
127 data_providers = {
128 'gmscore': gmscore_data,
Christoffer Quist Adamsena2a58772018-10-03 09:47:46 +0200129 'nest': nest_data,
Rico Windb57bbc12018-09-20 19:23:32 +0200130 'youtube': youtube_data,
131 'chrome': chrome_data,
132 'gmail': gmail_data
133 }
134 # Check to ensure that we add all variants here.
135 assert len(APPS) == len(data_providers)
136 for app, data in data_providers.iteritems():
137 for version in data.VERSIONS:
138 for type in data.VERSIONS[version]:
Rico Wind3f9302b2018-09-21 08:53:09 +0200139 if (app, version, type) not in DISABLED_PERMUTATIONS:
Tamas Keneza730a7e2018-12-10 15:02:28 +0100140 for use_r8lib in [False, True]:
141 yield app, version, type, use_r8lib
Rico Windb57bbc12018-09-20 19:23:32 +0200142
143def run_all(options, args):
144 # Args will be destroyed
145 assert len(args) == 0
Tamas Keneza730a7e2018-12-10 15:02:28 +0100146 for name, version, type, use_r8lib in get_permutations():
Rico Windb57bbc12018-09-20 19:23:32 +0200147 compiler = 'r8' if type == 'deploy' else 'd8'
Tamas Keneza730a7e2018-12-10 15:02:28 +0100148 if use_r8lib:
149 compiler = 'r8lib-' + compiler
150 print('Executing %s with %s %s %s' % (compiler, name, version, type))
151
Rico Windb57bbc12018-09-20 19:23:32 +0200152 fixed_options = copy.copy(options)
153 fixed_options.app = name
154 fixed_options.version = version
155 fixed_options.compiler = compiler
156 fixed_options.type = type
157 exit_code = run_with_options(fixed_options, [])
158 if exit_code != 0:
159 print('Failed %s %s %s with %s' % (name, version, type, compiler))
160 exit(exit_code)
161
Tamas Kenez5b1c5852017-07-21 13:38:33 +0200162def main(argv):
Tamas Kenez5b1c5852017-07-21 13:38:33 +0200163 (options, args) = ParseOptions(argv)
Rico Wind139eece2018-09-25 09:42:09 +0200164 if not options.ignore_java_version:
165 utils.check_java_version()
166
Rico Windb57bbc12018-09-20 19:23:32 +0200167 if options.run_all:
168 return run_all(options, args)
169 return run_with_options(options, args)
170
171def run_with_options(options, args):
172 app_provided_pg_conf = False;
Rico Wind1f4172c2018-09-06 16:29:03 +0200173 if options.golem:
174 golem.link_third_party()
Rico Wind9bd3f5f2018-10-01 10:53:18 +0200175 options.out = os.getcwd()
Mads Ager418d1ca2017-05-22 09:35:49 +0200176 outdir = options.out
177 data = None
178 if options.app == 'gmscore':
179 options.version = options.version or 'v9'
180 data = gmscore_data
Christoffer Quist Adamsena2a58772018-10-03 09:47:46 +0200181 elif options.app == 'nest':
182 options.version = options.version or '20180926'
183 data = nest_data
Mads Ager418d1ca2017-05-22 09:35:49 +0200184 elif options.app == 'youtube':
Søren Gjessecc33fb42017-06-09 10:25:08 +0200185 options.version = options.version or '12.22'
Mads Ager418d1ca2017-05-22 09:35:49 +0200186 data = youtube_data
Rico Wind86bfc832018-09-18 07:48:21 +0200187 elif options.app == 'chrome':
188 options.version = options.version or 'default'
189 data = chrome_data
Søren Gjesse5ecb04a2017-06-13 09:44:32 +0200190 elif options.app == 'gmail':
191 options.version = options.version or '170604.16'
192 data = gmail_data
Mads Ager418d1ca2017-05-22 09:35:49 +0200193 else:
Tamas Kenez5b1c5852017-07-21 13:38:33 +0200194 raise Exception("You need to specify '--app={}'".format('|'.join(APPS)))
195
196 if options.compiler not in COMPILERS:
197 raise Exception("You need to specify '--compiler={}'"
198 .format('|'.join(COMPILERS)))
Mads Ager418d1ca2017-05-22 09:35:49 +0200199
200 if not options.version in data.VERSIONS.keys():
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200201 print('No version {} for application {}'
202 .format(options.version, options.app))
203 print('Valid versions are {}'.format(data.VERSIONS.keys()))
Mads Ager418d1ca2017-05-22 09:35:49 +0200204 return 1
205
206 version = data.VERSIONS[options.version]
207
Tamas Kenez3fdaabd2017-06-15 13:05:12 +0200208 if not options.type:
Tamas Keneza730a7e2018-12-10 15:02:28 +0100209 options.type = 'deploy' if options.compiler in R8_COMPILERS \
Tamas Kenez3fdaabd2017-06-15 13:05:12 +0200210 else 'proguarded'
211
Mads Ager418d1ca2017-05-22 09:35:49 +0200212 if options.type not in version:
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200213 print('No type {} for version {}'.format(options.type, options.version))
214 print('Valid types are {}'.format(version.keys()))
Mads Ager418d1ca2017-05-22 09:35:49 +0200215 return 1
216 values = version[options.type]
217 inputs = None
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200218 # For R8 'deploy' the JAR is located using the Proguard configuration
Christoffer Quist Adamsena2a58772018-10-03 09:47:46 +0200219 # -injars option. For chrome and nest we don't have the injars in the
220 # proguard files.
Tamas Keneza730a7e2018-12-10 15:02:28 +0100221 if 'inputs' in values and (options.compiler not in R8_COMPILERS
Rico Wind86bfc832018-09-18 07:48:21 +0200222 or options.type != 'deploy'
Christoffer Quist Adamsena2a58772018-10-03 09:47:46 +0200223 or options.app == 'chrome'
224 or options.app == 'nest'):
Mads Ager418d1ca2017-05-22 09:35:49 +0200225 inputs = values['inputs']
226
227 args.extend(['--output', outdir])
Ian Zerny877c1862017-07-06 11:12:26 +0200228 if 'min-api' in values:
229 args.extend(['--min-api', values['min-api']])
Søren Gjesse932881f2017-06-13 10:43:36 +0200230
Søren Gjesse8ae55eb2018-09-28 11:11:36 +0200231 if 'main-dex-list' in values:
232 args.extend(['--main-dex-list', values['main-dex-list']])
233
Tamas Keneza730a7e2018-12-10 15:02:28 +0100234 if options.compiler in R8_COMPILERS:
Søren Gjesse932881f2017-06-13 10:43:36 +0200235 if 'pgconf' in values and not options.k:
236 for pgconf in values['pgconf']:
237 args.extend(['--pg-conf', pgconf])
Søren Gjesseda2ac8d2017-06-22 14:14:36 +0200238 app_provided_pg_conf = True
Søren Gjesse932881f2017-06-13 10:43:36 +0200239 if options.k:
240 args.extend(['--pg-conf', options.k])
Søren Gjessec801ecc2017-08-03 13:40:06 +0200241 if 'maindexrules' in values:
242 for rules in values['maindexrules']:
243 args.extend(['--main-dex-rules', rules])
Søren Gjesse932881f2017-06-13 10:43:36 +0200244
Mads Ager418d1ca2017-05-22 09:35:49 +0200245 if not options.no_libraries and 'libraries' in values:
246 for lib in values['libraries']:
247 args.extend(['--lib', lib])
248
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200249 if not outdir.endswith('.zip') and not outdir.endswith('.jar') \
250 and not os.path.exists(outdir):
Mads Ager418d1ca2017-05-22 09:35:49 +0200251 os.makedirs(outdir)
252
Søren Gjesse8ae55eb2018-09-28 11:11:36 +0200253 # Additional flags for the compiler from the configuration file.
254 if 'flags' in values:
255 args.extend(values['flags'].split(' '))
Tamas Keneza730a7e2018-12-10 15:02:28 +0100256 if options.compiler in R8_COMPILERS:
Søren Gjesse932881f2017-06-13 10:43:36 +0200257 if 'r8-flags' in values:
258 args.extend(values['r8-flags'].split(' '))
Tamas Kenez139acc12017-06-14 17:14:58 +0200259
Søren Gjesse8ae55eb2018-09-28 11:11:36 +0200260 # Additional flags for the compiler from the command line.
Tamas Kenez139acc12017-06-14 17:14:58 +0200261 if options.compiler_flags:
262 args.extend(options.compiler_flags.split(' '))
263 if options.r8_flags:
264 args.extend(options.r8_flags.split(' '))
Mads Ager418d1ca2017-05-22 09:35:49 +0200265
266 if inputs:
267 args.extend(inputs)
268
Tamas Kenezf2ee2a32017-06-21 10:30:20 +0200269 t0 = time.time()
Mads Ager418d1ca2017-05-22 09:35:49 +0200270 if options.dump_args_file:
271 with open(options.dump_args_file, 'w') as args_file:
272 args_file.writelines([arg + os.linesep for arg in args])
273 else:
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200274 with utils.TempDir() as temp:
275 if options.print_memoryuse and not options.track_memory_to_file:
276 options.track_memory_to_file = os.path.join(temp,
277 utils.MEMORY_USE_TMP_FILE)
Tamas Keneza730a7e2018-12-10 15:02:28 +0100278 if options.compiler in R8_COMPILERS and app_provided_pg_conf:
Mathias Ravdd6a6de2018-05-18 10:18:33 +0200279 # Ensure that output of -printmapping and -printseeds go to the output
280 # location and not where the app Proguard configuration places them.
281 if outdir.endswith('.zip') or outdir.endswith('.jar'):
282 pg_outdir = os.path.dirname(outdir)
283 else:
284 pg_outdir = outdir
285 additional_pg_conf = GenerateAdditionalProguardConfiguration(
286 temp, os.path.abspath(pg_outdir))
287 args.extend(['--pg-conf', additional_pg_conf])
Rico Wind1f4172c2018-09-06 16:29:03 +0200288 build = not options.no_build and not options.golem
Christoffer Quist Adamsen21c66602018-08-09 16:22:54 +0200289 exit_code = toolhelper.run(options.compiler, args,
Rico Wind1f4172c2018-09-06 16:29:03 +0200290 build=build,
Mathias Ravdd6a6de2018-05-18 10:18:33 +0200291 debug=not options.no_debug,
292 profile=options.profile,
Mathias Rava4042f52018-05-23 09:58:58 +0200293 track_memory_file=options.track_memory_to_file)
Christoffer Quist Adamsen21c66602018-08-09 16:22:54 +0200294 if exit_code != 0:
295 return exit_code
296
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200297 if options.print_memoryuse:
298 print('{}(MemoryUse): {}'
299 .format(options.print_memoryuse,
300 utils.grep_memoryuse(options.track_memory_to_file)))
Mads Ager418d1ca2017-05-22 09:35:49 +0200301
Tamas Kenezf2ee2a32017-06-21 10:30:20 +0200302 if options.print_runtimeraw:
303 print('{}(RunTimeRaw): {} ms'
304 .format(options.print_runtimeraw, 1000.0 * (time.time() - t0)))
305
Tamas Kenez02bff032017-07-18 12:13:58 +0200306 if options.print_dexsegments:
307 dex_files = glob(os.path.join(outdir, '*.dex'))
308 utils.print_dexsegments(options.print_dexsegments, dex_files)
Rico Windb57bbc12018-09-20 19:23:32 +0200309 return 0
Tamas Kenez02bff032017-07-18 12:13:58 +0200310
Mads Ager418d1ca2017-05-22 09:35:49 +0200311if __name__ == '__main__':
Tamas Kenez5b1c5852017-07-21 13:38:33 +0200312 sys.exit(main(sys.argv[1:]))