blob: 36268c42f5fdd0f1509c7fa3450647af21e86892 [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
91 version_file = None
92 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
102 version_file = 'VERSION.txt'
103 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
112 version_file = 'VERSION_JDK11_LEGACY.txt'
113 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
122 version_file = 'VERSION_JDK11_MINIMAL.txt'
123 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
132 version_file = 'VERSION_JDK11.txt'
133 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
142 version_file = 'VERSION_JDK11_NIO.txt'
143 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 ])
174 with utils.ChangedWorkingDirectory(checkout_dir):
175 with open(version_file) as version_file:
176 version_file_lines = version_file.readlines()
177 for line in version_file_lines:
178 if not line.startswith('#'):
179 desugar_jdk_libs_version = line.strip()
180 if (version != desugar_jdk_libs_version):
181 raise Exception(
182 "Version mismatch. Configuration has version '"
183 + version +
184 "', and desugar_jdk_libs has version '" +
185 desugar_jdk_libs_version + "'")
Søren Gjessee6893fd2022-04-26 13:34:51 +0200186
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200187 # Build desugared library configuration.
188 print("Building desugared library configuration " + version)
189 maven_zip = join(tmp_dir, 'desugar_configuration.zip')
190 create_maven_release.generate_desugar_configuration_maven_zip(
191 maven_zip, configuration, implementation, conversions)
192 unzip_dir = join(tmp_dir, 'desugar_jdk_libs_configuration_unzipped')
193 cmd = ['unzip', '-q', maven_zip, '-d', unzip_dir]
194 subprocess.check_call(cmd)
195 cmd = [
196 'mvn', 'deploy:deploy-file', '-Durl=file:' + args.repo_root,
197 '-DrepositoryId=someName',
198 '-Dfile=' + jar_file(unzip_dir, configuration_artifact, version),
199 '-DpomFile=' + pom_file(unzip_dir, configuration_artifact, version)
200 ]
201 subprocess.check_call(cmd)
Søren Gjessee6893fd2022-04-26 13:34:51 +0200202
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200203 undesugared_if_needed = None
204 if not args.release_version:
205 # Build desugared library.
206 print("Building desugared library " + version)
207 with utils.ChangedWorkingDirectory(checkout_dir):
208 subprocess.check_call([
209 'bazel', '--bazelrc=/dev/null', 'build',
210 '--spawn_strategy=local', '--verbose_failures',
211 implementation_build_target
212 ])
Søren Gjesse79298102022-08-23 16:25:41 +0200213
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200214 # Undesugar desugared library if needed.
215 undesugared_if_needed = join(checkout_dir,
216 implementation_build_output)
217 if (args.variant == Variant.jdk11_minimal or
218 args.variant == Variant.jdk11 or
219 args.variant == Variant.jdk11_nio):
220 undesugared_if_needed = join(tmp_dir, 'undesugared.zip')
221 archive_desugar_jdk_libs.Undesugar(
222 str(args.variant),
223 join(checkout_dir, implementation_build_output), version,
224 undesugared_if_needed)
225 else:
226 # Download the already built and undesugared library from release archive.
227 undesugared_if_needed = join(tmp_dir, implementation_maven_zip)
228 urllib.request.urlretrieve(
229 ('https://storage.googleapis.com/r8-releases/raw/%s/%s/%s' %
230 (release_archive_location, version, implementation_maven_zip)),
231 undesugared_if_needed)
Søren Gjesse79298102022-08-23 16:25:41 +0200232
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200233 unzip_dir = join(tmp_dir, 'desugar_jdk_libs_unzipped')
234 cmd = ['unzip', '-q', undesugared_if_needed, '-d', unzip_dir]
235 subprocess.check_call(cmd)
236 cmd = [
237 'mvn', 'deploy:deploy-file', '-Durl=file:' + args.repo_root,
238 '-DrepositoryId=someName',
239 '-Dfile=' + jar_file(unzip_dir, artifact, version),
240 '-DpomFile=' + pom_file(unzip_dir, artifact, version)
241 ]
242 subprocess.check_call(cmd)
Søren Gjessee6893fd2022-04-26 13:34:51 +0200243
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200244 print()
245 print("Artifacts:")
246 print(" com.android.tools:%s:%s" % (configuration_artifact, version))
247 print(" com.android.tools:%s:%s" % (artifact, version))
248 print()
249 print("deployed to Maven repository at " + args.repo_root + ".")
250 print()
Søren Gjessea28c4612023-11-01 14:55:27 +0100251 print("For Kotlin Script add")
252 print()
253 print(" maven(url = \"file:///tmp/repo\")")
254 print()
255 print(
256 "to dependencyResolutionManagement.repositories in settings.gradle.kts, and use"
257 )
258 print(
259 'the "changing" property of the coreLibraryDesugaring dependency:')
260 print()
261 print(" coreLibraryDesugaring('com.android.tools:%s:%s') {" %
262 (artifact, version))
263 print(" isChanging = true")
264 print(" }")
265 print()
266
Søren Gjesse3d3a9202023-11-02 11:04:28 +0100267 print("For Groovy add")
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200268 print()
269 print(" maven {")
270 print(" url uri('file://" + args.repo_root + "')")
271 print(" }")
272 print()
273 print(
274 "to dependencyResolutionManagement.repositories in settings.gradle, and use"
275 )
276 print(
277 'the "changing" property of the coreLibraryDesugaring dependency:')
278 print()
279 print(" coreLibraryDesugaring('com.android.tools:%s:%s') {" %
280 (artifact, version))
281 print(" changing = true")
282 print(" }")
283 print()
284 print(
Søren Gjessea28c4612023-11-01 14:55:27 +0100285 'If not using the "changing" property remember to run gradle with ' +
286 "--refresh-dependencies (./gradlew --refresh-dependencies ...) " +
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200287 "to ensure the cache is not used when the same version is published."
288 + "multiple times.")
289
Søren Gjessee6893fd2022-04-26 13:34:51 +0200290
Søren Gjesse2b047692022-08-19 16:34:38 +0200291def main():
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200292 args = parse_options()
293 if args.desugar_jdk_libs_checkout and args.release_version:
294 raise Exception(
295 'Options --desugar-jdk-libs-checkout and --release-version are mutually exclusive'
296 )
297 if args.desugar_jdk_libs_revision and args.release_version:
298 raise Exception(
299 'Options --desugar-jdk-libs-revision and --release-version are mutually exclusive'
300 )
301 if args.desugar_jdk_libs_checkout and args.desugar_jdk_libs_revision:
302 raise Exception(
303 'Options --desugar-jdk-libs-checkout and --desugar-jdk-libs-revision are mutually exclusive'
304 )
305 if args.clear_repo:
306 shutil.rmtree(args.repo_root, ignore_errors=True)
307 utils.makedirs_if_needed(args.repo_root)
308 if (args.variant):
309 run(args)
310 else:
311 for v in Variant:
312 args.variant = v
313 run(args)
314
Søren Gjesse2b047692022-08-19 16:34:38 +0200315
Søren Gjessee6893fd2022-04-26 13:34:51 +0200316if __name__ == '__main__':
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200317 sys.exit(main())