blob: b6189e60b0787a0e1aab5fd34f8c5660b4e1bbd6 [file] [log] [blame]
Ian Zernya0d27cf2021-10-14 13:55:34 +02001#!/usr/bin/env python3
2# Copyright (c) 2021, 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
Ian Zernyd31dd732021-10-15 11:49:26 +02007import os
Ian Zernya0d27cf2021-10-14 13:55:34 +02008import subprocess
9import sys
10
11import jdk
Ian Zernyd31dd732021-10-15 11:49:26 +020012import utils
Ian Zernya0d27cf2021-10-14 13:55:34 +020013
Ian Zernyd31dd732021-10-15 11:49:26 +020014VERSION_EXTRACTOR = """
15import com.android.tools.r8.Version;
16public class VersionExtractor {
17 public static void main(String[] args) {
18 System.out.println(Version.LABEL);
19 }
20}
21"""
Ian Zernya0d27cf2021-10-14 13:55:34 +020022
23def parse_options():
24 parser = argparse.ArgumentParser(description='Tag R8 Versions')
25 parser.add_argument(
Morten Krogh-Jespersenb8b44982022-05-19 13:44:06 +020026 '--classpath',
27 action='append',
28 help='Dependencies to add to classpath')
Ian Zernya0d27cf2021-10-14 13:55:34 +020029 parser.add_argument(
Morten Krogh-Jespersenb8b44982022-05-19 13:44:06 +020030 '--debug-agent',
31 action='store_true',
32 default=False,
33 help='Create a socket for debugging')
Ian Zernya0d27cf2021-10-14 13:55:34 +020034 parser.add_argument(
Morten Krogh-Jespersenb8b44982022-05-19 13:44:06 +020035 '--excldeps-variant',
36 action='store_true',
37 default=False,
38 help='Mark this artifact as an "excldeps" variant of the compiler')
Ian Zernya0d27cf2021-10-14 13:55:34 +020039 parser.add_argument(
Morten Krogh-Jespersenb8b44982022-05-19 13:44:06 +020040 '--lib',
41 action='append',
42 help='Additional libraries (JDK 1.8 rt.jar already included)')
Morten Krogh-Jespersen378aa612021-11-23 13:27:25 +010043 parser.add_argument(
Morten Krogh-Jespersenb8b44982022-05-19 13:44:06 +020044 '--output',
45 required=True,
46 help='The output path for the r8lib')
Ian Zernycf8ef512022-05-04 14:54:16 +020047 parser.add_argument(
Morten Krogh-Jespersenb8b44982022-05-19 13:44:06 +020048 '--pg-conf',
49 action='append',
50 help='Keep configuration')
51 parser.add_argument(
52 '--r8jar',
53 required=True,
54 help='The R8 jar to compile')
Ian Zernya0d27cf2021-10-14 13:55:34 +020055 return parser.parse_args()
56
Ian Zerny848da972021-10-14 19:40:16 +020057def get_r8_version(r8jar):
Ian Zernyd31dd732021-10-15 11:49:26 +020058 with utils.TempDir() as temp:
59 name = os.path.join(temp, "VersionExtractor.java")
60 fd = open(name, 'w')
61 fd.write(VERSION_EXTRACTOR)
62 fd.close()
63 cmd = [jdk.GetJavacExecutable(), '-cp', r8jar, name]
64 print(' '.join(cmd))
Morten Krogh-Jespersen8680e082021-10-15 20:01:51 +020065 cp_separator = ';' if utils.IsWindows() else ':'
Ian Zernyd31dd732021-10-15 11:49:26 +020066 subprocess.check_call(cmd)
67 output = subprocess.check_output([
68 jdk.GetJavaExecutable(),
69 '-cp',
Morten Krogh-Jespersen8680e082021-10-15 20:01:51 +020070 cp_separator.join([r8jar, os.path.dirname(name)]),
Ian Zernyd31dd732021-10-15 11:49:26 +020071 'VersionExtractor'
72 ]).decode('UTF-8').strip()
73 if output == 'main':
74 return subprocess.check_output(
Ian Zerny848da972021-10-14 19:40:16 +020075 ['git', 'rev-parse', 'HEAD']).decode('UTF-8').strip()
Ian Zernyd31dd732021-10-15 11:49:26 +020076 else:
77 return output
Ian Zerny848da972021-10-14 19:40:16 +020078
Ian Zernya0d27cf2021-10-14 13:55:34 +020079def main():
80 args = parse_options()
Ian Zernyd31dd732021-10-15 11:49:26 +020081 if not os.path.exists(args.r8jar):
82 print("Could not find jar: " + args.r8jar)
83 return 1
Ian Zerny848da972021-10-14 19:40:16 +020084 version = get_r8_version(args.r8jar)
Ian Zernycf8ef512022-05-04 14:54:16 +020085 variant = '+excldeps' if args.excldeps_variant else ''
86 map_id_template = version + variant
Ian Zerny848da972021-10-14 19:40:16 +020087 source_file_template = 'R8_%MAP_ID_%MAP_HASH'
Ian Zernya0d27cf2021-10-14 13:55:34 +020088 # TODO(b/139725780): See if we can remove or lower the heap size (-Xmx8g).
89 cmd = [jdk.GetJavaExecutable(), '-Xmx8g', '-ea']
Morten Krogh-Jespersenb8b44982022-05-19 13:44:06 +020090 if args.debug_agent:
91 cmd.extend(['-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005'])
Ian Zernya0d27cf2021-10-14 13:55:34 +020092 cmd.extend(['-cp', 'build/libs/r8_with_deps.jar', 'com.android.tools.r8.R8'])
93 cmd.append(args.r8jar)
94 cmd.append('--classfile')
Ian Zerny848da972021-10-14 19:40:16 +020095 cmd.extend(['--map-id-template', map_id_template])
96 cmd.extend(['--source-file-template', source_file_template])
Ian Zernya0d27cf2021-10-14 13:55:34 +020097 cmd.extend(['--output', args.output])
98 cmd.extend(['--pg-map-output', args.output + '.map'])
Ian Zerny3ac86842022-10-05 11:49:35 +020099 cmd.extend(['--lib', jdk.GetJdkHome()])
Ian Zernya0d27cf2021-10-14 13:55:34 +0200100 if args.pg_conf:
101 for pgconf in args.pg_conf:
102 cmd.extend(['--pg-conf', pgconf])
103 if args.lib:
104 for lib in args.lib:
105 cmd.extend(['--lib', lib])
Morten Krogh-Jespersen378aa612021-11-23 13:27:25 +0100106 if args.classpath:
107 for cp in args.classpath:
108 cmd.extend(['--classpath', cp])
Ian Zernya0d27cf2021-10-14 13:55:34 +0200109 print(' '.join(cmd))
110 subprocess.check_call(cmd)
111
112if __name__ == '__main__':
113 sys.exit(main())