blob: 6f62ff70e978aedbc1dc4661f846300e3fd8c2c2 [file] [log] [blame]
Ian Zernya2703f82021-05-31 11:11:51 +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
7import jdk
8import os.path
9import re
10import subprocess
11import sys
12import urllib.request
13
14import utils
15
16# Grep match string for 'Version X.Y.Z[-dev]'
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020017VERSION_EXP = '^Version [[:digit:]]\+.[[:digit:]]\+.[[:digit:]]\+\(\|-dev\)$'
Ian Zernya2703f82021-05-31 11:11:51 +020018
19# R8 is located in the 'builder' library.
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020020AGP_MAVEN = "https://dl.google.com/android/maven2/com/android/tools/build/builder"
21
Ian Zernya2703f82021-05-31 11:11:51 +020022
23def parse_options():
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020024 parser = argparse.ArgumentParser(description='Tag R8 Versions')
25 parser.add_argument('--branch',
26 help='The R8 branch to tag versions on, eg, origin/3.0')
27 parser.add_argument('--agp',
28 help='The AGP to compute the tag for, eg, 4.2.0-beta03')
29 parser.add_argument(
30 '--dry-run',
31 default=False,
32 action='store_true',
33 help='Print the state changing commands without running them.')
34 return parser.parse_args()
35
Ian Zernya2703f82021-05-31 11:11:51 +020036
37def run(options, cmd):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020038 print(' '.join(cmd))
39 if not options.dry_run:
40 subprocess.check_call(cmd)
41
Ian Zernya2703f82021-05-31 11:11:51 +020042
43def main():
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020044 args = parse_options()
45 if args.branch:
46 tag_r8_branch(args.branch, args)
47 elif args.agp:
48 if (args.agp == 'all'):
49 tag_all_agp_versions(args)
50 else:
51 tag_agp_version(args.agp, args)
Morten Krogh-Jespersena23fa102022-07-01 08:51:59 +020052 else:
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020053 print("Should use a top-level option, such as --branch or --agp.")
54 return 1
55 return 0
56
Ian Zernya2703f82021-05-31 11:11:51 +020057
58def prepare_print_version(dist, temp):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020059 wrapper_file = os.path.join(
60 utils.REPO_ROOT,
61 'src/main/java/com/android/tools/r8/utils/PrintR8Version.java')
62 cmd = [
63 jdk.GetJavacExecutable(),
64 wrapper_file,
65 '-d',
66 temp,
67 '-cp',
68 dist,
69 ]
70 utils.PrintCmd(cmd)
71 subprocess.check_output(cmd)
72 return temp
73
Ian Zernya2703f82021-05-31 11:11:51 +020074
75def get_tag_info_on_origin(tag):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020076 output = subprocess.check_output(
77 ['git', 'ls-remote', '--tags', 'origin', tag]).decode('utf-8')
78 if len(output.strip()) == 0:
79 return None
80 return output
81
Ian Zernya2703f82021-05-31 11:11:51 +020082
Morten Krogh-Jespersena23fa102022-07-01 08:51:59 +020083def tag_all_agp_versions(args):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020084 with utils.TempDir() as temp:
85 url = "%s/maven-metadata.xml" % AGP_MAVEN
86 metadata = os.path.join(temp, "maven-metadata.xml")
87 try:
88 urllib.request.urlretrieve(url, metadata)
89 except urllib.error.HTTPError as e:
90 print('Could not find maven-metadata.xml for agp')
91 print(e)
92 return 1
93 with open(metadata, 'r') as file:
94 data = file.read()
95 pattern = r'<version>(.+)</version>'
96 matches = re.findall(pattern, data)
97 matches.reverse()
98 for version in matches:
99 print('Tagging agp version ' + version)
100 tag_agp_version(version, args)
Morten Krogh-Jespersena23fa102022-07-01 08:51:59 +0200101
102
Ian Zernya2703f82021-05-31 11:11:51 +0200103def tag_agp_version(agp, args):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200104 tag = 'agp-%s' % agp
105 result = get_tag_info_on_origin(tag)
106 if result:
107 print('Tag %s is already present' % tag)
108 print(result)
109 subprocess.call(['git', 'show', '--oneline', '-s', tag])
110 return 0
111 with utils.TempDir() as temp:
112 url = "%s/%s/builder-%s.jar" % (AGP_MAVEN, agp, agp)
113 jar = os.path.join(temp, "agp.jar")
114 try:
115 urllib.request.urlretrieve(url, jar)
116 except urllib.error.HTTPError as e:
117 print('Could not find jar for agp %s' % agp)
118 print(e)
119 return 1
120 print_version_helper = prepare_print_version(utils.R8_JAR, temp)
121 output = subprocess.check_output([
122 jdk.GetJavaExecutable(), '-cp',
123 ':'.join([jar, print_version_helper]),
124 'com.android.tools.r8.utils.PrintR8Version'
125 ]).decode('utf-8')
126 version = output.split(' ')[0]
127 run(args, ['git', 'tag', '-f', tag, '-m', tag, '%s^{}' % version])
Rico Wind94b77642023-11-27 08:22:45 +0100128 run(args, ['git', 'push', '-o', 'push-justification=b/313360935',
129 'origin', tag])
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200130
Ian Zernya2703f82021-05-31 11:11:51 +0200131
132def tag_r8_branch(branch, args):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200133 if not branch.startswith('origin/'):
134 print('Expected branch to start with origin/')
135 return 1
136 output = subprocess.check_output(
137 ['git', 'log', '--pretty=format:%H\t%s', '--grep', VERSION_EXP,
138 branch]).decode('utf-8')
139 for l in output.split('\n'):
140 (hash, subject) = l.split('\t')
141 m = re.search('Version (.+)', subject)
142 if not m:
143 print('Unable to find a version for line: %s' % l)
144 continue
145 version = m.group(1)
146 result = get_tag_info_on_origin(version)
147 if not result:
148 run(args, ['git', 'tag', '-a', version, '-m', version, hash])
Rico Wind94b77642023-11-27 08:22:45 +0100149 run(args, ['git', 'push', '-o', 'push-justification=b/313360935',
150 'origin', version])
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200151 if args.dry_run:
152 print('Dry run complete. None of the above have been executed.')
Ian Zernya2703f82021-05-31 11:11:51 +0200153
154
155if __name__ == '__main__':
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200156 sys.exit(main())