Ian Zerny | a0d27cf | 2021-10-14 13:55:34 +0200 | [diff] [blame] | 1 | #!/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 | |
| 6 | import argparse |
Ian Zerny | d31dd73 | 2021-10-15 11:49:26 +0200 | [diff] [blame] | 7 | import os |
Ian Zerny | a0d27cf | 2021-10-14 13:55:34 +0200 | [diff] [blame] | 8 | import subprocess |
| 9 | import sys |
| 10 | |
| 11 | import jdk |
Ian Zerny | d31dd73 | 2021-10-15 11:49:26 +0200 | [diff] [blame] | 12 | import utils |
Ian Zerny | a0d27cf | 2021-10-14 13:55:34 +0200 | [diff] [blame] | 13 | |
Ian Zerny | d31dd73 | 2021-10-15 11:49:26 +0200 | [diff] [blame] | 14 | VERSION_EXTRACTOR = """ |
| 15 | import com.android.tools.r8.Version; |
| 16 | public class VersionExtractor { |
| 17 | public static void main(String[] args) { |
| 18 | System.out.println(Version.LABEL); |
| 19 | } |
| 20 | } |
| 21 | """ |
Ian Zerny | a0d27cf | 2021-10-14 13:55:34 +0200 | [diff] [blame] | 22 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 23 | |
Ian Zerny | a0d27cf | 2021-10-14 13:55:34 +0200 | [diff] [blame] | 24 | def parse_options(): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 25 | 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 Zerny | a0d27cf | 2021-10-14 13:55:34 +0200 | [diff] [blame] | 59 | |
Ian Zerny | 848da97 | 2021-10-14 19:40:16 +0200 | [diff] [blame] | 60 | def get_r8_version(r8jar): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 61 | 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 Zerny | 848da97 | 2021-10-14 19:40:16 +0200 | [diff] [blame] | 81 | |
Ian Zerny | a0d27cf | 2021-10-14 13:55:34 +0200 | [diff] [blame] | 82 | def main(): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 83 | 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 Zerny | 8a7a68f | 2023-11-06 11:24:57 +0100 | [diff] [blame] | 97 | cmd.append('-Dcom.android.tools.r8.enableKeepAnnotations=1') |
Christoffer Adamsen | 5901b19 | 2024-08-01 13:11:47 +0200 | [diff] [blame] | 98 | # TODO(b/356344563): Remove when this is default. |
| 99 | cmd.append('-Dcom.android.tools.r8.enableEmptyMemberRulesToDefaultInitRuleConversion=0') |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 100 | 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 Zerny | 51cb415 | 2023-11-17 10:47:13 +0100 | [diff] [blame] | 108 | cmd.extend(['--pg-conf-output', args.output + '.config']) |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 109 | 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 Zerny | a0d27cf | 2021-10-14 13:55:34 +0200 | [diff] [blame] | 126 | |
| 127 | if __name__ == '__main__': |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 128 | sys.exit(main()) |