blob: a27cf24dc47460a3e3969bc41f0aad9e813b1721 [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')
Morten Krogh-Jespersend1a11852023-06-14 14:48:44 +020055 parser.add_argument(
56 '--r8compiler',
Morten Krogh-Jespersend1a11852023-06-14 14:48:44 +020057 default='build/libs/r8_with_deps.jar',
58 help='The R8 compiler to use')
Ian Zernya0d27cf2021-10-14 13:55:34 +020059 return parser.parse_args()
60
Ian Zerny848da972021-10-14 19:40:16 +020061def get_r8_version(r8jar):
Ian Zernyd31dd732021-10-15 11:49:26 +020062 with utils.TempDir() as temp:
63 name = os.path.join(temp, "VersionExtractor.java")
64 fd = open(name, 'w')
65 fd.write(VERSION_EXTRACTOR)
66 fd.close()
67 cmd = [jdk.GetJavacExecutable(), '-cp', r8jar, name]
68 print(' '.join(cmd))
Morten Krogh-Jespersen8680e082021-10-15 20:01:51 +020069 cp_separator = ';' if utils.IsWindows() else ':'
Ian Zernyd31dd732021-10-15 11:49:26 +020070 subprocess.check_call(cmd)
71 output = subprocess.check_output([
72 jdk.GetJavaExecutable(),
73 '-cp',
Morten Krogh-Jespersen8680e082021-10-15 20:01:51 +020074 cp_separator.join([r8jar, os.path.dirname(name)]),
Ian Zernyd31dd732021-10-15 11:49:26 +020075 'VersionExtractor'
76 ]).decode('UTF-8').strip()
77 if output == 'main':
78 return subprocess.check_output(
Ian Zerny848da972021-10-14 19:40:16 +020079 ['git', 'rev-parse', 'HEAD']).decode('UTF-8').strip()
Ian Zernyd31dd732021-10-15 11:49:26 +020080 else:
81 return output
Ian Zerny848da972021-10-14 19:40:16 +020082
Ian Zernya0d27cf2021-10-14 13:55:34 +020083def main():
84 args = parse_options()
Ian Zernyd31dd732021-10-15 11:49:26 +020085 if not os.path.exists(args.r8jar):
86 print("Could not find jar: " + args.r8jar)
87 return 1
Ian Zerny848da972021-10-14 19:40:16 +020088 version = get_r8_version(args.r8jar)
Ian Zernycf8ef512022-05-04 14:54:16 +020089 variant = '+excldeps' if args.excldeps_variant else ''
90 map_id_template = version + variant
Ian Zerny848da972021-10-14 19:40:16 +020091 source_file_template = 'R8_%MAP_ID_%MAP_HASH'
Ian Zernya0d27cf2021-10-14 13:55:34 +020092 # TODO(b/139725780): See if we can remove or lower the heap size (-Xmx8g).
93 cmd = [jdk.GetJavaExecutable(), '-Xmx8g', '-ea']
Morten Krogh-Jespersenb8b44982022-05-19 13:44:06 +020094 if args.debug_agent:
95 cmd.extend(['-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005'])
Morten Krogh-Jespersend1a11852023-06-14 14:48:44 +020096 cmd.extend(['-cp', args.r8compiler, 'com.android.tools.r8.R8'])
Ian Zernya0d27cf2021-10-14 13:55:34 +020097 cmd.append(args.r8jar)
98 cmd.append('--classfile')
Ian Zerny848da972021-10-14 19:40:16 +020099 cmd.extend(['--map-id-template', map_id_template])
100 cmd.extend(['--source-file-template', source_file_template])
Ian Zernya0d27cf2021-10-14 13:55:34 +0200101 cmd.extend(['--output', args.output])
102 cmd.extend(['--pg-map-output', args.output + '.map'])
Morten Krogh-Jesperseneda55812023-04-25 09:26:07 +0200103 cmd.extend(['--partition-map-output', args.output + '_map.zip'])
Ian Zerny3ac86842022-10-05 11:49:35 +0200104 cmd.extend(['--lib', jdk.GetJdkHome()])
Ian Zernya0d27cf2021-10-14 13:55:34 +0200105 if args.pg_conf:
106 for pgconf in args.pg_conf:
107 cmd.extend(['--pg-conf', pgconf])
108 if args.lib:
109 for lib in args.lib:
110 cmd.extend(['--lib', lib])
Morten Krogh-Jespersen378aa612021-11-23 13:27:25 +0100111 if args.classpath:
112 for cp in args.classpath:
113 cmd.extend(['--classpath', cp])
Ian Zernya0d27cf2021-10-14 13:55:34 +0200114 print(' '.join(cmd))
115 subprocess.check_call(cmd)
116
117if __name__ == '__main__':
118 sys.exit(main())