Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 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 |
| 23 | import optparse |
| 24 | import os |
| 25 | import re |
| 26 | import shutil |
| 27 | import subprocess |
| 28 | import sys |
| 29 | import utils |
| 30 | |
| 31 | VERSION_FILE = 'VERSION.txt' |
| 32 | LIBRARY_NAME = 'desugar_jdk_libs' |
| 33 | |
| 34 | def ParseOptions(argv): |
| 35 | result = optparse.OptionParser() |
| 36 | result.add_option('--dry-run', '--dry_run', |
| 37 | help='Running on bot, use third_party dependency.', |
| 38 | default=False, |
| 39 | action='store_true') |
| 40 | result.add_option('--dry-run-output', '--dry_run_output', |
| 41 | help='Output directory for dry run.', |
| 42 | type="string", action="store") |
| 43 | result.add_option('--github-account', '--github_account', |
| 44 | help='GitHub account to clone from.', |
| 45 | default="google", |
| 46 | type="string", action="store") |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 47 | result.add_option('--build_only', '--build-only', |
| 48 | help='Build desugared library without archiving.', |
| 49 | type="string", action="store") |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 50 | (options, args) = result.parse_args(argv) |
| 51 | return (options, args) |
| 52 | |
| 53 | |
Søren Gjesse | 07b6aea | 2019-12-12 11:11:58 +0100 | [diff] [blame] | 54 | def GetVersion(version_file_name): |
| 55 | with open(version_file_name, 'r') as version_file: |
Søren Gjesse | f298e7a | 2019-12-06 09:57:36 +0100 | [diff] [blame] | 56 | lines = [line.strip() for line in version_file.readlines()] |
| 57 | lines = [line for line in lines if not line.startswith('#')] |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 58 | if len(lines) != 1: |
| 59 | raise Exception('Version file ' |
Søren Gjesse | 07b6aea | 2019-12-12 11:11:58 +0100 | [diff] [blame] | 60 | + version_file + ' is expected to have exactly one line') |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 61 | version = lines[0].strip() |
Søren Gjesse | 1e17153 | 2019-09-03 09:44:22 +0200 | [diff] [blame] | 62 | utils.check_basic_semver_version( |
Søren Gjesse | 07b6aea | 2019-12-12 11:11:58 +0100 | [diff] [blame] | 63 | version, 'in version file ' + version_file_name) |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 64 | return version |
| 65 | |
| 66 | |
Rico Wind | 1b52acf | 2021-03-21 12:36:55 +0100 | [diff] [blame] | 67 | def Upload(options, file_name, storage_path, destination, is_main): |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 68 | print('Uploading %s to %s' % (file_name, destination)) |
| 69 | if options.dry_run: |
| 70 | if options.dry_run_output: |
| 71 | dry_run_destination = \ |
| 72 | os.path.join(options.dry_run_output, os.path.basename(file_name)) |
| 73 | print('Dry run, not actually uploading. Copying to ' |
| 74 | + dry_run_destination) |
| 75 | shutil.copyfile(file_name, dry_run_destination) |
| 76 | else: |
| 77 | print('Dry run, not actually uploading') |
| 78 | else: |
| 79 | utils.upload_file_to_cloud_storage(file_name, destination) |
| 80 | print('File available at: %s' % |
Rico Wind | 70d614f | 2020-01-31 08:45:21 +0100 | [diff] [blame] | 81 | destination.replace('gs://', 'https://storage.googleapis.com/', 1)) |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 82 | |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 83 | def CloneDesugaredLibrary(github_account, checkout_dir): |
| 84 | git_utils.GitClone( |
| 85 | 'https://github.com/' |
| 86 | + github_account + '/' + LIBRARY_NAME, checkout_dir) |
| 87 | |
Søren Gjesse | ef19577 | 2021-03-11 16:04:42 +0100 | [diff] [blame] | 88 | def BuildDesugaredLibrary(checkout_dir, variant): |
| 89 | if (variant != 'jdk8' and variant != 'jdk11'): |
| 90 | raise Exception('Variant ' + variant + 'is not supported') |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 91 | with utils.ChangedWorkingDirectory(checkout_dir): |
| 92 | bazel = os.path.join(utils.BAZEL_TOOL, 'lib', 'bazel', 'bin', 'bazel') |
Søren Gjesse | ef19577 | 2021-03-11 16:04:42 +0100 | [diff] [blame] | 93 | cmd = [bazel, 'build', 'maven_release' + ('_jdk11' if variant == 'jdk11' else '')] |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 94 | utils.PrintCmd(cmd) |
| 95 | subprocess.check_call(cmd) |
| 96 | cmd = [bazel, 'shutdown'] |
| 97 | utils.PrintCmd(cmd) |
| 98 | subprocess.check_call(cmd) |
| 99 | |
| 100 | # Locate the library jar and the maven zip with the jar from the |
| 101 | # bazel build. |
Søren Gjesse | ef19577 | 2021-03-11 16:04:42 +0100 | [diff] [blame] | 102 | if variant == 'jdk8': |
| 103 | library_jar = os.path.join( |
| 104 | checkout_dir, 'bazel-bin', 'src', 'share', 'classes', 'java', 'libjava.jar') |
| 105 | else: |
| 106 | library_jar = os.path.join( |
Søren Gjesse | 8a933c5 | 2021-07-07 16:07:46 +0200 | [diff] [blame] | 107 | checkout_dir, 'bazel-bin', 'jdk11', 'src', 'd8_java_base_selected.jar') |
Søren Gjesse | ef19577 | 2021-03-11 16:04:42 +0100 | [diff] [blame] | 108 | maven_zip = os.path.join( |
| 109 | checkout_dir, |
| 110 | 'bazel-bin', |
| 111 | LIBRARY_NAME + ('_jdk11' if variant != 'jdk11' else '') +'.zip') |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 112 | return (library_jar, maven_zip) |
| 113 | |
| 114 | |
| 115 | def MustBeExistingDirectory(path): |
| 116 | if (not os.path.exists(path) or not os.path.isdir(path)): |
| 117 | raise Exception(path + ' does not exist or is not a directory') |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 118 | |
| 119 | def Main(argv): |
| 120 | (options, args) = ParseOptions(argv) |
| 121 | if (len(args) > 0): |
| 122 | raise Exception('Unsupported arguments') |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 123 | 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] | 124 | raise Exception('You are not a bot, don\'t archive builds. ' |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 125 | + 'Use --dry-run or --build-only to test locally') |
| 126 | if options.dry_run_output: |
| 127 | MustBeExistingDirectory(options.dry_run_output) |
| 128 | if options.build_only: |
| 129 | MustBeExistingDirectory(options.build_only) |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 130 | if utils.is_bot(): |
| 131 | archive.SetRLimitToMax() |
| 132 | |
| 133 | # Make sure bazel is extracted in third_party. |
| 134 | utils.DownloadFromGoogleCloudStorage(utils.BAZEL_SHA_FILE) |
Søren Gjesse | 699f636 | 2019-10-09 14:56:33 +0200 | [diff] [blame] | 135 | utils.DownloadFromGoogleCloudStorage(utils.JAVA8_SHA_FILE) |
Søren Gjesse | ef19577 | 2021-03-11 16:04:42 +0100 | [diff] [blame] | 136 | utils.DownloadFromGoogleCloudStorage(utils.JAVA11_SHA_FILE) |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 137 | |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 138 | if options.build_only: |
| 139 | with utils.TempDir() as checkout_dir: |
| 140 | CloneDesugaredLibrary(options.github_account, checkout_dir) |
Søren Gjesse | ef19577 | 2021-03-11 16:04:42 +0100 | [diff] [blame] | 141 | (library_jar, maven_zip) = BuildDesugaredLibrary(checkout_dir, "jdk8") |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 142 | shutil.copyfile( |
| 143 | library_jar, |
| 144 | os.path.join(options.build_only, os.path.basename(library_jar))) |
| 145 | shutil.copyfile( |
| 146 | maven_zip, |
| 147 | os.path.join(options.build_only, os.path.basename(maven_zip))) |
| 148 | return |
| 149 | |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 150 | # Only handling versioned desugar_jdk_libs. |
Rico Wind | 1b52acf | 2021-03-21 12:36:55 +0100 | [diff] [blame] | 151 | is_main = False |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 152 | |
| 153 | with utils.TempDir() as checkout_dir: |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 154 | CloneDesugaredLibrary(options.github_account, checkout_dir) |
| 155 | version = GetVersion(os.path.join(checkout_dir, VERSION_FILE)) |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 156 | |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 157 | destination = archive.GetVersionDestination( |
Rico Wind | 1b52acf | 2021-03-21 12:36:55 +0100 | [diff] [blame] | 158 | 'gs://', LIBRARY_NAME + '/' + version, is_main) |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 159 | if utils.cloud_storage_exists(destination) and not options.dry_run: |
| 160 | raise Exception( |
| 161 | 'Target archive directory %s already exists' % destination) |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 162 | |
Søren Gjesse | ef19577 | 2021-03-11 16:04:42 +0100 | [diff] [blame] | 163 | (library_jar, maven_zip) = BuildDesugaredLibrary(checkout_dir, "jdk8") |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 164 | |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 165 | storage_path = LIBRARY_NAME + '/' + version |
| 166 | # Upload the jar file with the library. |
| 167 | destination = archive.GetUploadDestination( |
Rico Wind | 1b52acf | 2021-03-21 12:36:55 +0100 | [diff] [blame] | 168 | storage_path, LIBRARY_NAME + '.jar', is_main) |
| 169 | Upload(options, library_jar, storage_path, destination, is_main) |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 170 | |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 171 | # Upload the maven zip file with the library. |
| 172 | destination = archive.GetUploadDestination( |
Rico Wind | 1b52acf | 2021-03-21 12:36:55 +0100 | [diff] [blame] | 173 | storage_path, LIBRARY_NAME + '.zip', is_main) |
| 174 | Upload(options, maven_zip, storage_path, destination, is_main) |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 175 | |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 176 | # Upload the jar file for accessing GCS as a maven repro. |
| 177 | maven_destination = archive.GetUploadDestination( |
| 178 | utils.get_maven_path('desugar_jdk_libs', version), |
| 179 | 'desugar_jdk_libs-%s.jar' % version, |
Rico Wind | 1b52acf | 2021-03-21 12:36:55 +0100 | [diff] [blame] | 180 | is_main) |
Søren Gjesse | 53d1448 | 2021-02-10 16:53:38 +0100 | [diff] [blame] | 181 | if options.dry_run: |
| 182 | print('Dry run, not actually creating maven repo') |
| 183 | else: |
| 184 | utils.upload_file_to_cloud_storage(library_jar, maven_destination) |
Rico Wind | 1b52acf | 2021-03-21 12:36:55 +0100 | [diff] [blame] | 185 | print('Maven repo root available at: %s' % archive.GetMavenUrl(is_main)) |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 186 | |
| 187 | |
| 188 | if __name__ == '__main__': |
| 189 | sys.exit(Main(sys.argv[1:])) |