blob: 2f13541a474bb26a971b70789847dca886cc0cd5 [file] [log] [blame]
Ian Zernydcb172e2022-02-22 15:36:45 +01001#!/usr/bin/env python3
Søren Gjesse1c115b52019-08-14 12:43:57 +02002# 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 Wind70d614f2020-01-31 08:45:21 +010018# https://storage.googleapis.com/r8-releases/raw can be treated as a maven
Søren Gjesse1c115b52019-08-14 12:43:57 +020019# repository to fetch the artifact com.android.tools:desugar_jdk_libs:1.0.0
20
21import archive
22import git_utils
Clément Béra54696f72021-11-16 09:27:22 +000023import jdk
Søren Gjesse1c115b52019-08-14 12:43:57 +020024import optparse
25import os
26import re
27import shutil
28import subprocess
29import sys
30import utils
31
Søren Gjessecb671832022-03-22 17:12:29 +010032VERSION_FILE = 'VERSION_JDK11.txt'
Søren Gjesse1c115b52019-08-14 12:43:57 +020033LIBRARY_NAME = 'desugar_jdk_libs'
34
35def ParseOptions(argv):
36 result = optparse.OptionParser()
Søren Gjesse124c4c42021-10-18 12:37:41 +020037 result.add_option('--variant',
38 help='.',
39 choices = ['jdk8', 'jdk11'],
Søren Gjessecb671832022-03-22 17:12:29 +010040 default='jdk11')
Søren Gjesse1c115b52019-08-14 12:43:57 +020041 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 Gjesse53d14482021-02-10 16:53:38 +010052 result.add_option('--build_only', '--build-only',
53 help='Build desugared library without archiving.',
54 type="string", action="store")
Søren Gjesse1c115b52019-08-14 12:43:57 +020055 (options, args) = result.parse_args(argv)
56 return (options, args)
57
58
Søren Gjesse07b6aea2019-12-12 11:11:58 +010059def GetVersion(version_file_name):
60 with open(version_file_name, 'r') as version_file:
Søren Gjessef298e7a2019-12-06 09:57:36 +010061 lines = [line.strip() for line in version_file.readlines()]
62 lines = [line for line in lines if not line.startswith('#')]
Søren Gjesse1c115b52019-08-14 12:43:57 +020063 if len(lines) != 1:
64 raise Exception('Version file '
Søren Gjesse07b6aea2019-12-12 11:11:58 +010065 + version_file + ' is expected to have exactly one line')
Søren Gjesse1c115b52019-08-14 12:43:57 +020066 version = lines[0].strip()
Søren Gjesse1e171532019-09-03 09:44:22 +020067 utils.check_basic_semver_version(
Søren Gjesse1c091482022-03-23 10:36:55 +010068 version, 'in version file ' + version_file_name, allowPrerelease = True)
Søren Gjesse1c115b52019-08-14 12:43:57 +020069 return version
70
71
Rico Wind1b52acf2021-03-21 12:36:55 +010072def Upload(options, file_name, storage_path, destination, is_main):
Søren Gjesse1c115b52019-08-14 12:43:57 +020073 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 Wind70d614f2020-01-31 08:45:21 +010086 destination.replace('gs://', 'https://storage.googleapis.com/', 1))
Søren Gjesse1c115b52019-08-14 12:43:57 +020087
Søren Gjesse53d14482021-02-10 16:53:38 +010088def CloneDesugaredLibrary(github_account, checkout_dir):
89 git_utils.GitClone(
90 'https://github.com/'
91 + github_account + '/' + LIBRARY_NAME, checkout_dir)
92
Clément Béra54696f72021-11-16 09:27:22 +000093def 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 Gjesseef195772021-03-11 16:04:42 +0100100def BuildDesugaredLibrary(checkout_dir, variant):
101 if (variant != 'jdk8' and variant != 'jdk11'):
102 raise Exception('Variant ' + variant + 'is not supported')
Søren Gjesse53d14482021-02-10 16:53:38 +0100103 with utils.ChangedWorkingDirectory(checkout_dir):
104 bazel = os.path.join(utils.BAZEL_TOOL, 'lib', 'bazel', 'bin', 'bazel')
Søren Gjesse124c4c42021-10-18 12:37:41 +0200105 cmd = [
106 bazel,
107 '--bazelrc=/dev/null',
108 'build',
Rico Wind8eb5cbd2022-03-04 13:48:09 +0100109 '--spawn_strategy=local',
Rico Wind55de5d52022-02-23 08:00:23 +0100110 '--verbose_failures',
Clément Béra54696f72021-11-16 09:27:22 +0000111 'maven_release' + ('_jdk11' if variant == 'jdk11' else '')]
Søren Gjesse53d14482021-02-10 16:53:38 +0100112 utils.PrintCmd(cmd)
Clément Béraf95490c2021-11-16 12:27:16 +0000113 subprocess.check_call(cmd, env=GetJavaEnv())
Søren Gjesse53d14482021-02-10 16:53:38 +0100114 cmd = [bazel, 'shutdown']
115 utils.PrintCmd(cmd)
Clément Béra54696f72021-11-16 09:27:22 +0000116 subprocess.check_call(cmd, env=GetJavaEnv())
Søren Gjesse53d14482021-02-10 16:53:38 +0100117
118 # Locate the library jar and the maven zip with the jar from the
119 # bazel build.
Søren Gjesseef195772021-03-11 16:04:42 +0100120 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érac1fecfa2021-11-19 12:26:23 +0000125 checkout_dir, 'bazel-bin', 'jdk11', 'src', 'd8_java_base_selected_with_addon.jar')
Søren Gjesseef195772021-03-11 16:04:42 +0100126 maven_zip = os.path.join(
127 checkout_dir,
128 'bazel-bin',
Søren Gjesse124c4c42021-10-18 12:37:41 +0200129 LIBRARY_NAME + ('_jdk11' if variant == 'jdk11' else '') +'.zip')
Søren Gjesse53d14482021-02-10 16:53:38 +0100130 return (library_jar, maven_zip)
131
132
133def 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 Gjesse1c115b52019-08-14 12:43:57 +0200136
137def Main(argv):
138 (options, args) = ParseOptions(argv)
139 if (len(args) > 0):
140 raise Exception('Unsupported arguments')
Søren Gjesse53d14482021-02-10 16:53:38 +0100141 if not utils.is_bot() and not (options.dry_run or options.build_only):
Søren Gjesse1c115b52019-08-14 12:43:57 +0200142 raise Exception('You are not a bot, don\'t archive builds. '
Søren Gjesse53d14482021-02-10 16:53:38 +0100143 + '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 Gjesse1c115b52019-08-14 12:43:57 +0200148 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 Gjesse699f6362019-10-09 14:56:33 +0200153 utils.DownloadFromGoogleCloudStorage(utils.JAVA8_SHA_FILE)
Søren Gjesseef195772021-03-11 16:04:42 +0100154 utils.DownloadFromGoogleCloudStorage(utils.JAVA11_SHA_FILE)
Søren Gjesse1c115b52019-08-14 12:43:57 +0200155
Søren Gjesse53d14482021-02-10 16:53:38 +0100156 if options.build_only:
157 with utils.TempDir() as checkout_dir:
158 CloneDesugaredLibrary(options.github_account, checkout_dir)
Søren Gjesse124c4c42021-10-18 12:37:41 +0200159 (library_jar, maven_zip) = BuildDesugaredLibrary(checkout_dir, options.variant)
Søren Gjesse53d14482021-02-10 16:53:38 +0100160 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 Gjesse1c115b52019-08-14 12:43:57 +0200168 # Only handling versioned desugar_jdk_libs.
Rico Wind1b52acf2021-03-21 12:36:55 +0100169 is_main = False
Søren Gjesse1c115b52019-08-14 12:43:57 +0200170
171 with utils.TempDir() as checkout_dir:
Søren Gjesse53d14482021-02-10 16:53:38 +0100172 CloneDesugaredLibrary(options.github_account, checkout_dir)
173 version = GetVersion(os.path.join(checkout_dir, VERSION_FILE))
Søren Gjesse1c115b52019-08-14 12:43:57 +0200174
Søren Gjesse53d14482021-02-10 16:53:38 +0100175 destination = archive.GetVersionDestination(
Rico Wind1b52acf2021-03-21 12:36:55 +0100176 'gs://', LIBRARY_NAME + '/' + version, is_main)
Søren Gjesse53d14482021-02-10 16:53:38 +0100177 if utils.cloud_storage_exists(destination) and not options.dry_run:
178 raise Exception(
179 'Target archive directory %s already exists' % destination)
Søren Gjesse1c115b52019-08-14 12:43:57 +0200180
Søren Gjesse124c4c42021-10-18 12:37:41 +0200181 (library_jar, maven_zip) = BuildDesugaredLibrary(checkout_dir, options.variant)
Søren Gjesse1c115b52019-08-14 12:43:57 +0200182
Søren Gjesse53d14482021-02-10 16:53:38 +0100183 storage_path = LIBRARY_NAME + '/' + version
184 # Upload the jar file with the library.
185 destination = archive.GetUploadDestination(
Rico Wind1b52acf2021-03-21 12:36:55 +0100186 storage_path, LIBRARY_NAME + '.jar', is_main)
187 Upload(options, library_jar, storage_path, destination, is_main)
Søren Gjesse1c115b52019-08-14 12:43:57 +0200188
Søren Gjesse53d14482021-02-10 16:53:38 +0100189 # Upload the maven zip file with the library.
190 destination = archive.GetUploadDestination(
Rico Wind1b52acf2021-03-21 12:36:55 +0100191 storage_path, LIBRARY_NAME + '.zip', is_main)
192 Upload(options, maven_zip, storage_path, destination, is_main)
Søren Gjesse1c115b52019-08-14 12:43:57 +0200193
Søren Gjesse53d14482021-02-10 16:53:38 +0100194 # 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 Wind1b52acf2021-03-21 12:36:55 +0100198 is_main)
Søren Gjesse53d14482021-02-10 16:53:38 +0100199 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 Wind1b52acf2021-03-21 12:36:55 +0100203 print('Maven repo root available at: %s' % archive.GetMavenUrl(is_main))
Søren Gjesse1c115b52019-08-14 12:43:57 +0200204
205
206if __name__ == '__main__':
207 sys.exit(Main(sys.argv[1:]))