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