Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 1 | #!/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 | |
Ian Zerny | 77bdf5f | 2020-07-08 11:46:24 +0200 | [diff] [blame] | 6 | import archive |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 7 | import argparse |
Ian Zerny | 77bdf5f | 2020-07-08 11:46:24 +0200 | [diff] [blame] | 8 | import jdk |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 9 | import os |
Ian Zerny | 77bdf5f | 2020-07-08 11:46:24 +0200 | [diff] [blame] | 10 | import retrace |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 11 | import subprocess |
| 12 | import sys |
| 13 | import zipfile |
| 14 | |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 15 | import utils |
| 16 | |
| 17 | |
| 18 | def make_parser(): |
| 19 | parser = argparse.ArgumentParser(description = 'Compile a dump artifact.') |
| 20 | parser.add_argument( |
| 21 | '-d', |
| 22 | '--dump', |
Christoffer Quist Adamsen | d84a6f0 | 2020-05-16 12:29:35 +0200 | [diff] [blame] | 23 | help='Dump file or directory to compile', |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 24 | default=None) |
| 25 | parser.add_argument( |
Rico Wind | 9ed87b9 | 2020-03-06 12:37:18 +0100 | [diff] [blame] | 26 | '--temp', |
| 27 | help='Temp directory to extract the dump to, allows you to rerun the command' |
| 28 | ' more easily in the terminal with changes', |
| 29 | default=None) |
| 30 | parser.add_argument( |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 31 | '-c', |
| 32 | '--compiler', |
Rico Wind | f73f18d | 2020-03-03 09:28:54 +0100 | [diff] [blame] | 33 | help='Compiler to use', |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 34 | default=None) |
| 35 | parser.add_argument( |
| 36 | '-v', |
| 37 | '--version', |
| 38 | help='Compiler version to use (default read from dump version file).' |
| 39 | 'Valid arguments are:' |
| 40 | ' "master" to run from your own tree,' |
| 41 | ' "X.Y.Z" to run a specific version, or' |
| 42 | ' <hash> to run that hash from master.', |
| 43 | default=None) |
| 44 | parser.add_argument( |
Morten Krogh-Jespersen | d8bceb5 | 2020-03-06 12:56:48 +0100 | [diff] [blame] | 45 | '--r8-jar', |
| 46 | help='Path to an R8 jar.', |
| 47 | default=None) |
| 48 | parser.add_argument( |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 49 | '--nolib', |
| 50 | help='Use the non-lib distribution (default uses the lib distribution)', |
| 51 | default=False, |
| 52 | action='store_true') |
| 53 | parser.add_argument( |
Rico Wind | 2865364 | 2020-03-31 13:59:07 +0200 | [diff] [blame] | 54 | '--printtimes', |
| 55 | help='Print timing information from r8', |
| 56 | default=False, |
| 57 | action='store_true') |
| 58 | parser.add_argument( |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 59 | '--ea', |
| 60 | help='Enable Java assertions when running the compiler (default disabled)', |
| 61 | default=False, |
| 62 | action='store_true') |
| 63 | parser.add_argument( |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 64 | '--classfile', |
| 65 | help='Run with classfile output', |
| 66 | default=False, |
| 67 | action='store_true') |
| 68 | parser.add_argument( |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 69 | '--debug-agent', |
| 70 | help='Enable Java debug agent and suspend compilation (default disabled)', |
| 71 | default=False, |
| 72 | action='store_true') |
Ian Zerny | 77bdf5f | 2020-07-08 11:46:24 +0200 | [diff] [blame] | 73 | parser.add_argument( |
| 74 | '--xmx', |
| 75 | help='Set JVM max heap size (-Xmx)', |
| 76 | default=None) |
| 77 | parser.add_argument( |
| 78 | '--threads', |
| 79 | help='Set the number of threads to use', |
| 80 | default=None) |
| 81 | parser.add_argument( |
| 82 | '--min-api', |
| 83 | help='Set min-api (default read from dump properties file)', |
| 84 | default=None) |
Søren Gjesse | 7360f2b | 2020-08-10 09:13:35 +0200 | [diff] [blame] | 85 | parser.add_argument( |
Clément Béra | daae4ca | 2020-10-27 14:26:41 +0000 | [diff] [blame] | 86 | '--desugared-lib', |
| 87 | help='Set desugared-library (default set from dump)', |
| 88 | default=None) |
| 89 | parser.add_argument( |
Søren Gjesse | 7360f2b | 2020-08-10 09:13:35 +0200 | [diff] [blame] | 90 | '--loop', |
| 91 | help='Run the compilation in a loop', |
| 92 | default=False, |
| 93 | action='store_true') |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 94 | return parser |
| 95 | |
| 96 | def error(msg): |
| 97 | print msg |
| 98 | sys.exit(1) |
| 99 | |
| 100 | class Dump(object): |
| 101 | |
| 102 | def __init__(self, directory): |
| 103 | self.directory = directory |
| 104 | |
| 105 | def if_exists(self, name): |
| 106 | f = os.path.join(self.directory, name) |
| 107 | if os.path.exists(f): |
| 108 | return f |
| 109 | return None |
| 110 | |
| 111 | def program_jar(self): |
| 112 | return self.if_exists('program.jar') |
| 113 | |
Christoffer Quist Adamsen | fcfc63a | 2020-05-15 19:04:24 +0200 | [diff] [blame] | 114 | def feature_jars(self): |
| 115 | feature_jars = [] |
| 116 | i = 1 |
| 117 | while True: |
| 118 | feature_jar = self.if_exists('feature-%s.jar' % i) |
| 119 | if feature_jar: |
| 120 | feature_jars.append(feature_jar) |
| 121 | i = i + 1 |
| 122 | else: |
| 123 | return feature_jars |
| 124 | |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 125 | def library_jar(self): |
| 126 | return self.if_exists('library.jar') |
| 127 | |
| 128 | def classpath_jar(self): |
| 129 | return self.if_exists('classpath.jar') |
| 130 | |
Clément Béra | daae4ca | 2020-10-27 14:26:41 +0000 | [diff] [blame] | 131 | def desugared_library_json(self): |
| 132 | return self.if_exists('desugared-library.json') |
| 133 | |
Christoffer Quist Adamsen | fcfc63a | 2020-05-15 19:04:24 +0200 | [diff] [blame] | 134 | def build_properties_file(self): |
| 135 | return self.if_exists('build.properties') |
| 136 | |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 137 | def config_file(self): |
| 138 | return self.if_exists('proguard.config') |
| 139 | |
| 140 | def version_file(self): |
| 141 | return self.if_exists('r8-version') |
| 142 | |
| 143 | def version(self): |
| 144 | f = self.version_file() |
| 145 | if f: |
| 146 | return open(f).read().split(' ')[0] |
| 147 | return None |
| 148 | |
| 149 | def read_dump(args, temp): |
| 150 | if args.dump is None: |
Christoffer Quist Adamsen | d84a6f0 | 2020-05-16 12:29:35 +0200 | [diff] [blame] | 151 | error("A dump file or directory must be specified") |
| 152 | if os.path.isdir(args.dump): |
| 153 | return Dump(args.dump) |
Christoffer Quist Adamsen | fcfc63a | 2020-05-15 19:04:24 +0200 | [diff] [blame] | 154 | dump_file = zipfile.ZipFile(os.path.abspath(args.dump), 'r') |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 155 | with utils.ChangedWorkingDirectory(temp): |
| 156 | dump_file.extractall() |
| 157 | return Dump(temp) |
| 158 | |
Christoffer Quist Adamsen | fcfc63a | 2020-05-15 19:04:24 +0200 | [diff] [blame] | 159 | def determine_build_properties(args, dump): |
| 160 | build_properties = {} |
| 161 | build_properties_file = dump.build_properties_file() |
| 162 | if build_properties_file: |
| 163 | with open(build_properties_file) as f: |
| 164 | build_properties_contents = f.readlines() |
| 165 | for line in build_properties_contents: |
| 166 | stripped = line.strip() |
| 167 | if stripped: |
| 168 | pair = stripped.split('=') |
| 169 | build_properties[pair[0]] = pair[1] |
| 170 | return build_properties |
| 171 | |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 172 | def determine_version(args, dump): |
| 173 | if args.version is None: |
| 174 | return dump.version() |
| 175 | return args.version |
| 176 | |
| 177 | def determine_compiler(args, dump): |
Rico Wind | f73f18d | 2020-03-03 09:28:54 +0100 | [diff] [blame] | 178 | compilers = ['d8', 'r8', 'r8full'] |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 179 | if args.compiler not in compilers: |
Rico Wind | f73f18d | 2020-03-03 09:28:54 +0100 | [diff] [blame] | 180 | error("Unable to determine a compiler to use. Specified %s," |
| 181 | " Valid options: %s" % (args.compiler, ', '.join(compilers))) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 182 | return args.compiler |
| 183 | |
| 184 | def determine_output(args, temp): |
| 185 | return os.path.join(temp, 'out.jar') |
| 186 | |
Ian Zerny | 77bdf5f | 2020-07-08 11:46:24 +0200 | [diff] [blame] | 187 | def determine_min_api(args, build_properties): |
| 188 | if args.min_api: |
| 189 | return args.min_api |
| 190 | if 'min-api' in build_properties: |
| 191 | return build_properties.get('min-api') |
| 192 | return None |
| 193 | |
Christoffer Quist Adamsen | fcfc63a | 2020-05-15 19:04:24 +0200 | [diff] [blame] | 194 | def determine_feature_output(feature_jar, temp): |
| 195 | return os.path.join(temp, os.path.basename(feature_jar)[:-4] + ".out.jar") |
| 196 | |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 197 | def determine_program_jar(args, dump): |
| 198 | if hasattr(args, 'program_jar') and args.program_jar: |
| 199 | return args.program_jar |
| 200 | return dump.program_jar() |
| 201 | |
| 202 | def determine_class_file(args, build_properties): |
| 203 | if args.classfile: |
| 204 | return args.classfile |
| 205 | if 'classfile' in build_properties: |
| 206 | return True |
| 207 | return None |
| 208 | |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 209 | def download_distribution(args, version, temp): |
| 210 | if version == 'master': |
| 211 | return utils.R8_JAR if args.nolib else utils.R8LIB_JAR |
| 212 | name = 'r8.jar' if args.nolib else 'r8lib.jar' |
| 213 | source = archive.GetUploadDestination(version, name, is_hash(version)) |
| 214 | dest = os.path.join(temp, 'r8.jar') |
| 215 | utils.download_file_from_cloud_storage(source, dest) |
| 216 | return dest |
| 217 | |
| 218 | def prepare_wrapper(dist, temp): |
| 219 | wrapper_file = os.path.join( |
| 220 | utils.REPO_ROOT, |
| 221 | 'src/main/java/com/android/tools/r8/utils/CompileDumpCompatR8.java') |
| 222 | subprocess.check_output([ |
| 223 | jdk.GetJavacExecutable(), |
| 224 | wrapper_file, |
| 225 | '-d', temp, |
| 226 | '-cp', dist, |
| 227 | ]) |
| 228 | return temp |
| 229 | |
| 230 | def is_hash(version): |
| 231 | return len(version) == 40 |
| 232 | |
Søren Gjesse | 7360f2b | 2020-08-10 09:13:35 +0200 | [diff] [blame] | 233 | def run1(out, args, otherargs): |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 234 | with utils.TempDir() as temp: |
Søren Gjesse | 7360f2b | 2020-08-10 09:13:35 +0200 | [diff] [blame] | 235 | if out: |
| 236 | temp = out |
Rico Wind | 9ed87b9 | 2020-03-06 12:37:18 +0100 | [diff] [blame] | 237 | if not os.path.exists(temp): |
| 238 | os.makedirs(temp) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 239 | dump = read_dump(args, temp) |
Ian Zerny | 46bc4cf | 2020-08-03 15:58:27 +0200 | [diff] [blame] | 240 | if not dump.program_jar(): |
| 241 | error("Cannot compile dump with no program classes") |
| 242 | if not dump.library_jar(): |
| 243 | print "WARNING: Unexpected lack of library classes in dump" |
Christoffer Quist Adamsen | fcfc63a | 2020-05-15 19:04:24 +0200 | [diff] [blame] | 244 | build_properties = determine_build_properties(args, dump) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 245 | version = determine_version(args, dump) |
| 246 | compiler = determine_compiler(args, dump) |
| 247 | out = determine_output(args, temp) |
Ian Zerny | 77bdf5f | 2020-07-08 11:46:24 +0200 | [diff] [blame] | 248 | min_api = determine_min_api(args, build_properties) |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 249 | classfile = determine_class_file(args, build_properties) |
Morten Krogh-Jespersen | d8bceb5 | 2020-03-06 12:56:48 +0100 | [diff] [blame] | 250 | jar = args.r8_jar if args.r8_jar else download_distribution(args, version, temp) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 251 | wrapper_dir = prepare_wrapper(jar, temp) |
| 252 | cmd = [jdk.GetJavaExecutable()] |
| 253 | if args.debug_agent: |
| 254 | if not args.nolib: |
| 255 | print "WARNING: Running debugging agent on r8lib is questionable..." |
| 256 | cmd.append( |
| 257 | '-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005') |
Ian Zerny | 77bdf5f | 2020-07-08 11:46:24 +0200 | [diff] [blame] | 258 | if args.xmx: |
| 259 | cmd.append('-Xmx' + args.xmx) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 260 | if args.ea: |
| 261 | cmd.append('-ea') |
Rico Wind | 2865364 | 2020-03-31 13:59:07 +0200 | [diff] [blame] | 262 | if args.printtimes: |
| 263 | cmd.append('-Dcom.android.tools.r8.printtimes=1') |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 264 | cmd.extend(['-cp', '%s:%s' % (wrapper_dir, jar)]) |
| 265 | if compiler == 'd8': |
| 266 | cmd.append('com.android.tools.r8.D8') |
| 267 | if compiler.startswith('r8'): |
| 268 | cmd.append('com.android.tools.r8.utils.CompileDumpCompatR8') |
| 269 | if compiler == 'r8': |
| 270 | cmd.append('--compat') |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 271 | # For recompilation of dumps run_on_app_dumps pass in a program jar. |
| 272 | cmd.append(determine_program_jar(args, dump)) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 273 | cmd.extend(['--output', out]) |
Christoffer Quist Adamsen | fcfc63a | 2020-05-15 19:04:24 +0200 | [diff] [blame] | 274 | for feature_jar in dump.feature_jars(): |
| 275 | cmd.extend(['--feature-jar', feature_jar, |
| 276 | determine_feature_output(feature_jar, temp)]) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 277 | if dump.library_jar(): |
| 278 | cmd.extend(['--lib', dump.library_jar()]) |
| 279 | if dump.classpath_jar(): |
| 280 | cmd.extend(['--classpath', dump.classpath_jar()]) |
Clément Béra | daae4ca | 2020-10-27 14:26:41 +0000 | [diff] [blame] | 281 | if dump.desugared_library_json(): |
| 282 | cmd.extend(['--desugared-lib', dump.desugared_library_json()]) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 283 | if compiler != 'd8' and dump.config_file(): |
Morten Krogh-Jespersen | 51a1635 | 2020-11-04 09:31:15 +0100 | [diff] [blame] | 284 | if hasattr(args, 'config_file_consumer') and args.config_file_consumer: |
| 285 | args.config_file_consumer(dump.config_file()) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 286 | cmd.extend(['--pg-conf', dump.config_file()]) |
Ian Zerny | 588120b | 2020-04-03 13:08:03 +0200 | [diff] [blame] | 287 | if compiler != 'd8': |
| 288 | cmd.extend(['--pg-map-output', '%s.map' % out]) |
Ian Zerny | 77bdf5f | 2020-07-08 11:46:24 +0200 | [diff] [blame] | 289 | if min_api: |
| 290 | cmd.extend(['--min-api', min_api]) |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 291 | if classfile: |
| 292 | cmd.extend(['--classfile']) |
Ian Zerny | 77bdf5f | 2020-07-08 11:46:24 +0200 | [diff] [blame] | 293 | if args.threads: |
| 294 | cmd.extend(['--threads', args.threads]) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 295 | cmd.extend(otherargs) |
| 296 | utils.PrintCmd(cmd) |
| 297 | try: |
| 298 | print subprocess.check_output(cmd, stderr=subprocess.STDOUT) |
| 299 | return 0 |
| 300 | except subprocess.CalledProcessError, e: |
| 301 | print e.output |
| 302 | if not args.nolib: |
| 303 | stacktrace = os.path.join(temp, 'stacktrace') |
| 304 | open(stacktrace, 'w+').write(e.output) |
| 305 | local_map = utils.R8LIB_MAP if version == 'master' else None |
| 306 | hash_or_version = None if version == 'master' else version |
| 307 | print "=" * 80 |
| 308 | print " RETRACED OUTPUT" |
| 309 | print "=" * 80 |
Morten Krogh-Jespersen | 64c5272 | 2020-10-27 08:33:42 +0100 | [diff] [blame] | 310 | retrace.run( |
| 311 | local_map, hash_or_version, stacktrace, is_hash(version), no_r8lib=False) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 312 | return 1 |
| 313 | |
Søren Gjesse | 7360f2b | 2020-08-10 09:13:35 +0200 | [diff] [blame] | 314 | def run(args, otherargs): |
| 315 | if (args.loop): |
| 316 | count = 1 |
| 317 | while True: |
| 318 | print('Iteration {:03d}'.format(count)) |
| 319 | out = args.temp |
| 320 | if out: |
| 321 | out = os.path.join(out, '{:03d}'.format(count)) |
| 322 | run1(out, args, otherargs) |
| 323 | count += 1 |
| 324 | else: |
| 325 | run1(args.temp, args, otherargs) |
| 326 | |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 327 | if __name__ == '__main__': |
| 328 | (args, otherargs) = make_parser().parse_known_args(sys.argv[1:]) |
| 329 | sys.exit(run(args, otherargs)) |