blob: 133f85218f920269c3df8ea6b677385071016c35 [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
6import optparse
7import os
8import r8
Søren Gjesse932881f2017-06-13 10:43:36 +02009import d8
Mads Ager418d1ca2017-05-22 09:35:49 +020010import sys
Søren Gjesse3a5aed92017-06-14 15:36:02 +020011import utils
Tamas Kenezf2ee2a32017-06-21 10:30:20 +020012import time
Mads Ager418d1ca2017-05-22 09:35:49 +020013
14import gmscore_data
15import youtube_data
Søren Gjesse5ecb04a2017-06-13 09:44:32 +020016import gmail_data
Mads Ager418d1ca2017-05-22 09:35:49 +020017
18TYPES = ['dex', 'deploy', 'proguarded']
Søren Gjesse5ecb04a2017-06-13 09:44:32 +020019APPS = ['gmscore', 'youtube', 'gmail']
Mads Ager418d1ca2017-05-22 09:35:49 +020020
21def ParseOptions():
22 result = optparse.OptionParser()
Søren Gjesse932881f2017-06-13 10:43:36 +020023 result.add_option('--compiler',
24 help='',
25 default='r8',
26 choices=['d8', 'r8'])
Mads Ager418d1ca2017-05-22 09:35:49 +020027 result.add_option('--app',
28 help='',
29 default='gmscore',
30 choices=APPS)
31 result.add_option('--type',
Tamas Kenez3fdaabd2017-06-15 13:05:12 +020032 help='Default for R8: deploy, for D8: proguarded',
Mads Ager418d1ca2017-05-22 09:35:49 +020033 choices=TYPES)
34 result.add_option('--out',
35 help='',
36 default=os.getcwd())
37 result.add_option('--no-build',
38 help='',
39 default=False,
40 action='store_true')
41 result.add_option('--no-libraries',
42 help='',
43 default=False,
44 action='store_true')
45 result.add_option('--no-debug',
46 help='Run without debug asserts.',
47 default=False,
48 action='store_true')
49 result.add_option('--version',
50 help='')
51 result.add_option('-k',
52 help='Override the default ProGuard keep rules')
Tamas Kenez139acc12017-06-14 17:14:58 +020053 result.add_option('--compiler-flags',
54 help='Additional option(s) for the compiler. ' +
55 'If passing several options use a quoted string.')
Mads Ager418d1ca2017-05-22 09:35:49 +020056 result.add_option('--r8-flags',
Tamas Kenez139acc12017-06-14 17:14:58 +020057 help='Additional option(s) for the compiler. ' +
58 'Same as --compiler-flags, keeping it for backward compatibility. ' +
Mads Ager418d1ca2017-05-22 09:35:49 +020059 'If passing several options use a quoted string.')
60 result.add_option('--track-memory-to-file',
61 help='Track how much memory the jvm is using while ' +
62 ' compiling. Output to the specified file.')
63 result.add_option('--profile',
64 help='Profile R8 run.',
65 default=False,
66 action='store_true')
67 result.add_option('--dump-args-file',
68 help='Dump a file with the arguments for the specified ' +
69 'configuration. For use as a @<file> argument to perform ' +
70 'the run.')
Tamas Kenezf2ee2a32017-06-21 10:30:20 +020071 result.add_option('--print-runtimeraw',
72 metavar='BENCHMARKNAME',
73 help='Prints the line \'<BENCHMARKNAME>(RunTimeRaw):' +
74 ' <elapsed> ms\' at the end where <elapsed> is' +
75 ' the elapsed time in milliseconds.')
Mads Ager418d1ca2017-05-22 09:35:49 +020076 return result.parse_args()
77
Søren Gjesse3a5aed92017-06-14 15:36:02 +020078# Most apps have the -printmapping and -printseeds in the Proguard
79# configuration. However we don't want to write these files in these
80# locations. Instead generate an auxiliary Proguard configuration
81# placing these two output files together with the dex output.
82def GenerateAdditionalProguardConfiguration(temp, outdir):
83 name = "output.config"
84 with open(os.path.join(temp, name), 'w') as file:
85 file.write('-printmapping ' + os.path.join(outdir, 'proguard.map') + "\n")
86 file.write('-printseeds ' + os.path.join(outdir, 'proguard.seeds') + "\n")
87 return os.path.abspath(file.name)
88
Mads Ager418d1ca2017-05-22 09:35:49 +020089def main():
90 (options, args) = ParseOptions()
91 outdir = options.out
92 data = None
93 if options.app == 'gmscore':
94 options.version = options.version or 'v9'
95 data = gmscore_data
96 elif options.app == 'youtube':
Søren Gjessecc33fb42017-06-09 10:25:08 +020097 options.version = options.version or '12.22'
Mads Ager418d1ca2017-05-22 09:35:49 +020098 data = youtube_data
Søren Gjesse5ecb04a2017-06-13 09:44:32 +020099 elif options.app == 'gmail':
100 options.version = options.version or '170604.16'
101 data = gmail_data
Mads Ager418d1ca2017-05-22 09:35:49 +0200102 else:
103 raise 'Unexpected'
104
105 if not options.version in data.VERSIONS.keys():
106 print 'No version %s for application %s' % (options.version, options.app)
107 print 'Valid versions are %s' % data.VERSIONS.keys()
108 return 1
109
110 version = data.VERSIONS[options.version]
111
Tamas Kenez3fdaabd2017-06-15 13:05:12 +0200112 if not options.type:
113 options.type = 'deploy' if options.compiler == 'r8' \
114 else 'proguarded'
115
Mads Ager418d1ca2017-05-22 09:35:49 +0200116 if options.type not in version:
117 print 'No type %s for version %s' % (options.type, options.version)
118 print 'Valid types are %s' % version.keys()
119 return 1
120 values = version[options.type]
121 inputs = None
Søren Gjesse932881f2017-06-13 10:43:36 +0200122 # For R8 'deploy' the JAR is located using the Proguard configuration -injars option.
123 if 'inputs' in values and (options.compiler != 'r8' or options.type != 'deploy'):
Mads Ager418d1ca2017-05-22 09:35:49 +0200124 inputs = values['inputs']
125
126 args.extend(['--output', outdir])
Søren Gjesse932881f2017-06-13 10:43:36 +0200127
128 if options.compiler == 'r8':
129 if 'pgmap' in values:
130 args.extend(['--pg-map', values['pgmap']])
131 if 'pgconf' in values and not options.k:
132 for pgconf in values['pgconf']:
133 args.extend(['--pg-conf', pgconf])
134 if options.k:
135 args.extend(['--pg-conf', options.k])
136
Mads Ager418d1ca2017-05-22 09:35:49 +0200137 if not options.no_libraries and 'libraries' in values:
138 for lib in values['libraries']:
139 args.extend(['--lib', lib])
140
141 if not outdir.endswith('.zip') and not outdir.endswith('.jar') and not os.path.exists(outdir):
142 os.makedirs(outdir)
143
Søren Gjesse932881f2017-06-13 10:43:36 +0200144 if options.compiler == 'r8':
145 if 'r8-flags' in values:
146 args.extend(values['r8-flags'].split(' '))
Tamas Kenez139acc12017-06-14 17:14:58 +0200147
148 if options.compiler_flags:
149 args.extend(options.compiler_flags.split(' '))
150 if options.r8_flags:
151 args.extend(options.r8_flags.split(' '))
Mads Ager418d1ca2017-05-22 09:35:49 +0200152
153 if inputs:
154 args.extend(inputs)
155
Tamas Kenezf2ee2a32017-06-21 10:30:20 +0200156 t0 = time.time()
157
Mads Ager418d1ca2017-05-22 09:35:49 +0200158 if options.dump_args_file:
159 with open(options.dump_args_file, 'w') as args_file:
160 args_file.writelines([arg + os.linesep for arg in args])
161 else:
Søren Gjesse932881f2017-06-13 10:43:36 +0200162 if options.compiler == 'd8':
163 d8.run(args, not options.no_build, not options.no_debug, options.profile,
164 options.track_memory_to_file)
165 else:
Søren Gjesse3a5aed92017-06-14 15:36:02 +0200166 with utils.TempDir() as temp:
167 if outdir.endswith('.zip') or outdir.endswith('.jar'):
168 pg_outdir = os.path.dirname(outdir)
169 else:
170 pg_outdir = outdir
171 additional_pg_conf = GenerateAdditionalProguardConfiguration(
172 temp, os.path.abspath(pg_outdir))
173 args.extend(['--pg-conf', additional_pg_conf])
174 r8.run(args, not options.no_build, not options.no_debug, options.profile,
175 options.track_memory_to_file)
Mads Ager418d1ca2017-05-22 09:35:49 +0200176
Tamas Kenezf2ee2a32017-06-21 10:30:20 +0200177 if options.print_runtimeraw:
178 print('{}(RunTimeRaw): {} ms'
179 .format(options.print_runtimeraw, 1000.0 * (time.time() - t0)))
180
Mads Ager418d1ca2017-05-22 09:35:49 +0200181if __name__ == '__main__':
182 sys.exit(main())