blob: 08cb6c11e2fa390a7ea89adde6d5de8bf8dabca3 [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
Søren Gjesse2b047692022-08-19 16:34:38 +020020class Variant(Enum):
Søren Gjessecd927782022-05-11 16:31:51 +020021 jdk8 = 'jdk8'
Søren Gjesse2b047692022-08-19 16:34:38 +020022 jdk11_legacy = 'jdk11_legacy'
23 jdk11_minimal = 'jdk11_minimal'
24 jdk11 = 'jdk11'
25 jdk11_nio = 'jdk11_nio'
Søren Gjessecd927782022-05-11 16:31:51 +020026
27 def __str__(self):
28 return self.value
29
Søren Gjessee6893fd2022-04-26 13:34:51 +020030def parse_options():
Søren Gjessecd927782022-05-11 16:31:51 +020031 parser = argparse.ArgumentParser(
32 description='Local desugared library repository for desugared library configurations')
33 parser.add_argument('--repo-root', '--repo_root',
Søren Gjessee6893fd2022-04-26 13:34:51 +020034 default='/tmp/repo',
35 metavar=('<path>'),
36 help='Location for Maven repository.')
Søren Gjessecd927782022-05-11 16:31:51 +020037 parser.add_argument('--clear-repo', '--clear_repo',
38 default=False,
39 action='store_true',
40 help='Clear the Maven repository so it only has one version present')
Søren Gjesse2b047692022-08-19 16:34:38 +020041 parser.add_argument('--variant', type=Variant, choices=list(Variant))
Søren Gjessecd927782022-05-11 16:31:51 +020042 parser.add_argument('--desugar-jdk-libs-checkout', '--desugar_jdk_libs_checkout',
Søren Gjesse8ed14452022-05-10 11:29:21 +020043 default=None,
44 metavar=('<path>'),
45 help='Use existing checkout of github.com/google/desugar_jdk_libs.')
Søren Gjesse8d91e1a2022-09-01 16:58:43 +020046 parser.add_argument('--desugar-jdk-libs-revision', '--desugar_jdk_libs_revision',
47 default=None,
48 metavar=('<revision>'),
49 help='Revision of github.com/google/desugar_jdk_libs to use.')
50 parser.add_argument('--release-version', '--release_version',
51 metavar=('<version>'),
52 help='The desugared library release version to use. This will pull from the archived releases')
Søren Gjessecd927782022-05-11 16:31:51 +020053 args = parser.parse_args()
Søren Gjessee6893fd2022-04-26 13:34:51 +020054 return args
55
56def jar_or_pom_file(unzip_dir, artifact, version, extension):
57 return join(
58 unzip_dir,
59 'com',
60 'android',
61 'tools',
62 artifact,
63 version,
64 artifact + '-' + version + '.' + extension)
65
66def jar_file(unzip_dir, artifact, version):
67 return jar_or_pom_file(unzip_dir, artifact, version, 'jar')
68
69def pom_file(unzip_dir, artifact, version):
70 return jar_or_pom_file(unzip_dir, artifact, version, 'pom')
71
Søren Gjesse2b047692022-08-19 16:34:38 +020072def run(args):
73 artifact = None
74 configuration_artifact = None
75 configuration = None
76 conversions = None
77 implementation = None
78 version_file = None
79 implementation_build_target = None
Søren Gjesse8d91e1a2022-09-01 16:58:43 +020080 implementation_maven_zip = None
81 release_archive_location = None
Søren Gjesse2b047692022-08-19 16:34:38 +020082 match args.variant:
83 case Variant.jdk8:
84 artifact = 'desugar_jdk_libs'
85 configuration_artifact = 'desugar_jdk_libs_configuration'
86 configuration = utils.DESUGAR_CONFIGURATION
87 conversions = utils.LIBRARY_DESUGAR_CONVERSIONS_LEGACY_ZIP
88 implementation = utils.DESUGAR_IMPLEMENTATION
89 version_file = 'VERSION.txt'
90 implementation_build_target = ':maven_release'
Søren Gjesse8d91e1a2022-09-01 16:58:43 +020091 implementation_maven_zip = 'desugar_jdk_libs.zip'
92 release_archive_location = 'desugar_jdk_libs'
Søren Gjesse2b047692022-08-19 16:34:38 +020093 case Variant.jdk11_legacy:
94 artifact = 'desugar_jdk_libs'
95 configuration_artifact = 'desugar_jdk_libs_configuration'
96 configuration = utils.DESUGAR_CONFIGURATION_JDK11_LEGACY
97 conversions = utils.LIBRARY_DESUGAR_CONVERSIONS_LEGACY_ZIP
98 implementation = utils.DESUGAR_IMPLEMENTATION_JDK11
99 version_file = 'VERSION_JDK11_LEGACY.txt'
100 implementation_build_target = ':maven_release_jdk11_legacy'
Søren Gjesse8d91e1a2022-09-01 16:58:43 +0200101 implementation_maven_zip = 'desugar_jdk_libs_jdk11_legacy.zip'
102 release_archive_location = 'desugar_jdk_libs'
Søren Gjesse2b047692022-08-19 16:34:38 +0200103 case Variant.jdk11_minimal:
104 artifact = 'desugar_jdk_libs_minimal'
105 configuration_artifact = 'desugar_jdk_libs_configuration_minimal'
106 configuration = utils.DESUGAR_CONFIGURATION_JDK11_MINIMAL
107 conversions = utils.LIBRARY_DESUGAR_CONVERSIONS_ZIP
108 implementation = utils.DESUGAR_IMPLEMENTATION_JDK11
109 version_file = 'VERSION_JDK11_MINIMAL.txt'
110 implementation_build_target = ':maven_release_jdk11_minimal'
Søren Gjesse8d91e1a2022-09-01 16:58:43 +0200111 implementation_maven_zip = 'desugar_jdk_libs_jdk11_minimal.zip'
112 release_archive_location = 'desugar_jdk_libs_minimal'
Søren Gjesse2b047692022-08-19 16:34:38 +0200113 case Variant.jdk11:
114 artifact = 'desugar_jdk_libs'
115 configuration_artifact = 'desugar_jdk_libs_configuration'
116 configuration = utils.DESUGAR_CONFIGURATION_JDK11
117 conversions = utils.LIBRARY_DESUGAR_CONVERSIONS_ZIP
118 implementation = utils.DESUGAR_IMPLEMENTATION_JDK11
119 version_file = 'VERSION_JDK11.txt'
120 implementation_build_target = ':maven_release_jdk11'
Søren Gjesse8d91e1a2022-09-01 16:58:43 +0200121 implementation_maven_zip = 'desugar_jdk_libs_jdk11.zip'
122 release_archive_location = 'desugar_jdk_libs'
Søren Gjesse2b047692022-08-19 16:34:38 +0200123 case Variant.jdk11_nio:
124 artifact = 'desugar_jdk_libs_nio'
125 configuration_artifact = 'desugar_jdk_libs_configuration_nio'
126 configuration = utils.DESUGAR_CONFIGURATION_JDK11_NIO
127 conversions = utils.LIBRARY_DESUGAR_CONVERSIONS_ZIP
128 implementation = utils.DESUGAR_IMPLEMENTATION_JDK11
129 version_file = 'VERSION_JDK11_NIO.txt'
130 implementation_build_target = ':maven_release_jdk11_nio'
Søren Gjesse8d91e1a2022-09-01 16:58:43 +0200131 implementation_maven_zip = 'desugar_jdk_libs_jdk11_nio.zip'
132 release_archive_location = 'desugar_jdk_libs_nio'
133 implementation_build_output = join('bazel-bin', implementation_maven_zip)
Søren Gjesse79298102022-08-23 16:25:41 +0200134 gradle.RunGradle([utils.R8])
Søren Gjesse8d91e1a2022-09-01 16:58:43 +0200135 with utils.TempDir() as tmp_dir:
136 (name, configuration_version) = utils.desugar_configuration_name_and_version(configuration, False)
137 if (args.release_version != None and args.release_version != configuration_version):
138 raise Exception(
139 'Configuration version %s is different for specified version %s'
140 % (configuration_version, version))
141 version = configuration_version
142 print("Name: %s" % name)
143 print("Version: %s" % version)
Søren Gjessee6893fd2022-04-26 13:34:51 +0200144 # Checkout desugar_jdk_libs from GitHub
Søren Gjesse8ed14452022-05-10 11:29:21 +0200145 use_existing_checkout = args.desugar_jdk_libs_checkout != None
Søren Gjessecd927782022-05-11 16:31:51 +0200146 checkout_dir = (args.desugar_jdk_libs_checkout
147 if use_existing_checkout
148 else join(tmp_dir, 'desugar_jdk_libs'))
Søren Gjesse8d91e1a2022-09-01 16:58:43 +0200149 if (not args.release_version and not use_existing_checkout):
150 subprocess.check_call(
151 ['git', 'clone', 'https://github.com/google/desugar_jdk_libs.git', checkout_dir])
152 if (args.desugar_jdk_libs_revision):
153 subprocess.check_call(
154 ['git', '-C', checkout_dir, 'checkout', args.desugar_jdk_libs_revision])
155 with utils.ChangedWorkingDirectory(checkout_dir):
156 with open(version_file) as version_file:
157 version_file_lines = version_file.readlines()
158 for line in version_file_lines:
159 if not line.startswith('#'):
160 desugar_jdk_libs_version = line.strip()
161 if (version != desugar_jdk_libs_version):
162 raise Exception(
163 "Version mismatch. Configuration has version '"
164 + version
165 + "', and desugar_jdk_libs has version '"
166 + desugar_jdk_libs_version
167 + "'")
Søren Gjessee6893fd2022-04-26 13:34:51 +0200168
169 # Build desugared library configuration.
170 print("Building desugared library configuration " + version)
171 maven_zip = join(tmp_dir, 'desugar_configuration.zip')
172 create_maven_release.generate_desugar_configuration_maven_zip(
173 maven_zip,
Søren Gjessecd927782022-05-11 16:31:51 +0200174 configuration,
Søren Gjesse2b047692022-08-19 16:34:38 +0200175 implementation,
176 conversions)
Søren Gjessee6893fd2022-04-26 13:34:51 +0200177 unzip_dir = join(tmp_dir, 'desugar_jdk_libs_configuration_unzipped')
178 cmd = ['unzip', '-q', maven_zip, '-d', unzip_dir]
Søren Gjesse2b047692022-08-19 16:34:38 +0200179 subprocess.check_call(cmd)
Søren Gjessee6893fd2022-04-26 13:34:51 +0200180 cmd = [
181 'mvn',
182 'deploy:deploy-file',
183 '-Durl=file:' + args.repo_root,
184 '-DrepositoryId=someName',
Søren Gjesse2b047692022-08-19 16:34:38 +0200185 '-Dfile=' + jar_file(unzip_dir, configuration_artifact, version),
186 '-DpomFile=' + pom_file(unzip_dir, configuration_artifact, version)]
187 subprocess.check_call(cmd)
Søren Gjessee6893fd2022-04-26 13:34:51 +0200188
Søren Gjesse8d91e1a2022-09-01 16:58:43 +0200189 undesugared_if_needed = None
190 if not args.release_version:
191 # Build desugared library.
192 print("Building desugared library " + version)
193 with utils.ChangedWorkingDirectory(checkout_dir):
194 subprocess.check_call([
195 'bazel',
196 '--bazelrc=/dev/null',
197 'build',
198 '--spawn_strategy=local',
199 '--verbose_failures',
200 implementation_build_target])
Søren Gjesse79298102022-08-23 16:25:41 +0200201
Søren Gjesse8d91e1a2022-09-01 16:58:43 +0200202 # Undesugar desugared library if needed.
203 undesugared_if_needed = join(checkout_dir, implementation_build_output)
204 if (args.variant == Variant.jdk11_minimal
205 or args.variant == Variant.jdk11
206 or args.variant == Variant.jdk11_nio):
207 undesugared_if_needed = join(tmp_dir, 'undesugared.zip')
208 archive_desugar_jdk_libs.Undesugar(
209 str(args.variant),
210 join(checkout_dir, implementation_build_output),
211 version,
212 undesugared_if_needed)
213 else:
214 # Download the already built and undesugared library from release archive.
215 undesugared_if_needed = join(tmp_dir, implementation_maven_zip)
216 urllib.request.urlretrieve(
217 ('https://storage.googleapis.com/r8-releases/raw/%s/%s/%s'
218 % (release_archive_location, version, implementation_maven_zip)),
Søren Gjesse79298102022-08-23 16:25:41 +0200219 undesugared_if_needed)
220
Søren Gjessee6893fd2022-04-26 13:34:51 +0200221 unzip_dir = join(tmp_dir, 'desugar_jdk_libs_unzipped')
222 cmd = [
223 'unzip',
224 '-q',
Søren Gjesse79298102022-08-23 16:25:41 +0200225 undesugared_if_needed,
Søren Gjessee6893fd2022-04-26 13:34:51 +0200226 '-d',
227 unzip_dir]
Søren Gjesse2b047692022-08-19 16:34:38 +0200228 subprocess.check_call(cmd)
Søren Gjessee6893fd2022-04-26 13:34:51 +0200229 cmd = [
230 'mvn',
231 'deploy:deploy-file',
232 '-Durl=file:' + args.repo_root,
233 '-DrepositoryId=someName',
Søren Gjesse2b047692022-08-19 16:34:38 +0200234 '-Dfile=' + jar_file(unzip_dir, artifact, version),
235 '-DpomFile=' + pom_file(unzip_dir, artifact, version)]
236 subprocess.check_call(cmd)
Søren Gjessee6893fd2022-04-26 13:34:51 +0200237
238 print()
239 print("Artifacts:")
Søren Gjesse2b047692022-08-19 16:34:38 +0200240 print(" com.android.tools:%s:%s" % (configuration_artifact, version))
241 print(" com.android.tools:%s:%s" % (artifact, version))
Søren Gjessee6893fd2022-04-26 13:34:51 +0200242 print()
243 print("deployed to Maven repository at " + args.repo_root + ".")
244 print()
245 print("Add")
246 print()
247 print(" maven {")
248 print(" url uri('file://" + args.repo_root + "')")
249 print(" }")
250 print()
Søren Gjessecd927782022-05-11 16:31:51 +0200251 print("to dependencyResolutionManagement.repositories in settings.gradle, and use")
252 print('the "changing" property of the coreLibraryDesugaring dependency:')
Søren Gjessee6893fd2022-04-26 13:34:51 +0200253 print()
Søren Gjesse2b047692022-08-19 16:34:38 +0200254 print(" coreLibraryDesugaring('com.android.tools:%s:%s') {" % (artifact, version))
Søren Gjessecd927782022-05-11 16:31:51 +0200255 print(" changing = true")
256 print(" }")
257 print()
Søren Gjesse79298102022-08-23 16:25:41 +0200258 print('If not using the "changing" propertyRemember to run gradle with '
Søren Gjessecd927782022-05-11 16:31:51 +0200259 + " --refresh-dependencies (./gradlew --refresh-dependencies ...) "
260 + "to ensure the cache is not used when the same version is published."
261 + "multiple times.")
Søren Gjessee6893fd2022-04-26 13:34:51 +0200262
Søren Gjesse2b047692022-08-19 16:34:38 +0200263def main():
264 args = parse_options()
Søren Gjesse8d91e1a2022-09-01 16:58:43 +0200265 if args.desugar_jdk_libs_checkout and args.release_version:
266 raise Exception(
267 'Options --desugar-jdk-libs-checkout and --release-version are mutually exclusive')
268 if args.desugar_jdk_libs_revision and args.release_version:
269 raise Exception(
270 'Options --desugar-jdk-libs-revision and --release-version are mutually exclusive')
271 if args.desugar_jdk_libs_checkout and args.desugar_jdk_libs_revision:
272 raise Exception(
273 'Options --desugar-jdk-libs-checkout and --desugar-jdk-libs-revision are mutually exclusive')
Søren Gjesse2b047692022-08-19 16:34:38 +0200274 if args.clear_repo:
275 shutil.rmtree(args.repo_root, ignore_errors=True)
276 utils.makedirs_if_needed(args.repo_root)
277 if (args.variant):
278 run(args)
279 else:
280 for v in Variant:
281 args.variant = v
282 run(args)
283
Søren Gjessee6893fd2022-04-26 13:34:51 +0200284if __name__ == '__main__':
285 sys.exit(main())