blob: 6baaada64c8fb79427ab7fa0398a4b79df05d791 [file] [log] [blame]
Ian Zernydcb172e2022-02-22 15:36:45 +01001#!/usr/bin/env python3
Ian Zerny5ffa58f2020-02-26 08:37:14 +01002# Copyright (c) 2020, 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
6import argparse
7import os
8import subprocess
9import sys
10import zipfile
11
Ian Zerny0e53ff12021-06-15 13:40:14 +020012import archive
13import jdk
14import retrace
Ian Zerny5ffa58f2020-02-26 08:37:14 +010015import utils
16
17
18def make_parser():
19 parser = argparse.ArgumentParser(description = 'Compile a dump artifact.')
20 parser.add_argument(
Ian Zerny0e53ff12021-06-15 13:40:14 +020021 '--summary',
22 help='List a summary of the contents of the dumps.',
23 default=False,
24 action='store_true')
25 parser.add_argument(
Ian Zerny5ffa58f2020-02-26 08:37:14 +010026 '-d',
27 '--dump',
Christoffer Quist Adamsend84a6f02020-05-16 12:29:35 +020028 help='Dump file or directory to compile',
Ian Zerny5ffa58f2020-02-26 08:37:14 +010029 default=None)
30 parser.add_argument(
Rico Wind9ed87b92020-03-06 12:37:18 +010031 '--temp',
32 help='Temp directory to extract the dump to, allows you to rerun the command'
33 ' more easily in the terminal with changes',
34 default=None)
35 parser.add_argument(
Ian Zerny5ffa58f2020-02-26 08:37:14 +010036 '-c',
37 '--compiler',
Rico Windf73f18d2020-03-03 09:28:54 +010038 help='Compiler to use',
Ian Zerny5ffa58f2020-02-26 08:37:14 +010039 default=None)
40 parser.add_argument(
Christoffer Quist Adamsenf86fc0b2021-12-10 12:39:36 +010041 '--minify',
42 help='Force enable/disable minification'
43 ' (defaults to app proguard config)',
44 choices=['default', 'force-enable', 'force-disable'],
45 default='default')
46 parser.add_argument(
47 '--optimize',
48 help='Force enable/disable optimizations'
49 ' (defaults to app proguard config)',
50 choices=['default', 'force-enable', 'force-disable'],
51 default='default')
52 parser.add_argument(
53 '--shrink',
54 help='Force enable/disable shrinking'
55 ' (defaults to app proguard config)',
56 choices=['default', 'force-enable', 'force-disable'],
57 default='default')
58 parser.add_argument(
Ian Zerny5ffa58f2020-02-26 08:37:14 +010059 '-v',
60 '--version',
61 help='Compiler version to use (default read from dump version file).'
62 'Valid arguments are:'
Rico Wind1b52acf2021-03-21 12:36:55 +010063 ' "main" to run from your own tree,'
Morten Krogh-Jespersen51db2b02020-11-11 12:49:26 +010064 ' "source" to run from build classes directly,'
Ian Zerny5ffa58f2020-02-26 08:37:14 +010065 ' "X.Y.Z" to run a specific version, or'
Rico Wind1b52acf2021-03-21 12:36:55 +010066 ' <hash> to run that hash from main.',
Ian Zerny5ffa58f2020-02-26 08:37:14 +010067 default=None)
68 parser.add_argument(
Morten Krogh-Jespersend8bceb52020-03-06 12:56:48 +010069 '--r8-jar',
70 help='Path to an R8 jar.',
71 default=None)
72 parser.add_argument(
Christoffer Quist Adamsen2f48f3f2021-10-14 17:25:03 +020073 '--r8-flags', '--r8_flags',
74 help='Additional option(s) for the compiler.')
75 parser.add_argument(
Morten Krogh-Jespersen1fbfd592021-11-26 13:41:32 +010076 '--override',
Morten Krogh-Jespersen9206a662020-11-23 08:47:06 +010077 help='Do not override any extracted dump in temp-dir',
78 default=False,
79 action='store_true')
80 parser.add_argument(
Ian Zerny5ffa58f2020-02-26 08:37:14 +010081 '--nolib',
82 help='Use the non-lib distribution (default uses the lib distribution)',
83 default=False,
84 action='store_true')
85 parser.add_argument(
Morten Krogh-Jespersen6c702402021-06-21 12:29:48 +020086 '--print-times',
Rico Wind28653642020-03-31 13:59:07 +020087 help='Print timing information from r8',
88 default=False,
89 action='store_true')
90 parser.add_argument(
Ian Zerny5ffa58f2020-02-26 08:37:14 +010091 '--ea',
92 help='Enable Java assertions when running the compiler (default disabled)',
93 default=False,
94 action='store_true')
95 parser.add_argument(
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +010096 '--classfile',
97 help='Run with classfile output',
98 default=False,
99 action='store_true')
100 parser.add_argument(
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100101 '--debug-agent',
102 help='Enable Java debug agent and suspend compilation (default disabled)',
103 default=False,
104 action='store_true')
Ian Zerny77bdf5f2020-07-08 11:46:24 +0200105 parser.add_argument(
106 '--xmx',
107 help='Set JVM max heap size (-Xmx)',
108 default=None)
109 parser.add_argument(
110 '--threads',
111 help='Set the number of threads to use',
112 default=None)
113 parser.add_argument(
114 '--min-api',
115 help='Set min-api (default read from dump properties file)',
116 default=None)
Søren Gjesse7360f2b2020-08-10 09:13:35 +0200117 parser.add_argument(
Clément Béradaae4ca2020-10-27 14:26:41 +0000118 '--desugared-lib',
119 help='Set desugared-library (default set from dump)',
120 default=None)
121 parser.add_argument(
Morten Krogh-Jespersend86a81b2020-11-13 12:33:26 +0100122 '--disable-desugared-lib',
123 help='Disable desugared-libary if it will be set from dump',
124 default=False,
125 action='store_true'
126 )
127 parser.add_argument(
Søren Gjesse7360f2b2020-08-10 09:13:35 +0200128 '--loop',
129 help='Run the compilation in a loop',
130 default=False,
131 action='store_true')
Morten Krogh-Jespersen9e201ea2022-06-14 12:41:50 +0200132 parser.add_argument(
133 '--enable-missing-library-api-modeling',
134 help='Run with api modeling',
135 default=False,
136 action='store_true')
137 parser.add_argument(
138 '--android-platform-build',
139 help='Run as a platform build',
140 default=False,
141 action='store_true')
Morten Krogh-Jespersenaa2f9ec2022-07-04 13:10:17 +0200142 parser.add_argument(
143 '--compilation-mode', '--compilation_mode',
144 help='Run compilation in specified mode',
145 choices=['debug', 'release'],
146 default=None)
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100147 return parser
148
149def error(msg):
Rico Wind3d369b42021-01-12 10:26:24 +0100150 print(msg)
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100151 sys.exit(1)
152
153class Dump(object):
154
155 def __init__(self, directory):
156 self.directory = directory
157
158 def if_exists(self, name):
159 f = os.path.join(self.directory, name)
160 if os.path.exists(f):
161 return f
162 return None
163
164 def program_jar(self):
165 return self.if_exists('program.jar')
166
Christoffer Quist Adamsenfcfc63a2020-05-15 19:04:24 +0200167 def feature_jars(self):
168 feature_jars = []
169 i = 1
170 while True:
171 feature_jar = self.if_exists('feature-%s.jar' % i)
172 if feature_jar:
173 feature_jars.append(feature_jar)
174 i = i + 1
175 else:
176 return feature_jars
177
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100178 def library_jar(self):
179 return self.if_exists('library.jar')
180
181 def classpath_jar(self):
182 return self.if_exists('classpath.jar')
183
Clément Béradaae4ca2020-10-27 14:26:41 +0000184 def desugared_library_json(self):
185 return self.if_exists('desugared-library.json')
186
Clément Béra64a3c4c2020-11-10 08:16:17 +0000187 def proguard_input_map(self):
188 if self.if_exists('proguard_input.config'):
Rico Wind3d369b42021-01-12 10:26:24 +0100189 print("Unimplemented: proguard_input configuration.")
Clément Béra64a3c4c2020-11-10 08:16:17 +0000190
Morten Krogh-Jespersen1e774252021-03-01 16:56:07 +0100191 def main_dex_list_resource(self):
Clément Béra64a3c4c2020-11-10 08:16:17 +0000192 if self.if_exists('main-dex-list.txt'):
Rico Wind3d369b42021-01-12 10:26:24 +0100193 print("Unimplemented: main-dex-list.")
Clément Béra64a3c4c2020-11-10 08:16:17 +0000194
Morten Krogh-Jespersen1e774252021-03-01 16:56:07 +0100195 def main_dex_rules_resource(self):
196 return self.if_exists('main-dex-rules.txt')
197
Christoffer Quist Adamsenfcfc63a2020-05-15 19:04:24 +0200198 def build_properties_file(self):
199 return self.if_exists('build.properties')
200
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100201 def config_file(self):
202 return self.if_exists('proguard.config')
203
204 def version_file(self):
205 return self.if_exists('r8-version')
206
207 def version(self):
208 f = self.version_file()
209 if f:
210 return open(f).read().split(' ')[0]
211 return None
212
Ian Zerny0e53ff12021-06-15 13:40:14 +0200213def read_dump_from_args(args, temp):
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100214 if args.dump is None:
Christoffer Quist Adamsend84a6f02020-05-16 12:29:35 +0200215 error("A dump file or directory must be specified")
Ian Zerny674099e2021-06-15 13:44:37 +0200216 return read_dump(args.dump, temp, args.override)
Ian Zerny0e53ff12021-06-15 13:40:14 +0200217
218def read_dump(dump, temp, override=False):
219 if os.path.isdir(dump):
220 return Dump(dump)
221 dump_file = zipfile.ZipFile(os.path.abspath(dump), 'r')
222 with utils.ChangedWorkingDirectory(temp, quiet=True):
223 if override or not os.path.isfile('r8-version'):
Morten Krogh-Jespersen9206a662020-11-23 08:47:06 +0100224 dump_file.extractall()
Ian Zerny202330b2021-05-03 13:23:04 +0200225 if not os.path.isfile('r8-version'):
Morten Krogh-Jespersen9206a662020-11-23 08:47:06 +0100226 error("Did not extract into %s. Either the zip file is invalid or the "
227 "dump is missing files" % temp)
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100228 return Dump(temp)
229
Christoffer Quist Adamsenfcfc63a2020-05-15 19:04:24 +0200230def determine_build_properties(args, dump):
231 build_properties = {}
232 build_properties_file = dump.build_properties_file()
233 if build_properties_file:
234 with open(build_properties_file) as f:
235 build_properties_contents = f.readlines()
236 for line in build_properties_contents:
237 stripped = line.strip()
238 if stripped:
239 pair = stripped.split('=')
240 build_properties[pair[0]] = pair[1]
241 return build_properties
242
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100243def determine_version(args, dump):
244 if args.version is None:
245 return dump.version()
246 return args.version
247
Søren Gjesse0f8d88b2021-09-28 15:15:54 +0200248def determine_compiler(args, build_properties):
Morten Krogh-Jespersenbb624e42021-04-19 14:33:48 +0200249 compilers = ['d8', 'r8', 'r8full', 'l8']
Søren Gjesse0f8d88b2021-09-28 15:15:54 +0200250 compiler = args.compiler
251 if not compiler and 'tool' in build_properties:
252 compiler = build_properties.get('tool').lower()
253 if (compiler == 'r8'):
254 if not 'force-proguard-compatibility' in build_properties:
255 error("Unable to determine R8 compiler variant from build.properties."
256 " No value for 'force-proguard-compatibility'.")
257 if build_properties.get('force-proguard-compatibility').lower() == 'false':
258 compiler = compiler + 'full'
259 if compiler not in compilers:
Rico Windf73f18d2020-03-03 09:28:54 +0100260 error("Unable to determine a compiler to use. Specified %s,"
261 " Valid options: %s" % (args.compiler, ', '.join(compilers)))
Søren Gjesse0f8d88b2021-09-28 15:15:54 +0200262 return compiler
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100263
264def determine_output(args, temp):
265 return os.path.join(temp, 'out.jar')
266
Ian Zerny77bdf5f2020-07-08 11:46:24 +0200267def determine_min_api(args, build_properties):
268 if args.min_api:
269 return args.min_api
270 if 'min-api' in build_properties:
271 return build_properties.get('min-api')
272 return None
273
Christoffer Quist Adamsenfcfc63a2020-05-15 19:04:24 +0200274def determine_feature_output(feature_jar, temp):
275 return os.path.join(temp, os.path.basename(feature_jar)[:-4] + ".out.jar")
276
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100277def determine_program_jar(args, dump):
278 if hasattr(args, 'program_jar') and args.program_jar:
279 return args.program_jar
280 return dump.program_jar()
281
282def determine_class_file(args, build_properties):
283 if args.classfile:
284 return args.classfile
285 if 'classfile' in build_properties:
286 return True
287 return None
288
Morten Krogh-Jespersen9e201ea2022-06-14 12:41:50 +0200289def determine_android_platform_build(args, build_properties):
290 if args.android_platform_build:
Morten Krogh-Jespersen9e201ea2022-06-14 12:41:50 +0200291 return True
Morten Krogh-Jespersenaa2f9ec2022-07-04 13:10:17 +0200292 return build_properties.get('android-platform-build') == 'true'
Morten Krogh-Jespersen9e201ea2022-06-14 12:41:50 +0200293
294def determine_enable_missing_library_api_modeling(args, build_properties):
295 if args.enable_missing_library_api_modeling:
Morten Krogh-Jespersen9e201ea2022-06-14 12:41:50 +0200296 return True
Morten Krogh-Jespersenaa2f9ec2022-07-04 13:10:17 +0200297 return build_properties.get('enable-missing-library-api-modeling') == 'true'
298
299def determine_compilation_mode(args, build_properties):
300 if args.compilation_mode:
301 return args.compilation_mode
302 return build_properties.get('mode')
Morten Krogh-Jespersen9e201ea2022-06-14 12:41:50 +0200303
Søren Gjesse1768e252021-10-06 12:50:36 +0200304def determine_properties(build_properties):
305 args = []
306 for key, value in build_properties.items():
307 # When writing dumps all system properties starting with com.android.tools.r8
308 # are written to the build.properties file in the format
309 # system-property-com.android.tools.r8.XXX=<value>
310 if key.startswith('system-property-'):
311 name = key[len('system-property-'):]
312 if name.endswith('dumpinputtofile') or name.endswith('dumpinputtodirectory'):
313 continue
314 if len(value) == 0:
315 args.append('-D' + name)
316 else:
317 args.append('-D' + name + '=' + value)
318 return args
319
Ian Zerny171c6a62022-04-04 14:25:30 +0200320def download_distribution(version, nolib, temp):
Rico Wind1b52acf2021-03-21 12:36:55 +0100321 if version == 'main':
Ian Zerny171c6a62022-04-04 14:25:30 +0200322 return utils.R8_JAR if nolib else utils.R8LIB_JAR
Morten Krogh-Jespersen51db2b02020-11-11 12:49:26 +0100323 if version == 'source':
324 return '%s:%s' % (utils.BUILD_JAVA_MAIN_DIR, utils.ALL_DEPS_JAR)
Ian Zerny171c6a62022-04-04 14:25:30 +0200325 name = 'r8.jar' if nolib else 'r8lib.jar'
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100326 source = archive.GetUploadDestination(version, name, is_hash(version))
327 dest = os.path.join(temp, 'r8.jar')
328 utils.download_file_from_cloud_storage(source, dest)
329 return dest
330
Rico Windc9e52f72021-08-19 08:30:26 +0200331
Christoffer Quist Adamsenf86fc0b2021-12-10 12:39:36 +0100332def clean_config(file, args):
Rico Windc9e52f72021-08-19 08:30:26 +0200333 with open(file) as f:
334 lines = f.readlines()
Christoffer Quist Adamsenf86fc0b2021-12-10 12:39:36 +0100335 minify = args.minify
336 optimize = args.optimize
337 shrink = args.shrink
Rico Windc9e52f72021-08-19 08:30:26 +0200338 with open(file, 'w') as f:
Christoffer Quist Adamsenf86fc0b2021-12-10 12:39:36 +0100339 if minify == 'force-disable':
340 print('Adding config line: -dontobfuscate')
341 f.write('-dontobfuscate\n')
342 if optimize == 'force-disable':
343 print('Adding config line: -dontoptimize')
344 f.write('-dontoptimize\n')
345 if shrink == 'force-disable':
346 print('Adding config line: -dontshrink')
347 f.write('-dontshrink\n')
Rico Windc9e52f72021-08-19 08:30:26 +0200348 for line in lines:
Christoffer Quist Adamsenf86fc0b2021-12-10 12:39:36 +0100349 if clean_config_line(line, minify, optimize, shrink):
Rico Windc9e52f72021-08-19 08:30:26 +0200350 print('Removing from config line: \n%s' % line)
Christoffer Quist Adamsenf86fc0b2021-12-10 12:39:36 +0100351 else:
352 f.write(line)
Rico Windc9e52f72021-08-19 08:30:26 +0200353
Christoffer Quist Adamsenf86fc0b2021-12-10 12:39:36 +0100354def clean_config_line(line, minify, optimize, shrink):
355 if ('-injars' in line or '-libraryjars' in line or
356 '-print' in line):
357 return True
358 if minify == 'force-enable' and '-dontobfuscate' in line:
359 return True
360 if optimize == 'force-enable' and '-dontoptimize' in line:
361 return True
362 if shrink == 'force-enable' and '-dontshrink' in line:
363 return True
364 return False
Rico Windc9e52f72021-08-19 08:30:26 +0200365
Morten Krogh-Jespersen327e76d2022-06-23 09:31:13 +0200366def prepare_r8_wrapper(dist, temp, jdkhome):
367 compile_with_javac(
368 dist,
369 temp,
370 jdkhome,
371 os.path.join(
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100372 utils.REPO_ROOT,
Morten Krogh-Jespersen327e76d2022-06-23 09:31:13 +0200373 'src/main/java/com/android/tools/r8/utils/CompileDumpCompatR8.java'))
374
375def prepare_d8_wrapper(dist, temp, jdkhome):
376 compile_with_javac(
377 dist,
378 temp,
379 jdkhome,
380 os.path.join(
381 utils.REPO_ROOT,
382 'src/main/java/com/android/tools/r8/utils/CompileDumpD8.java'))
383
384def compile_with_javac(dist, temp, jdkhome, path):
Ian Zerny268c9632020-11-13 11:36:35 +0100385 cmd = [
Rico Wind33936d12021-04-07 08:04:14 +0200386 jdk.GetJavacExecutable(jdkhome),
Morten Krogh-Jespersen327e76d2022-06-23 09:31:13 +0200387 path,
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100388 '-d', temp,
389 '-cp', dist,
Ian Zerny268c9632020-11-13 11:36:35 +0100390 ]
391 utils.PrintCmd(cmd)
392 subprocess.check_output(cmd)
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100393
394def is_hash(version):
395 return len(version) == 40
396
Rico Wind33936d12021-04-07 08:04:14 +0200397def run1(out, args, otherargs, jdkhome=None):
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100398 with utils.TempDir() as temp:
Søren Gjesse7360f2b2020-08-10 09:13:35 +0200399 if out:
400 temp = out
Rico Wind9ed87b92020-03-06 12:37:18 +0100401 if not os.path.exists(temp):
402 os.makedirs(temp)
Ian Zerny0e53ff12021-06-15 13:40:14 +0200403 dump = read_dump_from_args(args, temp)
Ian Zerny46bc4cf2020-08-03 15:58:27 +0200404 if not dump.program_jar():
405 error("Cannot compile dump with no program classes")
406 if not dump.library_jar():
Rico Wind3d369b42021-01-12 10:26:24 +0100407 print("WARNING: Unexpected lack of library classes in dump")
Christoffer Quist Adamsenfcfc63a2020-05-15 19:04:24 +0200408 build_properties = determine_build_properties(args, dump)
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100409 version = determine_version(args, dump)
Søren Gjesse0f8d88b2021-09-28 15:15:54 +0200410 compiler = determine_compiler(args, build_properties)
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100411 out = determine_output(args, temp)
Ian Zerny77bdf5f2020-07-08 11:46:24 +0200412 min_api = determine_min_api(args, build_properties)
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100413 classfile = determine_class_file(args, build_properties)
Morten Krogh-Jespersen9e201ea2022-06-14 12:41:50 +0200414 android_platform_build = determine_android_platform_build(args, build_properties)
415 enable_missing_library_api_modeling = determine_enable_missing_library_api_modeling(args, build_properties)
Morten Krogh-Jespersenaa2f9ec2022-07-04 13:10:17 +0200416 mode = determine_compilation_mode(args, build_properties)
Ian Zerny171c6a62022-04-04 14:25:30 +0200417 jar = args.r8_jar if args.r8_jar else download_distribution(version, args.nolib, temp)
Ian Zerny268c9632020-11-13 11:36:35 +0100418 if ':' not in jar and not os.path.exists(jar):
419 error("Distribution does not exist: " + jar)
Morten Krogh-Jespersen327e76d2022-06-23 09:31:13 +0200420 prepare_r8_wrapper(jar, temp, jdkhome)
421 prepare_d8_wrapper(jar, temp, jdkhome)
Rico Wind33936d12021-04-07 08:04:14 +0200422 cmd = [jdk.GetJavaExecutable(jdkhome)]
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100423 if args.debug_agent:
424 if not args.nolib:
Rico Wind3d369b42021-01-12 10:26:24 +0100425 print("WARNING: Running debugging agent on r8lib is questionable...")
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100426 cmd.append(
427 '-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005')
Ian Zerny77bdf5f2020-07-08 11:46:24 +0200428 if args.xmx:
429 cmd.append('-Xmx' + args.xmx)
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100430 if args.ea:
431 cmd.append('-ea')
Morten Krogh-Jespersen23f0b032021-07-06 18:10:15 +0200432 cmd.append('-Dcom.android.tools.r8.enableTestAssertions=1')
Morten Krogh-Jespersen6c702402021-06-21 12:29:48 +0200433 if args.print_times:
Rico Wind28653642020-03-31 13:59:07 +0200434 cmd.append('-Dcom.android.tools.r8.printtimes=1')
Christoffer Quist Adamsen2f48f3f2021-10-14 17:25:03 +0200435 if args.r8_flags:
436 cmd.extend(args.r8_flags.split(' '))
Morten Krogh-Jespersen43f3cea2020-11-12 17:09:51 +0100437 if hasattr(args, 'properties'):
438 cmd.extend(args.properties);
Søren Gjesse1768e252021-10-06 12:50:36 +0200439 cmd.extend(determine_properties(build_properties))
Morten Krogh-Jespersen327e76d2022-06-23 09:31:13 +0200440 cmd.extend(['-cp', '%s:%s' % (temp, jar)])
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100441 if compiler == 'd8':
Morten Krogh-Jespersen327e76d2022-06-23 09:31:13 +0200442 cmd.append('com.android.tools.r8.utils.CompileDumpD8')
Morten Krogh-Jespersenbb624e42021-04-19 14:33:48 +0200443 if compiler == 'l8':
444 cmd.append('com.android.tools.r8.L8')
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100445 if compiler.startswith('r8'):
446 cmd.append('com.android.tools.r8.utils.CompileDumpCompatR8')
447 if compiler == 'r8':
448 cmd.append('--compat')
Morten Krogh-Jespersenaa2f9ec2022-07-04 13:10:17 +0200449 if mode == 'debug':
450 cmd.append('--debug')
451 else:
452 cmd.append('--release')
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100453 # For recompilation of dumps run_on_app_dumps pass in a program jar.
454 cmd.append(determine_program_jar(args, dump))
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100455 cmd.extend(['--output', out])
Christoffer Quist Adamsenfcfc63a2020-05-15 19:04:24 +0200456 for feature_jar in dump.feature_jars():
457 cmd.extend(['--feature-jar', feature_jar,
458 determine_feature_output(feature_jar, temp)])
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100459 if dump.library_jar():
460 cmd.extend(['--lib', dump.library_jar()])
Morten Krogh-Jespersenbb624e42021-04-19 14:33:48 +0200461 if dump.classpath_jar() and compiler != 'l8':
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100462 cmd.extend(['--classpath', dump.classpath_jar()])
Morten Krogh-Jespersend86a81b2020-11-13 12:33:26 +0100463 if dump.desugared_library_json() and not args.disable_desugared_lib:
Clément Béradaae4ca2020-10-27 14:26:41 +0000464 cmd.extend(['--desugared-lib', dump.desugared_library_json()])
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100465 if compiler != 'd8' and dump.config_file():
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100466 if hasattr(args, 'config_file_consumer') and args.config_file_consumer:
467 args.config_file_consumer(dump.config_file())
Rico Windc9e52f72021-08-19 08:30:26 +0200468 else:
469 # If we get a dump from the wild we can't use -injars, -libraryjars or
470 # -print{mapping,usage}
Christoffer Quist Adamsenf86fc0b2021-12-10 12:39:36 +0100471 clean_config(dump.config_file(), args)
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100472 cmd.extend(['--pg-conf', dump.config_file()])
Morten Krogh-Jespersen1e774252021-03-01 16:56:07 +0100473 if dump.main_dex_rules_resource():
474 cmd.extend(['--main-dex-rules', dump.main_dex_rules_resource()])
Morten Krogh-Jespersenbb624e42021-04-19 14:33:48 +0200475 if compiler == 'l8':
476 if dump.config_file():
477 cmd.extend(['--pg-map-output', '%s.map' % out])
478 elif compiler != 'd8':
Ian Zerny588120b2020-04-03 13:08:03 +0200479 cmd.extend(['--pg-map-output', '%s.map' % out])
Ian Zerny77bdf5f2020-07-08 11:46:24 +0200480 if min_api:
481 cmd.extend(['--min-api', min_api])
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100482 if classfile:
483 cmd.extend(['--classfile'])
Morten Krogh-Jespersen9e201ea2022-06-14 12:41:50 +0200484 if android_platform_build:
485 cmd.extend(['--android-platform-build'])
486 if enable_missing_library_api_modeling:
487 cmd.extend(['--enable-missing-library-api-modeling'])
Ian Zerny77bdf5f2020-07-08 11:46:24 +0200488 if args.threads:
489 cmd.extend(['--threads', args.threads])
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100490 cmd.extend(otherargs)
491 utils.PrintCmd(cmd)
492 try:
Morten Krogh-Jespersen6c702402021-06-21 12:29:48 +0200493 print(subprocess.check_output(cmd, stderr=subprocess.STDOUT).decode('utf-8'))
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100494 return 0
Rico Wind3d369b42021-01-12 10:26:24 +0100495 except subprocess.CalledProcessError as e:
Ian Zerny9ff31b72021-04-22 11:36:26 +0200496 if args.nolib \
497 or version == 'source' \
498 or not try_retrace_output(e, version, temp):
Morten Krogh-Jespersen86222742021-03-02 11:13:33 +0100499 print(e.output.decode('UTF-8'))
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100500 return 1
501
Ian Zerny9ff31b72021-04-22 11:36:26 +0200502def try_retrace_output(e, version, temp):
503 try:
504 stacktrace = os.path.join(temp, 'stacktrace')
505 open(stacktrace, 'w+').write(e.output.decode('UTF-8'))
506 print("=" * 80)
507 print(" RETRACED OUTPUT")
508 print("=" * 80)
509 retrace.run(get_map_file(version, temp), stacktrace, no_r8lib=False)
510 return True
511 except Exception as e2:
512 print("Failed to retrace for version: %s" % version)
513 print(e2)
514 return False
515
516def get_map_file(version, temp):
517 if version == 'main':
518 return utils.R8LIB_MAP
519 download_path = archive.GetUploadDestination(
520 version,
521 'r8lib.jar.map',
522 is_hash(version))
523 if utils.file_exists_on_cloud_storage(download_path):
524 map_path = os.path.join(temp, 'mapping.map')
525 utils.download_file_from_cloud_storage(download_path, map_path)
526 return map_path
527 else:
528 print('Could not find map file from argument: %s.' % version)
529 return None
530
Ian Zerny0e53ff12021-06-15 13:40:14 +0200531def summarize_dump_files(dumpfiles):
532 if len(dumpfiles) == 0:
533 error('Summary command expects a list of dumps to summarize')
534 for f in dumpfiles:
535 print(f + ':')
536 try:
537 with utils.TempDir() as temp:
538 dump = read_dump(f, temp)
539 summarize_dump(dump)
540 except IOError as e:
541 print("Error: " + str(e))
542 except zipfile.BadZipfile as e:
543 print("Error: " + str(e))
544
545def summarize_dump(dump):
546 version = dump.version()
547 if not version:
548 print('No dump version info')
549 return
550 print('version=' + version)
551 props = dump.build_properties_file()
552 if props:
553 with open(props) as props_file:
554 print(props_file.read())
555 if dump.library_jar():
556 print('library.jar present')
557 if dump.classpath_jar():
558 print('classpath.jar present')
559 prog = dump.program_jar()
560 if prog:
561 print('program.jar content:')
562 summarize_jar(prog)
563
564def summarize_jar(jar):
565 with zipfile.ZipFile(jar) as zip:
566 pkgs = {}
567 for info in zip.infolist():
568 if info.filename.endswith('.class'):
569 pkg, clazz = os.path.split(info.filename)
570 count = pkgs.get(pkg, 0)
571 pkgs[pkg] = count + 1
572 sorted = list(pkgs.keys())
573 sorted.sort()
574 for p in sorted:
575 print(' ' + p + ': ' + str(pkgs[p]))
576
Søren Gjesse7360f2b2020-08-10 09:13:35 +0200577def run(args, otherargs):
Ian Zerny0e53ff12021-06-15 13:40:14 +0200578 if args.summary:
579 summarize_dump_files(otherargs)
580 elif args.loop:
Søren Gjesse7360f2b2020-08-10 09:13:35 +0200581 count = 1
582 while True:
583 print('Iteration {:03d}'.format(count))
584 out = args.temp
585 if out:
586 out = os.path.join(out, '{:03d}'.format(count))
587 run1(out, args, otherargs)
588 count += 1
589 else:
590 run1(args.temp, args, otherargs)
591
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100592if __name__ == '__main__':
593 (args, otherargs) = make_parser().parse_known_args(sys.argv[1:])
594 sys.exit(run(args, otherargs))