blob: d7bf1a930588a2f7ecbcb2a52593a0825224b762 [file] [log] [blame]
Ian Zernydcb172e2022-02-22 15:36:45 +01001#!/usr/bin/env python3
Rico Windb4621c12017-08-28 12:48:53 +02002# Copyright (c) 2017, 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
Mads Agerac794132017-11-09 11:38:45 +01006import create_maven_release
Mathias Ravdd6a6de2018-05-18 10:18:33 +02007import gradle
Ian Zerny3f54e222019-02-12 10:51:17 +01008import jdk
Rico Wind63a13562018-12-10 14:31:02 +01009import optparse
Rico Windb4621c12017-08-28 12:48:53 +020010import os
Morten Krogh-Jespersenfe0d7e12020-01-31 08:50:17 +010011try:
12 import resource
13except ImportError:
14 # Not a Unix system. Do what Gandalf tells you not to.
15 pass
Mathias Ravdd6a6de2018-05-18 10:18:33 +020016import shutil
Rico Wind0c24ae72017-09-08 11:33:56 +020017import subprocess
Rico Windb4621c12017-08-28 12:48:53 +020018import sys
Mathias Ravdd6a6de2018-05-18 10:18:33 +020019import toolhelper
Rico Windb4621c12017-08-28 12:48:53 +020020import utils
Yohann Roussel73f58e12017-10-13 17:33:14 +020021import zipfile
Tamas Kenez180be092018-12-05 15:23:06 +010022from build_r8lib import build_r8lib
Rico Windb4621c12017-08-28 12:48:53 +020023
Rico Wind792e8c72017-08-30 09:43:46 +020024ARCHIVE_BUCKET = 'r8-releases'
Rico Windb4621c12017-08-28 12:48:53 +020025
Rico Wind63a13562018-12-10 14:31:02 +010026def ParseOptions():
27 result = optparse.OptionParser()
28 result.add_option('--dry-run', '--dry_run',
29 help='Build only, no upload.',
30 default=False, action='store_true')
Søren Gjessec425e6a2019-06-28 11:41:14 +020031 result.add_option('--dry-run-output', '--dry_run_output',
32 help='Output directory for \'build only, no upload\'.',
33 type="string", action="store")
Rico Wind63a13562018-12-10 14:31:02 +010034 return result.parse_args()
35
Rico Windb4621c12017-08-28 12:48:53 +020036def GetVersion():
Rico Windd31b5892022-04-25 11:06:30 +020037 output = subprocess.check_output([
38 jdk.GetJavaExecutable(), '-cp', utils.R8_JAR, 'com.android.tools.r8.R8',
39 '--version'
40 ]).decode('utf-8')
41 r8_version = output.splitlines()[0].strip()
42 return r8_version.split()[1]
Rico Windb4621c12017-08-28 12:48:53 +020043
Rico Wind0c24ae72017-09-08 11:33:56 +020044def GetGitBranches():
45 return subprocess.check_output(['git', 'show', '-s', '--pretty=%d', 'HEAD'])
Rico Windb4621c12017-08-28 12:48:53 +020046
Rico Wind0c24ae72017-09-08 11:33:56 +020047def GetGitHash():
Rico Windfd186372022-02-28 08:55:48 +010048 return subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode('utf-8').strip()
Rico Windb4621c12017-08-28 12:48:53 +020049
Rico Wind1b52acf2021-03-21 12:36:55 +010050def IsMain(version):
Rico Windfd186372022-02-28 08:55:48 +010051 branches = subprocess.check_output(['git', 'branch', '-r', '--contains',
52 'HEAD']).decode('utf-8')
Rico Wind1b52acf2021-03-21 12:36:55 +010053 # CL runs from gerrit does not have a branch, we always treat them as main
Rico Windd450ba12019-04-24 13:18:40 +020054 # commits to archive these to the hash based location
55 if len(branches) == 0:
56 return True
Rico Wind1b52acf2021-03-21 12:36:55 +010057 if not version == 'main':
Rico Wind0c24ae72017-09-08 11:33:56 +020058 # Sanity check, we don't want to archive on top of release builds EVER
59 # Note that even though we branch, we never push the bots to build the same
Rico Wind1b52acf2021-03-21 12:36:55 +010060 # commit as main on a branch since we always change the version to
61 # not be just 'main' (or we crash here :-)).
62 if 'origin/main' in branches:
63 raise Exception('We are seeing origin/main in a commit that '
64 'don\'t have \'main\' as version')
Mads Agerac794132017-11-09 11:38:45 +010065 return False
Rico Wind1b52acf2021-03-21 12:36:55 +010066 if not 'origin/main' in branches:
67 raise Exception('We are not seeing origin/main '
68 'in a commit that have \'main\' as version')
Mads Agerac794132017-11-09 11:38:45 +010069 return True
Rico Wind0c24ae72017-09-08 11:33:56 +020070
Rico Windc0b16382018-05-17 13:23:43 +020071def GetStorageDestination(storage_prefix,
72 version_or_path,
73 file_name,
Rico Wind1b52acf2021-03-21 12:36:55 +010074 is_main):
75 # We archive main commits under raw/main instead of directly under raw
Rico Windc0b16382018-05-17 13:23:43 +020076 version_dir = GetVersionDestination(storage_prefix,
77 version_or_path,
Rico Wind1b52acf2021-03-21 12:36:55 +010078 is_main)
Rico Wind1a29c4f2018-01-25 08:43:08 +010079 return '%s/%s' % (version_dir, file_name)
80
Rico Wind1b52acf2021-03-21 12:36:55 +010081def GetVersionDestination(storage_prefix, version_or_path, is_main):
82 archive_dir = 'raw/main' if is_main else 'raw'
Rico Windc0b16382018-05-17 13:23:43 +020083 return '%s%s/%s/%s' % (storage_prefix, ARCHIVE_BUCKET,
84 archive_dir, version_or_path)
Rico Wind0c24ae72017-09-08 11:33:56 +020085
Rico Wind1b52acf2021-03-21 12:36:55 +010086def GetUploadDestination(version_or_path, file_name, is_main):
87 return GetStorageDestination('gs://', version_or_path, file_name, is_main)
Rico Wind0c24ae72017-09-08 11:33:56 +020088
Rico Wind1b52acf2021-03-21 12:36:55 +010089def GetUrl(version_or_path, file_name, is_main):
Rico Wind70d614f2020-01-31 08:45:21 +010090 return GetStorageDestination('https://storage.googleapis.com/',
Rico Wind1b52acf2021-03-21 12:36:55 +010091 version_or_path, file_name, is_main)
Rico Windc0b16382018-05-17 13:23:43 +020092
Rico Wind1b52acf2021-03-21 12:36:55 +010093def GetMavenUrl(is_main):
94 return GetVersionDestination('https://storage.googleapis.com/', '', is_main)
Rico Windb4621c12017-08-28 12:48:53 +020095
Rico Wind7219bb02019-03-18 08:30:12 +010096def SetRLimitToMax():
97 (soft, hard) = resource.getrlimit(resource.RLIMIT_NOFILE)
98 resource.setrlimit(resource.RLIMIT_NOFILE, (hard, hard))
99
Rico Windcea9ce02019-03-06 14:25:52 +0100100def PrintResourceInfo():
101 (soft, hard) = resource.getrlimit(resource.RLIMIT_NOFILE)
102 print('INFO: Open files soft limit: %s' % soft)
103 print('INFO: Open files hard limit: %s' % hard)
104
Rico Windb4621c12017-08-28 12:48:53 +0200105def Main():
Rico Wind63a13562018-12-10 14:31:02 +0100106 (options, args) = ParseOptions()
Rico Wind089ca042019-03-06 13:27:25 +0000107 if not utils.is_bot() and not options.dry_run:
Søren Gjessec425e6a2019-06-28 11:41:14 +0200108 raise Exception('You are not a bot, don\'t archive builds. '
109 + 'Use --dry-run to test locally')
Søren Gjessec425e6a2019-06-28 11:41:14 +0200110 if (options.dry_run_output and
111 (not os.path.exists(options.dry_run_output) or
112 not os.path.isdir(options.dry_run_output))):
113 raise Exception(options.dry_run_output
114 + ' does not exist or is not a directory')
Tamas Kenez180be092018-12-05 15:23:06 +0100115
Morten Krogh-Jespersenfe0d7e12020-01-31 08:50:17 +0100116 if utils.is_bot() and not utils.IsWindows():
Rico Wind7219bb02019-03-18 08:30:12 +0100117 SetRLimitToMax()
Morten Krogh-Jespersenfe0d7e12020-01-31 08:50:17 +0100118 if not utils.IsWindows():
119 PrintResourceInfo()
Søren Gjesse6e5e5842019-09-03 08:48:30 +0200120
Yohann Roussel73f58e12017-10-13 17:33:14 +0200121 with utils.TempDir() as temp:
Rico Wind257044c2019-11-22 08:21:21 +0100122
Yohann Roussel73f58e12017-10-13 17:33:14 +0200123 version_file = os.path.join(temp, 'r8-version.properties')
124 with open(version_file,'w') as version_writer:
125 version_writer.write('version.sha=' + GetGitHash() + '\n')
Søren Gjessec425e6a2019-06-28 11:41:14 +0200126 if not os.environ.get('SWARMING_BOT_ID') and not options.dry_run:
127 raise Exception('Environment variable SWARMING_BOT_ID not set')
128
129 releaser = \
130 ("<local developer build>" if options.dry_run
131 else 'releaser=go/r8bot ('
132 + os.environ.get('SWARMING_BOT_ID') + ')\n')
133 version_writer.write(releaser)
Yohann Roussel73f58e12017-10-13 17:33:14 +0200134 version_writer.write('version-file.version.code=1\n')
135
Rico Windcdc39b62022-04-08 12:37:57 +0200136 # Create maven release which uses a build that exclude dependencies.
137 create_maven_release.generate_r8_maven_zip(utils.MAVEN_ZIP, version_file=version_file)
138 create_maven_release.generate_r8_maven_zip(
139 utils.MAVEN_ZIP_LIB, is_r8lib=True, version_file=version_file)
140
141 # Generate and copy a full build without dependencies.
142 gradle.RunGradleExcludeDeps([utils.R8, utils.R8_SRC])
143 shutil.copyfile(utils.R8_JAR, utils.R8_FULL_EXCLUDE_DEPS_JAR)
144
145 # Ensure all archived artifacts has been built before archiving.
146 # The target tasks postfixed by 'lib' depend on the actual target task so
147 # building it invokes the original task first.
148 # The '-Pno_internal' flag is important because we generate the lib based on uses in tests.
149 gradle.RunGradle([
150 utils.R8,
Rico Windcdc39b62022-04-08 12:37:57 +0200151 utils.R8LIB,
152 utils.R8LIB_NO_DEPS,
153 utils.R8RETRACE,
154 utils.R8RETRACE_NO_DEPS,
155 utils.LIBRARY_DESUGAR_CONVERSIONS,
156 '-Pno_internal'
157 ])
158
159 # Create maven release of the desuage_jdk_libs configuration. This require
160 # an r8.jar with dependencies to have been built.
161 create_maven_release.generate_desugar_configuration_maven_zip(
162 utils.DESUGAR_CONFIGURATION_MAVEN_ZIP,
163 utils.DESUGAR_CONFIGURATION,
164 utils.DESUGAR_IMPLEMENTATION)
165 create_maven_release.generate_desugar_configuration_maven_zip(
166 utils.DESUGAR_CONFIGURATION_LEGACY_JDK11_MAVEN_ZIP,
167 utils.DESUGAR_CONFIGURATION_JDK11_LEGACY,
168 utils.DESUGAR_IMPLEMENTATION_JDK11)
169
170 version = GetVersion()
171 is_main = IsMain(version)
172 if is_main:
173 # On main we use the git hash to archive with
174 print('On main, using git hash for archiving')
175 version = GetGitHash()
176
177 destination = GetVersionDestination('gs://', version, is_main)
178 if utils.cloud_storage_exists(destination) and not options.dry_run:
179 raise Exception('Target archive directory %s already exists' % destination)
180
181 # Create pom file for our maven repository that we build for testing.
182 default_pom_file = os.path.join(temp, 'r8.pom')
183 create_maven_release.write_default_r8_pom_file(default_pom_file, version)
184
Tamas Kenez03ab76f2018-12-07 14:33:25 +0100185 for file in [
Tamas Kenez03ab76f2018-12-07 14:33:25 +0100186 utils.R8_JAR,
187 utils.R8LIB_JAR,
Tamas Kenez54b939a2018-12-07 15:55:26 +0100188 utils.R8LIB_JAR + '.map',
Tamas Kenez03ab76f2018-12-07 14:33:25 +0100189 utils.R8_SRC_JAR,
190 utils.R8_FULL_EXCLUDE_DEPS_JAR,
191 utils.R8LIB_EXCLUDE_DEPS_JAR,
Tamas Kenez54b939a2018-12-07 15:55:26 +0100192 utils.R8LIB_EXCLUDE_DEPS_JAR + '.map',
Morten Krogh-Jespersen98ee89a2021-10-25 20:59:02 +0200193 utils.R8RETRACE_JAR,
194 utils.R8RETRACE_EXCLUDE_DEPS_JAR,
Tamas Kenez03ab76f2018-12-07 14:33:25 +0100195 utils.MAVEN_ZIP,
Rico Wind8fc8bfa2019-03-22 09:57:36 +0100196 utils.MAVEN_ZIP_LIB,
Søren Gjesse6e5e5842019-09-03 08:48:30 +0200197 utils.DESUGAR_CONFIGURATION,
198 utils.DESUGAR_CONFIGURATION_MAVEN_ZIP,
Tamas Kenez03ab76f2018-12-07 14:33:25 +0100199 utils.GENERATED_LICENSE,
200 ]:
Mads Agerac794132017-11-09 11:38:45 +0100201 file_name = os.path.basename(file)
Yohann Roussel73f58e12017-10-13 17:33:14 +0200202 tagged_jar = os.path.join(temp, file_name)
Mads Agerac794132017-11-09 11:38:45 +0100203 shutil.copyfile(file, tagged_jar)
Mads Agerb10c07f2017-11-27 13:25:52 +0100204 if file_name.endswith('.jar') and not file_name.endswith('-src.jar'):
Mads Agerafc0cda2017-11-27 13:04:27 +0100205 with zipfile.ZipFile(tagged_jar, 'a') as zip:
206 zip.write(version_file, os.path.basename(version_file))
Rico Wind1b52acf2021-03-21 12:36:55 +0100207 destination = GetUploadDestination(version, file_name, is_main)
Yohann Roussel73f58e12017-10-13 17:33:14 +0200208 print('Uploading %s to %s' % (tagged_jar, destination))
Rico Wind63a13562018-12-10 14:31:02 +0100209 if options.dry_run:
Søren Gjessec425e6a2019-06-28 11:41:14 +0200210 if options.dry_run_output:
211 dry_run_destination = os.path.join(options.dry_run_output, file_name)
212 print('Dry run, not actually uploading. Copying to '
Søren Gjesse6e5e5842019-09-03 08:48:30 +0200213 + dry_run_destination)
Søren Gjessec425e6a2019-06-28 11:41:14 +0200214 shutil.copyfile(tagged_jar, dry_run_destination)
215 else:
216 print('Dry run, not actually uploading')
Rico Wind63a13562018-12-10 14:31:02 +0100217 else:
218 utils.upload_file_to_cloud_storage(tagged_jar, destination)
Rico Wind1b52acf2021-03-21 12:36:55 +0100219 print('File available at: %s' % GetUrl(version, file_name, is_main))
Søren Gjesse6e5e5842019-09-03 08:48:30 +0200220
221 # Upload R8 to a maven compatible location.
Rico Windc0b16382018-05-17 13:23:43 +0200222 if file == utils.R8_JAR:
Søren Gjesse1c115b52019-08-14 12:43:57 +0200223 maven_dst = GetUploadDestination(utils.get_maven_path('r8', version),
Rico Wind1b52acf2021-03-21 12:36:55 +0100224 'r8-%s.jar' % version, is_main)
Rico Wind257044c2019-11-22 08:21:21 +0100225 maven_pom_dst = GetUploadDestination(
226 utils.get_maven_path('r8', version),
Rico Wind1b52acf2021-03-21 12:36:55 +0100227 'r8-%s.pom' % version, is_main)
Rico Wind63a13562018-12-10 14:31:02 +0100228 if options.dry_run:
Søren Gjesse6e5e5842019-09-03 08:48:30 +0200229 print('Dry run, not actually creating maven repo for R8')
Rico Wind63a13562018-12-10 14:31:02 +0100230 else:
231 utils.upload_file_to_cloud_storage(tagged_jar, maven_dst)
Rico Wind257044c2019-11-22 08:21:21 +0100232 utils.upload_file_to_cloud_storage(default_pom_file, maven_pom_dst)
Rico Wind1b52acf2021-03-21 12:36:55 +0100233 print('Maven repo root available at: %s' % GetMavenUrl(is_main))
Rico Windc0b16382018-05-17 13:23:43 +0200234
Søren Gjesse6e5e5842019-09-03 08:48:30 +0200235 # Upload desugar_jdk_libs configuration to a maven compatible location.
236 if file == utils.DESUGAR_CONFIGURATION:
Rico Wind92f796f2020-08-25 14:36:18 +0200237 jar_basename = 'desugar_jdk_libs_configuration.jar'
238 jar_version_name = 'desugar_jdk_libs_configuration-%s.jar' % version
Søren Gjesse6e5e5842019-09-03 08:48:30 +0200239 maven_dst = GetUploadDestination(
240 utils.get_maven_path('desugar_jdk_libs_configuration', version),
Rico Wind1b52acf2021-03-21 12:36:55 +0100241 jar_version_name, is_main)
Søren Gjesse6e5e5842019-09-03 08:48:30 +0200242
243 with utils.TempDir() as tmp_dir:
Rico Wind92f796f2020-08-25 14:36:18 +0200244 desugar_jdk_libs_configuration_jar = os.path.join(tmp_dir,
245 jar_version_name)
Søren Gjesse6e5e5842019-09-03 08:48:30 +0200246 create_maven_release.generate_jar_with_desugar_configuration(
Søren Gjesse17fc67d2019-12-04 14:50:17 +0100247 utils.DESUGAR_CONFIGURATION,
Søren Gjessedd1f8152020-10-30 13:00:01 +0100248 utils.DESUGAR_IMPLEMENTATION,
Søren Gjesse17fc67d2019-12-04 14:50:17 +0100249 utils.LIBRARY_DESUGAR_CONVERSIONS_ZIP,
250 desugar_jdk_libs_configuration_jar)
Søren Gjesse6e5e5842019-09-03 08:48:30 +0200251
252 if options.dry_run:
253 print('Dry run, not actually creating maven repo for '
254 + 'desugar configuration.')
Søren Gjesse706f7552019-09-23 13:34:58 +0200255 if options.dry_run_output:
256 shutil.copyfile(
257 desugar_jdk_libs_configuration_jar,
Rico Wind92f796f2020-08-25 14:36:18 +0200258 os.path.join(options.dry_run_output, jar_version_name))
Søren Gjesse6e5e5842019-09-03 08:48:30 +0200259 else:
260 utils.upload_file_to_cloud_storage(
261 desugar_jdk_libs_configuration_jar, maven_dst)
Rico Wind1b52acf2021-03-21 12:36:55 +0100262 print('Maven repo root available at: %s' % GetMavenUrl(is_main))
Rico Wind92f796f2020-08-25 14:36:18 +0200263 # Also archive the jar as non maven destination for Google3
264 jar_destination = GetUploadDestination(
Rico Wind1b52acf2021-03-21 12:36:55 +0100265 version, jar_basename, is_main)
Rico Wind92f796f2020-08-25 14:36:18 +0200266 utils.upload_file_to_cloud_storage(
267 desugar_jdk_libs_configuration_jar, jar_destination)
268
Rico Windb4621c12017-08-28 12:48:53 +0200269
270if __name__ == '__main__':
271 sys.exit(Main())