Ian Zerny | dcb172e | 2022-02-22 15:36:45 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 2 | # Copyright (c) 2019, 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 | # This script is designed to run on a buildbot to build from the source |
| 7 | # of https://github.com/google/desugar_jdk_libs and publish to the |
| 8 | # r8-release Cloud Storage Bucket. |
| 9 | # |
| 10 | # These files are uploaded: |
| 11 | # |
| 12 | # raw/desugar_jdk_libs/<VERSION>/desugar_jdk_libs.jar |
| 13 | # raw/desugar_jdk_libs/<VERSION>/desugar_jdk_libs.zip |
| 14 | # raw/com/android/tools/desugar_jdk_libs/<VERSION>/desugar_jdk_libs-<VERSION>.jar |
| 15 | # |
| 16 | # The first two are the raw jar file and the maven compatible zip file. The |
| 17 | # third is the raw jar file placed and named so that the URL |
Rico Wind | 70d614f | 2020-01-31 08:45:21 +0100 | [diff] [blame] | 18 | # https://storage.googleapis.com/r8-releases/raw can be treated as a maven |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 19 | # repository to fetch the artifact com.android.tools:desugar_jdk_libs:1.0.0 |
| 20 | |
| 21 | import archive |
| 22 | import git_utils |
Clément Béra | 54696f7 | 2021-11-16 09:27:22 +0000 | [diff] [blame] | 23 | import jdk |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 24 | import optparse |
| 25 | import os |
| 26 | import re |
| 27 | import shutil |
| 28 | import subprocess |
| 29 | import sys |
| 30 | import utils |
| 31 | |
Søren Gjesse | cb67183 | 2022-03-22 17:12:29 +0100 | [diff] [blame] | 32 | VERSION_FILE = 'VERSION_JDK11.txt' |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 33 | LIBRARY_NAME = 'desugar_jdk_libs' |
| 34 | |
| 35 | def ParseOptions(argv): |
| 36 | result = optparse.OptionParser() |
Søren Gjesse | 124c4c4 | 2021-10-18 12:37:41 +0200 | [diff] [blame] | 37 | result.add_option('--variant', |
| 38 | help='.', |
| 39 | choices = ['jdk8', 'jdk11'], |
Søren Gjesse | cb67183 | 2022-03-22 17:12:29 +0100 | [diff] [blame] | 40 | default='jdk11') |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 41 | result.add_option('--dry-run', '--dry_run', |
| 42 | help='Running on bot, use third_party dependency.', |
| 43 | default=False, |
| 44 | action='store_true') |
| 45 | result.add_option('--dry-run-output', '--dry_run_output', |
| 46 | help='Output directory for dry run.', |
| 47 | type="string", action="store") |
| 48 | result.add_option('--github-account', '--github_account', |
| 49 | help='GitHub account to clone from.', |
| 50 | default="google", |
| 51 | type="string", action="store") |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 52 | result.add_option('--build_only', '--build-only', |
| 53 | help='Build desugared library without archiving.', |
| 54 | type="string", action="store") |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 55 | (options, args) = result.parse_args(argv) |
| 56 | return (options, args) |
| 57 | |
| 58 | |
Søren Gjesse | 07b6aea | 2019-12-12 11:11:58 +0100 | [diff] [blame] | 59 | def GetVersion(version_file_name): |
| 60 | with open(version_file_name, 'r') as version_file: |
Søren Gjesse | f298e7a | 2019-12-06 09:57:36 +0100 | [diff] [blame] | 61 | lines = [line.strip() for line in version_file.readlines()] |
| 62 | lines = [line for line in lines if not line.startswith('#')] |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 63 | if len(lines) != 1: |
| 64 | raise Exception('Version file ' |
Søren Gjesse | 07b6aea | 2019-12-12 11:11:58 +0100 | [diff] [blame] | 65 | + version_file + ' is expected to have exactly one line') |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 66 | version = lines[0].strip() |
Søren Gjesse | 1e17153 | 2019-09-03 09:44:22 +0200 | [diff] [blame] | 67 | utils.check_basic_semver_version( |
Søren Gjesse | 1c09148 | 2022-03-23 10:36:55 +0100 | [diff] [blame] | 68 | version, 'in version file ' + version_file_name, allowPrerelease = True) |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 69 | return version |
| 70 | |
| 71 | |
Rico Wind | 1b52acf | 2021-03-21 12:36:55 +0100 | [diff] [blame] | 72 | def Upload(options, file_name, storage_path, destination, is_main): |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 73 | print('Uploading %s to %s' % (file_name, destination)) |
| 74 | if options.dry_run: |
| 75 | if options.dry_run_output: |
| 76 | dry_run_destination = \ |
| 77 | os.path.join(options.dry_run_output, os.path.basename(file_name)) |
| 78 | print('Dry run, not actually uploading. Copying to ' |
| 79 | + dry_run_destination) |
| 80 | shutil.copyfile(file_name, dry_run_destination) |
| 81 | else: |
| 82 | print('Dry run, not actually uploading') |
| 83 | else: |
| 84 | utils.upload_file_to_cloud_storage(file_name, destination) |
| 85 | print('File available at: %s' % |
Rico Wind | 70d614f | 2020-01-31 08:45:21 +0100 | [diff] [blame] | 86 | destination.replace('gs://', 'https://storage.googleapis.com/', 1)) |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 87 | |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 88 | def CloneDesugaredLibrary(github_account, checkout_dir): |
| 89 | git_utils.GitClone( |
| 90 | 'https://github.com/' |
| 91 | + github_account + '/' + LIBRARY_NAME, checkout_dir) |
| 92 | |
Clément Béra | 54696f7 | 2021-11-16 09:27:22 +0000 | [diff] [blame] | 93 | def GetJavaEnv(): |
| 94 | java_env = dict(os.environ, JAVA_HOME = jdk.GetJdk11Home()) |
| 95 | java_env['PATH'] = java_env['PATH'] + os.pathsep + os.path.join(jdk.GetJdk11Home(), 'bin') |
| 96 | java_env['GRADLE_OPTS'] = '-Xmx1g' |
| 97 | return java_env |
| 98 | |
| 99 | |
Søren Gjesse | ef19577 | 2021-03-11 16:04:42 +0100 | [diff] [blame] | 100 | def BuildDesugaredLibrary(checkout_dir, variant): |
| 101 | if (variant != 'jdk8' and variant != 'jdk11'): |
| 102 | raise Exception('Variant ' + variant + 'is not supported') |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 103 | with utils.ChangedWorkingDirectory(checkout_dir): |
| 104 | bazel = os.path.join(utils.BAZEL_TOOL, 'lib', 'bazel', 'bin', 'bazel') |
Søren Gjesse | 124c4c4 | 2021-10-18 12:37:41 +0200 | [diff] [blame] | 105 | cmd = [ |
| 106 | bazel, |
| 107 | '--bazelrc=/dev/null', |
| 108 | 'build', |
Rico Wind | 8eb5cbd | 2022-03-04 13:48:09 +0100 | [diff] [blame] | 109 | '--spawn_strategy=local', |
Rico Wind | 55de5d5 | 2022-02-23 08:00:23 +0100 | [diff] [blame] | 110 | '--verbose_failures', |
Clément Béra | 54696f7 | 2021-11-16 09:27:22 +0000 | [diff] [blame] | 111 | 'maven_release' + ('_jdk11' if variant == 'jdk11' else '')] |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 112 | utils.PrintCmd(cmd) |
Clément Béra | f95490c | 2021-11-16 12:27:16 +0000 | [diff] [blame] | 113 | subprocess.check_call(cmd, env=GetJavaEnv()) |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 114 | cmd = [bazel, 'shutdown'] |
| 115 | utils.PrintCmd(cmd) |
Clément Béra | 54696f7 | 2021-11-16 09:27:22 +0000 | [diff] [blame] | 116 | subprocess.check_call(cmd, env=GetJavaEnv()) |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 117 | |
| 118 | # Locate the library jar and the maven zip with the jar from the |
| 119 | # bazel build. |
Søren Gjesse | ef19577 | 2021-03-11 16:04:42 +0100 | [diff] [blame] | 120 | if variant == 'jdk8': |
| 121 | library_jar = os.path.join( |
| 122 | checkout_dir, 'bazel-bin', 'src', 'share', 'classes', 'java', 'libjava.jar') |
| 123 | else: |
| 124 | library_jar = os.path.join( |
Clément Béra | c1fecfa | 2021-11-19 12:26:23 +0000 | [diff] [blame] | 125 | checkout_dir, 'bazel-bin', 'jdk11', 'src', 'd8_java_base_selected_with_addon.jar') |
Søren Gjesse | ef19577 | 2021-03-11 16:04:42 +0100 | [diff] [blame] | 126 | maven_zip = os.path.join( |
| 127 | checkout_dir, |
| 128 | 'bazel-bin', |
Søren Gjesse | 124c4c4 | 2021-10-18 12:37:41 +0200 | [diff] [blame] | 129 | LIBRARY_NAME + ('_jdk11' if variant == 'jdk11' else '') +'.zip') |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 130 | return (library_jar, maven_zip) |
| 131 | |
| 132 | |
| 133 | def MustBeExistingDirectory(path): |
| 134 | if (not os.path.exists(path) or not os.path.isdir(path)): |
| 135 | raise Exception(path + ' does not exist or is not a directory') |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 136 | |
| 137 | def Main(argv): |
| 138 | (options, args) = ParseOptions(argv) |
| 139 | if (len(args) > 0): |
| 140 | raise Exception('Unsupported arguments') |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 141 | if not utils.is_bot() and not (options.dry_run or options.build_only): |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 142 | raise Exception('You are not a bot, don\'t archive builds. ' |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 143 | + 'Use --dry-run or --build-only to test locally') |
| 144 | if options.dry_run_output: |
| 145 | MustBeExistingDirectory(options.dry_run_output) |
| 146 | if options.build_only: |
| 147 | MustBeExistingDirectory(options.build_only) |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 148 | if utils.is_bot(): |
| 149 | archive.SetRLimitToMax() |
| 150 | |
| 151 | # Make sure bazel is extracted in third_party. |
| 152 | utils.DownloadFromGoogleCloudStorage(utils.BAZEL_SHA_FILE) |
Søren Gjesse | 699f636 | 2019-10-09 14:56:33 +0200 | [diff] [blame] | 153 | utils.DownloadFromGoogleCloudStorage(utils.JAVA8_SHA_FILE) |
Søren Gjesse | ef19577 | 2021-03-11 16:04:42 +0100 | [diff] [blame] | 154 | utils.DownloadFromGoogleCloudStorage(utils.JAVA11_SHA_FILE) |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 155 | |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 156 | if options.build_only: |
| 157 | with utils.TempDir() as checkout_dir: |
| 158 | CloneDesugaredLibrary(options.github_account, checkout_dir) |
Søren Gjesse | 124c4c4 | 2021-10-18 12:37:41 +0200 | [diff] [blame] | 159 | (library_jar, maven_zip) = BuildDesugaredLibrary(checkout_dir, options.variant) |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 160 | shutil.copyfile( |
| 161 | library_jar, |
| 162 | os.path.join(options.build_only, os.path.basename(library_jar))) |
| 163 | shutil.copyfile( |
| 164 | maven_zip, |
| 165 | os.path.join(options.build_only, os.path.basename(maven_zip))) |
| 166 | return |
| 167 | |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 168 | # Only handling versioned desugar_jdk_libs. |
Rico Wind | 1b52acf | 2021-03-21 12:36:55 +0100 | [diff] [blame] | 169 | is_main = False |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 170 | |
| 171 | with utils.TempDir() as checkout_dir: |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 172 | CloneDesugaredLibrary(options.github_account, checkout_dir) |
| 173 | version = GetVersion(os.path.join(checkout_dir, VERSION_FILE)) |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 174 | |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 175 | destination = archive.GetVersionDestination( |
Rico Wind | 1b52acf | 2021-03-21 12:36:55 +0100 | [diff] [blame] | 176 | 'gs://', LIBRARY_NAME + '/' + version, is_main) |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 177 | if utils.cloud_storage_exists(destination) and not options.dry_run: |
| 178 | raise Exception( |
| 179 | 'Target archive directory %s already exists' % destination) |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 180 | |
Søren Gjesse | 124c4c4 | 2021-10-18 12:37:41 +0200 | [diff] [blame] | 181 | (library_jar, maven_zip) = BuildDesugaredLibrary(checkout_dir, options.variant) |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 182 | |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 183 | storage_path = LIBRARY_NAME + '/' + version |
| 184 | # Upload the jar file with the library. |
| 185 | destination = archive.GetUploadDestination( |
Rico Wind | 1b52acf | 2021-03-21 12:36:55 +0100 | [diff] [blame] | 186 | storage_path, LIBRARY_NAME + '.jar', is_main) |
| 187 | Upload(options, library_jar, storage_path, destination, is_main) |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 188 | |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 189 | # Upload the maven zip file with the library. |
| 190 | destination = archive.GetUploadDestination( |
Rico Wind | 1b52acf | 2021-03-21 12:36:55 +0100 | [diff] [blame] | 191 | storage_path, LIBRARY_NAME + '.zip', is_main) |
| 192 | Upload(options, maven_zip, storage_path, destination, is_main) |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 193 | |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 194 | # Upload the jar file for accessing GCS as a maven repro. |
| 195 | maven_destination = archive.GetUploadDestination( |
| 196 | utils.get_maven_path('desugar_jdk_libs', version), |
| 197 | 'desugar_jdk_libs-%s.jar' % version, |
Rico Wind | 1b52acf | 2021-03-21 12:36:55 +0100 | [diff] [blame] | 198 | is_main) |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 199 | if options.dry_run: |
| 200 | print('Dry run, not actually creating maven repo') |
| 201 | else: |
| 202 | utils.upload_file_to_cloud_storage(library_jar, maven_destination) |
Rico Wind | 1b52acf | 2021-03-21 12:36:55 +0100 | [diff] [blame] | 203 | print('Maven repo root available at: %s' % archive.GetMavenUrl(is_main)) |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 204 | |
| 205 | |
| 206 | if __name__ == '__main__': |
| 207 | sys.exit(Main(sys.argv[1:])) |