Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 1 | #!/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 | |
| 6 | import optparse |
| 7 | import os |
| 8 | import r8 |
Søren Gjesse | 932881f | 2017-06-13 10:43:36 +0200 | [diff] [blame] | 9 | import d8 |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 10 | import sys |
Søren Gjesse | 3a5aed9 | 2017-06-14 15:36:02 +0200 | [diff] [blame] | 11 | import utils |
Tamas Kenez | f2ee2a3 | 2017-06-21 10:30:20 +0200 | [diff] [blame^] | 12 | import time |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 13 | |
| 14 | import gmscore_data |
| 15 | import youtube_data |
Søren Gjesse | 5ecb04a | 2017-06-13 09:44:32 +0200 | [diff] [blame] | 16 | import gmail_data |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 17 | |
| 18 | TYPES = ['dex', 'deploy', 'proguarded'] |
Søren Gjesse | 5ecb04a | 2017-06-13 09:44:32 +0200 | [diff] [blame] | 19 | APPS = ['gmscore', 'youtube', 'gmail'] |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 20 | |
| 21 | def ParseOptions(): |
| 22 | result = optparse.OptionParser() |
Søren Gjesse | 932881f | 2017-06-13 10:43:36 +0200 | [diff] [blame] | 23 | result.add_option('--compiler', |
| 24 | help='', |
| 25 | default='r8', |
| 26 | choices=['d8', 'r8']) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 27 | result.add_option('--app', |
| 28 | help='', |
| 29 | default='gmscore', |
| 30 | choices=APPS) |
| 31 | result.add_option('--type', |
Tamas Kenez | 3fdaabd | 2017-06-15 13:05:12 +0200 | [diff] [blame] | 32 | help='Default for R8: deploy, for D8: proguarded', |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 33 | 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 Kenez | 139acc1 | 2017-06-14 17:14:58 +0200 | [diff] [blame] | 53 | result.add_option('--compiler-flags', |
| 54 | help='Additional option(s) for the compiler. ' + |
| 55 | 'If passing several options use a quoted string.') |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 56 | result.add_option('--r8-flags', |
Tamas Kenez | 139acc1 | 2017-06-14 17:14:58 +0200 | [diff] [blame] | 57 | help='Additional option(s) for the compiler. ' + |
| 58 | 'Same as --compiler-flags, keeping it for backward compatibility. ' + |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 59 | '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 Kenez | f2ee2a3 | 2017-06-21 10:30:20 +0200 | [diff] [blame^] | 71 | 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 Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 76 | return result.parse_args() |
| 77 | |
Søren Gjesse | 3a5aed9 | 2017-06-14 15:36:02 +0200 | [diff] [blame] | 78 | # 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. |
| 82 | def 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 Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 89 | def 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 Gjesse | cc33fb4 | 2017-06-09 10:25:08 +0200 | [diff] [blame] | 97 | options.version = options.version or '12.22' |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 98 | data = youtube_data |
Søren Gjesse | 5ecb04a | 2017-06-13 09:44:32 +0200 | [diff] [blame] | 99 | elif options.app == 'gmail': |
| 100 | options.version = options.version or '170604.16' |
| 101 | data = gmail_data |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 102 | 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 Kenez | 3fdaabd | 2017-06-15 13:05:12 +0200 | [diff] [blame] | 112 | if not options.type: |
| 113 | options.type = 'deploy' if options.compiler == 'r8' \ |
| 114 | else 'proguarded' |
| 115 | |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 116 | 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 Gjesse | 932881f | 2017-06-13 10:43:36 +0200 | [diff] [blame] | 122 | # 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 Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 124 | inputs = values['inputs'] |
| 125 | |
| 126 | args.extend(['--output', outdir]) |
Søren Gjesse | 932881f | 2017-06-13 10:43:36 +0200 | [diff] [blame] | 127 | |
| 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 Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 137 | 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 Gjesse | 932881f | 2017-06-13 10:43:36 +0200 | [diff] [blame] | 144 | if options.compiler == 'r8': |
| 145 | if 'r8-flags' in values: |
| 146 | args.extend(values['r8-flags'].split(' ')) |
Tamas Kenez | 139acc1 | 2017-06-14 17:14:58 +0200 | [diff] [blame] | 147 | |
| 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 Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 152 | |
| 153 | if inputs: |
| 154 | args.extend(inputs) |
| 155 | |
Tamas Kenez | f2ee2a3 | 2017-06-21 10:30:20 +0200 | [diff] [blame^] | 156 | t0 = time.time() |
| 157 | |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 158 | 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 Gjesse | 932881f | 2017-06-13 10:43:36 +0200 | [diff] [blame] | 162 | 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 Gjesse | 3a5aed9 | 2017-06-14 15:36:02 +0200 | [diff] [blame] | 166 | 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 Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 176 | |
Tamas Kenez | f2ee2a3 | 2017-06-21 10:30:20 +0200 | [diff] [blame^] | 177 | if options.print_runtimeraw: |
| 178 | print('{}(RunTimeRaw): {} ms' |
| 179 | .format(options.print_runtimeraw, 1000.0 * (time.time() - t0))) |
| 180 | |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 181 | if __name__ == '__main__': |
| 182 | sys.exit(main()) |