Ian Zerny | dcb172e | 2022-02-22 15:36:45 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 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 | |
| 6 | import argparse |
| 7 | import os |
Christoffer Quist Adamsen | 3f21c2b | 2023-02-28 09:37:22 +0100 | [diff] [blame] | 8 | import shutil |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 9 | import subprocess |
| 10 | import sys |
| 11 | import zipfile |
| 12 | |
Ian Zerny | 0e53ff1 | 2021-06-15 13:40:14 +0200 | [diff] [blame] | 13 | import archive |
| 14 | import jdk |
| 15 | import retrace |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 16 | import utils |
| 17 | |
| 18 | |
| 19 | def make_parser(): |
| 20 | parser = argparse.ArgumentParser(description = 'Compile a dump artifact.') |
| 21 | parser.add_argument( |
Ian Zerny | 0e53ff1 | 2021-06-15 13:40:14 +0200 | [diff] [blame] | 22 | '--summary', |
| 23 | help='List a summary of the contents of the dumps.', |
| 24 | default=False, |
| 25 | action='store_true') |
| 26 | parser.add_argument( |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 27 | '-d', |
| 28 | '--dump', |
Christoffer Quist Adamsen | d84a6f0 | 2020-05-16 12:29:35 +0200 | [diff] [blame] | 29 | help='Dump file or directory to compile', |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 30 | default=None) |
| 31 | parser.add_argument( |
Søren Gjesse | aee909b | 2023-09-22 17:16:12 +0200 | [diff] [blame] | 32 | '-o', |
| 33 | '--output', |
| 34 | help='File to output (defaults to out.jar in temp)', |
| 35 | default=None) |
| 36 | parser.add_argument( |
Rico Wind | 9ed87b9 | 2020-03-06 12:37:18 +0100 | [diff] [blame] | 37 | '--temp', |
| 38 | help='Temp directory to extract the dump to, allows you to rerun the command' |
| 39 | ' more easily in the terminal with changes', |
| 40 | default=None) |
| 41 | parser.add_argument( |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 42 | '-c', |
| 43 | '--compiler', |
Rico Wind | f73f18d | 2020-03-03 09:28:54 +0100 | [diff] [blame] | 44 | help='Compiler to use', |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 45 | default=None) |
| 46 | parser.add_argument( |
Christoffer Quist Adamsen | f86fc0b | 2021-12-10 12:39:36 +0100 | [diff] [blame] | 47 | '--minify', |
| 48 | help='Force enable/disable minification' |
| 49 | ' (defaults to app proguard config)', |
| 50 | choices=['default', 'force-enable', 'force-disable'], |
| 51 | default='default') |
| 52 | parser.add_argument( |
| 53 | '--optimize', |
| 54 | help='Force enable/disable optimizations' |
| 55 | ' (defaults to app proguard config)', |
| 56 | choices=['default', 'force-enable', 'force-disable'], |
| 57 | default='default') |
| 58 | parser.add_argument( |
| 59 | '--shrink', |
| 60 | help='Force enable/disable shrinking' |
| 61 | ' (defaults to app proguard config)', |
| 62 | choices=['default', 'force-enable', 'force-disable'], |
| 63 | default='default') |
| 64 | parser.add_argument( |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 65 | '-v', |
| 66 | '--version', |
| 67 | help='Compiler version to use (default read from dump version file).' |
| 68 | 'Valid arguments are:' |
Rico Wind | 1b52acf | 2021-03-21 12:36:55 +0100 | [diff] [blame] | 69 | ' "main" to run from your own tree,' |
Morten Krogh-Jespersen | 51db2b0 | 2020-11-11 12:49:26 +0100 | [diff] [blame] | 70 | ' "source" to run from build classes directly,' |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 71 | ' "X.Y.Z" to run a specific version, or' |
Rico Wind | 1b52acf | 2021-03-21 12:36:55 +0100 | [diff] [blame] | 72 | ' <hash> to run that hash from main.', |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 73 | default=None) |
| 74 | parser.add_argument( |
Morten Krogh-Jespersen | d8bceb5 | 2020-03-06 12:56:48 +0100 | [diff] [blame] | 75 | '--r8-jar', |
| 76 | help='Path to an R8 jar.', |
| 77 | default=None) |
| 78 | parser.add_argument( |
Christoffer Quist Adamsen | 2f48f3f | 2021-10-14 17:25:03 +0200 | [diff] [blame] | 79 | '--r8-flags', '--r8_flags', |
| 80 | help='Additional option(s) for the compiler.') |
| 81 | parser.add_argument( |
Christoffer Quist Adamsen | 3f21c2b | 2023-02-28 09:37:22 +0100 | [diff] [blame] | 82 | '--pg-conf', '--pg_conf', |
| 83 | help='Keep rule file(s).', |
| 84 | action='append') |
| 85 | parser.add_argument( |
Morten Krogh-Jespersen | 1fbfd59 | 2021-11-26 13:41:32 +0100 | [diff] [blame] | 86 | '--override', |
Morten Krogh-Jespersen | 9206a66 | 2020-11-23 08:47:06 +0100 | [diff] [blame] | 87 | help='Do not override any extracted dump in temp-dir', |
| 88 | default=False, |
| 89 | action='store_true') |
| 90 | parser.add_argument( |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 91 | '--nolib', |
| 92 | help='Use the non-lib distribution (default uses the lib distribution)', |
| 93 | default=False, |
| 94 | action='store_true') |
| 95 | parser.add_argument( |
Morten Krogh-Jespersen | 6c70240 | 2021-06-21 12:29:48 +0200 | [diff] [blame] | 96 | '--print-times', |
Rico Wind | 2865364 | 2020-03-31 13:59:07 +0200 | [diff] [blame] | 97 | help='Print timing information from r8', |
| 98 | default=False, |
| 99 | action='store_true') |
| 100 | parser.add_argument( |
Christoffer Quist Adamsen | 08da5e5 | 2023-09-22 09:37:37 +0200 | [diff] [blame] | 101 | '--disable-assertions', '--disable_assertions', '-da', |
| 102 | help='Disable Java assertions when running the compiler (default enabled)', |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 103 | default=False, |
| 104 | action='store_true') |
| 105 | parser.add_argument( |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 106 | '--classfile', |
| 107 | help='Run with classfile output', |
| 108 | default=False, |
| 109 | action='store_true') |
| 110 | parser.add_argument( |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 111 | '--debug-agent', |
| 112 | help='Enable Java debug agent and suspend compilation (default disabled)', |
| 113 | default=False, |
| 114 | action='store_true') |
Ian Zerny | 77bdf5f | 2020-07-08 11:46:24 +0200 | [diff] [blame] | 115 | parser.add_argument( |
| 116 | '--xmx', |
| 117 | help='Set JVM max heap size (-Xmx)', |
| 118 | default=None) |
| 119 | parser.add_argument( |
| 120 | '--threads', |
| 121 | help='Set the number of threads to use', |
| 122 | default=None) |
| 123 | parser.add_argument( |
| 124 | '--min-api', |
| 125 | help='Set min-api (default read from dump properties file)', |
| 126 | default=None) |
Søren Gjesse | 7360f2b | 2020-08-10 09:13:35 +0200 | [diff] [blame] | 127 | parser.add_argument( |
Clément Béra | daae4ca | 2020-10-27 14:26:41 +0000 | [diff] [blame] | 128 | '--desugared-lib', |
| 129 | help='Set desugared-library (default set from dump)', |
| 130 | default=None) |
| 131 | parser.add_argument( |
Morten Krogh-Jespersen | d86a81b | 2020-11-13 12:33:26 +0100 | [diff] [blame] | 132 | '--disable-desugared-lib', |
| 133 | help='Disable desugared-libary if it will be set from dump', |
| 134 | default=False, |
| 135 | action='store_true' |
| 136 | ) |
| 137 | parser.add_argument( |
Søren Gjesse | 7360f2b | 2020-08-10 09:13:35 +0200 | [diff] [blame] | 138 | '--loop', |
| 139 | help='Run the compilation in a loop', |
| 140 | default=False, |
| 141 | action='store_true') |
Morten Krogh-Jespersen | 9e201ea | 2022-06-14 12:41:50 +0200 | [diff] [blame] | 142 | parser.add_argument( |
| 143 | '--enable-missing-library-api-modeling', |
| 144 | help='Run with api modeling', |
| 145 | default=False, |
| 146 | action='store_true') |
| 147 | parser.add_argument( |
| 148 | '--android-platform-build', |
| 149 | help='Run as a platform build', |
| 150 | default=False, |
| 151 | action='store_true') |
Morten Krogh-Jespersen | aa2f9ec | 2022-07-04 13:10:17 +0200 | [diff] [blame] | 152 | parser.add_argument( |
| 153 | '--compilation-mode', '--compilation_mode', |
| 154 | help='Run compilation in specified mode', |
| 155 | choices=['debug', 'release'], |
| 156 | default=None) |
Ian Zerny | dfc3689 | 2023-08-25 14:11:57 +0200 | [diff] [blame] | 157 | parser.add_argument( |
| 158 | '--new-gradle', |
| 159 | help='Use the new gradle build (defaults to old build)', |
| 160 | default=False, |
| 161 | action='store_true') |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 162 | return parser |
| 163 | |
| 164 | def error(msg): |
Rico Wind | 3d369b4 | 2021-01-12 10:26:24 +0100 | [diff] [blame] | 165 | print(msg) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 166 | sys.exit(1) |
| 167 | |
| 168 | class Dump(object): |
| 169 | |
| 170 | def __init__(self, directory): |
| 171 | self.directory = directory |
| 172 | |
| 173 | def if_exists(self, name): |
| 174 | f = os.path.join(self.directory, name) |
| 175 | if os.path.exists(f): |
| 176 | return f |
| 177 | return None |
| 178 | |
| 179 | def program_jar(self): |
| 180 | return self.if_exists('program.jar') |
| 181 | |
Christoffer Quist Adamsen | fcfc63a | 2020-05-15 19:04:24 +0200 | [diff] [blame] | 182 | def feature_jars(self): |
| 183 | feature_jars = [] |
| 184 | i = 1 |
| 185 | while True: |
| 186 | feature_jar = self.if_exists('feature-%s.jar' % i) |
| 187 | if feature_jar: |
| 188 | feature_jars.append(feature_jar) |
| 189 | i = i + 1 |
| 190 | else: |
| 191 | return feature_jars |
| 192 | |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 193 | def library_jar(self): |
| 194 | return self.if_exists('library.jar') |
| 195 | |
| 196 | def classpath_jar(self): |
| 197 | return self.if_exists('classpath.jar') |
| 198 | |
Clément Béra | daae4ca | 2020-10-27 14:26:41 +0000 | [diff] [blame] | 199 | def desugared_library_json(self): |
| 200 | return self.if_exists('desugared-library.json') |
| 201 | |
Clément Béra | 64a3c4c | 2020-11-10 08:16:17 +0000 | [diff] [blame] | 202 | def proguard_input_map(self): |
| 203 | if self.if_exists('proguard_input.config'): |
Rico Wind | 3d369b4 | 2021-01-12 10:26:24 +0100 | [diff] [blame] | 204 | print("Unimplemented: proguard_input configuration.") |
Clément Béra | 64a3c4c | 2020-11-10 08:16:17 +0000 | [diff] [blame] | 205 | |
Morten Krogh-Jespersen | 1e77425 | 2021-03-01 16:56:07 +0100 | [diff] [blame] | 206 | def main_dex_list_resource(self): |
Christoffer Quist Adamsen | cd7f7ec | 2022-08-26 13:39:11 +0200 | [diff] [blame] | 207 | return self.if_exists('main-dex-list.txt') |
Clément Béra | 64a3c4c | 2020-11-10 08:16:17 +0000 | [diff] [blame] | 208 | |
Morten Krogh-Jespersen | 1e77425 | 2021-03-01 16:56:07 +0100 | [diff] [blame] | 209 | def main_dex_rules_resource(self): |
| 210 | return self.if_exists('main-dex-rules.txt') |
| 211 | |
Christoffer Quist Adamsen | 819273a | 2023-03-02 15:20:45 +0100 | [diff] [blame] | 212 | def art_profile_resources(self): |
| 213 | art_profile_resources = [] |
| 214 | while True: |
| 215 | current_art_profile_index = len(art_profile_resources) + 1 |
| 216 | art_profile_resource = self.if_exists( |
| 217 | 'art-profile-%s.txt' % current_art_profile_index) |
| 218 | if art_profile_resource is None: |
| 219 | return art_profile_resources |
| 220 | art_profile_resources.append(art_profile_resource) |
| 221 | |
Christoffer Quist Adamsen | cd7f7ec | 2022-08-26 13:39:11 +0200 | [diff] [blame] | 222 | def startup_profile_resources(self): |
| 223 | startup_profile_resources = [] |
| 224 | while True: |
| 225 | current_startup_profile_index = len(startup_profile_resources) + 1 |
| 226 | startup_profile_resource = self.if_exists( |
| 227 | 'startup-profile-%s.txt' % current_startup_profile_index) |
| 228 | if startup_profile_resource is None: |
| 229 | return startup_profile_resources |
| 230 | startup_profile_resources.append(startup_profile_resource) |
| 231 | |
Christoffer Quist Adamsen | fcfc63a | 2020-05-15 19:04:24 +0200 | [diff] [blame] | 232 | def build_properties_file(self): |
| 233 | return self.if_exists('build.properties') |
| 234 | |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 235 | def config_file(self): |
| 236 | return self.if_exists('proguard.config') |
| 237 | |
| 238 | def version_file(self): |
| 239 | return self.if_exists('r8-version') |
| 240 | |
| 241 | def version(self): |
| 242 | f = self.version_file() |
| 243 | if f: |
| 244 | return open(f).read().split(' ')[0] |
| 245 | return None |
| 246 | |
Ian Zerny | 0e53ff1 | 2021-06-15 13:40:14 +0200 | [diff] [blame] | 247 | def read_dump_from_args(args, temp): |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 248 | if args.dump is None: |
Christoffer Quist Adamsen | d84a6f0 | 2020-05-16 12:29:35 +0200 | [diff] [blame] | 249 | error("A dump file or directory must be specified") |
Ian Zerny | 674099e | 2021-06-15 13:44:37 +0200 | [diff] [blame] | 250 | return read_dump(args.dump, temp, args.override) |
Ian Zerny | 0e53ff1 | 2021-06-15 13:40:14 +0200 | [diff] [blame] | 251 | |
| 252 | def read_dump(dump, temp, override=False): |
| 253 | if os.path.isdir(dump): |
| 254 | return Dump(dump) |
| 255 | dump_file = zipfile.ZipFile(os.path.abspath(dump), 'r') |
Christoffer Quist Adamsen | be6661d | 2023-08-24 11:01:12 +0200 | [diff] [blame] | 256 | r8_version_file = os.path.join(temp, 'r8-version') |
| 257 | |
| 258 | if override or not os.path.isfile(r8_version_file): |
| 259 | dump_file.extractall(temp) |
| 260 | if not os.path.isfile(r8_version_file): |
| 261 | error("Did not extract into %s. Either the zip file is invalid or the " |
| 262 | "dump is missing files" % temp) |
| 263 | return Dump(temp) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 264 | |
Christoffer Quist Adamsen | fcfc63a | 2020-05-15 19:04:24 +0200 | [diff] [blame] | 265 | def determine_build_properties(args, dump): |
| 266 | build_properties = {} |
| 267 | build_properties_file = dump.build_properties_file() |
| 268 | if build_properties_file: |
| 269 | with open(build_properties_file) as f: |
| 270 | build_properties_contents = f.readlines() |
| 271 | for line in build_properties_contents: |
| 272 | stripped = line.strip() |
| 273 | if stripped: |
| 274 | pair = stripped.split('=') |
| 275 | build_properties[pair[0]] = pair[1] |
Rico Wind | cfe1b2a | 2023-03-03 08:11:39 +0100 | [diff] [blame] | 276 | if 'mode' not in build_properties: |
| 277 | build_properties['mode'] = 'release' |
Christoffer Quist Adamsen | fcfc63a | 2020-05-15 19:04:24 +0200 | [diff] [blame] | 278 | return build_properties |
| 279 | |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 280 | def determine_version(args, dump): |
| 281 | if args.version is None: |
| 282 | return dump.version() |
| 283 | return args.version |
| 284 | |
Søren Gjesse | 0f8d88b | 2021-09-28 15:15:54 +0200 | [diff] [blame] | 285 | def determine_compiler(args, build_properties): |
Morten Krogh-Jespersen | dd12e78 | 2023-03-01 16:34:51 +0100 | [diff] [blame] | 286 | compilers = ['d8', 'r8', 'r8full', 'l8', 'l8d8', 'tracereferences'] |
Søren Gjesse | 0f8d88b | 2021-09-28 15:15:54 +0200 | [diff] [blame] | 287 | compiler = args.compiler |
| 288 | if not compiler and 'tool' in build_properties: |
| 289 | compiler = build_properties.get('tool').lower() |
Morten Krogh-Jespersen | dd12e78 | 2023-03-01 16:34:51 +0100 | [diff] [blame] | 290 | if compiler == 'r8': |
Søren Gjesse | 0f8d88b | 2021-09-28 15:15:54 +0200 | [diff] [blame] | 291 | if not 'force-proguard-compatibility' in build_properties: |
| 292 | error("Unable to determine R8 compiler variant from build.properties." |
| 293 | " No value for 'force-proguard-compatibility'.") |
| 294 | if build_properties.get('force-proguard-compatibility').lower() == 'false': |
| 295 | compiler = compiler + 'full' |
Morten Krogh-Jespersen | dd12e78 | 2023-03-01 16:34:51 +0100 | [diff] [blame] | 296 | if compiler == 'TraceReferences': |
| 297 | compiler = build_properties.get('tool').lower() |
Søren Gjesse | 0f8d88b | 2021-09-28 15:15:54 +0200 | [diff] [blame] | 298 | if compiler not in compilers: |
Rico Wind | f73f18d | 2020-03-03 09:28:54 +0100 | [diff] [blame] | 299 | error("Unable to determine a compiler to use. Specified %s," |
| 300 | " Valid options: %s" % (args.compiler, ', '.join(compilers))) |
Søren Gjesse | 0f8d88b | 2021-09-28 15:15:54 +0200 | [diff] [blame] | 301 | return compiler |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 302 | |
Morten Krogh-Jespersen | dd12e78 | 2023-03-01 16:34:51 +0100 | [diff] [blame] | 303 | def determine_trace_references_commands(build_properties, output): |
| 304 | trace_ref_consumer = build_properties.get('trace_references_consumer') |
| 305 | if trace_ref_consumer == 'com.android.tools.r8.tracereferences.TraceReferencesCheckConsumer': |
| 306 | return ["--check"] |
| 307 | else: |
| 308 | assert trace_ref_consumer == 'com.android.tools.r8.tracereferences.TraceReferencesKeepRules' |
| 309 | args = ['--allowobfuscation'] if build_properties.get('minification') == 'true' else [] |
| 310 | args.extend(['--keep-rules', '--output', output]) |
| 311 | return args |
| 312 | |
Christoffer Quist Adamsen | 7953d0a | 2023-02-27 12:08:33 +0100 | [diff] [blame] | 313 | def is_l8_compiler(compiler): |
| 314 | return compiler.startswith('l8') |
| 315 | |
Christoffer Quist Adamsen | 3f21c2b | 2023-02-28 09:37:22 +0100 | [diff] [blame] | 316 | def is_r8_compiler(compiler): |
| 317 | return compiler.startswith('r8') |
| 318 | |
| 319 | def determine_config_files(args, dump, temp): |
| 320 | if args.pg_conf: |
| 321 | config_files = [] |
| 322 | for config_file in args.pg_conf: |
| 323 | dst = os.path.join(temp, 'proguard-%s.config' % len(config_files)) |
| 324 | shutil.copyfile(config_file, dst) |
| 325 | config_files.append(dst) |
| 326 | return config_files |
| 327 | dump_config_file = dump.config_file() |
| 328 | if dump_config_file: |
| 329 | return [dump_config_file] |
| 330 | return [] |
| 331 | |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 332 | def determine_output(args, temp): |
Søren Gjesse | aee909b | 2023-09-22 17:16:12 +0200 | [diff] [blame] | 333 | if (args.output): |
| 334 | return args.output |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 335 | return os.path.join(temp, 'out.jar') |
| 336 | |
Ian Zerny | 77bdf5f | 2020-07-08 11:46:24 +0200 | [diff] [blame] | 337 | def determine_min_api(args, build_properties): |
| 338 | if args.min_api: |
| 339 | return args.min_api |
| 340 | if 'min-api' in build_properties: |
| 341 | return build_properties.get('min-api') |
| 342 | return None |
| 343 | |
Christoffer Quist Adamsen | 819273a | 2023-03-02 15:20:45 +0100 | [diff] [blame] | 344 | def determine_residual_art_profile_output(art_profile, temp): |
| 345 | return os.path.join(temp, os.path.basename(art_profile)[:-4] + ".out.txt") |
| 346 | |
Christoffer Quist Adamsen | 3f21c2b | 2023-02-28 09:37:22 +0100 | [diff] [blame] | 347 | def determine_desugared_lib_pg_conf_output(temp): |
| 348 | return os.path.join(temp, 'desugared-library-keep-rules.config') |
| 349 | |
Christoffer Quist Adamsen | fcfc63a | 2020-05-15 19:04:24 +0200 | [diff] [blame] | 350 | def determine_feature_output(feature_jar, temp): |
| 351 | return os.path.join(temp, os.path.basename(feature_jar)[:-4] + ".out.jar") |
| 352 | |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 353 | def determine_program_jar(args, dump): |
| 354 | if hasattr(args, 'program_jar') and args.program_jar: |
| 355 | return args.program_jar |
| 356 | return dump.program_jar() |
| 357 | |
| 358 | def determine_class_file(args, build_properties): |
Christoffer Quist Adamsen | 0a3c16e | 2023-02-15 14:34:44 +0100 | [diff] [blame] | 359 | return args.classfile \ |
| 360 | or build_properties.get('backend', 'dex').lower() == 'cf' |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 361 | |
Morten Krogh-Jespersen | 9e201ea | 2022-06-14 12:41:50 +0200 | [diff] [blame] | 362 | def determine_android_platform_build(args, build_properties): |
| 363 | if args.android_platform_build: |
Morten Krogh-Jespersen | 9e201ea | 2022-06-14 12:41:50 +0200 | [diff] [blame] | 364 | return True |
Morten Krogh-Jespersen | aa2f9ec | 2022-07-04 13:10:17 +0200 | [diff] [blame] | 365 | return build_properties.get('android-platform-build') == 'true' |
Morten Krogh-Jespersen | 9e201ea | 2022-06-14 12:41:50 +0200 | [diff] [blame] | 366 | |
| 367 | def determine_enable_missing_library_api_modeling(args, build_properties): |
| 368 | if args.enable_missing_library_api_modeling: |
Morten Krogh-Jespersen | 9e201ea | 2022-06-14 12:41:50 +0200 | [diff] [blame] | 369 | return True |
Morten Krogh-Jespersen | aa2f9ec | 2022-07-04 13:10:17 +0200 | [diff] [blame] | 370 | return build_properties.get('enable-missing-library-api-modeling') == 'true' |
| 371 | |
| 372 | def determine_compilation_mode(args, build_properties): |
| 373 | if args.compilation_mode: |
| 374 | return args.compilation_mode |
| 375 | return build_properties.get('mode') |
Morten Krogh-Jespersen | 9e201ea | 2022-06-14 12:41:50 +0200 | [diff] [blame] | 376 | |
Søren Gjesse | 1768e25 | 2021-10-06 12:50:36 +0200 | [diff] [blame] | 377 | def determine_properties(build_properties): |
| 378 | args = [] |
| 379 | for key, value in build_properties.items(): |
| 380 | # When writing dumps all system properties starting with com.android.tools.r8 |
| 381 | # are written to the build.properties file in the format |
| 382 | # system-property-com.android.tools.r8.XXX=<value> |
| 383 | if key.startswith('system-property-'): |
| 384 | name = key[len('system-property-'):] |
| 385 | if name.endswith('dumpinputtofile') or name.endswith('dumpinputtodirectory'): |
| 386 | continue |
| 387 | if len(value) == 0: |
| 388 | args.append('-D' + name) |
| 389 | else: |
| 390 | args.append('-D' + name + '=' + value) |
| 391 | return args |
| 392 | |
Ian Zerny | dfc3689 | 2023-08-25 14:11:57 +0200 | [diff] [blame] | 393 | def download_distribution(version, args, temp): |
| 394 | nolib = args.nolib |
Rico Wind | 1b52acf | 2021-03-21 12:36:55 +0100 | [diff] [blame] | 395 | if version == 'main': |
Ian Zerny | dfc3689 | 2023-08-25 14:11:57 +0200 | [diff] [blame] | 396 | if args.new_gradle: |
| 397 | return "%s:%s" % ( |
| 398 | "d8_r8/main/build/libs/deps.jar", |
| 399 | "d8_r8/main/build/libs/r8.jar") |
| 400 | else: |
| 401 | return utils.R8_JAR if nolib else utils.R8LIB_JAR |
Morten Krogh-Jespersen | 51db2b0 | 2020-11-11 12:49:26 +0100 | [diff] [blame] | 402 | if version == 'source': |
| 403 | return '%s:%s' % (utils.BUILD_JAVA_MAIN_DIR, utils.ALL_DEPS_JAR) |
Ian Zerny | 171c6a6 | 2022-04-04 14:25:30 +0200 | [diff] [blame] | 404 | name = 'r8.jar' if nolib else 'r8lib.jar' |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 405 | source = archive.GetUploadDestination(version, name, is_hash(version)) |
| 406 | dest = os.path.join(temp, 'r8.jar') |
| 407 | utils.download_file_from_cloud_storage(source, dest) |
| 408 | return dest |
| 409 | |
Christoffer Quist Adamsen | 3f21c2b | 2023-02-28 09:37:22 +0100 | [diff] [blame] | 410 | def clean_configs(files, args): |
| 411 | for file in files: |
| 412 | clean_config(file, args) |
Rico Wind | c9e52f7 | 2021-08-19 08:30:26 +0200 | [diff] [blame] | 413 | |
Christoffer Quist Adamsen | f86fc0b | 2021-12-10 12:39:36 +0100 | [diff] [blame] | 414 | def clean_config(file, args): |
Rico Wind | c9e52f7 | 2021-08-19 08:30:26 +0200 | [diff] [blame] | 415 | with open(file) as f: |
| 416 | lines = f.readlines() |
Christoffer Quist Adamsen | f86fc0b | 2021-12-10 12:39:36 +0100 | [diff] [blame] | 417 | minify = args.minify |
| 418 | optimize = args.optimize |
| 419 | shrink = args.shrink |
Rico Wind | c9e52f7 | 2021-08-19 08:30:26 +0200 | [diff] [blame] | 420 | with open(file, 'w') as f: |
Christoffer Quist Adamsen | f86fc0b | 2021-12-10 12:39:36 +0100 | [diff] [blame] | 421 | if minify == 'force-disable': |
| 422 | print('Adding config line: -dontobfuscate') |
| 423 | f.write('-dontobfuscate\n') |
| 424 | if optimize == 'force-disable': |
| 425 | print('Adding config line: -dontoptimize') |
| 426 | f.write('-dontoptimize\n') |
| 427 | if shrink == 'force-disable': |
| 428 | print('Adding config line: -dontshrink') |
| 429 | f.write('-dontshrink\n') |
Rico Wind | c9e52f7 | 2021-08-19 08:30:26 +0200 | [diff] [blame] | 430 | for line in lines: |
Christoffer Quist Adamsen | f86fc0b | 2021-12-10 12:39:36 +0100 | [diff] [blame] | 431 | if clean_config_line(line, minify, optimize, shrink): |
Rico Wind | c9e52f7 | 2021-08-19 08:30:26 +0200 | [diff] [blame] | 432 | print('Removing from config line: \n%s' % line) |
Christoffer Quist Adamsen | f86fc0b | 2021-12-10 12:39:36 +0100 | [diff] [blame] | 433 | else: |
| 434 | f.write(line) |
Rico Wind | c9e52f7 | 2021-08-19 08:30:26 +0200 | [diff] [blame] | 435 | |
Christoffer Quist Adamsen | f86fc0b | 2021-12-10 12:39:36 +0100 | [diff] [blame] | 436 | def clean_config_line(line, minify, optimize, shrink): |
Søren Gjesse | a5dc7ed | 2023-03-20 13:25:38 +0100 | [diff] [blame] | 437 | if line.lstrip().startswith('#'): |
| 438 | return False |
Christoffer Quist Adamsen | f86fc0b | 2021-12-10 12:39:36 +0100 | [diff] [blame] | 439 | if ('-injars' in line or '-libraryjars' in line or |
Christoffer Quist Adamsen | 3f21c2b | 2023-02-28 09:37:22 +0100 | [diff] [blame] | 440 | '-print' in line or '-applymapping' in line): |
Christoffer Quist Adamsen | f86fc0b | 2021-12-10 12:39:36 +0100 | [diff] [blame] | 441 | return True |
| 442 | if minify == 'force-enable' and '-dontobfuscate' in line: |
| 443 | return True |
| 444 | if optimize == 'force-enable' and '-dontoptimize' in line: |
| 445 | return True |
| 446 | if shrink == 'force-enable' and '-dontshrink' in line: |
| 447 | return True |
| 448 | return False |
Rico Wind | c9e52f7 | 2021-08-19 08:30:26 +0200 | [diff] [blame] | 449 | |
Morten Krogh-Jespersen | 327e76d | 2022-06-23 09:31:13 +0200 | [diff] [blame] | 450 | def prepare_r8_wrapper(dist, temp, jdkhome): |
Christoffer Quist Adamsen | 819273a | 2023-03-02 15:20:45 +0100 | [diff] [blame] | 451 | compile_wrapper_with_javac( |
Morten Krogh-Jespersen | 327e76d | 2022-06-23 09:31:13 +0200 | [diff] [blame] | 452 | dist, |
| 453 | temp, |
| 454 | jdkhome, |
| 455 | os.path.join( |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 456 | utils.REPO_ROOT, |
Morten Krogh-Jespersen | 327e76d | 2022-06-23 09:31:13 +0200 | [diff] [blame] | 457 | 'src/main/java/com/android/tools/r8/utils/CompileDumpCompatR8.java')) |
| 458 | |
| 459 | def prepare_d8_wrapper(dist, temp, jdkhome): |
Christoffer Quist Adamsen | 819273a | 2023-03-02 15:20:45 +0100 | [diff] [blame] | 460 | compile_wrapper_with_javac( |
Morten Krogh-Jespersen | 327e76d | 2022-06-23 09:31:13 +0200 | [diff] [blame] | 461 | dist, |
| 462 | temp, |
| 463 | jdkhome, |
| 464 | os.path.join( |
| 465 | utils.REPO_ROOT, |
| 466 | 'src/main/java/com/android/tools/r8/utils/CompileDumpD8.java')) |
| 467 | |
Christoffer Quist Adamsen | 819273a | 2023-03-02 15:20:45 +0100 | [diff] [blame] | 468 | def compile_wrapper_with_javac(dist, temp, jdkhome, path): |
| 469 | base_path = os.path.join( |
| 470 | utils.REPO_ROOT, |
| 471 | 'src/main/java/com/android/tools/r8/utils/CompileDumpBase.java') |
Ian Zerny | 268c963 | 2020-11-13 11:36:35 +0100 | [diff] [blame] | 472 | cmd = [ |
Rico Wind | 33936d1 | 2021-04-07 08:04:14 +0200 | [diff] [blame] | 473 | jdk.GetJavacExecutable(jdkhome), |
Morten Krogh-Jespersen | 327e76d | 2022-06-23 09:31:13 +0200 | [diff] [blame] | 474 | path, |
Christoffer Quist Adamsen | 819273a | 2023-03-02 15:20:45 +0100 | [diff] [blame] | 475 | base_path, |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 476 | '-d', temp, |
| 477 | '-cp', dist, |
Ian Zerny | 268c963 | 2020-11-13 11:36:35 +0100 | [diff] [blame] | 478 | ] |
| 479 | utils.PrintCmd(cmd) |
| 480 | subprocess.check_output(cmd) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 481 | |
| 482 | def is_hash(version): |
| 483 | return len(version) == 40 |
| 484 | |
Christoffer Quist Adamsen | be6661d | 2023-08-24 11:01:12 +0200 | [diff] [blame] | 485 | def run1(out, args, otherargs, jdkhome=None, worker_id=None): |
Ian Zerny | 09d4283 | 2022-11-17 07:45:02 +0100 | [diff] [blame] | 486 | jvmargs = [] |
| 487 | compilerargs = [] |
| 488 | for arg in otherargs: |
| 489 | if arg.startswith('-D'): |
| 490 | jvmargs.append(arg) |
| 491 | else: |
| 492 | compilerargs.append(arg) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 493 | with utils.TempDir() as temp: |
Søren Gjesse | 7360f2b | 2020-08-10 09:13:35 +0200 | [diff] [blame] | 494 | if out: |
| 495 | temp = out |
Rico Wind | 9ed87b9 | 2020-03-06 12:37:18 +0100 | [diff] [blame] | 496 | if not os.path.exists(temp): |
| 497 | os.makedirs(temp) |
Ian Zerny | 0e53ff1 | 2021-06-15 13:40:14 +0200 | [diff] [blame] | 498 | dump = read_dump_from_args(args, temp) |
Ian Zerny | 46bc4cf | 2020-08-03 15:58:27 +0200 | [diff] [blame] | 499 | if not dump.program_jar(): |
| 500 | error("Cannot compile dump with no program classes") |
| 501 | if not dump.library_jar(): |
Rico Wind | 3d369b4 | 2021-01-12 10:26:24 +0100 | [diff] [blame] | 502 | print("WARNING: Unexpected lack of library classes in dump") |
Christoffer Quist Adamsen | fcfc63a | 2020-05-15 19:04:24 +0200 | [diff] [blame] | 503 | build_properties = determine_build_properties(args, dump) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 504 | version = determine_version(args, dump) |
Søren Gjesse | 0f8d88b | 2021-09-28 15:15:54 +0200 | [diff] [blame] | 505 | compiler = determine_compiler(args, build_properties) |
Christoffer Quist Adamsen | 3f21c2b | 2023-02-28 09:37:22 +0100 | [diff] [blame] | 506 | config_files = determine_config_files(args, dump, temp) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 507 | out = determine_output(args, temp) |
Ian Zerny | 77bdf5f | 2020-07-08 11:46:24 +0200 | [diff] [blame] | 508 | min_api = determine_min_api(args, build_properties) |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 509 | classfile = determine_class_file(args, build_properties) |
Morten Krogh-Jespersen | 9e201ea | 2022-06-14 12:41:50 +0200 | [diff] [blame] | 510 | android_platform_build = determine_android_platform_build(args, build_properties) |
| 511 | enable_missing_library_api_modeling = determine_enable_missing_library_api_modeling(args, build_properties) |
Morten Krogh-Jespersen | aa2f9ec | 2022-07-04 13:10:17 +0200 | [diff] [blame] | 512 | mode = determine_compilation_mode(args, build_properties) |
Ian Zerny | dfc3689 | 2023-08-25 14:11:57 +0200 | [diff] [blame] | 513 | jar = args.r8_jar if args.r8_jar else download_distribution(version, args, temp) |
Ian Zerny | 268c963 | 2020-11-13 11:36:35 +0100 | [diff] [blame] | 514 | if ':' not in jar and not os.path.exists(jar): |
| 515 | error("Distribution does not exist: " + jar) |
Rico Wind | 33936d1 | 2021-04-07 08:04:14 +0200 | [diff] [blame] | 516 | cmd = [jdk.GetJavaExecutable(jdkhome)] |
Ian Zerny | 09d4283 | 2022-11-17 07:45:02 +0100 | [diff] [blame] | 517 | cmd.extend(jvmargs) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 518 | if args.debug_agent: |
| 519 | if not args.nolib: |
Rico Wind | 3d369b4 | 2021-01-12 10:26:24 +0100 | [diff] [blame] | 520 | print("WARNING: Running debugging agent on r8lib is questionable...") |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 521 | cmd.append( |
| 522 | '-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005') |
Ian Zerny | 77bdf5f | 2020-07-08 11:46:24 +0200 | [diff] [blame] | 523 | if args.xmx: |
| 524 | cmd.append('-Xmx' + args.xmx) |
Christoffer Quist Adamsen | 08da5e5 | 2023-09-22 09:37:37 +0200 | [diff] [blame] | 525 | if not args.disable_assertions: |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 526 | cmd.append('-ea') |
Morten Krogh-Jespersen | 23f0b03 | 2021-07-06 18:10:15 +0200 | [diff] [blame] | 527 | cmd.append('-Dcom.android.tools.r8.enableTestAssertions=1') |
Morten Krogh-Jespersen | 6c70240 | 2021-06-21 12:29:48 +0200 | [diff] [blame] | 528 | if args.print_times: |
Rico Wind | 2865364 | 2020-03-31 13:59:07 +0200 | [diff] [blame] | 529 | cmd.append('-Dcom.android.tools.r8.printtimes=1') |
Christoffer Quist Adamsen | 2f48f3f | 2021-10-14 17:25:03 +0200 | [diff] [blame] | 530 | if args.r8_flags: |
| 531 | cmd.extend(args.r8_flags.split(' ')) |
Morten Krogh-Jespersen | 43f3cea | 2020-11-12 17:09:51 +0100 | [diff] [blame] | 532 | if hasattr(args, 'properties'): |
Morten Krogh-Jespersen | dd12e78 | 2023-03-01 16:34:51 +0100 | [diff] [blame] | 533 | cmd.extend(args.properties) |
Søren Gjesse | 1768e25 | 2021-10-06 12:50:36 +0200 | [diff] [blame] | 534 | cmd.extend(determine_properties(build_properties)) |
Morten Krogh-Jespersen | 327e76d | 2022-06-23 09:31:13 +0200 | [diff] [blame] | 535 | cmd.extend(['-cp', '%s:%s' % (temp, jar)]) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 536 | if compiler == 'd8': |
Ian Zerny | 1ad7cac | 2023-01-09 14:03:04 +0100 | [diff] [blame] | 537 | prepare_d8_wrapper(jar, temp, jdkhome) |
Morten Krogh-Jespersen | 327e76d | 2022-06-23 09:31:13 +0200 | [diff] [blame] | 538 | cmd.append('com.android.tools.r8.utils.CompileDumpD8') |
Christoffer Quist Adamsen | 7953d0a | 2023-02-27 12:08:33 +0100 | [diff] [blame] | 539 | if is_l8_compiler(compiler): |
Morten Krogh-Jespersen | bb624e4 | 2021-04-19 14:33:48 +0200 | [diff] [blame] | 540 | cmd.append('com.android.tools.r8.L8') |
Morten Krogh-Jespersen | dd12e78 | 2023-03-01 16:34:51 +0100 | [diff] [blame] | 541 | if compiler == 'tracereferences': |
| 542 | cmd.append('com.android.tools.r8.tracereferences.TraceReferences') |
| 543 | cmd.extend(determine_trace_references_commands(build_properties, out)) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 544 | if compiler.startswith('r8'): |
Ian Zerny | 1ad7cac | 2023-01-09 14:03:04 +0100 | [diff] [blame] | 545 | prepare_r8_wrapper(jar, temp, jdkhome) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 546 | cmd.append('com.android.tools.r8.utils.CompileDumpCompatR8') |
| 547 | if compiler == 'r8': |
| 548 | cmd.append('--compat') |
Morten Krogh-Jespersen | dd12e78 | 2023-03-01 16:34:51 +0100 | [diff] [blame] | 549 | if compiler != 'tracereferences': |
| 550 | assert mode == 'debug' or mode == 'release' |
| 551 | cmd.append('--' + mode) |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 552 | # For recompilation of dumps run_on_app_dumps pass in a program jar. |
Morten Krogh-Jespersen | dd12e78 | 2023-03-01 16:34:51 +0100 | [diff] [blame] | 553 | program_jar = determine_program_jar(args, dump) |
| 554 | if compiler != 'tracereferences': |
| 555 | cmd.append(program_jar) |
| 556 | cmd.extend(['--output', out]) |
| 557 | else: |
| 558 | cmd.extend(['--source', program_jar]) |
Christoffer Quist Adamsen | fcfc63a | 2020-05-15 19:04:24 +0200 | [diff] [blame] | 559 | for feature_jar in dump.feature_jars(): |
| 560 | cmd.extend(['--feature-jar', feature_jar, |
| 561 | determine_feature_output(feature_jar, temp)]) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 562 | if dump.library_jar(): |
| 563 | cmd.extend(['--lib', dump.library_jar()]) |
Christoffer Quist Adamsen | 7953d0a | 2023-02-27 12:08:33 +0100 | [diff] [blame] | 564 | if dump.classpath_jar() and not is_l8_compiler(compiler): |
Morten Krogh-Jespersen | dd12e78 | 2023-03-01 16:34:51 +0100 | [diff] [blame] | 565 | cmd.extend( |
| 566 | ['--target' if compiler == 'tracereferences' else '--classpath', |
| 567 | dump.classpath_jar()]) |
Morten Krogh-Jespersen | d86a81b | 2020-11-13 12:33:26 +0100 | [diff] [blame] | 568 | if dump.desugared_library_json() and not args.disable_desugared_lib: |
Clément Béra | daae4ca | 2020-10-27 14:26:41 +0000 | [diff] [blame] | 569 | cmd.extend(['--desugared-lib', dump.desugared_library_json()]) |
Christoffer Quist Adamsen | 3f21c2b | 2023-02-28 09:37:22 +0100 | [diff] [blame] | 570 | if not is_l8_compiler(compiler): |
| 571 | cmd.extend([ |
| 572 | '--desugared-lib-pg-conf-output', |
| 573 | determine_desugared_lib_pg_conf_output(temp)]) |
| 574 | if (is_r8_compiler(compiler) or compiler == 'l8') and config_files: |
| 575 | if hasattr(args, 'config_files_consumer') and args.config_files_consumer: |
| 576 | args.config_files_consumer(config_files) |
Rico Wind | c9e52f7 | 2021-08-19 08:30:26 +0200 | [diff] [blame] | 577 | else: |
| 578 | # If we get a dump from the wild we can't use -injars, -libraryjars or |
| 579 | # -print{mapping,usage} |
Christoffer Quist Adamsen | 3f21c2b | 2023-02-28 09:37:22 +0100 | [diff] [blame] | 580 | clean_configs(config_files, args) |
| 581 | for config_file in config_files: |
| 582 | cmd.extend(['--pg-conf', config_file]) |
| 583 | cmd.extend(['--pg-map-output', '%s.map' % out]) |
Christoffer Quist Adamsen | cd7f7ec | 2022-08-26 13:39:11 +0200 | [diff] [blame] | 584 | if dump.main_dex_list_resource(): |
| 585 | cmd.extend(['--main-dex-list', dump.main_dex_list_resource()]) |
Morten Krogh-Jespersen | 1e77425 | 2021-03-01 16:56:07 +0100 | [diff] [blame] | 586 | if dump.main_dex_rules_resource(): |
| 587 | cmd.extend(['--main-dex-rules', dump.main_dex_rules_resource()]) |
Christoffer Quist Adamsen | 819273a | 2023-03-02 15:20:45 +0100 | [diff] [blame] | 588 | for art_profile_resource in dump.art_profile_resources(): |
| 589 | residual_art_profile_output = \ |
| 590 | determine_residual_art_profile_output(art_profile_resource, temp) |
| 591 | cmd.extend([ |
| 592 | '--art-profile', art_profile_resource, residual_art_profile_output]) |
Christoffer Quist Adamsen | cd7f7ec | 2022-08-26 13:39:11 +0200 | [diff] [blame] | 593 | for startup_profile_resource in dump.startup_profile_resources(): |
| 594 | cmd.extend(['--startup-profile', startup_profile_resource]) |
Ian Zerny | 77bdf5f | 2020-07-08 11:46:24 +0200 | [diff] [blame] | 595 | if min_api: |
| 596 | cmd.extend(['--min-api', min_api]) |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 597 | if classfile: |
| 598 | cmd.extend(['--classfile']) |
Morten Krogh-Jespersen | 9e201ea | 2022-06-14 12:41:50 +0200 | [diff] [blame] | 599 | if android_platform_build: |
| 600 | cmd.extend(['--android-platform-build']) |
| 601 | if enable_missing_library_api_modeling: |
| 602 | cmd.extend(['--enable-missing-library-api-modeling']) |
Ian Zerny | 77bdf5f | 2020-07-08 11:46:24 +0200 | [diff] [blame] | 603 | if args.threads: |
| 604 | cmd.extend(['--threads', args.threads]) |
Ian Zerny | 09d4283 | 2022-11-17 07:45:02 +0100 | [diff] [blame] | 605 | cmd.extend(compilerargs) |
Christoffer Quist Adamsen | be6661d | 2023-08-24 11:01:12 +0200 | [diff] [blame] | 606 | utils.PrintCmd(cmd, worker_id=worker_id) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 607 | try: |
Morten Krogh-Jespersen | 6c70240 | 2021-06-21 12:29:48 +0200 | [diff] [blame] | 608 | print(subprocess.check_output(cmd, stderr=subprocess.STDOUT).decode('utf-8')) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 609 | return 0 |
Rico Wind | 3d369b4 | 2021-01-12 10:26:24 +0100 | [diff] [blame] | 610 | except subprocess.CalledProcessError as e: |
Ian Zerny | 9ff31b7 | 2021-04-22 11:36:26 +0200 | [diff] [blame] | 611 | if args.nolib \ |
| 612 | or version == 'source' \ |
| 613 | or not try_retrace_output(e, version, temp): |
Morten Krogh-Jespersen | 8622274 | 2021-03-02 11:13:33 +0100 | [diff] [blame] | 614 | print(e.output.decode('UTF-8')) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 615 | return 1 |
| 616 | |
Ian Zerny | 9ff31b7 | 2021-04-22 11:36:26 +0200 | [diff] [blame] | 617 | def try_retrace_output(e, version, temp): |
| 618 | try: |
| 619 | stacktrace = os.path.join(temp, 'stacktrace') |
| 620 | open(stacktrace, 'w+').write(e.output.decode('UTF-8')) |
| 621 | print("=" * 80) |
| 622 | print(" RETRACED OUTPUT") |
| 623 | print("=" * 80) |
Rico Wind | 759360f | 2023-09-29 08:10:47 +0200 | [diff] [blame] | 624 | retrace.run(get_map_file(version, temp), stacktrace, None, no_r8lib=False) |
Ian Zerny | 9ff31b7 | 2021-04-22 11:36:26 +0200 | [diff] [blame] | 625 | return True |
| 626 | except Exception as e2: |
| 627 | print("Failed to retrace for version: %s" % version) |
| 628 | print(e2) |
| 629 | return False |
| 630 | |
| 631 | def get_map_file(version, temp): |
| 632 | if version == 'main': |
| 633 | return utils.R8LIB_MAP |
| 634 | download_path = archive.GetUploadDestination( |
| 635 | version, |
| 636 | 'r8lib.jar.map', |
| 637 | is_hash(version)) |
| 638 | if utils.file_exists_on_cloud_storage(download_path): |
| 639 | map_path = os.path.join(temp, 'mapping.map') |
| 640 | utils.download_file_from_cloud_storage(download_path, map_path) |
| 641 | return map_path |
| 642 | else: |
| 643 | print('Could not find map file from argument: %s.' % version) |
| 644 | return None |
| 645 | |
Ian Zerny | 0e53ff1 | 2021-06-15 13:40:14 +0200 | [diff] [blame] | 646 | def summarize_dump_files(dumpfiles): |
| 647 | if len(dumpfiles) == 0: |
| 648 | error('Summary command expects a list of dumps to summarize') |
| 649 | for f in dumpfiles: |
| 650 | print(f + ':') |
| 651 | try: |
| 652 | with utils.TempDir() as temp: |
| 653 | dump = read_dump(f, temp) |
| 654 | summarize_dump(dump) |
| 655 | except IOError as e: |
| 656 | print("Error: " + str(e)) |
| 657 | except zipfile.BadZipfile as e: |
| 658 | print("Error: " + str(e)) |
| 659 | |
| 660 | def summarize_dump(dump): |
| 661 | version = dump.version() |
| 662 | if not version: |
| 663 | print('No dump version info') |
| 664 | return |
| 665 | print('version=' + version) |
| 666 | props = dump.build_properties_file() |
| 667 | if props: |
| 668 | with open(props) as props_file: |
| 669 | print(props_file.read()) |
| 670 | if dump.library_jar(): |
| 671 | print('library.jar present') |
| 672 | if dump.classpath_jar(): |
| 673 | print('classpath.jar present') |
| 674 | prog = dump.program_jar() |
| 675 | if prog: |
| 676 | print('program.jar content:') |
| 677 | summarize_jar(prog) |
| 678 | |
| 679 | def summarize_jar(jar): |
| 680 | with zipfile.ZipFile(jar) as zip: |
| 681 | pkgs = {} |
| 682 | for info in zip.infolist(): |
| 683 | if info.filename.endswith('.class'): |
| 684 | pkg, clazz = os.path.split(info.filename) |
| 685 | count = pkgs.get(pkg, 0) |
| 686 | pkgs[pkg] = count + 1 |
| 687 | sorted = list(pkgs.keys()) |
| 688 | sorted.sort() |
| 689 | for p in sorted: |
| 690 | print(' ' + p + ': ' + str(pkgs[p])) |
| 691 | |
Søren Gjesse | 7360f2b | 2020-08-10 09:13:35 +0200 | [diff] [blame] | 692 | def run(args, otherargs): |
Ian Zerny | 0e53ff1 | 2021-06-15 13:40:14 +0200 | [diff] [blame] | 693 | if args.summary: |
| 694 | summarize_dump_files(otherargs) |
| 695 | elif args.loop: |
Søren Gjesse | 7360f2b | 2020-08-10 09:13:35 +0200 | [diff] [blame] | 696 | count = 1 |
| 697 | while True: |
| 698 | print('Iteration {:03d}'.format(count)) |
| 699 | out = args.temp |
| 700 | if out: |
| 701 | out = os.path.join(out, '{:03d}'.format(count)) |
| 702 | run1(out, args, otherargs) |
| 703 | count += 1 |
| 704 | else: |
| 705 | run1(args.temp, args, otherargs) |
| 706 | |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 707 | if __name__ == '__main__': |
| 708 | (args, otherargs) = make_parser().parse_known_args(sys.argv[1:]) |
| 709 | sys.exit(run(args, otherargs)) |