Søren Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 1 | #!/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 | |
| 6 | import argparse |
Søren Gjesse | cd92778 | 2022-05-11 16:31:51 +0200 | [diff] [blame] | 7 | from enum import Enum |
Søren Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 8 | import os |
| 9 | from os.path import join |
| 10 | import shutil |
| 11 | import subprocess |
| 12 | import sys |
Søren Gjesse | 8d91e1a | 2022-09-01 16:58:43 +0200 | [diff] [blame] | 13 | import urllib.request |
Søren Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 14 | |
Søren Gjesse | 7929810 | 2022-08-23 16:25:41 +0200 | [diff] [blame] | 15 | import gradle |
Søren Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 16 | import utils |
| 17 | import create_maven_release |
Søren Gjesse | 7929810 | 2022-08-23 16:25:41 +0200 | [diff] [blame] | 18 | import archive_desugar_jdk_libs |
Søren Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 19 | |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 20 | class Variant(Enum): |
Søren Gjesse | cd92778 | 2022-05-11 16:31:51 +0200 | [diff] [blame] | 21 | jdk8 = 'jdk8' |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 22 | jdk11_legacy = 'jdk11_legacy' |
| 23 | jdk11_minimal = 'jdk11_minimal' |
| 24 | jdk11 = 'jdk11' |
| 25 | jdk11_nio = 'jdk11_nio' |
Søren Gjesse | cd92778 | 2022-05-11 16:31:51 +0200 | [diff] [blame] | 26 | |
| 27 | def __str__(self): |
| 28 | return self.value |
| 29 | |
Søren Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 30 | def parse_options(): |
Søren Gjesse | cd92778 | 2022-05-11 16:31:51 +0200 | [diff] [blame] | 31 | parser = argparse.ArgumentParser( |
| 32 | description='Local desugared library repository for desugared library configurations') |
| 33 | parser.add_argument('--repo-root', '--repo_root', |
Søren Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 34 | default='/tmp/repo', |
| 35 | metavar=('<path>'), |
| 36 | help='Location for Maven repository.') |
Søren Gjesse | cd92778 | 2022-05-11 16:31:51 +0200 | [diff] [blame] | 37 | 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 Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 41 | parser.add_argument('--variant', type=Variant, choices=list(Variant)) |
Søren Gjesse | cd92778 | 2022-05-11 16:31:51 +0200 | [diff] [blame] | 42 | parser.add_argument('--desugar-jdk-libs-checkout', '--desugar_jdk_libs_checkout', |
Søren Gjesse | 8ed1445 | 2022-05-10 11:29:21 +0200 | [diff] [blame] | 43 | default=None, |
| 44 | metavar=('<path>'), |
| 45 | help='Use existing checkout of github.com/google/desugar_jdk_libs.') |
Søren Gjesse | 8d91e1a | 2022-09-01 16:58:43 +0200 | [diff] [blame] | 46 | 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 Gjesse | cd92778 | 2022-05-11 16:31:51 +0200 | [diff] [blame] | 53 | args = parser.parse_args() |
Søren Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 54 | return args |
| 55 | |
| 56 | def 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 | |
| 66 | def jar_file(unzip_dir, artifact, version): |
| 67 | return jar_or_pom_file(unzip_dir, artifact, version, 'jar') |
| 68 | |
| 69 | def pom_file(unzip_dir, artifact, version): |
| 70 | return jar_or_pom_file(unzip_dir, artifact, version, 'pom') |
| 71 | |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 72 | def 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 Gjesse | 8d91e1a | 2022-09-01 16:58:43 +0200 | [diff] [blame] | 80 | implementation_maven_zip = None |
| 81 | release_archive_location = None |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 82 | 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 Gjesse | 8d91e1a | 2022-09-01 16:58:43 +0200 | [diff] [blame] | 91 | implementation_maven_zip = 'desugar_jdk_libs.zip' |
| 92 | release_archive_location = 'desugar_jdk_libs' |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 93 | 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 Gjesse | 8d91e1a | 2022-09-01 16:58:43 +0200 | [diff] [blame] | 101 | implementation_maven_zip = 'desugar_jdk_libs_jdk11_legacy.zip' |
| 102 | release_archive_location = 'desugar_jdk_libs' |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 103 | 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 Gjesse | 8d91e1a | 2022-09-01 16:58:43 +0200 | [diff] [blame] | 111 | implementation_maven_zip = 'desugar_jdk_libs_jdk11_minimal.zip' |
| 112 | release_archive_location = 'desugar_jdk_libs_minimal' |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 113 | 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 Gjesse | 8d91e1a | 2022-09-01 16:58:43 +0200 | [diff] [blame] | 121 | implementation_maven_zip = 'desugar_jdk_libs_jdk11.zip' |
| 122 | release_archive_location = 'desugar_jdk_libs' |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 123 | 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 Gjesse | 8d91e1a | 2022-09-01 16:58:43 +0200 | [diff] [blame] | 131 | 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 Gjesse | 7929810 | 2022-08-23 16:25:41 +0200 | [diff] [blame] | 134 | gradle.RunGradle([utils.R8]) |
Søren Gjesse | 8d91e1a | 2022-09-01 16:58:43 +0200 | [diff] [blame] | 135 | 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 Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 144 | # Checkout desugar_jdk_libs from GitHub |
Søren Gjesse | 8ed1445 | 2022-05-10 11:29:21 +0200 | [diff] [blame] | 145 | use_existing_checkout = args.desugar_jdk_libs_checkout != None |
Søren Gjesse | cd92778 | 2022-05-11 16:31:51 +0200 | [diff] [blame] | 146 | checkout_dir = (args.desugar_jdk_libs_checkout |
| 147 | if use_existing_checkout |
| 148 | else join(tmp_dir, 'desugar_jdk_libs')) |
Søren Gjesse | 8d91e1a | 2022-09-01 16:58:43 +0200 | [diff] [blame] | 149 | 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 Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 168 | |
| 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 Gjesse | cd92778 | 2022-05-11 16:31:51 +0200 | [diff] [blame] | 174 | configuration, |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 175 | implementation, |
| 176 | conversions) |
Søren Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 177 | unzip_dir = join(tmp_dir, 'desugar_jdk_libs_configuration_unzipped') |
| 178 | cmd = ['unzip', '-q', maven_zip, '-d', unzip_dir] |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 179 | subprocess.check_call(cmd) |
Søren Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 180 | cmd = [ |
| 181 | 'mvn', |
| 182 | 'deploy:deploy-file', |
| 183 | '-Durl=file:' + args.repo_root, |
| 184 | '-DrepositoryId=someName', |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 185 | '-Dfile=' + jar_file(unzip_dir, configuration_artifact, version), |
| 186 | '-DpomFile=' + pom_file(unzip_dir, configuration_artifact, version)] |
| 187 | subprocess.check_call(cmd) |
Søren Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 188 | |
Søren Gjesse | 8d91e1a | 2022-09-01 16:58:43 +0200 | [diff] [blame] | 189 | 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 Gjesse | 7929810 | 2022-08-23 16:25:41 +0200 | [diff] [blame] | 201 | |
Søren Gjesse | 8d91e1a | 2022-09-01 16:58:43 +0200 | [diff] [blame] | 202 | # 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 Gjesse | 7929810 | 2022-08-23 16:25:41 +0200 | [diff] [blame] | 219 | undesugared_if_needed) |
| 220 | |
Søren Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 221 | unzip_dir = join(tmp_dir, 'desugar_jdk_libs_unzipped') |
| 222 | cmd = [ |
| 223 | 'unzip', |
| 224 | '-q', |
Søren Gjesse | 7929810 | 2022-08-23 16:25:41 +0200 | [diff] [blame] | 225 | undesugared_if_needed, |
Søren Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 226 | '-d', |
| 227 | unzip_dir] |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 228 | subprocess.check_call(cmd) |
Søren Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 229 | cmd = [ |
| 230 | 'mvn', |
| 231 | 'deploy:deploy-file', |
| 232 | '-Durl=file:' + args.repo_root, |
| 233 | '-DrepositoryId=someName', |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 234 | '-Dfile=' + jar_file(unzip_dir, artifact, version), |
| 235 | '-DpomFile=' + pom_file(unzip_dir, artifact, version)] |
| 236 | subprocess.check_call(cmd) |
Søren Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 237 | |
| 238 | print() |
| 239 | print("Artifacts:") |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 240 | print(" com.android.tools:%s:%s" % (configuration_artifact, version)) |
| 241 | print(" com.android.tools:%s:%s" % (artifact, version)) |
Søren Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 242 | 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 Gjesse | cd92778 | 2022-05-11 16:31:51 +0200 | [diff] [blame] | 251 | print("to dependencyResolutionManagement.repositories in settings.gradle, and use") |
| 252 | print('the "changing" property of the coreLibraryDesugaring dependency:') |
Søren Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 253 | print() |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 254 | print(" coreLibraryDesugaring('com.android.tools:%s:%s') {" % (artifact, version)) |
Søren Gjesse | cd92778 | 2022-05-11 16:31:51 +0200 | [diff] [blame] | 255 | print(" changing = true") |
| 256 | print(" }") |
| 257 | print() |
Søren Gjesse | 7929810 | 2022-08-23 16:25:41 +0200 | [diff] [blame] | 258 | print('If not using the "changing" propertyRemember to run gradle with ' |
Søren Gjesse | cd92778 | 2022-05-11 16:31:51 +0200 | [diff] [blame] | 259 | + " --refresh-dependencies (./gradlew --refresh-dependencies ...) " |
| 260 | + "to ensure the cache is not used when the same version is published." |
| 261 | + "multiple times.") |
Søren Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 262 | |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 263 | def main(): |
| 264 | args = parse_options() |
Søren Gjesse | 8d91e1a | 2022-09-01 16:58:43 +0200 | [diff] [blame] | 265 | 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 Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 274 | 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 Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 284 | if __name__ == '__main__': |
| 285 | sys.exit(main()) |