blob: 574830ce2556962027e4aa253ed40c7d9bffba24 [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
Mads Ager418d1ca2017-05-22 09:35:49 +020012
13import gmscore_data
14import youtube_data
Søren Gjesse5ecb04a2017-06-13 09:44:32 +020015import gmail_data
Mads Ager418d1ca2017-05-22 09:35:49 +020016
17TYPES = ['dex', 'deploy', 'proguarded']
Søren Gjesse5ecb04a2017-06-13 09:44:32 +020018APPS = ['gmscore', 'youtube', 'gmail']
Mads Ager418d1ca2017-05-22 09:35:49 +020019
20def ParseOptions():
21 result = optparse.OptionParser()
Søren Gjesse932881f2017-06-13 10:43:36 +020022 result.add_option('--compiler',
23 help='',
24 default='r8',
25 choices=['d8', 'r8'])
Mads Ager418d1ca2017-05-22 09:35:49 +020026 result.add_option('--app',
27 help='',
28 default='gmscore',
29 choices=APPS)
30 result.add_option('--type',
Tamas Kenez3fdaabd2017-06-15 13:05:12 +020031 help='Default for R8: deploy, for D8: proguarded',
Mads Ager418d1ca2017-05-22 09:35:49 +020032 choices=TYPES)
33 result.add_option('--out',
34 help='',
35 default=os.getcwd())
36 result.add_option('--no-build',
37 help='',
38 default=False,
39 action='store_true')
40 result.add_option('--no-libraries',
41 help='',
42 default=False,
43 action='store_true')
44 result.add_option('--no-debug',
45 help='Run without debug asserts.',
46 default=False,
47 action='store_true')
48 result.add_option('--version',
49 help='')
50 result.add_option('-k',
51 help='Override the default ProGuard keep rules')
Tamas Kenez139acc12017-06-14 17:14:58 +020052 result.add_option('--compiler-flags',
53 help='Additional option(s) for the compiler. ' +
54 'If passing several options use a quoted string.')
Mads Ager418d1ca2017-05-22 09:35:49 +020055 result.add_option('--r8-flags',
Tamas Kenez139acc12017-06-14 17:14:58 +020056 help='Additional option(s) for the compiler. ' +
57 'Same as --compiler-flags, keeping it for backward compatibility. ' +
Mads Ager418d1ca2017-05-22 09:35:49 +020058 'If passing several options use a quoted string.')
59 result.add_option('--track-memory-to-file',
60 help='Track how much memory the jvm is using while ' +
61 ' compiling. Output to the specified file.')
62 result.add_option('--profile',
63 help='Profile R8 run.',
64 default=False,
65 action='store_true')
66 result.add_option('--dump-args-file',
67 help='Dump a file with the arguments for the specified ' +
68 'configuration. For use as a @<file> argument to perform ' +
69 'the run.')
70 return result.parse_args()
71
Søren Gjesse3a5aed92017-06-14 15:36:02 +020072# Most apps have the -printmapping and -printseeds in the Proguard
73# configuration. However we don't want to write these files in these
74# locations. Instead generate an auxiliary Proguard configuration
75# placing these two output files together with the dex output.
76def GenerateAdditionalProguardConfiguration(temp, outdir):
77 name = "output.config"
78 with open(os.path.join(temp, name), 'w') as file:
79 file.write('-printmapping ' + os.path.join(outdir, 'proguard.map') + "\n")
80 file.write('-printseeds ' + os.path.join(outdir, 'proguard.seeds') + "\n")
81 return os.path.abspath(file.name)
82
Mads Ager418d1ca2017-05-22 09:35:49 +020083def main():
84 (options, args) = ParseOptions()
85 outdir = options.out
86 data = None
87 if options.app == 'gmscore':
88 options.version = options.version or 'v9'
89 data = gmscore_data
90 elif options.app == 'youtube':
Søren Gjessecc33fb42017-06-09 10:25:08 +020091 options.version = options.version or '12.22'
Mads Ager418d1ca2017-05-22 09:35:49 +020092 data = youtube_data
Søren Gjesse5ecb04a2017-06-13 09:44:32 +020093 elif options.app == 'gmail':
94 options.version = options.version or '170604.16'
95 data = gmail_data
Mads Ager418d1ca2017-05-22 09:35:49 +020096 else:
97 raise 'Unexpected'
98
99 if not options.version in data.VERSIONS.keys():
100 print 'No version %s for application %s' % (options.version, options.app)
101 print 'Valid versions are %s' % data.VERSIONS.keys()
102 return 1
103
104 version = data.VERSIONS[options.version]
105
Tamas Kenez3fdaabd2017-06-15 13:05:12 +0200106 if not options.type:
107 options.type = 'deploy' if options.compiler == 'r8' \
108 else 'proguarded'
109
Mads Ager418d1ca2017-05-22 09:35:49 +0200110 if options.type not in version:
111 print 'No type %s for version %s' % (options.type, options.version)
112 print 'Valid types are %s' % version.keys()
113 return 1
114 values = version[options.type]
115 inputs = None
Søren Gjesse932881f2017-06-13 10:43:36 +0200116 # For R8 'deploy' the JAR is located using the Proguard configuration -injars option.
117 if 'inputs' in values and (options.compiler != 'r8' or options.type != 'deploy'):
Mads Ager418d1ca2017-05-22 09:35:49 +0200118 inputs = values['inputs']
119
120 args.extend(['--output', outdir])
Søren Gjesse932881f2017-06-13 10:43:36 +0200121
122 if options.compiler == 'r8':
123 if 'pgmap' in values:
124 args.extend(['--pg-map', values['pgmap']])
125 if 'pgconf' in values and not options.k:
126 for pgconf in values['pgconf']:
127 args.extend(['--pg-conf', pgconf])
128 if options.k:
129 args.extend(['--pg-conf', options.k])
Søren Gjessef1bc41e2017-06-19 16:33:16 +0200130 if 'multidexrules' in values:
131 for rules in values['multidexrules']:
132 args.extend(['--multidex-rules', rules])
Søren Gjesse932881f2017-06-13 10:43:36 +0200133
Mads Ager418d1ca2017-05-22 09:35:49 +0200134 if not options.no_libraries and 'libraries' in values:
135 for lib in values['libraries']:
136 args.extend(['--lib', lib])
137
138 if not outdir.endswith('.zip') and not outdir.endswith('.jar') and not os.path.exists(outdir):
139 os.makedirs(outdir)
140
Søren Gjesse932881f2017-06-13 10:43:36 +0200141 if options.compiler == 'r8':
142 if 'r8-flags' in values:
143 args.extend(values['r8-flags'].split(' '))
Tamas Kenez139acc12017-06-14 17:14:58 +0200144
145 if options.compiler_flags:
146 args.extend(options.compiler_flags.split(' '))
147 if options.r8_flags:
148 args.extend(options.r8_flags.split(' '))
Mads Ager418d1ca2017-05-22 09:35:49 +0200149
150 if inputs:
151 args.extend(inputs)
152
153 if options.dump_args_file:
154 with open(options.dump_args_file, 'w') as args_file:
155 args_file.writelines([arg + os.linesep for arg in args])
156 else:
Søren Gjesse932881f2017-06-13 10:43:36 +0200157 if options.compiler == 'd8':
158 d8.run(args, not options.no_build, not options.no_debug, options.profile,
159 options.track_memory_to_file)
160 else:
Søren Gjesse3a5aed92017-06-14 15:36:02 +0200161 with utils.TempDir() as temp:
162 if outdir.endswith('.zip') or outdir.endswith('.jar'):
163 pg_outdir = os.path.dirname(outdir)
164 else:
165 pg_outdir = outdir
166 additional_pg_conf = GenerateAdditionalProguardConfiguration(
167 temp, os.path.abspath(pg_outdir))
168 args.extend(['--pg-conf', additional_pg_conf])
169 r8.run(args, not options.no_build, not options.no_debug, options.profile,
170 options.track_memory_to_file)
Mads Ager418d1ca2017-05-22 09:35:49 +0200171
172if __name__ == '__main__':
173 sys.exit(main())