blob: fb3b901c34081f1e2ffe6292535e3e4bedea6f23 [file] [log] [blame]
Ian Zerny5ffa58f2020-02-26 08:37:14 +01001#!/usr/bin/env python
2# 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(
41 '-v',
42 '--version',
43 help='Compiler version to use (default read from dump version file).'
44 'Valid arguments are:'
Rico Wind1b52acf2021-03-21 12:36:55 +010045 ' "main" to run from your own tree,'
Morten Krogh-Jespersen51db2b02020-11-11 12:49:26 +010046 ' "source" to run from build classes directly,'
Ian Zerny5ffa58f2020-02-26 08:37:14 +010047 ' "X.Y.Z" to run a specific version, or'
Rico Wind1b52acf2021-03-21 12:36:55 +010048 ' <hash> to run that hash from main.',
Ian Zerny5ffa58f2020-02-26 08:37:14 +010049 default=None)
50 parser.add_argument(
Morten Krogh-Jespersend8bceb52020-03-06 12:56:48 +010051 '--r8-jar',
52 help='Path to an R8 jar.',
53 default=None)
54 parser.add_argument(
Morten Krogh-Jespersen9206a662020-11-23 08:47:06 +010055 '-override',
56 help='Do not override any extracted dump in temp-dir',
57 default=False,
58 action='store_true')
59 parser.add_argument(
Ian Zerny5ffa58f2020-02-26 08:37:14 +010060 '--nolib',
61 help='Use the non-lib distribution (default uses the lib distribution)',
62 default=False,
63 action='store_true')
64 parser.add_argument(
Morten Krogh-Jespersen6c702402021-06-21 12:29:48 +020065 '--print-times',
Rico Wind28653642020-03-31 13:59:07 +020066 help='Print timing information from r8',
67 default=False,
68 action='store_true')
69 parser.add_argument(
Ian Zerny5ffa58f2020-02-26 08:37:14 +010070 '--ea',
71 help='Enable Java assertions when running the compiler (default disabled)',
72 default=False,
73 action='store_true')
74 parser.add_argument(
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +010075 '--classfile',
76 help='Run with classfile output',
77 default=False,
78 action='store_true')
79 parser.add_argument(
Ian Zerny5ffa58f2020-02-26 08:37:14 +010080 '--debug-agent',
81 help='Enable Java debug agent and suspend compilation (default disabled)',
82 default=False,
83 action='store_true')
Ian Zerny77bdf5f2020-07-08 11:46:24 +020084 parser.add_argument(
85 '--xmx',
86 help='Set JVM max heap size (-Xmx)',
87 default=None)
88 parser.add_argument(
89 '--threads',
90 help='Set the number of threads to use',
91 default=None)
92 parser.add_argument(
93 '--min-api',
94 help='Set min-api (default read from dump properties file)',
95 default=None)
Søren Gjesse7360f2b2020-08-10 09:13:35 +020096 parser.add_argument(
Clément Béradaae4ca2020-10-27 14:26:41 +000097 '--desugared-lib',
98 help='Set desugared-library (default set from dump)',
99 default=None)
100 parser.add_argument(
Morten Krogh-Jespersend86a81b2020-11-13 12:33:26 +0100101 '--disable-desugared-lib',
102 help='Disable desugared-libary if it will be set from dump',
103 default=False,
104 action='store_true'
105 )
106 parser.add_argument(
Søren Gjesse7360f2b2020-08-10 09:13:35 +0200107 '--loop',
108 help='Run the compilation in a loop',
109 default=False,
110 action='store_true')
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100111 return parser
112
113def error(msg):
Rico Wind3d369b42021-01-12 10:26:24 +0100114 print(msg)
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100115 sys.exit(1)
116
117class Dump(object):
118
119 def __init__(self, directory):
120 self.directory = directory
121
122 def if_exists(self, name):
123 f = os.path.join(self.directory, name)
124 if os.path.exists(f):
125 return f
126 return None
127
128 def program_jar(self):
129 return self.if_exists('program.jar')
130
Christoffer Quist Adamsenfcfc63a2020-05-15 19:04:24 +0200131 def feature_jars(self):
132 feature_jars = []
133 i = 1
134 while True:
135 feature_jar = self.if_exists('feature-%s.jar' % i)
136 if feature_jar:
137 feature_jars.append(feature_jar)
138 i = i + 1
139 else:
140 return feature_jars
141
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100142 def library_jar(self):
143 return self.if_exists('library.jar')
144
145 def classpath_jar(self):
146 return self.if_exists('classpath.jar')
147
Clément Béradaae4ca2020-10-27 14:26:41 +0000148 def desugared_library_json(self):
149 return self.if_exists('desugared-library.json')
150
Clément Béra64a3c4c2020-11-10 08:16:17 +0000151 def proguard_input_map(self):
152 if self.if_exists('proguard_input.config'):
Rico Wind3d369b42021-01-12 10:26:24 +0100153 print("Unimplemented: proguard_input configuration.")
Clément Béra64a3c4c2020-11-10 08:16:17 +0000154
Morten Krogh-Jespersen1e774252021-03-01 16:56:07 +0100155 def main_dex_list_resource(self):
Clément Béra64a3c4c2020-11-10 08:16:17 +0000156 if self.if_exists('main-dex-list.txt'):
Rico Wind3d369b42021-01-12 10:26:24 +0100157 print("Unimplemented: main-dex-list.")
Clément Béra64a3c4c2020-11-10 08:16:17 +0000158
Morten Krogh-Jespersen1e774252021-03-01 16:56:07 +0100159 def main_dex_rules_resource(self):
160 return self.if_exists('main-dex-rules.txt')
161
Christoffer Quist Adamsenfcfc63a2020-05-15 19:04:24 +0200162 def build_properties_file(self):
163 return self.if_exists('build.properties')
164
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100165 def config_file(self):
166 return self.if_exists('proguard.config')
167
168 def version_file(self):
169 return self.if_exists('r8-version')
170
171 def version(self):
172 f = self.version_file()
173 if f:
174 return open(f).read().split(' ')[0]
175 return None
176
Ian Zerny0e53ff12021-06-15 13:40:14 +0200177def read_dump_from_args(args, temp):
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100178 if args.dump is None:
Christoffer Quist Adamsend84a6f02020-05-16 12:29:35 +0200179 error("A dump file or directory must be specified")
Ian Zerny674099e2021-06-15 13:44:37 +0200180 return read_dump(args.dump, temp, args.override)
Ian Zerny0e53ff12021-06-15 13:40:14 +0200181
182def read_dump(dump, temp, override=False):
183 if os.path.isdir(dump):
184 return Dump(dump)
185 dump_file = zipfile.ZipFile(os.path.abspath(dump), 'r')
186 with utils.ChangedWorkingDirectory(temp, quiet=True):
187 if override or not os.path.isfile('r8-version'):
Morten Krogh-Jespersen9206a662020-11-23 08:47:06 +0100188 dump_file.extractall()
Ian Zerny202330b2021-05-03 13:23:04 +0200189 if not os.path.isfile('r8-version'):
Morten Krogh-Jespersen9206a662020-11-23 08:47:06 +0100190 error("Did not extract into %s. Either the zip file is invalid or the "
191 "dump is missing files" % temp)
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100192 return Dump(temp)
193
Christoffer Quist Adamsenfcfc63a2020-05-15 19:04:24 +0200194def determine_build_properties(args, dump):
195 build_properties = {}
196 build_properties_file = dump.build_properties_file()
197 if build_properties_file:
198 with open(build_properties_file) as f:
199 build_properties_contents = f.readlines()
200 for line in build_properties_contents:
201 stripped = line.strip()
202 if stripped:
203 pair = stripped.split('=')
204 build_properties[pair[0]] = pair[1]
205 return build_properties
206
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100207def determine_version(args, dump):
208 if args.version is None:
209 return dump.version()
210 return args.version
211
Søren Gjesse0f8d88b2021-09-28 15:15:54 +0200212def determine_compiler(args, build_properties):
Morten Krogh-Jespersenbb624e42021-04-19 14:33:48 +0200213 compilers = ['d8', 'r8', 'r8full', 'l8']
Søren Gjesse0f8d88b2021-09-28 15:15:54 +0200214 compiler = args.compiler
215 if not compiler and 'tool' in build_properties:
216 compiler = build_properties.get('tool').lower()
217 if (compiler == 'r8'):
218 if not 'force-proguard-compatibility' in build_properties:
219 error("Unable to determine R8 compiler variant from build.properties."
220 " No value for 'force-proguard-compatibility'.")
221 if build_properties.get('force-proguard-compatibility').lower() == 'false':
222 compiler = compiler + 'full'
223 if compiler not in compilers:
Rico Windf73f18d2020-03-03 09:28:54 +0100224 error("Unable to determine a compiler to use. Specified %s,"
225 " Valid options: %s" % (args.compiler, ', '.join(compilers)))
Søren Gjesse0f8d88b2021-09-28 15:15:54 +0200226 return compiler
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100227
228def determine_output(args, temp):
229 return os.path.join(temp, 'out.jar')
230
Ian Zerny77bdf5f2020-07-08 11:46:24 +0200231def determine_min_api(args, build_properties):
232 if args.min_api:
233 return args.min_api
234 if 'min-api' in build_properties:
235 return build_properties.get('min-api')
236 return None
237
Christoffer Quist Adamsenfcfc63a2020-05-15 19:04:24 +0200238def determine_feature_output(feature_jar, temp):
239 return os.path.join(temp, os.path.basename(feature_jar)[:-4] + ".out.jar")
240
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100241def determine_program_jar(args, dump):
242 if hasattr(args, 'program_jar') and args.program_jar:
243 return args.program_jar
244 return dump.program_jar()
245
246def determine_class_file(args, build_properties):
247 if args.classfile:
248 return args.classfile
249 if 'classfile' in build_properties:
250 return True
251 return None
252
Søren Gjesse1768e252021-10-06 12:50:36 +0200253def determine_properties(build_properties):
254 args = []
255 for key, value in build_properties.items():
256 # When writing dumps all system properties starting with com.android.tools.r8
257 # are written to the build.properties file in the format
258 # system-property-com.android.tools.r8.XXX=<value>
259 if key.startswith('system-property-'):
260 name = key[len('system-property-'):]
261 if name.endswith('dumpinputtofile') or name.endswith('dumpinputtodirectory'):
262 continue
263 if len(value) == 0:
264 args.append('-D' + name)
265 else:
266 args.append('-D' + name + '=' + value)
267 return args
268
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100269def download_distribution(args, version, temp):
Rico Wind1b52acf2021-03-21 12:36:55 +0100270 if version == 'main':
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100271 return utils.R8_JAR if args.nolib else utils.R8LIB_JAR
Morten Krogh-Jespersen51db2b02020-11-11 12:49:26 +0100272 if version == 'source':
273 return '%s:%s' % (utils.BUILD_JAVA_MAIN_DIR, utils.ALL_DEPS_JAR)
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100274 name = 'r8.jar' if args.nolib else 'r8lib.jar'
275 source = archive.GetUploadDestination(version, name, is_hash(version))
276 dest = os.path.join(temp, 'r8.jar')
277 utils.download_file_from_cloud_storage(source, dest)
278 return dest
279
Rico Windc9e52f72021-08-19 08:30:26 +0200280
281def clean_config(file):
282 with open(file) as f:
283 lines = f.readlines()
284 with open(file, 'w') as f:
285 for line in lines:
286 if ('-injars' not in line and '-libraryjars' not in line and
287 '-print' not in line):
288 f.write(line)
289 else:
290 print('Removing from config line: \n%s' % line)
291
292
Rico Wind33936d12021-04-07 08:04:14 +0200293def prepare_wrapper(dist, temp, jdkhome):
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100294 wrapper_file = os.path.join(
295 utils.REPO_ROOT,
296 'src/main/java/com/android/tools/r8/utils/CompileDumpCompatR8.java')
Ian Zerny268c9632020-11-13 11:36:35 +0100297 cmd = [
Rico Wind33936d12021-04-07 08:04:14 +0200298 jdk.GetJavacExecutable(jdkhome),
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100299 wrapper_file,
300 '-d', temp,
301 '-cp', dist,
Ian Zerny268c9632020-11-13 11:36:35 +0100302 ]
303 utils.PrintCmd(cmd)
304 subprocess.check_output(cmd)
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100305 return temp
306
307def is_hash(version):
308 return len(version) == 40
309
Rico Wind33936d12021-04-07 08:04:14 +0200310def run1(out, args, otherargs, jdkhome=None):
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100311 with utils.TempDir() as temp:
Søren Gjesse7360f2b2020-08-10 09:13:35 +0200312 if out:
313 temp = out
Rico Wind9ed87b92020-03-06 12:37:18 +0100314 if not os.path.exists(temp):
315 os.makedirs(temp)
Ian Zerny0e53ff12021-06-15 13:40:14 +0200316 dump = read_dump_from_args(args, temp)
Ian Zerny46bc4cf2020-08-03 15:58:27 +0200317 if not dump.program_jar():
318 error("Cannot compile dump with no program classes")
319 if not dump.library_jar():
Rico Wind3d369b42021-01-12 10:26:24 +0100320 print("WARNING: Unexpected lack of library classes in dump")
Christoffer Quist Adamsenfcfc63a2020-05-15 19:04:24 +0200321 build_properties = determine_build_properties(args, dump)
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100322 version = determine_version(args, dump)
Søren Gjesse0f8d88b2021-09-28 15:15:54 +0200323 compiler = determine_compiler(args, build_properties)
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100324 out = determine_output(args, temp)
Ian Zerny77bdf5f2020-07-08 11:46:24 +0200325 min_api = determine_min_api(args, build_properties)
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100326 classfile = determine_class_file(args, build_properties)
Morten Krogh-Jespersend8bceb52020-03-06 12:56:48 +0100327 jar = args.r8_jar if args.r8_jar else download_distribution(args, version, temp)
Ian Zerny268c9632020-11-13 11:36:35 +0100328 if ':' not in jar and not os.path.exists(jar):
329 error("Distribution does not exist: " + jar)
Rico Wind33936d12021-04-07 08:04:14 +0200330 wrapper_dir = prepare_wrapper(jar, temp, jdkhome)
331 cmd = [jdk.GetJavaExecutable(jdkhome)]
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100332 if args.debug_agent:
333 if not args.nolib:
Rico Wind3d369b42021-01-12 10:26:24 +0100334 print("WARNING: Running debugging agent on r8lib is questionable...")
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100335 cmd.append(
336 '-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005')
Ian Zerny77bdf5f2020-07-08 11:46:24 +0200337 if args.xmx:
338 cmd.append('-Xmx' + args.xmx)
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100339 if args.ea:
340 cmd.append('-ea')
Morten Krogh-Jespersen23f0b032021-07-06 18:10:15 +0200341 cmd.append('-Dcom.android.tools.r8.enableTestAssertions=1')
Morten Krogh-Jespersen6c702402021-06-21 12:29:48 +0200342 if args.print_times:
Rico Wind28653642020-03-31 13:59:07 +0200343 cmd.append('-Dcom.android.tools.r8.printtimes=1')
Morten Krogh-Jespersen43f3cea2020-11-12 17:09:51 +0100344 if hasattr(args, 'properties'):
345 cmd.extend(args.properties);
Søren Gjesse1768e252021-10-06 12:50:36 +0200346 cmd.extend(determine_properties(build_properties))
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100347 cmd.extend(['-cp', '%s:%s' % (wrapper_dir, jar)])
348 if compiler == 'd8':
349 cmd.append('com.android.tools.r8.D8')
Morten Krogh-Jespersenbb624e42021-04-19 14:33:48 +0200350 if compiler == 'l8':
351 cmd.append('com.android.tools.r8.L8')
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100352 if compiler.startswith('r8'):
353 cmd.append('com.android.tools.r8.utils.CompileDumpCompatR8')
354 if compiler == 'r8':
355 cmd.append('--compat')
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100356 # For recompilation of dumps run_on_app_dumps pass in a program jar.
357 cmd.append(determine_program_jar(args, dump))
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100358 cmd.extend(['--output', out])
Christoffer Quist Adamsenfcfc63a2020-05-15 19:04:24 +0200359 for feature_jar in dump.feature_jars():
360 cmd.extend(['--feature-jar', feature_jar,
361 determine_feature_output(feature_jar, temp)])
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100362 if dump.library_jar():
363 cmd.extend(['--lib', dump.library_jar()])
Morten Krogh-Jespersenbb624e42021-04-19 14:33:48 +0200364 if dump.classpath_jar() and compiler != 'l8':
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100365 cmd.extend(['--classpath', dump.classpath_jar()])
Morten Krogh-Jespersend86a81b2020-11-13 12:33:26 +0100366 if dump.desugared_library_json() and not args.disable_desugared_lib:
Clément Béradaae4ca2020-10-27 14:26:41 +0000367 cmd.extend(['--desugared-lib', dump.desugared_library_json()])
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100368 if compiler != 'd8' and dump.config_file():
Morten Krogh-Jespersen51a16352020-11-04 09:31:15 +0100369 if hasattr(args, 'config_file_consumer') and args.config_file_consumer:
370 args.config_file_consumer(dump.config_file())
Rico Windc9e52f72021-08-19 08:30:26 +0200371 else:
372 # If we get a dump from the wild we can't use -injars, -libraryjars or
373 # -print{mapping,usage}
374 clean_config(dump.config_file())
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100375 cmd.extend(['--pg-conf', dump.config_file()])
Morten Krogh-Jespersen1e774252021-03-01 16:56:07 +0100376 if dump.main_dex_rules_resource():
377 cmd.extend(['--main-dex-rules', dump.main_dex_rules_resource()])
Morten Krogh-Jespersenbb624e42021-04-19 14:33:48 +0200378 if compiler == 'l8':
379 if dump.config_file():
380 cmd.extend(['--pg-map-output', '%s.map' % out])
381 elif compiler != 'd8':
Ian Zerny588120b2020-04-03 13:08:03 +0200382 cmd.extend(['--pg-map-output', '%s.map' % out])
Ian Zerny77bdf5f2020-07-08 11:46:24 +0200383 if min_api:
384 cmd.extend(['--min-api', min_api])
Morten Krogh-Jespersen45d7a7b2020-11-02 08:31:09 +0100385 if classfile:
386 cmd.extend(['--classfile'])
Ian Zerny77bdf5f2020-07-08 11:46:24 +0200387 if args.threads:
388 cmd.extend(['--threads', args.threads])
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100389 cmd.extend(otherargs)
390 utils.PrintCmd(cmd)
391 try:
Morten Krogh-Jespersen6c702402021-06-21 12:29:48 +0200392 print(subprocess.check_output(cmd, stderr=subprocess.STDOUT).decode('utf-8'))
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100393 return 0
Rico Wind3d369b42021-01-12 10:26:24 +0100394 except subprocess.CalledProcessError as e:
Ian Zerny9ff31b72021-04-22 11:36:26 +0200395 if args.nolib \
396 or version == 'source' \
397 or not try_retrace_output(e, version, temp):
Morten Krogh-Jespersen86222742021-03-02 11:13:33 +0100398 print(e.output.decode('UTF-8'))
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100399 return 1
400
Ian Zerny9ff31b72021-04-22 11:36:26 +0200401def try_retrace_output(e, version, temp):
402 try:
403 stacktrace = os.path.join(temp, 'stacktrace')
404 open(stacktrace, 'w+').write(e.output.decode('UTF-8'))
405 print("=" * 80)
406 print(" RETRACED OUTPUT")
407 print("=" * 80)
408 retrace.run(get_map_file(version, temp), stacktrace, no_r8lib=False)
409 return True
410 except Exception as e2:
411 print("Failed to retrace for version: %s" % version)
412 print(e2)
413 return False
414
415def get_map_file(version, temp):
416 if version == 'main':
417 return utils.R8LIB_MAP
418 download_path = archive.GetUploadDestination(
419 version,
420 'r8lib.jar.map',
421 is_hash(version))
422 if utils.file_exists_on_cloud_storage(download_path):
423 map_path = os.path.join(temp, 'mapping.map')
424 utils.download_file_from_cloud_storage(download_path, map_path)
425 return map_path
426 else:
427 print('Could not find map file from argument: %s.' % version)
428 return None
429
Ian Zerny0e53ff12021-06-15 13:40:14 +0200430def summarize_dump_files(dumpfiles):
431 if len(dumpfiles) == 0:
432 error('Summary command expects a list of dumps to summarize')
433 for f in dumpfiles:
434 print(f + ':')
435 try:
436 with utils.TempDir() as temp:
437 dump = read_dump(f, temp)
438 summarize_dump(dump)
439 except IOError as e:
440 print("Error: " + str(e))
441 except zipfile.BadZipfile as e:
442 print("Error: " + str(e))
443
444def summarize_dump(dump):
445 version = dump.version()
446 if not version:
447 print('No dump version info')
448 return
449 print('version=' + version)
450 props = dump.build_properties_file()
451 if props:
452 with open(props) as props_file:
453 print(props_file.read())
454 if dump.library_jar():
455 print('library.jar present')
456 if dump.classpath_jar():
457 print('classpath.jar present')
458 prog = dump.program_jar()
459 if prog:
460 print('program.jar content:')
461 summarize_jar(prog)
462
463def summarize_jar(jar):
464 with zipfile.ZipFile(jar) as zip:
465 pkgs = {}
466 for info in zip.infolist():
467 if info.filename.endswith('.class'):
468 pkg, clazz = os.path.split(info.filename)
469 count = pkgs.get(pkg, 0)
470 pkgs[pkg] = count + 1
471 sorted = list(pkgs.keys())
472 sorted.sort()
473 for p in sorted:
474 print(' ' + p + ': ' + str(pkgs[p]))
475
Søren Gjesse7360f2b2020-08-10 09:13:35 +0200476def run(args, otherargs):
Ian Zerny0e53ff12021-06-15 13:40:14 +0200477 if args.summary:
478 summarize_dump_files(otherargs)
479 elif args.loop:
Søren Gjesse7360f2b2020-08-10 09:13:35 +0200480 count = 1
481 while True:
482 print('Iteration {:03d}'.format(count))
483 out = args.temp
484 if out:
485 out = os.path.join(out, '{:03d}'.format(count))
486 run1(out, args, otherargs)
487 count += 1
488 else:
489 run1(args.temp, args, otherargs)
490
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100491if __name__ == '__main__':
492 (args, otherargs) = make_parser().parse_known_args(sys.argv[1:])
493 sys.exit(run(args, otherargs))