blob: c485ed2e5cd047485f3c9d87889affa611d7ffaf [file] [log] [blame]
Tamas Keneze4d79c42017-07-21 12:31:50 +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
6# Run ProGuard and the DX or CompatDX (= D8) tool on GmsCore V10.
7
8from __future__ import print_function
9from glob import glob
10from os import makedirs
11from os.path import exists, join, splitext
12from subprocess import check_call
13import argparse
14import fnmatch
15import gmscore_data
Ian Zerny3f54e222019-02-12 10:51:17 +010016import jdk
Tamas Keneze4d79c42017-07-21 12:31:50 +020017import os
18import stat
19import sys
20import time
21
22import gmail_data
23import gmscore_data
Rico Wind1f4172c2018-09-06 16:29:03 +020024import golem
Tamas Keneze4d79c42017-07-21 12:31:50 +020025import proguard
26import utils
27import youtube_data
28
29APPS = ['gmscore', 'youtube']
30DX_JAR = join(utils.REPO_ROOT, 'tools', 'linux', 'dx', 'framework', 'dx.jar')
Tamas Keneze4d79c42017-07-21 12:31:50 +020031
32def 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 Wind1f4172c2018-09-06 16:29:03 +020043 parser.add_argument('--golem',
44 help = 'Link in third party dependencies.',
45 default = False,
46 action = 'store_true')
Tamas Keneze4d79c42017-07-21 12:31:50 +020047 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
63def Main(argv):
Tamas Keneze4d79c42017-07-21 12:31:50 +020064 options = parse_arguments(argv)
Rico Wind1f4172c2018-09-06 16:29:03 +020065 if options.golem:
66 golem.link_third_party()
Ian Zerny09e8a462019-03-26 08:59:32 +010067 utils.check_java_version()
Tamas Keneze4d79c42017-07-21 12:31:50 +020068 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 Zerny517c7662018-07-11 15:22:29 +0200125 proguard.run(
126 args,
127 track_memory_file = track_memory_file,
128 stdout=open(os.devnull, 'w'))
Tamas Keneze4d79c42017-07-21 12:31:50 +0200129 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 Kenez0cad51c2017-08-21 14:42:01 +0200134 jar = utils.COMPATDX_JAR
Tamas Keneze4d79c42017-07-21 12:31:50 +0200135 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 Zerny3f54e222019-02-12 10:51:17 +0100144 cmd.extend([jdk.GetJavaExecutable(), '-jar', jar, '--multi-dex',
Tamas Keneze4d79c42017-07-21 12:31:50 +0200145 '--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
165if __name__ == '__main__':
166 sys.exit(Main(sys.argv[1:]))