blob: 1f0286952ebc576e10ea58b7def795e093f0d39c [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
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020023
Ian Zernya0d27cf2021-10-14 13:55:34 +020024def parse_options():
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020025 parser = argparse.ArgumentParser(description='Tag R8 Versions')
26 parser.add_argument('--classpath',
27 action='append',
28 help='Dependencies to add to classpath')
29 parser.add_argument('--debug-agent',
30 action='store_true',
31 default=False,
32 help='Create a socket for debugging')
33 parser.add_argument(
34 '--excldeps-variant',
35 action='store_true',
36 default=False,
37 help='Mark this artifact as an "excldeps" variant of the compiler')
38 parser.add_argument('--debug-variant',
39 action='store_true',
40 default=False,
41 help='Compile with debug flag')
42 parser.add_argument(
43 '--lib',
44 action='append',
45 help='Additional libraries (JDK 1.8 rt.jar already included)')
46 parser.add_argument('--output',
47 required=True,
48 help='The output path for the r8lib')
49 parser.add_argument('--pg-conf', action='append', help='Keep configuration')
50 parser.add_argument('--pg-map',
51 default=None,
52 help='Input map for distribution and composition')
53 parser.add_argument('--r8jar', required=True, help='The R8 jar to compile')
54 parser.add_argument('--r8compiler',
55 default='build/libs/r8_with_deps.jar',
56 help='The R8 compiler to use')
57 return parser.parse_args()
58
Ian Zernya0d27cf2021-10-14 13:55:34 +020059
Ian Zerny848da972021-10-14 19:40:16 +020060def get_r8_version(r8jar):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020061 with utils.TempDir() as temp:
62 name = os.path.join(temp, "VersionExtractor.java")
63 fd = open(name, 'w')
64 fd.write(VERSION_EXTRACTOR)
65 fd.close()
66 cmd = [jdk.GetJavacExecutable(), '-cp', r8jar, name]
67 print(' '.join(cmd))
68 cp_separator = ';' if utils.IsWindows() else ':'
69 subprocess.check_call(cmd)
70 output = subprocess.check_output([
71 jdk.GetJavaExecutable(), '-cp',
72 cp_separator.join([r8jar, os.path.dirname(name)]),
73 'VersionExtractor'
74 ]).decode('UTF-8').strip()
75 if output == 'main':
76 return subprocess.check_output(['git', 'rev-parse',
77 'HEAD']).decode('UTF-8').strip()
78 else:
79 return output
80
Ian Zerny848da972021-10-14 19:40:16 +020081
Ian Zernya0d27cf2021-10-14 13:55:34 +020082def main():
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020083 args = parse_options()
84 if not os.path.exists(args.r8jar):
85 print("Could not find jar: " + args.r8jar)
86 return 1
87 version = get_r8_version(args.r8jar)
88 variant = '+excldeps' if args.excldeps_variant else ''
89 map_id_template = version + variant
90 source_file_template = 'R8_%MAP_ID_%MAP_HASH'
91 # TODO(b/139725780): See if we can remove or lower the heap size (-Xmx8g).
92 cmd = [jdk.GetJavaExecutable(), '-Xmx8g', '-ea']
93 if args.debug_agent:
94 cmd.extend([
95 '-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005'
96 ])
Ian Zerny8a7a68f2023-11-06 11:24:57 +010097 cmd.append('-Dcom.android.tools.r8.enableKeepAnnotations=1')
Christoffer Adamsen5901b192024-08-01 13:11:47 +020098 # TODO(b/356344563): Remove when this is default.
99 cmd.append('-Dcom.android.tools.r8.enableEmptyMemberRulesToDefaultInitRuleConversion=0')
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200100 cmd.extend(['-cp', args.r8compiler, 'com.android.tools.r8.R8'])
101 cmd.append(args.r8jar)
102 if args.debug_variant:
103 cmd.append('--debug')
104 cmd.append('--classfile')
105 cmd.extend(['--map-id-template', map_id_template])
106 cmd.extend(['--source-file-template', source_file_template])
107 cmd.extend(['--output', args.output])
Ian Zerny51cb4152023-11-17 10:47:13 +0100108 cmd.extend(['--pg-conf-output', args.output + '.config'])
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200109 cmd.extend(['--pg-map-output', args.output + '.map'])
110 cmd.extend(['--partition-map-output', args.output + '_map.zip'])
111 cmd.extend(['--lib', jdk.GetJdkHome()])
112 if args.pg_conf:
113 for pgconf in args.pg_conf:
114 cmd.extend(['--pg-conf', pgconf])
115 if args.lib:
116 for lib in args.lib:
117 cmd.extend(['--lib', lib])
118 if args.classpath:
119 for cp in args.classpath:
120 cmd.extend(['--classpath', cp])
121 if args.pg_map:
122 cmd.extend(['--pg-map', args.pg_map])
123 print(' '.join(cmd))
124 subprocess.check_call(cmd)
125
Ian Zernya0d27cf2021-10-14 13:55:34 +0200126
127if __name__ == '__main__':
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200128 sys.exit(main())