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 |
| 9 | import sys |
| 10 | |
| 11 | import gmscore_data |
| 12 | import youtube_data |
| 13 | |
| 14 | TYPES = ['dex', 'deploy', 'proguarded'] |
| 15 | APPS = ['gmscore', 'youtube'] |
| 16 | |
| 17 | def ParseOptions(): |
| 18 | result = optparse.OptionParser() |
| 19 | result.add_option('--app', |
| 20 | help='', |
| 21 | default='gmscore', |
| 22 | choices=APPS) |
| 23 | result.add_option('--type', |
| 24 | help='', |
| 25 | default='deploy', |
| 26 | choices=TYPES) |
| 27 | result.add_option('--out', |
| 28 | help='', |
| 29 | default=os.getcwd()) |
| 30 | result.add_option('--no-build', |
| 31 | help='', |
| 32 | default=False, |
| 33 | action='store_true') |
| 34 | result.add_option('--no-libraries', |
| 35 | help='', |
| 36 | default=False, |
| 37 | action='store_true') |
| 38 | result.add_option('--no-debug', |
| 39 | help='Run without debug asserts.', |
| 40 | default=False, |
| 41 | action='store_true') |
| 42 | result.add_option('--version', |
| 43 | help='') |
| 44 | result.add_option('-k', |
| 45 | help='Override the default ProGuard keep rules') |
| 46 | result.add_option('--r8-flags', |
| 47 | help='Additional option(s) for R8. ' + |
| 48 | 'If passing several options use a quoted string.') |
| 49 | result.add_option('--track-memory-to-file', |
| 50 | help='Track how much memory the jvm is using while ' + |
| 51 | ' compiling. Output to the specified file.') |
| 52 | result.add_option('--profile', |
| 53 | help='Profile R8 run.', |
| 54 | default=False, |
| 55 | action='store_true') |
| 56 | result.add_option('--dump-args-file', |
| 57 | help='Dump a file with the arguments for the specified ' + |
| 58 | 'configuration. For use as a @<file> argument to perform ' + |
| 59 | 'the run.') |
| 60 | return result.parse_args() |
| 61 | |
| 62 | def main(): |
| 63 | (options, args) = ParseOptions() |
| 64 | outdir = options.out |
| 65 | data = None |
| 66 | if options.app == 'gmscore': |
| 67 | options.version = options.version or 'v9' |
| 68 | data = gmscore_data |
| 69 | elif options.app == 'youtube': |
| 70 | options.version = options.version or '12.10' |
| 71 | data = youtube_data |
| 72 | else: |
| 73 | raise 'Unexpected' |
| 74 | |
| 75 | if not options.version in data.VERSIONS.keys(): |
| 76 | print 'No version %s for application %s' % (options.version, options.app) |
| 77 | print 'Valid versions are %s' % data.VERSIONS.keys() |
| 78 | return 1 |
| 79 | |
| 80 | version = data.VERSIONS[options.version] |
| 81 | |
| 82 | if options.type not in version: |
| 83 | print 'No type %s for version %s' % (options.type, options.version) |
| 84 | print 'Valid types are %s' % version.keys() |
| 85 | return 1 |
| 86 | values = version[options.type] |
| 87 | inputs = None |
| 88 | # For 'deploy' the JAR is located using the Proguard configuration -injars option. |
| 89 | if 'inputs' in values and options.type != 'deploy': |
| 90 | inputs = values['inputs'] |
| 91 | |
| 92 | args.extend(['--output', outdir]) |
| 93 | if 'pgmap' in values: |
| 94 | args.extend(['--pg-map', values['pgmap']]) |
| 95 | if 'pgconf' in values and not options.k: |
| 96 | for pgconf in values['pgconf']: |
| 97 | args.extend(['--pg-conf', pgconf]) |
| 98 | if options.k: |
| 99 | args.extend(['--pg-conf', options.k]) |
| 100 | if not options.no_libraries and 'libraries' in values: |
| 101 | for lib in values['libraries']: |
| 102 | args.extend(['--lib', lib]) |
| 103 | |
| 104 | if not outdir.endswith('.zip') and not outdir.endswith('.jar') and not os.path.exists(outdir): |
| 105 | os.makedirs(outdir) |
| 106 | |
| 107 | if 'r8-flags' in values: |
| 108 | args.extend(values['r8-flags'].split(' ')) |
| 109 | if options.r8_flags: |
| 110 | args.extend(options.r8_flags.split(' ')) |
| 111 | |
| 112 | if inputs: |
| 113 | args.extend(inputs) |
| 114 | |
| 115 | if options.dump_args_file: |
| 116 | with open(options.dump_args_file, 'w') as args_file: |
| 117 | args_file.writelines([arg + os.linesep for arg in args]) |
| 118 | else: |
| 119 | r8.run(args, not options.no_build, not options.no_debug, options.profile, |
| 120 | options.track_memory_to_file) |
| 121 | |
| 122 | if __name__ == '__main__': |
| 123 | sys.exit(main()) |