blob: d2c1ef1a6cb8444329dc1972473c5197bb3a317b [file] [log] [blame]
Søren Gjesse1c115b52019-08-14 12:43:57 +02001#!/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 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
23import optparse
24import os
25import re
26import shutil
27import subprocess
28import sys
29import utils
30
31VERSION_FILE = 'VERSION.txt'
32LIBRARY_NAME = 'desugar_jdk_libs'
33
34def 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 Gjesse53d14482021-02-10 16:53:38 +010047 result.add_option('--build_only', '--build-only',
48 help='Build desugared library without archiving.',
49 type="string", action="store")
Søren Gjesse1c115b52019-08-14 12:43:57 +020050 (options, args) = result.parse_args(argv)
51 return (options, args)
52
53
Søren Gjesse07b6aea2019-12-12 11:11:58 +010054def GetVersion(version_file_name):
55 with open(version_file_name, 'r') as version_file:
Søren Gjessef298e7a2019-12-06 09:57:36 +010056 lines = [line.strip() for line in version_file.readlines()]
57 lines = [line for line in lines if not line.startswith('#')]
Søren Gjesse1c115b52019-08-14 12:43:57 +020058 if len(lines) != 1:
59 raise Exception('Version file '
Søren Gjesse07b6aea2019-12-12 11:11:58 +010060 + version_file + ' is expected to have exactly one line')
Søren Gjesse1c115b52019-08-14 12:43:57 +020061 version = lines[0].strip()
Søren Gjesse1e171532019-09-03 09:44:22 +020062 utils.check_basic_semver_version(
Søren Gjesse07b6aea2019-12-12 11:11:58 +010063 version, 'in version file ' + version_file_name)
Søren Gjesse1c115b52019-08-14 12:43:57 +020064 return version
65
66
Rico Wind1b52acf2021-03-21 12:36:55 +010067def Upload(options, file_name, storage_path, destination, is_main):
Søren Gjesse1c115b52019-08-14 12:43:57 +020068 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 Wind70d614f2020-01-31 08:45:21 +010081 destination.replace('gs://', 'https://storage.googleapis.com/', 1))
Søren Gjesse1c115b52019-08-14 12:43:57 +020082
Søren Gjesse53d14482021-02-10 16:53:38 +010083def CloneDesugaredLibrary(github_account, checkout_dir):
84 git_utils.GitClone(
85 'https://github.com/'
86 + github_account + '/' + LIBRARY_NAME, checkout_dir)
87
Søren Gjesseef195772021-03-11 16:04:42 +010088def BuildDesugaredLibrary(checkout_dir, variant):
89 if (variant != 'jdk8' and variant != 'jdk11'):
90 raise Exception('Variant ' + variant + 'is not supported')
Søren Gjesse53d14482021-02-10 16:53:38 +010091 with utils.ChangedWorkingDirectory(checkout_dir):
92 bazel = os.path.join(utils.BAZEL_TOOL, 'lib', 'bazel', 'bin', 'bazel')
Søren Gjesseef195772021-03-11 16:04:42 +010093 cmd = [bazel, 'build', 'maven_release' + ('_jdk11' if variant == 'jdk11' else '')]
Søren Gjesse53d14482021-02-10 16:53:38 +010094 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 Gjesseef195772021-03-11 16:04:42 +0100102 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 Gjesse8a933c52021-07-07 16:07:46 +0200107 checkout_dir, 'bazel-bin', 'jdk11', 'src', 'd8_java_base_selected.jar')
Søren Gjesseef195772021-03-11 16:04:42 +0100108 maven_zip = os.path.join(
109 checkout_dir,
110 'bazel-bin',
111 LIBRARY_NAME + ('_jdk11' if variant != 'jdk11' else '') +'.zip')
Søren Gjesse53d14482021-02-10 16:53:38 +0100112 return (library_jar, maven_zip)
113
114
115def 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 Gjesse1c115b52019-08-14 12:43:57 +0200118
119def Main(argv):
120 (options, args) = ParseOptions(argv)
121 if (len(args) > 0):
122 raise Exception('Unsupported arguments')
Søren Gjesse53d14482021-02-10 16:53:38 +0100123 if not utils.is_bot() and not (options.dry_run or options.build_only):
Søren Gjesse1c115b52019-08-14 12:43:57 +0200124 raise Exception('You are not a bot, don\'t archive builds. '
Søren Gjesse53d14482021-02-10 16:53:38 +0100125 + '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 Gjesse1c115b52019-08-14 12:43:57 +0200130 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 Gjesse699f6362019-10-09 14:56:33 +0200135 utils.DownloadFromGoogleCloudStorage(utils.JAVA8_SHA_FILE)
Søren Gjesseef195772021-03-11 16:04:42 +0100136 utils.DownloadFromGoogleCloudStorage(utils.JAVA11_SHA_FILE)
Søren Gjesse1c115b52019-08-14 12:43:57 +0200137
Søren Gjesse53d14482021-02-10 16:53:38 +0100138 if options.build_only:
139 with utils.TempDir() as checkout_dir:
140 CloneDesugaredLibrary(options.github_account, checkout_dir)
Søren Gjesseef195772021-03-11 16:04:42 +0100141 (library_jar, maven_zip) = BuildDesugaredLibrary(checkout_dir, "jdk8")
Søren Gjesse53d14482021-02-10 16:53:38 +0100142 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 Gjesse1c115b52019-08-14 12:43:57 +0200150 # Only handling versioned desugar_jdk_libs.
Rico Wind1b52acf2021-03-21 12:36:55 +0100151 is_main = False
Søren Gjesse1c115b52019-08-14 12:43:57 +0200152
153 with utils.TempDir() as checkout_dir:
Søren Gjesse53d14482021-02-10 16:53:38 +0100154 CloneDesugaredLibrary(options.github_account, checkout_dir)
155 version = GetVersion(os.path.join(checkout_dir, VERSION_FILE))
Søren Gjesse1c115b52019-08-14 12:43:57 +0200156
Søren Gjesse53d14482021-02-10 16:53:38 +0100157 destination = archive.GetVersionDestination(
Rico Wind1b52acf2021-03-21 12:36:55 +0100158 'gs://', LIBRARY_NAME + '/' + version, is_main)
Søren Gjesse53d14482021-02-10 16:53:38 +0100159 if utils.cloud_storage_exists(destination) and not options.dry_run:
160 raise Exception(
161 'Target archive directory %s already exists' % destination)
Søren Gjesse1c115b52019-08-14 12:43:57 +0200162
Søren Gjesseef195772021-03-11 16:04:42 +0100163 (library_jar, maven_zip) = BuildDesugaredLibrary(checkout_dir, "jdk8")
Søren Gjesse1c115b52019-08-14 12:43:57 +0200164
Søren Gjesse53d14482021-02-10 16:53:38 +0100165 storage_path = LIBRARY_NAME + '/' + version
166 # Upload the jar file with the library.
167 destination = archive.GetUploadDestination(
Rico Wind1b52acf2021-03-21 12:36:55 +0100168 storage_path, LIBRARY_NAME + '.jar', is_main)
169 Upload(options, library_jar, storage_path, destination, is_main)
Søren Gjesse1c115b52019-08-14 12:43:57 +0200170
Søren Gjesse53d14482021-02-10 16:53:38 +0100171 # Upload the maven zip file with the library.
172 destination = archive.GetUploadDestination(
Rico Wind1b52acf2021-03-21 12:36:55 +0100173 storage_path, LIBRARY_NAME + '.zip', is_main)
174 Upload(options, maven_zip, storage_path, destination, is_main)
Søren Gjesse1c115b52019-08-14 12:43:57 +0200175
Søren Gjesse53d14482021-02-10 16:53:38 +0100176 # 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 Wind1b52acf2021-03-21 12:36:55 +0100180 is_main)
Søren Gjesse53d14482021-02-10 16:53:38 +0100181 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 Wind1b52acf2021-03-21 12:36:55 +0100185 print('Maven repo root available at: %s' % archive.GetMavenUrl(is_main))
Søren Gjesse1c115b52019-08-14 12:43:57 +0200186
187
188if __name__ == '__main__':
189 sys.exit(Main(sys.argv[1:]))