blob: ebf14ba7b74133d9cada65f091b4b3d35a600d7a [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]'
17VERSION_EXP='^Version [[:digit:]]\+.[[:digit:]]\+.[[:digit:]]\+\(\|-dev\)$'
18
19# R8 is located in the 'builder' library.
20AGP_MAVEN="https://dl.google.com/android/maven2/com/android/tools/build/builder"
21
22def parse_options():
23 parser = argparse.ArgumentParser(description='Tag R8 Versions')
24 parser.add_argument(
25 '--branch',
26 help='The R8 branch to tag versions on, eg, origin/3.0')
27 parser.add_argument(
28 '--agp',
29 help='The AGP to compute the tag for, eg, 4.2.0-beta03')
30 parser.add_argument(
31 '--dry-run',
32 default=False,
33 action='store_true',
34 help='Print the state changing commands without running them.')
35 return parser.parse_args()
36
37def run(options, cmd):
38 print(' '.join(cmd))
39 if not options.dry_run:
40 subprocess.check_call(cmd)
41
42def main():
43 args = parse_options()
44 if args.branch:
45 tag_r8_branch(args.branch, args)
46 elif args.agp:
47 tag_agp_version(args.agp, args)
48 else:
49 print("Should use a top-level option, such as --branch or --agp.")
50 return 1
51 return 0
52
53def prepare_print_version(dist, temp):
54 wrapper_file = os.path.join(
55 utils.REPO_ROOT,
56 'src/main/java/com/android/tools/r8/utils/PrintR8Version.java')
57 cmd = [
58 jdk.GetJavacExecutable(),
59 wrapper_file,
60 '-d', temp,
61 '-cp', dist,
62 ]
63 utils.PrintCmd(cmd)
64 subprocess.check_output(cmd)
65 return temp
66
67def get_tag_info_on_origin(tag):
68 output = subprocess.check_output(
69 ['git', 'ls-remote', '--tags', 'origin', tag]).decode('utf-8')
70 if len(output.strip()) == 0:
71 return None
72 return output
73
74def tag_agp_version(agp, args):
75 tag = 'agp-%s' % agp
76 result = get_tag_info_on_origin(tag)
77 if result:
78 print('Tag %s is already present' % tag)
79 print(result)
80 subprocess.call(['git', 'show', '--oneline', '-s', tag])
81 return 0
82 with utils.TempDir() as temp:
83 url = "%s/%s/builder-%s.jar" % (AGP_MAVEN, agp, agp)
84 jar = os.path.join(temp, "agp.jar")
85 try:
86 urllib.request.urlretrieve(url, jar)
87 except urllib.error.HTTPError as e:
88 print('Could not find jar for agp %s' % agp)
89 print(e)
90 return 1
91 print_version_helper = prepare_print_version(utils.R8_JAR, temp)
92 output = subprocess.check_output([
93 jdk.GetJavaExecutable(),
94 '-cp', ':'.join([jar, print_version_helper]),
95 'com.android.tools.r8.utils.PrintR8Version'
96 ]).decode('utf-8')
97 version = output.split(' ')[0]
98 run(args, ['git', 'tag', '-f', tag, '-m', tag, '%s^{}' % version])
99 run(args, ['git', 'push', 'origin', tag])
100
101def tag_r8_branch(branch, args):
102 if not branch.startswith('origin/'):
103 print('Expected branch to start with origin/')
104 return 1
105 output = subprocess.check_output([
106 'git', 'log', '--pretty=format:%H\t%s', '--grep', VERSION_EXP, branch
107 ]).decode('utf-8')
108 for l in output.split('\n'):
109 (hash, subject) = l.split('\t')
110 m = re.search('Version (.+)', subject)
111 if not m:
112 print('Unable to find a version for line: %s' % l)
113 continue
114 version = m.group(1)
115 result = get_tag_info_on_origin(version)
116 if not result:
117 run(args, ['git', 'tag', '-a', version, '-m', version, hash])
118 run(args, ['git', 'push', 'origin', version])
119 if args.dry_run:
120 print('Dry run complete. None of the above have been executed.')
121
122
123if __name__ == '__main__':
124 sys.exit(main())