blob: 4a06135fdd158d6d138da9bca9484db8d516b97b [file] [log] [blame]
Morten Krogh-Jespersen8a4d1ca2021-01-19 08:27:02 +01001#!/usr/bin/env python3
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +02002# Copyright (c) 2019, 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 datetime
8import os.path
9import re
Ian Zerny68f5c132023-11-01 12:57:07 +010010import shutil
Søren Gjesse4062e9c2023-09-01 12:21:08 +020011import stat
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +020012import subprocess
13import sys
Christoffer Quist Adamsend408d8d2021-01-26 19:50:39 +010014import urllib.request
Søren Gjesse2d6e9372020-11-04 08:17:28 +010015import xml.etree.ElementTree
Søren Gjesse975c5df2019-12-10 13:48:05 +010016import zipfile
Ian Zernycf1eb3c2020-09-24 08:27:23 +020017
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +020018import utils
19
Søren Gjesse448da112023-08-10 09:30:55 +020020R8_DEV_BRANCH = '8.3'
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020021R8_VERSION_FILE = os.path.join('src', 'main', 'java', 'com', 'android', 'tools',
22 'r8', 'Version.java')
Søren Gjesse27251592019-11-06 17:35:23 +010023THIS_FILE_RELATIVE = os.path.join('tools', 'r8_release.py')
Søren Gjesse376b74c2020-06-22 08:42:44 +020024GMAVEN_PUBLISHER = '/google/bin/releases/android-devtools/gmaven/publisher/gmaven-publisher'
Søren Gjesse975c5df2019-12-10 13:48:05 +010025
26DESUGAR_JDK_LIBS = 'desugar_jdk_libs'
27DESUGAR_JDK_LIBS_CONFIGURATION = DESUGAR_JDK_LIBS + '_configuration'
28ANDROID_TOOLS_PACKAGE = 'com.android.tools'
Ian Zerny59dfa4c2019-10-25 10:34:36 +020029
Søren Gjesse07b6aea2019-12-12 11:11:58 +010030GITHUB_DESUGAR_JDK_LIBS = 'https://github.com/google/desugar_jdk_libs'
Ian Zerny59dfa4c2019-10-25 10:34:36 +020031
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020032
Søren Gjesse4062e9c2023-09-01 12:21:08 +020033def install_gerrit_change_id_hook(checkout_dir):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020034 with utils.ChangedWorkingDirectory(checkout_dir):
35 # Fancy way of getting the string ".git".
36 git_dir = subprocess.check_output(['git', 'rev-parse', '--git-dir'
37 ]).decode('utf-8').strip()
38 commit_msg_hooks = '%s/hooks/commit-msg' % git_dir
39 if not os.path.exists(os.path.dirname(commit_msg_hooks)):
40 os.mkdir(os.path.dirname(commit_msg_hooks))
41 # Install commit hook to generate Gerrit 'Change-Id:'.
42 urllib.request.urlretrieve(
43 'https://gerrit-review.googlesource.com/tools/hooks/commit-msg',
44 commit_msg_hooks)
45 st = os.stat(commit_msg_hooks)
46 os.chmod(commit_msg_hooks,
47 st.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
48
Søren Gjesse4062e9c2023-09-01 12:21:08 +020049
Morten Krogh-Jespersend86a3012020-02-21 15:06:13 +010050def checkout_r8(temp, branch):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020051 subprocess.check_call(['git', 'clone', utils.REPO_SOURCE, temp])
52 with utils.ChangedWorkingDirectory(temp):
53 subprocess.check_call([
54 'git', 'new-branch', '--upstream',
55 'origin/%s' % branch, 'dev-release'
56 ])
57 install_gerrit_change_id_hook(temp)
58 return temp
Morten Krogh-Jespersend86a3012020-02-21 15:06:13 +010059
60
Ian Zerny59dfa4c2019-10-25 10:34:36 +020061def prepare_release(args):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020062 if args.version:
63 print("Cannot manually specify version when making a dev release.")
64 sys.exit(1)
Ian Zerny59dfa4c2019-10-25 10:34:36 +020065
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020066 def make_release(args):
67 commithash = args.dev_release
Ian Zerny59dfa4c2019-10-25 10:34:36 +020068
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020069 with utils.TempDir() as temp:
70 with utils.ChangedWorkingDirectory(checkout_r8(temp,
71 R8_DEV_BRANCH)):
72 # Compute the current and new version on the branch.
73 result = None
74 for line in open(R8_VERSION_FILE, 'r'):
75 result = re.match(
76 r'.*LABEL = "%s\.(\d+)\-dev";' % R8_DEV_BRANCH, line)
77 if result:
78 break
79 if not result or not result.group(1):
80 print('Failed to find version label matching %s(\d+)-dev'\
81 % R8_DEV_BRANCH)
82 sys.exit(1)
83 try:
84 patch_version = int(result.group(1))
85 except ValueError:
86 print('Failed to convert version to integer: %s' %
87 result.group(1))
Ian Zerny59dfa4c2019-10-25 10:34:36 +020088
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020089 old_version = '%s.%s-dev' % (R8_DEV_BRANCH, patch_version)
90 version = '%s.%s-dev' % (R8_DEV_BRANCH, patch_version + 1)
Ian Zerny59dfa4c2019-10-25 10:34:36 +020091
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020092 # Verify that the merge point from main is not empty.
93 merge_diff_output = subprocess.check_output(
94 ['git', 'diff', 'HEAD..%s' % commithash]).decode('utf-8')
95 other_diff = version_change_diff(merge_diff_output, old_version,
96 "main")
97 if not other_diff:
98 print('Merge point from main (%s)' % commithash, \
99 'is the same as exiting release (%s).' % old_version)
100 sys.exit(1)
Ian Zerny59dfa4c2019-10-25 10:34:36 +0200101
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200102 if args.dev_pre_cherry_pick:
103 for pre_commit in args.dev_pre_cherry_pick:
104 subprocess.check_call(
105 ['git', 'cherry-pick', '--no-edit', pre_commit])
Rico Wind140a5642019-11-26 10:44:21 +0100106
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200107 # Merge the desired commit from main on to the branch.
108 subprocess.check_call(
109 ['git', 'merge', '--no-ff', '--no-edit', commithash])
Ian Zerny59dfa4c2019-10-25 10:34:36 +0200110
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200111 # Rewrite the version, commit and validate.
112 sed(old_version, version, R8_VERSION_FILE)
Ian Zerny59dfa4c2019-10-25 10:34:36 +0200113
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200114 subprocess.check_call(
115 ['git', 'commit', '-a', '-m',
116 'Version %s' % version])
Ian Zerny59dfa4c2019-10-25 10:34:36 +0200117
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200118 version_diff_output = subprocess.check_output(
119 ['git', 'diff', '%s..HEAD' % commithash]).decode('utf-8')
Ian Zerny59dfa4c2019-10-25 10:34:36 +0200120
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200121 validate_version_change_diff(version_diff_output, "main",
122 version)
Ian Zerny59dfa4c2019-10-25 10:34:36 +0200123
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200124 cmd = ['git', 'cl', 'upload', '--no-squash']
125 if args.bypass_hooks:
126 cmd.append('--bypass-hooks')
127 maybe_check_call(args, cmd)
Ian Zerny59dfa4c2019-10-25 10:34:36 +0200128
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200129 if args.dry_run:
130 input(
131 'DryRun: check %s for content of version %s [enter to continue]:'
132 % (temp, version))
Ian Zerny59dfa4c2019-10-25 10:34:36 +0200133
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200134 return "%s dev version %s from hash %s for review" % (
135 'DryRun: omitted upload of' if args.dry_run else 'Uploaded',
136 version, commithash)
Ian Zerny59dfa4c2019-10-25 10:34:36 +0200137
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200138 return make_release
Ian Zerny59dfa4c2019-10-25 10:34:36 +0200139
140
Søren Gjesse4e5c6fe2019-11-05 17:17:43 +0100141def maybe_tag(args, version):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200142 maybe_check_call(args,
143 ['git', 'tag', '-a', version, '-m',
144 '"%s"' % version])
145 maybe_check_call(args, ['git', 'push', 'origin', 'refs/tags/%s' % version])
Søren Gjesse4e5c6fe2019-11-05 17:17:43 +0100146
Morten Krogh-Jespersend86a3012020-02-21 15:06:13 +0100147
Ian Zerny59dfa4c2019-10-25 10:34:36 +0200148def version_change_diff(diff, old_version, new_version):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200149 invalid_line = None
150 for line in str(diff).splitlines():
151 if line.startswith('- ') and \
152 line != '- public static final String LABEL = "%s";' % old_version:
153 invalid_line = line
154 elif line.startswith('+ ') and \
155 line != '+ public static final String LABEL = "%s";' % new_version:
156 invalid_line = line
157 return invalid_line
Ian Zerny59dfa4c2019-10-25 10:34:36 +0200158
Søren Gjesseddeb02e2019-11-06 15:48:07 +0100159
Søren Gjesse4e5c6fe2019-11-05 17:17:43 +0100160def validate_version_change_diff(version_diff_output, old_version, new_version):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200161 invalid = version_change_diff(version_diff_output, old_version, new_version)
162 if invalid:
163 print("Unexpected diff:")
164 print("=" * 80)
165 print(version_diff_output)
166 print("=" * 80)
167 accept_string = 'THE DIFF IS OK!'
168 answer = input("Accept the additonal diff as part of the release? "
169 "Type '%s' to accept: " % accept_string)
170 if answer != accept_string:
171 print("You did not type '%s'" % accept_string)
172 print('Aborting dev release for %s' % version)
173 sys.exit(1)
Søren Gjesse4e5c6fe2019-11-05 17:17:43 +0100174
Ian Zerny59dfa4c2019-10-25 10:34:36 +0200175
176def maybe_check_call(args, cmd):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200177 if args.dry_run:
178 print('DryRun:', ' '.join(cmd))
179 else:
180 print(' '.join(cmd))
181 return subprocess.check_call(cmd)
Ian Zerny59dfa4c2019-10-25 10:34:36 +0200182
183
Ian Zerny8c5f7b42023-05-25 11:24:05 +0200184def update_prebuilds(r8_checkout, version, checkout, keepanno=False):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200185 path = os.path.join(r8_checkout, 'tools', 'update_prebuilds_in_android.py')
186 commit_arg = '--commit_hash=' if len(version) == 40 else '--version='
187 cmd = [path, '--targets=lib', '--maps', commit_arg + version, checkout]
188 if keepanno:
189 cmd.append("--keepanno")
190 subprocess.check_call(cmd)
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200191
192
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200193def release_studio_or_aosp(r8_checkout,
194 path,
195 options,
196 git_message,
197 keepanno=False):
198 with utils.ChangedWorkingDirectory(path):
199 if not options.use_existing_work_branch:
200 subprocess.call(['repo', 'abandon', 'update-r8'])
201 if not options.no_sync:
202 subprocess.check_call(['repo', 'sync', '-cq', '-j', '16'])
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200203
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200204 prebuilts_r8 = os.path.join(path, 'prebuilts', 'r8')
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200205
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200206 if not options.use_existing_work_branch:
207 with utils.ChangedWorkingDirectory(prebuilts_r8):
208 subprocess.check_call(['repo', 'start', 'update-r8'])
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200209
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200210 update_prebuilds(r8_checkout, options.version, path, keepanno)
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200211
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200212 with utils.ChangedWorkingDirectory(prebuilts_r8):
213 if not options.use_existing_work_branch:
214 subprocess.check_call(
215 ['git', 'commit', '-a', '-m', git_message])
216 else:
217 print('Not committing when --use-existing-work-branch. ' +
218 'Commit message should be:\n\n' + git_message + '\n')
219 # Don't upload if requested not to, or if changes are not committed due
220 # to --use-existing-work-branch
221 if not options.no_upload and not options.use_existing_work_branch:
222 process = subprocess.Popen(
223 ['repo', 'upload', '.', '--verify', '--current-branch'],
224 stdin=subprocess.PIPE)
225 return process.communicate(input=b'y\n')[0]
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200226
227
Morten Krogh-Jespersenb4a7acc2019-10-02 10:46:32 +0200228def prepare_aosp(args):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200229 assert args.version
230 assert os.path.exists(args.aosp), "Could not find AOSP path %s" % args.aosp
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200231
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200232 def release_aosp(options):
233 print("Releasing for AOSP")
234 if options.dry_run:
235 return 'DryRun: omitting AOSP release for %s' % options.version
Ian Zerny59dfa4c2019-10-25 10:34:36 +0200236
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200237 git_message = ("""Update D8 and R8 to %s
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200238
Morten Krogh-Jespersendbbf9a82020-08-11 21:38:08 +0200239Version: %s
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200240This build IS NOT suitable for preview or public release.
241
Morten Krogh-Jespersenb4a7acc2019-10-02 10:46:32 +0200242Built here: go/r8-releases/raw/%s
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200243
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200244Test: TARGET_PRODUCT=aosp_arm64 m -j core-oj""" %
245 (args.version, args.version, args.version))
246 # Fixes to Android U branch is based of 8.2.2-dev where the keepanno library
247 # is not built.
248 keepanno = not args.version.startswith('8.2.2-udc')
249 return release_studio_or_aosp(utils.REPO_ROOT,
250 args.aosp,
251 options,
252 git_message,
253 keepanno=keepanno)
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200254
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200255 return release_aosp
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200256
257
Søren Gjesse376b74c2020-06-22 08:42:44 +0200258def prepare_maven(args):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200259 assert args.version
Søren Gjesse376b74c2020-06-22 08:42:44 +0200260
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200261 def release_maven(options):
262 gfile = '/bigstore/r8-releases/raw/%s/r8lib.zip' % args.version
263 release_id = gmaven_publisher_stage(options, [gfile])
Søren Gjesse376b74c2020-06-22 08:42:44 +0200264
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200265 print("Staged Release ID " + release_id + ".\n")
266 gmaven_publisher_stage_redir_test_info(
267 release_id, "com.android.tools:r8:%s" % args.version, "r8lib.jar")
Søren Gjesse376b74c2020-06-22 08:42:44 +0200268
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200269 print
270 answer = input("Continue with publishing [y/N]:")
Søren Gjesse376b74c2020-06-22 08:42:44 +0200271
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200272 if answer != 'y':
273 print('Aborting release to Google maven')
274 sys.exit(1)
Søren Gjesse376b74c2020-06-22 08:42:44 +0200275
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200276 gmaven_publisher_publish(args, release_id)
Søren Gjesse376b74c2020-06-22 08:42:44 +0200277
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200278 print("")
279 print("Published. Use the email workflow for approval.")
Søren Gjesse376b74c2020-06-22 08:42:44 +0200280
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200281 return release_maven
282
Søren Gjesse376b74c2020-06-22 08:42:44 +0200283
Ian Zernyacfaf292020-09-24 07:14:56 +0200284# ------------------------------------------------------ column 70 --v
Ian Zernycf1eb3c2020-09-24 08:27:23 +0200285def git_message_dev(version, bugs):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200286 return """Update D8 R8 to %s
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200287
Ian Zernyacfaf292020-09-24 07:14:56 +0200288This is a development snapshot, it's fine to use for studio canary
289build, but not for BETA or release, for those we would need a release
290version of R8 binaries. This build IS suitable for preview release
291but IS NOT suitable for public release.
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200292
Morten Krogh-Jespersenb4a7acc2019-10-02 10:46:32 +0200293Built here: go/r8-releases/raw/%s
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200294Test: ./gradlew check
Ian Zernyfec00092022-03-16 08:25:07 +0100295Bug: %s""" % (version, version, '\nBug: '.join(map(bug_fmt, bugs)))
Morten Krogh-Jespersenb4a7acc2019-10-02 10:46:32 +0200296
297
Morten Krogh-Jespersena1396cd2019-10-02 16:14:40 +0200298def git_message_release(version, bugs):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200299 return """D8 R8 version %s
Morten Krogh-Jespersena1396cd2019-10-02 16:14:40 +0200300
301Built here: go/r8-releases/raw/%s/
302Test: ./gradlew check
Morten Krogh-Jespersend86a3012020-02-21 15:06:13 +0100303
Ian Zernyfec00092022-03-16 08:25:07 +0100304Bug: %s""" % (version, version, '\nBug: '.join(map(bug_fmt, bugs)))
Morten Krogh-Jespersena1396cd2019-10-02 16:14:40 +0200305
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200306
Ian Zernyfec00092022-03-16 08:25:07 +0100307def bug_fmt(bug):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200308 return "b/%s" % bug
309
Morten Krogh-Jespersena1396cd2019-10-02 16:14:40 +0200310
Morten Krogh-Jespersenb4a7acc2019-10-02 10:46:32 +0200311def prepare_studio(args):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200312 assert args.version
313 assert os.path.exists(args.studio), ("Could not find STUDIO path %s" %
314 args.studio)
Søren Gjesse25e61912023-11-06 16:02:07 +0100315 if (not args.studio.endswith('-dev')
316 and not args.studio.endswith('-dev/')
317 and not args.studio_legacy_release):
318 print("Please use the new release process, see go/r8-release-prebuilts. "
319 + "If for some reason the legacy release process is needed "
320 + "pass --studio-legacy-release")
321 sys.exit(1)
Morten Krogh-Jespersenb4a7acc2019-10-02 10:46:32 +0200322
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200323 def release_studio(options):
324 print("Releasing for STUDIO")
325 if options.dry_run:
326 return 'DryRun: omitting studio release for %s' % options.version
Ian Zerny59dfa4c2019-10-25 10:34:36 +0200327
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200328 if 'dev' in options.version:
329 git_message = git_message_dev(options.version, options.bug)
330 r8_checkout = utils.REPO_ROOT
331 return release_studio_or_aosp(r8_checkout, args.studio, options,
332 git_message)
333 else:
334 with utils.TempDir() as temp:
335 checkout_r8(temp,
336 options.version[0:options.version.rindex('.')])
337 git_message = git_message_release(options.version, options.bug)
338 return release_studio_or_aosp(temp, args.studio, options,
339 git_message)
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200340
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200341 return release_studio
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200342
343
344def g4_cp(old, new, file):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200345 subprocess.check_call('g4 cp {%s,%s}/%s' % (old, new, file), shell=True)
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200346
347
348def g4_open(file):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200349 if not os.access(file, os.W_OK):
350 subprocess.check_call('g4 open %s' % file, shell=True)
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200351
352
Rico Wind8d008ca2020-04-22 12:13:15 +0200353def g4_change(version):
Ian Zerny68f5c132023-11-01 12:57:07 +0100354 message = f'Update R8 to {version}'
355 if version == 'main':
356 message = f'DO NOT SUBMIT: {message}'
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200357 return subprocess.check_output(
Ian Zerny68f5c132023-11-01 12:57:07 +0100358 f'g4 change --desc "{message}\n"',
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200359 shell=True).decode('utf-8')
360
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200361
Morten Krogh-Jespersen1f2f5142020-09-21 11:20:23 +0200362def get_cl_id(c4_change_output):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200363 startIndex = c4_change_output.find('Change ') + len('Change ')
364 endIndex = c4_change_output.find(' ', startIndex)
365 cl = c4_change_output[startIndex:endIndex]
366 assert cl.isdigit()
367 return cl
368
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200369
370def sed(pattern, replace, path):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200371 with open(path, "r") as sources:
372 lines = sources.readlines()
373 with open(path, "w") as sources:
374 for line in lines:
375 sources.write(re.sub(pattern, replace, line))
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200376
Morten Krogh-Jespersenb4a7acc2019-10-02 10:46:32 +0200377def download_file(version, file, dst):
Ian Zerny68f5c132023-11-01 12:57:07 +0100378 if version == 'main':
379 src = os.path.join(utils.LIBS, file)
380 if os.path.exists(src):
381 shutil.copyfile(src, dst)
382 else:
383 print(f"WARNING: no file found for {src}. Skipping.")
384 return
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200385 dir = 'raw' if len(version) != 40 else 'raw/main'
386 urllib.request.urlretrieve(
387 ('https://storage.googleapis.com/r8-releases/%s/%s/%s' %
388 (dir, version, file)), dst)
389
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200390
Søren Gjesseb093b672020-06-23 16:23:10 +0200391def download_gfile(gfile, dst):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200392 if not gfile.startswith('/bigstore/r8-releases'):
393 print('Unexpected gfile prefix for %s' % gfile)
394 sys.exit(1)
Søren Gjesseb093b672020-06-23 16:23:10 +0200395
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200396 urllib.request.urlretrieve(
397 'https://storage.googleapis.com/%s' % gfile[len('/bigstore/'):], dst)
398
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200399
400def blaze_run(target):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200401 return subprocess.check_output('blaze run %s' % target,
402 shell=True,
403 stderr=subprocess.STDOUT).decode('utf-8')
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200404
405
Ian Zerny59dfa4c2019-10-25 10:34:36 +0200406def prepare_google3(args):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200407 assert args.version
408 # Check if an existing client exists.
409 if not args.use_existing_work_branch:
410 check_no_google3_client(args, args.p4_client)
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200411
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200412 def release_google3(options):
413 print("Releasing for Google 3")
414 if options.dry_run:
415 return 'DryRun: omitting g3 release for %s' % options.version
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200416
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200417 google3_base = subprocess.check_output(
418 ['p4', 'g4d', '-f', args.p4_client]).decode('utf-8').rstrip()
419 third_party_r8 = os.path.join(google3_base, 'third_party', 'java', 'r8')
420 today = datetime.date.today()
421 with utils.ChangedWorkingDirectory(third_party_r8):
422 # download files
423 g4_open('full.jar')
424 g4_open('src.jar')
425 g4_open('lib.jar')
426 g4_open('lib.jar.map')
427 g4_open('retrace_full.jar')
428 g4_open('retrace_lib.jar')
429 g4_open('retrace_lib.jar.map')
430 g4_open('desugar_jdk_libs_configuration.jar')
Ian Zerny68f5c132023-11-01 12:57:07 +0100431 g4_open('threading-module-blocking.jar')
432 g4_open('threading-module-single-threaded.jar')
433 download_file(options.version,
434 'r8-full-exclude-deps.jar',
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200435 'full.jar')
Ian Zerny68f5c132023-11-01 12:57:07 +0100436 download_file(options.version,
437 'r8-full-exclude-deps.jar',
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200438 'retrace_full.jar')
Ian Zerny68f5c132023-11-01 12:57:07 +0100439 download_file(options.version,
440 'r8-src.jar',
441 'src.jar')
442 download_file(options.version,
443 'r8lib-exclude-deps.jar',
444 'lib.jar')
445 download_file(options.version,
446 'r8lib-exclude-deps.jar.map',
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200447 'lib.jar.map')
Ian Zerny68f5c132023-11-01 12:57:07 +0100448 download_file(options.version,
449 'desugar_jdk_libs_configuration.jar',
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200450 'desugar_jdk_libs_configuration.jar')
Ian Zerny68f5c132023-11-01 12:57:07 +0100451 download_file(options.version,
452 'r8retrace-exclude-deps.jar',
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200453 'retrace_lib.jar')
Ian Zerny68f5c132023-11-01 12:57:07 +0100454 download_file(options.version,
455 'r8retrace-exclude-deps.jar.map',
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200456 'retrace_lib.jar.map')
Ian Zerny68f5c132023-11-01 12:57:07 +0100457 download_file(options.version,
458 'threading-module-blocking.jar',
459 'threading-module-blocking.jar')
460 download_file(options.version,
461 'threading-module-single-threaded.jar',
462 'threading-module-single-threaded.jar')
463 if options.version != 'main':
464 g4_open('METADATA')
465 metadata_path = os.path.join(third_party_r8, 'METADATA')
466 match_count = 0
467 match_count_expected = 12
468 version_match_regexp = r'[1-9]\.[0-9]{1,2}\.[0-9]{1,3}-dev'
469 for line in open(metadata_path, 'r'):
470 result = re.search(version_match_regexp, line)
471 if result:
472 match_count = match_count + 1
473 if match_count != match_count_expected:
474 print(f"""WARNING:
475 Could not find the previous -dev release string to replace in METADATA.
476 Expected to find it mentioned {match_count_expected} times, but found
477 {match_count} occurrences. Please update {metadata_path} manually and
478 run again with options --google3 --use-existing-work-branch.
479 """)
480 sys.exit(1)
481 sed(version_match_regexp, options.version, metadata_path)
482 sed(r'\{ year.*\}',
483 f'{{ year: {today.year} month: {today.month} day: {today.day} }}',
484 metadata_path)
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200485 subprocess.check_output('chmod u+w *', shell=True)
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200486
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200487 with utils.ChangedWorkingDirectory(google3_base):
488 blaze_result = blaze_run('//third_party/java/r8:d8 -- --version')
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200489
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200490 assert options.version in blaze_result
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200491
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200492 if not options.no_upload:
493 change_result = g4_change(options.version)
494 change_result += 'Run \'(g4d ' + args.p4_client \
495 + ' && tap_presubmit -p all --train -c ' \
496 + get_cl_id(change_result) + ')\' for running TAP global' \
497 + ' presubmit using the train.\n' \
498 + 'Run \'(g4d ' + args.p4_client \
499 + ' && tap_presubmit -p all --notrain --detach --email' \
500 + ' --skip_flaky_targets --skip_already_failing -c ' \
501 + get_cl_id(change_result) + ')\' for running an isolated' \
502 + ' TAP global presubmit.'
503 return change_result
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200504
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200505 return release_google3
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200506
507
Morten Krogh-Jespersen04527942022-08-02 13:50:11 +0200508def prepare_google3_retrace(args):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200509 assert args.version
510 # Check if an existing client exists.
511 if not args.use_existing_work_branch:
512 check_no_google3_client(args, args.p4_client)
Morten Krogh-Jespersen04527942022-08-02 13:50:11 +0200513
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200514 def release_google3_retrace(options):
515 print("Releasing Retrace for Google 3")
516 if options.dry_run:
517 return 'DryRun: omitting g3 release for %s' % options.version
Morten Krogh-Jespersen04527942022-08-02 13:50:11 +0200518
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200519 google3_base = subprocess.check_output(
520 ['p4', 'g4d', '-f', args.p4_client]).decode('utf-8').rstrip()
521 third_party_r8 = os.path.join(google3_base, 'third_party', 'java', 'r8')
522 with utils.ChangedWorkingDirectory(third_party_r8):
523 # download files
524 g4_open('retrace_full.jar')
525 g4_open('retrace_lib.jar')
526 g4_open('retrace_lib.jar.map')
527 download_file(options.version, 'r8-full-exclude-deps.jar',
528 'retrace_full.jar')
529 download_file(options.version, 'r8retrace-exclude-deps.jar',
530 'retrace_lib.jar')
531 download_file(options.version, 'r8lib-exclude-deps.jar.map',
532 'retrace_lib.jar.map')
533 g4_open('METADATA')
534 metadata_path = os.path.join(third_party_r8, 'METADATA')
535 match_count = 0
536 version_match_regexp = r'[1-9]\.[0-9]{1,2}\.[0-9]{1,3}-dev/r8retrace-exclude-deps.jar'
537 for line in open(metadata_path, 'r'):
538 result = re.search(version_match_regexp, line)
539 if result:
540 match_count = match_count + 1
541 if match_count != 1:
542 print((
543 "Could not find the previous retrace release string to replace in "
544 +
545 "METADATA. Expected to find is mentioned 1 times. Please update %s "
546 + "manually and run again with options --google3retrace " +
547 "--use-existing-work-branch.") % metadata_path)
548 sys.exit(1)
549 sed(version_match_regexp,
550 options.version + "/r8retrace-exclude-deps.jar", metadata_path)
551 subprocess.check_output('chmod u+w *', shell=True)
Morten Krogh-Jespersen04527942022-08-02 13:50:11 +0200552
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200553 with utils.ChangedWorkingDirectory(google3_base):
554 blaze_result = blaze_run(
555 '//third_party/java/r8:retrace -- --version')
Morten Krogh-Jespersen04527942022-08-02 13:50:11 +0200556
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200557 print(blaze_result)
558 assert options.version in blaze_result
Morten Krogh-Jespersen04527942022-08-02 13:50:11 +0200559
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200560 if not options.no_upload:
561 change_result = g4_change(options.version)
562 change_result += 'Run \'(g4d ' + args.p4_client \
563 + ' && tap_presubmit -p all --train -c ' \
564 + get_cl_id(change_result) + ')\' for running TAP global' \
565 + ' presubmit using the train.\n' \
566 + 'Run \'(g4d ' + args.p4_client \
567 + ' && tap_presubmit -p all --notrain --detach --email' \
568 + ' --skip_flaky_targets --skip_already_failing -c ' \
569 + get_cl_id(change_result) + ')\' for running an isolated' \
570 + ' TAP global presubmit.'
571 return change_result
Morten Krogh-Jespersen04527942022-08-02 13:50:11 +0200572
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200573 return release_google3_retrace
574
Morten Krogh-Jespersen04527942022-08-02 13:50:11 +0200575
Søren Gjesse73355ec2020-06-29 09:19:44 +0200576def update_desugar_library_in_studio(args):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200577 assert os.path.exists(args.studio), ("Could not find STUDIO path %s" %
578 args.studio)
Søren Gjesse73355ec2020-06-29 09:19:44 +0200579
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200580 def make_release(args):
581 library_version = args.update_desugar_library_in_studio[0]
582 configuration_version = args.update_desugar_library_in_studio[1]
583 change_name = 'update-desugar-library-dependencies'
Søren Gjesse73355ec2020-06-29 09:19:44 +0200584
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200585 with utils.ChangedWorkingDirectory(args.studio):
586 if not args.use_existing_work_branch:
587 subprocess.call(['repo', 'abandon', change_name])
588 if not args.no_sync:
589 subprocess.check_call(['repo', 'sync', '-cq', '-j', '16'])
Søren Gjesse73355ec2020-06-29 09:19:44 +0200590
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200591 cmd = [
592 'tools/base/bazel/bazel', 'run',
593 '//tools/base/bazel:add_dependency', '--',
594 '--repo=https://maven.google.com com.android.tools:desugar_jdk_libs:%s'
595 % library_version
596 ]
597 utils.PrintCmd(cmd)
598 subprocess.check_call(" ".join(cmd), shell=True)
599 cmd = ['tools/base/bazel/bazel', 'shutdown']
600 utils.PrintCmd(cmd)
601 subprocess.check_call(cmd)
Søren Gjesse73355ec2020-06-29 09:19:44 +0200602
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200603 prebuilts_tools = os.path.join(args.studio, 'prebuilts', 'tools')
Søren Gjesse73355ec2020-06-29 09:19:44 +0200604 with utils.ChangedWorkingDirectory(prebuilts_tools):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200605 if not args.use_existing_work_branch:
606 with utils.ChangedWorkingDirectory(prebuilts_tools):
607 subprocess.check_call(['repo', 'start', change_name])
608 m2_dir = os.path.join('common', 'm2', 'repository', 'com',
609 'android', 'tools')
610 subprocess.check_call([
611 'git', 'add',
612 os.path.join(m2_dir, DESUGAR_JDK_LIBS, library_version)
613 ])
614 subprocess.check_call([
615 'git', 'add',
616 os.path.join(m2_dir, DESUGAR_JDK_LIBS_CONFIGURATION,
617 configuration_version)
618 ])
Søren Gjesse73355ec2020-06-29 09:19:44 +0200619
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200620 git_message = ("""Update library desugaring dependencies
Søren Gjesse73355ec2020-06-29 09:19:44 +0200621
622 com.android.tools:desugar_jdk_libs:%s
623 com.android.tools:desugar_jdk_libs_configuration:%s
624
625Bug: %s
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200626Test: L8ToolTest, L8DexDesugarTest""" %
627 (library_version, configuration_version,
628 '\nBug: '.join(map(bug_fmt, args.bug))))
Søren Gjesse73355ec2020-06-29 09:19:44 +0200629
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200630 if not args.use_existing_work_branch:
631 subprocess.check_call(
632 ['git', 'commit', '-a', '-m', git_message])
633 else:
634 print('Not committing when --use-existing-work-branch. ' +
635 'Commit message should be:\n\n' + git_message + '\n')
636 # Don't upload if requested not to, or if changes are not committed due
637 # to --use-existing-work-branch
638 if not args.no_upload and not args.use_existing_work_branch:
639 process = subprocess.Popen(['repo', 'upload', '.', '--verify'],
640 stdin=subprocess.PIPE)
641 return process.communicate(input='y\n')[0]
Søren Gjesse73355ec2020-06-29 09:19:44 +0200642
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200643 return make_release
Søren Gjesse73355ec2020-06-29 09:19:44 +0200644
645
Søren Gjesse975c5df2019-12-10 13:48:05 +0100646def prepare_desugar_library(args):
647
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200648 def make_release(args):
649 library_version = args.desugar_library[0]
650 configuration_version = args.desugar_library[1]
Søren Gjesse975c5df2019-12-10 13:48:05 +0100651
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200652 # TODO(b/237636871): Cleanup and generalize.
653 if (not (library_version.startswith('1.1') or
654 library_version.startswith('1.2') or
655 library_version.startswith('2.0'))):
656 print(
657 "Release script does not support desugared library version %s" %
658 library_version)
659 sys.exit(1)
Søren Gjessee78b1652022-06-30 12:38:36 +0200660
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200661 postfixes = ['']
662 if library_version.startswith('1.2'):
663 postfixes = ['_legacy']
664 if library_version.startswith('2.0'):
665 postfixes = ['_minimal', '', '_nio']
Søren Gjesseb093b672020-06-23 16:23:10 +0200666
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200667 with utils.TempDir() as temp:
668 with utils.ChangedWorkingDirectory(temp):
669 artifacts = []
670 for postfix in postfixes:
671 group_postfix = ('' if postfix == '_legacy' else postfix)
672 archive_postfix = (postfix
673 if library_version.startswith('1.1') else
674 '_jdk11' + postfix)
675 library_jar = DESUGAR_JDK_LIBS + postfix + '.jar'
676 library_archive = DESUGAR_JDK_LIBS + archive_postfix + '.zip'
677 configuration_archive = DESUGAR_JDK_LIBS_CONFIGURATION + archive_postfix + '.zip'
678 library_gfile = ('/bigstore/r8-releases/raw/%s/%s/%s' %
679 (DESUGAR_JDK_LIBS + group_postfix,
680 library_version, library_archive))
681 configuration_gfile = (
682 '/bigstore/r8-releases/raw/main/%s/%s' %
683 (configuration_version, configuration_archive))
Søren Gjesse975c5df2019-12-10 13:48:05 +0100684
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200685 download_gfile(library_gfile, library_archive)
686 download_gfile(configuration_gfile, configuration_archive)
687 check_configuration(configuration_archive, group_postfix)
688 artifacts.append(library_gfile)
689 artifacts.append(configuration_gfile)
Søren Gjesseb093b672020-06-23 16:23:10 +0200690
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200691 release_id = gmaven_publisher_stage(args, artifacts)
Søren Gjesseb093b672020-06-23 16:23:10 +0200692
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200693 print("Staged Release ID " + release_id + ".\n")
694 library_artifact_id = \
695 '%s:%s:%s' % (ANDROID_TOOLS_PACKAGE, DESUGAR_JDK_LIBS, library_version)
696 gmaven_publisher_stage_redir_test_info(release_id,
697 library_artifact_id,
698 library_jar)
Søren Gjesseb093b672020-06-23 16:23:10 +0200699
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200700 print("")
701 answer = input("Continue with publishing [y/N]:")
Søren Gjesse975c5df2019-12-10 13:48:05 +0100702
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200703 if answer != 'y':
704 print('Aborting release to Google maven')
705 sys.exit(1)
Søren Gjesse975c5df2019-12-10 13:48:05 +0100706
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200707 gmaven_publisher_publish(args, release_id)
Søren Gjesseb093b672020-06-23 16:23:10 +0200708
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200709 print("")
710 print("Published. Use the email workflow for approval.")
Søren Gjesse975c5df2019-12-10 13:48:05 +0100711
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200712 return make_release
Søren Gjesse975c5df2019-12-10 13:48:05 +0100713
714
Søren Gjesse3e459092023-01-05 09:40:28 +0100715def check_configuration(configuration_archive, postfix):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200716 zip = zipfile.ZipFile(configuration_archive)
717 zip.extractall()
718 dirs = os.listdir(
719 os.path.join('com', 'android', 'tools',
720 DESUGAR_JDK_LIBS_CONFIGURATION + postfix))
721 if len(dirs) != 1:
722 print('Unexpected archive content, %s' + dirs)
723 sys.exit(1)
Søren Gjesse975c5df2019-12-10 13:48:05 +0100724
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200725 version = dirs[0]
726 pom_file = os.path.join(
727 'com', 'android', 'tools', DESUGAR_JDK_LIBS_CONFIGURATION + postfix,
728 version,
729 '%s-%s.pom' % (DESUGAR_JDK_LIBS_CONFIGURATION + postfix, version))
730 version_from_pom = extract_version_from_pom(pom_file)
731 if version != version_from_pom:
732 print('Version mismatch, %s != %s' % (version, version_from_pom))
733 sys.exit(1)
734
Søren Gjesse07b6aea2019-12-12 11:11:58 +0100735
736def check_no_google3_client(args, client_name):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200737 if not args.use_existing_work_branch:
738 clients = subprocess.check_output('g4 myclients',
739 shell=True).decode('utf-8')
740 if ':%s:' % client_name in clients:
741 if args.delete_work_branch:
742 subprocess.check_call('g4 citc -d -f %s' % client_name,
743 shell=True)
744 else:
745 print(("Remove the existing '%s' client before continuing " +
746 "(force delete: 'g4 citc -d -f %s'), " +
747 "or use either --use-existing-work-branch or " +
748 "--delete-work-branch.") % (client_name, client_name))
749 sys.exit(1)
Søren Gjesse07b6aea2019-12-12 11:11:58 +0100750
751
Søren Gjesse975c5df2019-12-10 13:48:05 +0100752def extract_version_from_pom(pom_file):
753 ns = "http://maven.apache.org/POM/4.0.0"
754 xml.etree.ElementTree.register_namespace('', ns)
755 tree = xml.etree.ElementTree.ElementTree()
756 tree.parse(pom_file)
757 return tree.getroot().find("{%s}version" % ns).text
758
Søren Gjesse376b74c2020-06-22 08:42:44 +0200759
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200760GMAVEN_PUBLISH_STAGE_RELEASE_ID_PATTERN = re.compile(
761 'Release ID = ([0-9a-f\-]+)')
Søren Gjesse376b74c2020-06-22 08:42:44 +0200762
Søren Gjesseb093b672020-06-23 16:23:10 +0200763
764def gmaven_publisher_stage(args, gfiles):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200765 if args.dry_run:
766 print('Dry-run, would have staged %s' % gfiles)
767 return 'dry-run-release-id'
Søren Gjesse376b74c2020-06-22 08:42:44 +0200768
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200769 print("Staging: %s" % ', '.join(gfiles))
770 print("")
Søren Gjesse376b74c2020-06-22 08:42:44 +0200771
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200772 cmd = [GMAVEN_PUBLISHER, 'stage', '--gfile', ','.join(gfiles)]
773 output = subprocess.check_output(cmd)
Søren Gjesse376b74c2020-06-22 08:42:44 +0200774
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200775 # Expect output to contain:
776 # [INFO] 06/19/2020 09:35:12 CEST: >>>>>>>>>> Staged
777 # [INFO] 06/19/2020 09:35:12 CEST: Release ID = 9171d015-18f6-4a90-9984-1c362589dc1b
778 # [INFO] 06/19/2020 09:35:12 CEST: Stage Path = /bigstore/studio_staging/maven2/sgjesse/9171d015-18f6-4a90-9984-1c362589dc1b
Søren Gjesse376b74c2020-06-22 08:42:44 +0200779
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200780 matches = GMAVEN_PUBLISH_STAGE_RELEASE_ID_PATTERN.findall(
781 output.decode("utf-8"))
782 if matches == None or len(matches) > 1:
783 print("Could not determine the release ID from the gmaven_publisher " +
784 "output. Expected a line with 'Release ID = <release id>'.")
785 print("Output was:")
786 print(output)
787 sys.exit(1)
788
Morten Krogh-Jespersen8a4d1ca2021-01-19 08:27:02 +0100789 print(output)
Søren Gjesse376b74c2020-06-22 08:42:44 +0200790
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200791 release_id = matches[0]
792 return release_id
Søren Gjesse376b74c2020-06-22 08:42:44 +0200793
Søren Gjesseb093b672020-06-23 16:23:10 +0200794
795def gmaven_publisher_stage_redir_test_info(release_id, artifact, dst):
796
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200797 redir_command = ("/google/data/ro/teams/android-devtools-infra/tools/redir "
798 + "--alsologtostderr " +
799 "--gcs_bucket_path=/bigstore/gmaven-staging/${USER}/%s " +
800 "--port=1480") % release_id
Søren Gjesseb093b672020-06-23 16:23:10 +0200801
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200802 get_command = (
803 "mvn org.apache.maven.plugins:maven-dependency-plugin:2.4:get " +
804 "-Dmaven.repo.local=/tmp/maven_repo_local " +
805 "-DremoteRepositories=http://localhost:1480 " + "-Dartifact=%s " +
806 "-Ddest=%s") % (artifact, dst)
Søren Gjesseb093b672020-06-23 16:23:10 +0200807
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200808 print("""To test the staged content with 'redir' run:
Søren Gjesseb093b672020-06-23 16:23:10 +0200809
810%s
811
Søren Gjesse3d3a9202023-11-02 11:04:28 +0100812Then add the following repository to settings.gradle.kts (Kotlin Script) to
813search the 'redir' repository:
814
815dependencyResolutionManagement {
816 repositories {
817 maven {
818 url = uri("http://localhost:1480")
819 isAllowInsecureProtocol = true
820 }
821 }
822}
823
824or the following to settings.gradle (Groovy);
Søren Gjesseb093b672020-06-23 16:23:10 +0200825
Søren Gjessee78b1652022-06-30 12:38:36 +0200826dependencyResolutionManagement {
827 repositories {
828 maven {
829 url 'http://localhost:1480'
830 allowInsecureProtocol true
831 }
Søren Gjesse44eee512021-09-24 11:44:11 +0200832 }
Søren Gjessee78b1652022-06-30 12:38:36 +0200833}
834
Søren Gjesse3d3a9202023-11-02 11:04:28 +0100835and add the following repository to gradle.build.kts (Kotlin Script) for the
836staged version:
837
838coreLibraryDesugaring("%s") {
839 isChanging = true
840}
841
842or the following to settings.gradle (Groovy);
Søren Gjessee78b1652022-06-30 12:38:36 +0200843
844dependencies {
845 coreLibraryDesugaring('%s') {
846 changing = true
Søren Gjesseb093b672020-06-23 16:23:10 +0200847 }
848}
849
850Use this commands to get artifact from 'redir':
851
852rm -rf /tmp/maven_repo_local
853%s
Søren Gjesse3d3a9202023-11-02 11:04:28 +0100854""" % (redir_command, artifact, artifact, get_command))
Søren Gjesseb093b672020-06-23 16:23:10 +0200855
856
Søren Gjesse376b74c2020-06-22 08:42:44 +0200857def gmaven_publisher_publish(args, release_id):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200858 if args.dry_run:
859 print('Dry-run, would have published %s' % release_id)
860 return
Søren Gjesse376b74c2020-06-22 08:42:44 +0200861
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200862 cmd = [GMAVEN_PUBLISHER, 'publish', release_id]
863 output = subprocess.check_output(cmd)
864
Søren Gjesse975c5df2019-12-10 13:48:05 +0100865
Søren Gjesseddeb02e2019-11-06 15:48:07 +0100866def branch_change_diff(diff, old_version, new_version):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200867 invalid_line = None
868 for line in str(diff).splitlines():
869 if line.startswith('-R8') and \
870 line != "-R8_DEV_BRANCH = '%s'" % old_version:
871 print(line)
872 invalid_line = line
873 elif line.startswith('+R8') and \
874 line != "+R8_DEV_BRANCH = '%s'" % new_version:
875 print(line)
876 invalid_line = line
877 return invalid_line
Søren Gjesseddeb02e2019-11-06 15:48:07 +0100878
879
880def validate_branch_change_diff(version_diff_output, old_version, new_version):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200881 invalid = branch_change_diff(version_diff_output, old_version, new_version)
882 if invalid:
883 print("")
884 print(
885 "The diff for the branch change in tools/release.py is not as expected:"
886 )
887 print("")
888 print("=" * 80)
889 print(version_diff_output)
890 print("=" * 80)
891 print("")
892 print("Validate the uploaded CL before landing.")
893 print("")
Søren Gjesseddeb02e2019-11-06 15:48:07 +0100894
895
Søren Gjesse4e5c6fe2019-11-05 17:17:43 +0100896def prepare_branch(args):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200897 branch_version = args.new_dev_branch[0]
898 commithash = args.new_dev_branch[1]
Søren Gjesse4e5c6fe2019-11-05 17:17:43 +0100899
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200900 current_semver = utils.check_basic_semver_version(
901 R8_DEV_BRANCH, ", current release branch version should be x.y", 2)
902 semver = utils.check_basic_semver_version(
903 branch_version, ", release branch version should be x.y", 2)
904 if not semver.larger_than(current_semver):
905 print('New branch version "' + branch_version +
906 '" must be strictly larger than the current "' + R8_DEV_BRANCH +
907 '"')
908 sys.exit(1)
Søren Gjesse4e5c6fe2019-11-05 17:17:43 +0100909
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200910 def make_branch(options):
911 with utils.TempDir() as temp:
912 subprocess.check_call(['git', 'clone', utils.REPO_SOURCE, temp])
913 with utils.ChangedWorkingDirectory(temp):
914 subprocess.check_call(
915 ['git', 'branch', branch_version, commithash])
Søren Gjesse4e5c6fe2019-11-05 17:17:43 +0100916
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200917 subprocess.check_call(['git', 'checkout', branch_version])
Søren Gjesse4e5c6fe2019-11-05 17:17:43 +0100918
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200919 # Rewrite the version, commit and validate.
920 old_version = 'main'
921 full_version = branch_version + '.0-dev'
922 version_prefix = 'public static final String LABEL = "'
923 sed(version_prefix + old_version, version_prefix + full_version,
924 R8_VERSION_FILE)
Søren Gjesse4e5c6fe2019-11-05 17:17:43 +0100925
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200926 subprocess.check_call(
927 ['git', 'commit', '-a', '-m',
928 'Version %s' % full_version])
Søren Gjesse4e5c6fe2019-11-05 17:17:43 +0100929
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200930 version_diff_output = subprocess.check_output(
931 ['git', 'diff', '%s..HEAD' % commithash])
Søren Gjesse4e5c6fe2019-11-05 17:17:43 +0100932
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200933 validate_version_change_diff(version_diff_output, old_version,
934 full_version)
Søren Gjesse4e5c6fe2019-11-05 17:17:43 +0100935
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200936 # Double check that we want to create a new release branch.
937 if not options.dry_run:
938 answer = input('Create new branch for %s [y/N]:' %
939 branch_version)
940 if answer != 'y':
941 print('Aborting new branch for %s' % branch_version)
942 sys.exit(1)
Søren Gjesse4e5c6fe2019-11-05 17:17:43 +0100943
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200944 maybe_check_call(
945 options,
946 ['git', 'push', 'origin',
947 'HEAD:%s' % branch_version])
948 maybe_tag(options, full_version)
Søren Gjesse4e5c6fe2019-11-05 17:17:43 +0100949
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200950 print(
951 'Updating tools/r8_release.py to make new dev releases on %s'
952 % branch_version)
Søren Gjesseddeb02e2019-11-06 15:48:07 +0100953
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200954 subprocess.check_call(
955 ['git', 'new-branch', 'update-release-script'])
Søren Gjesseddeb02e2019-11-06 15:48:07 +0100956
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200957 # Check this file for the setting of the current dev branch.
958 result = None
959 for line in open(THIS_FILE_RELATIVE, 'r'):
960 result = re.match(r"^R8_DEV_BRANCH = '(\d+).(\d+)'", line)
961 if result:
962 break
963 if not result or not result.group(1):
964 print('Failed to find version label in %s' %
965 THIS_FILE_RELATIVE)
966 sys.exit(1)
Søren Gjesseddeb02e2019-11-06 15:48:07 +0100967
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200968 # Update this file with the new dev branch.
969 sed(
970 "R8_DEV_BRANCH = '%s.%s" %
971 (result.group(1), result.group(2)),
972 "R8_DEV_BRANCH = '%s.%s" %
973 (str(semver.major), str(semver.minor)), THIS_FILE_RELATIVE)
Søren Gjesseddeb02e2019-11-06 15:48:07 +0100974
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200975 message = \
976 'Prepare %s for branch %s' % (THIS_FILE_RELATIVE, branch_version)
977 subprocess.check_call(['git', 'commit', '-a', '-m', message])
Søren Gjesseddeb02e2019-11-06 15:48:07 +0100978
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200979 branch_diff_output = subprocess.check_output(
980 ['git', 'diff', 'HEAD~'])
Søren Gjesseddeb02e2019-11-06 15:48:07 +0100981
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200982 validate_branch_change_diff(branch_diff_output, R8_DEV_BRANCH,
983 branch_version)
Søren Gjesseddeb02e2019-11-06 15:48:07 +0100984
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200985 maybe_check_call(options,
986 ['git', 'cl', 'upload', '-f', '-m', message])
Søren Gjesseddeb02e2019-11-06 15:48:07 +0100987
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200988 print('')
989 print('Make sure to send out the branch change CL for review.')
990 print('')
Søren Gjesse4e5c6fe2019-11-05 17:17:43 +0100991
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200992 return make_branch
Søren Gjesse4e5c6fe2019-11-05 17:17:43 +0100993
994
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200995def parse_options():
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200996 result = argparse.ArgumentParser(description='Release r8')
997 group = result.add_mutually_exclusive_group()
998 group.add_argument('--dev-release',
999 metavar=('<main hash>'),
1000 help='The hash to use for the new dev version of R8')
1001 group.add_argument(
1002 '--version',
1003 metavar=('<version>'),
1004 help=
1005 'The new version of R8 (e.g., 1.4.51) to release to selected channels')
1006 group.add_argument(
1007 '--desugar-library',
1008 nargs=2,
1009 metavar=('<version>', '<configuration hash>'),
1010 help='The new version of com.android.tools:desugar_jdk_libs')
1011 group.add_argument(
1012 '--update-desugar-library-in-studio',
1013 nargs=2,
1014 metavar=('<version>', '<configuration version>'),
1015 help='Update studio mirror of com.android.tools:desugar_jdk_libs')
1016 group.add_argument(
1017 '--new-dev-branch',
1018 nargs=2,
1019 metavar=('<version>', '<main hash>'),
1020 help='Create a new branch starting a version line (e.g. 2.0)')
1021 result.add_argument('--dev-pre-cherry-pick',
1022 metavar=('<main hash(s)>'),
1023 default=[],
1024 action='append',
1025 help='List of commits to cherry pick before doing full '
1026 'merge, mostly used for reverting cherry picks')
1027 result.add_argument('--no-sync',
1028 '--no_sync',
1029 default=False,
1030 action='store_true',
1031 help='Do not sync repos before uploading')
1032 result.add_argument('--bug',
1033 metavar=('<bug(s)>'),
1034 default=[],
1035 action='append',
1036 help='List of bugs for release version')
1037 result.add_argument('--no-bugs',
1038 default=False,
1039 action='store_true',
1040 help='Allow Studio release without specifying any bugs')
1041 result.add_argument(
1042 '--studio',
1043 metavar=('<path>'),
1044 help='Release for studio by setting the path to a studio '
1045 'checkout')
Søren Gjesse25e61912023-11-06 16:02:07 +01001046 result.add_argument('--studio-legacy-release',
1047 default=False,
1048 action='store_true',
1049 help='Allow Studio release using the legacy process')
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001050 result.add_argument('--aosp',
1051 metavar=('<path>'),
1052 help='Release for aosp by setting the path to the '
1053 'checkout')
1054 result.add_argument('--maven',
1055 default=False,
1056 action='store_true',
1057 help='Release to Google Maven')
1058 result.add_argument('--google3',
1059 default=False,
1060 action='store_true',
1061 help='Release for google 3')
1062 result.add_argument('--google3retrace',
1063 default=False,
1064 action='store_true',
1065 help='Release retrace for google 3')
1066 result.add_argument('--p4-client',
1067 default='update-r8',
1068 metavar=('<client name>'),
1069 help='P4 client name for google 3')
1070 result.add_argument(
1071 '--use-existing-work-branch',
1072 '--use_existing_work_branch',
1073 default=False,
1074 action='store_true',
1075 help='Use existing work branch/CL in aosp/studio/google3')
1076 result.add_argument('--delete-work-branch',
1077 '--delete_work_branch',
1078 default=False,
1079 action='store_true',
1080 help='Delete CL in google3')
1081 result.add_argument('--bypass-hooks',
1082 '--bypass_hooks',
1083 default=False,
1084 action='store_true',
1085 help="Bypass hooks when uploading")
1086 result.add_argument('--no-upload',
1087 '--no_upload',
1088 default=False,
1089 action='store_true',
1090 help="Don't upload for code review")
1091 result.add_argument(
1092 '--dry-run',
1093 default=False,
1094 action='store_true',
1095 help='Only perform non-commiting tasks and print others.')
1096 result.add_argument('--dry-run-output',
1097 '--dry_run_output',
1098 default=os.getcwd(),
1099 metavar=('<path>'),
1100 help='Location for dry run output.')
1101 args = result.parse_args()
1102 if (len(args.bug) > 0 and args.no_bugs):
1103 print("Use of '--bug' and '--no-bugs' are mutually exclusive")
1104 sys.exit(1)
Søren Gjesse5aa4a682022-08-26 09:17:27 +02001105
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001106 if (args.studio and args.version and not 'dev' in args.version and
1107 args.bug == [] and not args.no_bugs):
1108 print("When releasing a release version to Android Studio add the " +
1109 "list of bugs by using '--bug'")
1110 sys.exit(1)
Morten Krogh-Jespersena1396cd2019-10-02 16:14:40 +02001111
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001112 if args.version and not 'dev' in args.version and args.google3:
1113 print("WARNING: You should not roll a release version into google 3")
Morten Krogh-Jespersen52d3ab02019-10-29 08:57:06 +01001114
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001115 return args
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +02001116
1117
1118def main():
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001119 args = parse_options()
1120 targets_to_run = []
Ian Zerny59dfa4c2019-10-25 10:34:36 +02001121
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001122 if args.new_dev_branch:
1123 if args.google3 or args.studio or args.aosp:
1124 print('Cannot create a branch and roll at the same time.')
1125 sys.exit(1)
1126 targets_to_run.append(prepare_branch(args))
Søren Gjesse4e5c6fe2019-11-05 17:17:43 +01001127
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001128 if args.dev_release:
1129 if args.google3 or args.studio or args.aosp:
1130 print('Cannot create a dev release and roll at the same time.')
1131 sys.exit(1)
1132 targets_to_run.append(prepare_release(args))
Ian Zerny59dfa4c2019-10-25 10:34:36 +02001133
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001134 if (args.google3 or args.maven or (args.studio and not args.no_sync) or
1135 (args.desugar_library and not args.dry_run)):
1136 utils.check_gcert()
Søren Gjesseb8dec612019-10-10 09:15:43 +02001137
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001138 if args.google3:
1139 targets_to_run.append(prepare_google3(args))
1140 if args.google3retrace:
1141 targets_to_run.append(prepare_google3_retrace(args))
1142 if args.studio and not args.update_desugar_library_in_studio:
1143 targets_to_run.append(prepare_studio(args))
1144 if args.aosp:
1145 targets_to_run.append(prepare_aosp(args))
1146 if args.maven:
1147 targets_to_run.append(prepare_maven(args))
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +02001148
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001149 if args.desugar_library:
1150 targets_to_run.append(prepare_desugar_library(args))
Søren Gjesse975c5df2019-12-10 13:48:05 +01001151
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001152 if args.update_desugar_library_in_studio:
1153 if not args.studio:
1154 print("--studio required")
1155 sys.exit(1)
1156 if args.bug == []:
1157 print(
1158 "Update studio mirror of com.android.tools:desugar_jdk_libs " +
1159 "requires at least one bug by using '--bug'")
1160 sys.exit(1)
1161 targets_to_run.append(update_desugar_library_in_studio(args))
Søren Gjesse73355ec2020-06-29 09:19:44 +02001162
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001163 final_results = []
1164 for target_closure in targets_to_run:
1165 final_results.append(target_closure(args))
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +02001166
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001167 print('\n\n**************************************************************')
1168 print('PRINTING SUMMARY')
1169 print('**************************************************************\n\n')
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +02001170
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001171 for result in final_results:
1172 if result is not None:
1173 print(result)
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +02001174
1175
1176if __name__ == '__main__':
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +02001177 sys.exit(main())