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 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 20 | |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 21 | class Variant(Enum): |
Søren Gjesse | cd92778 | 2022-05-11 16:31:51 +0200 | [diff] [blame] | 22 | jdk8 = 'jdk8' |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 23 | jdk11_legacy = 'jdk11_legacy' |
| 24 | jdk11_minimal = 'jdk11_minimal' |
| 25 | jdk11 = 'jdk11' |
| 26 | jdk11_nio = 'jdk11_nio' |
Søren Gjesse | cd92778 | 2022-05-11 16:31:51 +0200 | [diff] [blame] | 27 | |
| 28 | def __str__(self): |
| 29 | return self.value |
| 30 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 31 | |
Søren Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 32 | def parse_options(): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 33 | 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 Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 71 | |
| 72 | def jar_or_pom_file(unzip_dir, artifact, version, extension): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 73 | return join(unzip_dir, 'com', 'android', 'tools', artifact, version, |
| 74 | artifact + '-' + version + '.' + extension) |
| 75 | |
Søren Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 76 | |
| 77 | def jar_file(unzip_dir, artifact, version): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 78 | return jar_or_pom_file(unzip_dir, artifact, version, 'jar') |
| 79 | |
Søren Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 80 | |
| 81 | def pom_file(unzip_dir, artifact, version): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 82 | return jar_or_pom_file(unzip_dir, artifact, version, 'pom') |
| 83 | |
Søren Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 84 | |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 85 | def run(args): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 86 | artifact = None |
| 87 | configuration_artifact = None |
| 88 | configuration = None |
| 89 | conversions = None |
| 90 | implementation = None |
Søren Gjesse | 1da5717 | 2024-07-31 11:10:37 +0200 | [diff] [blame] | 91 | version_file_name = None |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 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 |
Søren Gjesse | 1da5717 | 2024-07-31 11:10:37 +0200 | [diff] [blame] | 102 | version_file_name = 'VERSION.txt' |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 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 |
Søren Gjesse | 1da5717 | 2024-07-31 11:10:37 +0200 | [diff] [blame] | 112 | version_file_name = 'VERSION_JDK11_LEGACY.txt' |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 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 |
Søren Gjesse | 1da5717 | 2024-07-31 11:10:37 +0200 | [diff] [blame] | 122 | version_file_name = 'VERSION_JDK11_MINIMAL.txt' |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 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 |
Søren Gjesse | 1da5717 | 2024-07-31 11:10:37 +0200 | [diff] [blame] | 132 | version_file_name = 'VERSION_JDK11.txt' |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 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 |
Søren Gjesse | 1da5717 | 2024-07-31 11:10:37 +0200 | [diff] [blame] | 142 | version_file_name = 'VERSION_JDK11_NIO.txt' |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 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 | ]) |
Søren Gjesse | 1da5717 | 2024-07-31 11:10:37 +0200 | [diff] [blame] | 174 | if not args.release_version: |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 175 | with utils.ChangedWorkingDirectory(checkout_dir): |
Søren Gjesse | 1da5717 | 2024-07-31 11:10:37 +0200 | [diff] [blame] | 176 | with open(version_file_name) as version_file: |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 177 | 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 Gjesse | 1da5717 | 2024-07-31 11:10:37 +0200 | [diff] [blame] | 184 | + 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 Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 191 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 192 | # 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 Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 207 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 208 | 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 Gjesse | 7929810 | 2022-08-23 16:25:41 +0200 | [diff] [blame] | 218 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 219 | # 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 Gjesse | 7929810 | 2022-08-23 16:25:41 +0200 | [diff] [blame] | 237 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 238 | 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 Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 248 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 249 | 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 Gjesse | a28c461 | 2023-11-01 14:55:27 +0100 | [diff] [blame] | 256 | 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 Gjesse | 3d3a920 | 2023-11-02 11:04:28 +0100 | [diff] [blame] | 272 | print("For Groovy add") |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 273 | 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 Gjesse | a28c461 | 2023-11-01 14:55:27 +0100 | [diff] [blame] | 290 | 'If not using the "changing" property remember to run gradle with ' + |
| 291 | "--refresh-dependencies (./gradlew --refresh-dependencies ...) " + |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 292 | "to ensure the cache is not used when the same version is published." |
| 293 | + "multiple times.") |
| 294 | |
Søren Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 295 | |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 296 | def main(): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 297 | 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 Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 320 | |
Søren Gjesse | e6893fd | 2022-04-26 13:34:51 +0200 | [diff] [blame] | 321 | if __name__ == '__main__': |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 322 | sys.exit(main()) |