Tamas Kenez | e4d79c4 | 2017-07-21 12:31:50 +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 | # Run ProGuard and the DX or CompatDX (= D8) tool on GmsCore V10. |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | from glob import glob |
| 10 | from os import makedirs |
| 11 | from os.path import exists, join, splitext |
| 12 | from subprocess import check_call |
| 13 | import argparse |
| 14 | import fnmatch |
| 15 | import gmscore_data |
| 16 | import os |
| 17 | import stat |
| 18 | import sys |
| 19 | import time |
| 20 | |
| 21 | import gmail_data |
| 22 | import gmscore_data |
| 23 | import proguard |
| 24 | import utils |
| 25 | import youtube_data |
| 26 | |
| 27 | APPS = ['gmscore', 'youtube'] |
| 28 | DX_JAR = join(utils.REPO_ROOT, 'tools', 'linux', 'dx', 'framework', 'dx.jar') |
Tamas Kenez | e4d79c4 | 2017-07-21 12:31:50 +0200 | [diff] [blame] | 29 | |
| 30 | def parse_arguments(argv): |
| 31 | parser = argparse.ArgumentParser( |
| 32 | description = 'Run ProGuard and the DX tool on GmsCore V10.') |
| 33 | parser.add_argument('--app', required = True, choices = APPS) |
| 34 | parser.add_argument('--out', |
| 35 | help = 'Output directory for the DX tool.', |
| 36 | default = os.getcwd()) |
| 37 | parser.add_argument('--compatdx', |
| 38 | help = 'Use CompatDx (D8) instead of DX.', |
| 39 | default = False, |
| 40 | action = 'store_true') |
| 41 | parser.add_argument('--print-runtimeraw', |
| 42 | metavar = 'BENCHMARKNAME', |
| 43 | help = 'Print the line \'<BENCHMARKNAME>(RunTimeRaw): <elapsed>' + |
| 44 | ' ms\' at the end where <elapsed> is the elapsed time in' + |
| 45 | ' milliseconds.') |
| 46 | parser.add_argument('--print-memoryuse', |
| 47 | metavar='BENCHMARKNAME', |
| 48 | help='Print the line \'<BENCHMARKNAME>(MemoryUse):' + |
| 49 | ' <mem>\' at the end where <mem> is the peak' + |
| 50 | ' peak resident set size (VmHWM) in bytes.') |
| 51 | parser.add_argument('--print-dexsegments', |
| 52 | metavar = 'BENCHMARKNAME', |
| 53 | help = 'Print the sizes of individual dex segments as ' + |
| 54 | '\'<BENCHMARKNAME>-<segment>(CodeSize): <bytes>\'') |
| 55 | return parser.parse_args(argv) |
| 56 | |
| 57 | def Main(argv): |
Tamas Kenez | 2cf47cf | 2017-07-25 10:22:52 +0200 | [diff] [blame] | 58 | utils.check_java_version() |
Tamas Kenez | e4d79c4 | 2017-07-21 12:31:50 +0200 | [diff] [blame] | 59 | options = parse_arguments(argv) |
| 60 | |
| 61 | outdir = options.out |
| 62 | |
| 63 | if options.app == 'gmscore': |
| 64 | version = 'v10' |
| 65 | data = gmscore_data |
| 66 | base = data.V10_BASE |
| 67 | elif options.app == 'youtube': |
| 68 | version = '12.22' |
| 69 | data = youtube_data |
| 70 | base = data.V12_22_BASE |
| 71 | else: |
| 72 | raise Exception('Unexpected') |
| 73 | |
| 74 | |
| 75 | args = ['-forceprocessing'] |
| 76 | |
| 77 | if not outdir.endswith('.zip') and not outdir.endswith('.jar') \ |
| 78 | and not exists(outdir): |
| 79 | makedirs(outdir) |
| 80 | |
| 81 | |
| 82 | values_deploy = data.VERSIONS[version]['deploy'] |
| 83 | values_proguarded = data.VERSIONS[version]['proguarded'] |
| 84 | assert 'pgconf' in values_deploy |
| 85 | |
| 86 | for pgconf in values_deploy['pgconf']: |
| 87 | args.extend(['@' + pgconf]) |
| 88 | |
| 89 | # find seeds file |
| 90 | inputs = data.VERSIONS[version]['proguarded']['inputs'] |
| 91 | assert len(inputs) == 1 |
| 92 | basename_wo_ext = splitext(os.path.basename(inputs[0]))[0] |
| 93 | seeds_filename = basename_wo_ext + '.seeds' |
| 94 | |
| 95 | seeds_files = [] |
| 96 | for root, dirnames, filenames in os.walk(join(base, 'blaze-out')): |
| 97 | for filename in fnmatch.filter(filenames, seeds_filename): |
| 98 | seeds_files.append(os.path.join(root, filename)) |
| 99 | assert len(seeds_files) == 1 |
| 100 | |
| 101 | seeds_path = seeds_files[0] |
| 102 | proguarded_jar_path = splitext(seeds_path)[0] + '.jar' |
| 103 | |
| 104 | # Remove write-protection from seeds file. The seeds file is an output of |
| 105 | # ProGuard so it aborts if this is not writeable. |
| 106 | st = os.stat(seeds_path) |
| 107 | os.chmod(seeds_path, |
| 108 | st.st_mode | stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH) |
| 109 | |
| 110 | t0 = time.time() |
| 111 | |
| 112 | proguard_memoryuse = None |
| 113 | |
| 114 | with utils.TempDir() as temp: |
| 115 | track_memory_file = None |
| 116 | if options.print_memoryuse: |
| 117 | track_memory_file = join(temp, utils.MEMORY_USE_TMP_FILE) |
| 118 | proguard.run(args, track_memory_file = track_memory_file) |
| 119 | if options.print_memoryuse: |
| 120 | proguard_memoryuse = utils.grep_memoryuse(track_memory_file) |
| 121 | |
| 122 | # run dex on the result |
| 123 | if options.compatdx: |
Tamas Kenez | 0cad51c | 2017-08-21 14:42:01 +0200 | [diff] [blame] | 124 | jar = utils.COMPATDX_JAR |
Tamas Kenez | e4d79c4 | 2017-07-21 12:31:50 +0200 | [diff] [blame] | 125 | else: |
| 126 | jar = DX_JAR |
| 127 | |
| 128 | with utils.TempDir() as temp: |
| 129 | track_memory_file = None |
| 130 | cmd = [] |
| 131 | if options.print_memoryuse: |
| 132 | track_memory_file = join(temp, utils.MEMORY_USE_TMP_FILE) |
| 133 | cmd.extend(['tools/track_memory.sh', track_memory_file]) |
| 134 | cmd.extend(['java', '-jar', jar, '--multi-dex', |
| 135 | '--output=' + outdir]) |
| 136 | if 'min-api' in values_proguarded: |
| 137 | cmd.append('--min-sdk-version=' + values_proguarded['min-api']) |
| 138 | cmd.extend(['--dex', proguarded_jar_path]) |
| 139 | utils.PrintCmd(cmd); |
| 140 | check_call(cmd) |
| 141 | if options.print_memoryuse: |
| 142 | dx_memoryuse = utils.grep_memoryuse(track_memory_file) |
| 143 | print('{}(MemoryUse): {}' |
| 144 | .format(options.print_memoryuse, |
| 145 | max(proguard_memoryuse, dx_memoryuse))) |
| 146 | |
| 147 | if options.print_runtimeraw: |
| 148 | print('{}(RunTimeRaw): {} ms' |
| 149 | .format(options.print_runtimeraw, 1000.0 * (time.time() - t0))) |
| 150 | |
| 151 | if options.print_dexsegments: |
| 152 | dex_files = glob(os.path.join(outdir, '*.dex')) |
| 153 | utils.print_dexsegments(options.print_dexsegments, dex_files) |
| 154 | |
| 155 | if __name__ == '__main__': |
| 156 | sys.exit(Main(sys.argv[1:])) |