blob: dfe4ed66abaa28acfa696a00fefb35214ebab4c2 [file] [log] [blame]
Søren Gjessee6893fd2022-04-26 13:34:51 +02001#!/usr/bin/env python3
2# Copyright (c) 2022, 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
Søren Gjessecd927782022-05-11 16:31:51 +02007from enum import Enum
Søren Gjessee6893fd2022-04-26 13:34:51 +02008import os
9from os.path import join
10import shutil
11import subprocess
12import sys
Søren Gjesse8d91e1a2022-09-01 16:58:43 +020013import urllib.request
Søren Gjessee6893fd2022-04-26 13:34:51 +020014
Søren Gjesse79298102022-08-23 16:25:41 +020015import gradle
Søren Gjessee6893fd2022-04-26 13:34:51 +020016import utils
17import create_maven_release
Søren Gjesse79298102022-08-23 16:25:41 +020018import archive_desugar_jdk_libs
Søren Gjessee6893fd2022-04-26 13:34:51 +020019
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020020
Søren Gjesse2b047692022-08-19 16:34:38 +020021class Variant(Enum):
Søren Gjessecd927782022-05-11 16:31:51 +020022 jdk8 = 'jdk8'
Søren Gjesse2b047692022-08-19 16:34:38 +020023 jdk11_legacy = 'jdk11_legacy'
24 jdk11_minimal = 'jdk11_minimal'
25 jdk11 = 'jdk11'
26 jdk11_nio = 'jdk11_nio'
Søren Gjessecd927782022-05-11 16:31:51 +020027
28 def __str__(self):
29 return self.value
30
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020031
Søren Gjessee6893fd2022-04-26 13:34:51 +020032def parse_options():
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020033 parser = argparse.ArgumentParser(
34 description=
35 'Local desugared library repository for desugared library configurations'
36 )
37 parser.add_argument('--repo-root',
38 '--repo_root',
39 default='/tmp/repo',
40 metavar=('<path>'),
41 help='Location for Maven repository.')
42 parser.add_argument(
43 '--clear-repo',
44 '--clear_repo',
45 default=False,
46 action='store_true',
47 help='Clear the Maven repository so it only has one version present')
48 parser.add_argument('--variant', type=Variant, choices=list(Variant))
49 parser.add_argument(
50 '--desugar-jdk-libs-checkout',
51 '--desugar_jdk_libs_checkout',
52 default=None,
53 metavar=('<path>'),
54 help='Use existing checkout of github.com/google/desugar_jdk_libs.')
55 parser.add_argument(
56 '--desugar-jdk-libs-revision',
57 '--desugar_jdk_libs_revision',
58 default=None,
59 metavar=('<revision>'),
60 help='Revision of github.com/google/desugar_jdk_libs to use.')
61 parser.add_argument(
62 '--release-version',
63 '--release_version',
64 metavar=('<version>'),
65 help=
66 'The desugared library release version to use. This will pull from the archived releases'
67 )
68 args = parser.parse_args()
69 return args
70
Søren Gjessee6893fd2022-04-26 13:34:51 +020071
72def jar_or_pom_file(unzip_dir, artifact, version, extension):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020073 return join(unzip_dir, 'com', 'android', 'tools', artifact, version,
74 artifact + '-' + version + '.' + extension)
75
Søren Gjessee6893fd2022-04-26 13:34:51 +020076
77def jar_file(unzip_dir, artifact, version):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020078 return jar_or_pom_file(unzip_dir, artifact, version, 'jar')
79
Søren Gjessee6893fd2022-04-26 13:34:51 +020080
81def pom_file(unzip_dir, artifact, version):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020082 return jar_or_pom_file(unzip_dir, artifact, version, 'pom')
83
Søren Gjessee6893fd2022-04-26 13:34:51 +020084
Søren Gjesse2b047692022-08-19 16:34:38 +020085def run(args):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020086 artifact = None
87 configuration_artifact = None
88 configuration = None
89 conversions = None
90 implementation = None
Søren Gjesse1da57172024-07-31 11:10:37 +020091 version_file_name = None
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020092 implementation_build_target = None
93 implementation_maven_zip = None
94 release_archive_location = None
95 match args.variant:
96 case Variant.jdk8:
97 artifact = 'desugar_jdk_libs'
98 configuration_artifact = 'desugar_jdk_libs_configuration'
99 configuration = utils.DESUGAR_CONFIGURATION
100 conversions = utils.LIBRARY_DESUGAR_CONVERSIONS_LEGACY_ZIP
101 implementation = utils.DESUGAR_IMPLEMENTATION
Søren Gjesse1da57172024-07-31 11:10:37 +0200102 version_file_name = 'VERSION.txt'
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200103 implementation_build_target = ':maven_release'
104 implementation_maven_zip = 'desugar_jdk_libs.zip'
105 release_archive_location = 'desugar_jdk_libs'
106 case Variant.jdk11_legacy:
107 artifact = 'desugar_jdk_libs'
108 configuration_artifact = 'desugar_jdk_libs_configuration'
109 configuration = utils.DESUGAR_CONFIGURATION_JDK11_LEGACY
110 conversions = utils.LIBRARY_DESUGAR_CONVERSIONS_LEGACY_ZIP
111 implementation = utils.DESUGAR_IMPLEMENTATION_JDK11
Søren Gjesse1da57172024-07-31 11:10:37 +0200112 version_file_name = 'VERSION_JDK11_LEGACY.txt'
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200113 implementation_build_target = ':maven_release_jdk11_legacy'
114 implementation_maven_zip = 'desugar_jdk_libs_jdk11_legacy.zip'
115 release_archive_location = 'desugar_jdk_libs'
116 case Variant.jdk11_minimal:
117 artifact = 'desugar_jdk_libs_minimal'
118 configuration_artifact = 'desugar_jdk_libs_configuration_minimal'
119 configuration = utils.DESUGAR_CONFIGURATION_JDK11_MINIMAL
120 conversions = utils.LIBRARY_DESUGAR_CONVERSIONS_ZIP
121 implementation = utils.DESUGAR_IMPLEMENTATION_JDK11
Søren Gjesse1da57172024-07-31 11:10:37 +0200122 version_file_name = 'VERSION_JDK11_MINIMAL.txt'
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200123 implementation_build_target = ':maven_release_jdk11_minimal'
124 implementation_maven_zip = 'desugar_jdk_libs_jdk11_minimal.zip'
125 release_archive_location = 'desugar_jdk_libs_minimal'
126 case Variant.jdk11:
127 artifact = 'desugar_jdk_libs'
128 configuration_artifact = 'desugar_jdk_libs_configuration'
129 configuration = utils.DESUGAR_CONFIGURATION_JDK11
130 conversions = utils.LIBRARY_DESUGAR_CONVERSIONS_ZIP
131 implementation = utils.DESUGAR_IMPLEMENTATION_JDK11
Søren Gjesse1da57172024-07-31 11:10:37 +0200132 version_file_name = 'VERSION_JDK11.txt'
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200133 implementation_build_target = ':maven_release_jdk11'
134 implementation_maven_zip = 'desugar_jdk_libs_jdk11.zip'
135 release_archive_location = 'desugar_jdk_libs'
136 case Variant.jdk11_nio:
137 artifact = 'desugar_jdk_libs_nio'
138 configuration_artifact = 'desugar_jdk_libs_configuration_nio'
139 configuration = utils.DESUGAR_CONFIGURATION_JDK11_NIO
140 conversions = utils.LIBRARY_DESUGAR_CONVERSIONS_ZIP
141 implementation = utils.DESUGAR_IMPLEMENTATION_JDK11
Søren Gjesse1da57172024-07-31 11:10:37 +0200142 version_file_name = 'VERSION_JDK11_NIO.txt'
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200143 implementation_build_target = ':maven_release_jdk11_nio'
144 implementation_maven_zip = 'desugar_jdk_libs_jdk11_nio.zip'
145 release_archive_location = 'desugar_jdk_libs_nio'
146 implementation_build_output = join('bazel-bin', implementation_maven_zip)
147 gradle.RunGradle([utils.GRADLE_TASK_R8])
148 with utils.TempDir() as tmp_dir:
149 (name,
150 configuration_version) = utils.desugar_configuration_name_and_version(
151 configuration, False)
152 if (args.release_version != None and
153 args.release_version != configuration_version):
154 raise Exception(
155 'Configuration version %s is different for specified version %s'
156 % (configuration_version, version))
157 version = configuration_version
158 print("Name: %s" % name)
159 print("Version: %s" % version)
160 # Checkout desugar_jdk_libs from GitHub
161 use_existing_checkout = args.desugar_jdk_libs_checkout != None
162 checkout_dir = (args.desugar_jdk_libs_checkout if use_existing_checkout
163 else join(tmp_dir, 'desugar_jdk_libs'))
164 if (not args.release_version and not use_existing_checkout):
165 subprocess.check_call([
166 'git', 'clone',
167 'https://github.com/google/desugar_jdk_libs.git', checkout_dir
168 ])
169 if (args.desugar_jdk_libs_revision):
170 subprocess.check_call([
171 'git', '-C', checkout_dir, 'checkout',
172 args.desugar_jdk_libs_revision
173 ])
Søren Gjesse1da57172024-07-31 11:10:37 +0200174 if not args.release_version:
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200175 with utils.ChangedWorkingDirectory(checkout_dir):
Søren Gjesse1da57172024-07-31 11:10:37 +0200176 with open(version_file_name) as version_file:
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200177 version_file_lines = version_file.readlines()
178 for line in version_file_lines:
179 if not line.startswith('#'):
180 desugar_jdk_libs_version = line.strip()
181 if (version != desugar_jdk_libs_version):
182 raise Exception(
183 "Version mismatch. Configuration has version '"
Søren Gjesse1da57172024-07-31 11:10:37 +0200184 + version
185 + "', and desugar_jdk_libs has version '"
186 + desugar_jdk_libs_version
187 + " in '" + version_file_name
188 + "'. If testing a new version use "
189 + "--desugar-jdk-libs-checkout with updated "
190 + "VERSION_*.txt and DEPENDENCIES_*.txt files.")
Søren Gjessee6893fd2022-04-26 13:34:51 +0200191
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200192 # Build desugared library configuration.
193 print("Building desugared library configuration " + version)
194 maven_zip = join(tmp_dir, 'desugar_configuration.zip')
195 create_maven_release.generate_desugar_configuration_maven_zip(
196 maven_zip, configuration, implementation, conversions)
197 unzip_dir = join(tmp_dir, 'desugar_jdk_libs_configuration_unzipped')
198 cmd = ['unzip', '-q', maven_zip, '-d', unzip_dir]
199 subprocess.check_call(cmd)
200 cmd = [
201 'mvn', 'deploy:deploy-file', '-Durl=file:' + args.repo_root,
202 '-DrepositoryId=someName',
203 '-Dfile=' + jar_file(unzip_dir, configuration_artifact, version),
204 '-DpomFile=' + pom_file(unzip_dir, configuration_artifact, version)
205 ]
206 subprocess.check_call(cmd)
Søren Gjessee6893fd2022-04-26 13:34:51 +0200207
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200208 undesugared_if_needed = None
209 if not args.release_version:
210 # Build desugared library.
211 print("Building desugared library " + version)
212 with utils.ChangedWorkingDirectory(checkout_dir):
213 subprocess.check_call([
214 'bazel', '--bazelrc=/dev/null', 'build',
215 '--spawn_strategy=local', '--verbose_failures',
216 implementation_build_target
217 ])
Søren Gjesse79298102022-08-23 16:25:41 +0200218
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200219 # Undesugar desugared library if needed.
220 undesugared_if_needed = join(checkout_dir,
221 implementation_build_output)
222 if (args.variant == Variant.jdk11_minimal or
223 args.variant == Variant.jdk11 or
224 args.variant == Variant.jdk11_nio):
225 undesugared_if_needed = join(tmp_dir, 'undesugared.zip')
226 archive_desugar_jdk_libs.Undesugar(
227 str(args.variant),
228 join(checkout_dir, implementation_build_output), version,
229 undesugared_if_needed)
230 else:
231 # Download the already built and undesugared library from release archive.
232 undesugared_if_needed = join(tmp_dir, implementation_maven_zip)
233 urllib.request.urlretrieve(
234 ('https://storage.googleapis.com/r8-releases/raw/%s/%s/%s' %
235 (release_archive_location, version, implementation_maven_zip)),
236 undesugared_if_needed)
Søren Gjesse79298102022-08-23 16:25:41 +0200237
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200238 unzip_dir = join(tmp_dir, 'desugar_jdk_libs_unzipped')
239 cmd = ['unzip', '-q', undesugared_if_needed, '-d', unzip_dir]
240 subprocess.check_call(cmd)
241 cmd = [
242 'mvn', 'deploy:deploy-file', '-Durl=file:' + args.repo_root,
243 '-DrepositoryId=someName',
244 '-Dfile=' + jar_file(unzip_dir, artifact, version),
245 '-DpomFile=' + pom_file(unzip_dir, artifact, version)
246 ]
247 subprocess.check_call(cmd)
Søren Gjessee6893fd2022-04-26 13:34:51 +0200248
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200249 print()
250 print("Artifacts:")
251 print(" com.android.tools:%s:%s" % (configuration_artifact, version))
252 print(" com.android.tools:%s:%s" % (artifact, version))
253 print()
254 print("deployed to Maven repository at " + args.repo_root + ".")
255 print()
Søren Gjessea28c4612023-11-01 14:55:27 +0100256 print("For Kotlin Script add")
257 print()
258 print(" maven(url = \"file:///tmp/repo\")")
259 print()
260 print(
261 "to dependencyResolutionManagement.repositories in settings.gradle.kts, and use"
262 )
263 print(
264 'the "changing" property of the coreLibraryDesugaring dependency:')
265 print()
266 print(" coreLibraryDesugaring('com.android.tools:%s:%s') {" %
267 (artifact, version))
268 print(" isChanging = true")
269 print(" }")
270 print()
271
Søren Gjesse3d3a9202023-11-02 11:04:28 +0100272 print("For Groovy add")
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200273 print()
274 print(" maven {")
275 print(" url uri('file://" + args.repo_root + "')")
276 print(" }")
277 print()
278 print(
279 "to dependencyResolutionManagement.repositories in settings.gradle, and use"
280 )
281 print(
282 'the "changing" property of the coreLibraryDesugaring dependency:')
283 print()
284 print(" coreLibraryDesugaring('com.android.tools:%s:%s') {" %
285 (artifact, version))
286 print(" changing = true")
287 print(" }")
288 print()
289 print(
Søren Gjessea28c4612023-11-01 14:55:27 +0100290 'If not using the "changing" property remember to run gradle with ' +
291 "--refresh-dependencies (./gradlew --refresh-dependencies ...) " +
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200292 "to ensure the cache is not used when the same version is published."
293 + "multiple times.")
294
Søren Gjessee6893fd2022-04-26 13:34:51 +0200295
Søren Gjesse2b047692022-08-19 16:34:38 +0200296def main():
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200297 args = parse_options()
298 if args.desugar_jdk_libs_checkout and args.release_version:
299 raise Exception(
300 'Options --desugar-jdk-libs-checkout and --release-version are mutually exclusive'
301 )
302 if args.desugar_jdk_libs_revision and args.release_version:
303 raise Exception(
304 'Options --desugar-jdk-libs-revision and --release-version are mutually exclusive'
305 )
306 if args.desugar_jdk_libs_checkout and args.desugar_jdk_libs_revision:
307 raise Exception(
308 'Options --desugar-jdk-libs-checkout and --desugar-jdk-libs-revision are mutually exclusive'
309 )
310 if args.clear_repo:
311 shutil.rmtree(args.repo_root, ignore_errors=True)
312 utils.makedirs_if_needed(args.repo_root)
313 if (args.variant):
314 run(args)
315 else:
316 for v in Variant:
317 args.variant = v
318 run(args)
319
Søren Gjesse2b047692022-08-19 16:34:38 +0200320
Søren Gjessee6893fd2022-04-26 13:34:51 +0200321if __name__ == '__main__':
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200322 sys.exit(main())