Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # 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 Ager | ac79413 | 2017-11-09 11:38:45 +0100 | [diff] [blame] | 6 | import create_maven_release |
Mathias Rav | dd6a6de | 2018-05-18 10:18:33 +0200 | [diff] [blame] | 7 | import gradle |
Ian Zerny | 3f54e22 | 2019-02-12 10:51:17 +0100 | [diff] [blame] | 8 | import jdk |
Rico Wind | 63a1356 | 2018-12-10 14:31:02 +0100 | [diff] [blame] | 9 | import optparse |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 10 | import os |
Rico Wind | cea9ce0 | 2019-03-06 14:25:52 +0100 | [diff] [blame] | 11 | import resource |
Mathias Rav | dd6a6de | 2018-05-18 10:18:33 +0200 | [diff] [blame] | 12 | import shutil |
Rico Wind | 0c24ae7 | 2017-09-08 11:33:56 +0200 | [diff] [blame] | 13 | import subprocess |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 14 | import sys |
Mathias Rav | dd6a6de | 2018-05-18 10:18:33 +0200 | [diff] [blame] | 15 | import toolhelper |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 16 | import utils |
Yohann Roussel | 73f58e1 | 2017-10-13 17:33:14 +0200 | [diff] [blame] | 17 | import zipfile |
Tamas Kenez | 180be09 | 2018-12-05 15:23:06 +0100 | [diff] [blame] | 18 | from build_r8lib import build_r8lib |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 19 | |
Rico Wind | 792e8c7 | 2017-08-30 09:43:46 +0200 | [diff] [blame] | 20 | ARCHIVE_BUCKET = 'r8-releases' |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 21 | |
Rico Wind | 63a1356 | 2018-12-10 14:31:02 +0100 | [diff] [blame] | 22 | def ParseOptions(): |
| 23 | result = optparse.OptionParser() |
| 24 | result.add_option('--dry-run', '--dry_run', |
| 25 | help='Build only, no upload.', |
| 26 | default=False, action='store_true') |
Søren Gjesse | c425e6a | 2019-06-28 11:41:14 +0200 | [diff] [blame] | 27 | result.add_option('--dry-run-output', '--dry_run_output', |
| 28 | help='Output directory for \'build only, no upload\'.', |
| 29 | type="string", action="store") |
Rico Wind | 63a1356 | 2018-12-10 14:31:02 +0100 | [diff] [blame] | 30 | return result.parse_args() |
| 31 | |
Mathias Rav | dd6a6de | 2018-05-18 10:18:33 +0200 | [diff] [blame] | 32 | def GetToolVersion(jar_path): |
Morten Krogh-Jespersen | 0de1373 | 2019-03-01 08:56:39 +0100 | [diff] [blame] | 33 | # TODO(mkroghj) This would not work for r8-lib, maybe use utils.getR8Version. |
Ian Zerny | 3f54e22 | 2019-02-12 10:51:17 +0100 | [diff] [blame] | 34 | output = subprocess.check_output([ |
| 35 | jdk.GetJavaExecutable(), '-jar', jar_path, '--version' |
| 36 | ]) |
Mathias Rav | dd6a6de | 2018-05-18 10:18:33 +0200 | [diff] [blame] | 37 | return output.splitlines()[0].strip() |
| 38 | |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 39 | def GetVersion(): |
Mathias Rav | dd6a6de | 2018-05-18 10:18:33 +0200 | [diff] [blame] | 40 | r8_version = GetToolVersion(utils.R8_JAR) |
| 41 | d8_version = GetToolVersion(utils.D8_JAR) |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 42 | # The version printed is "D8 vVERSION_NUMBER" and "R8 vVERSION_NUMBER" |
| 43 | # Sanity check that versions match. |
| 44 | if d8_version.split()[1] != r8_version.split()[1]: |
| 45 | raise Exception( |
| 46 | 'Version mismatch: \n%s\n%s' % (d8_version, r8_version)) |
| 47 | return d8_version.split()[1] |
| 48 | |
Rico Wind | 0c24ae7 | 2017-09-08 11:33:56 +0200 | [diff] [blame] | 49 | def GetGitBranches(): |
| 50 | return subprocess.check_output(['git', 'show', '-s', '--pretty=%d', 'HEAD']) |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 51 | |
Rico Wind | 0c24ae7 | 2017-09-08 11:33:56 +0200 | [diff] [blame] | 52 | def GetGitHash(): |
| 53 | return subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip() |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 54 | |
Rico Wind | 0c24ae7 | 2017-09-08 11:33:56 +0200 | [diff] [blame] | 55 | def IsMaster(version): |
Rico Wind | 2f72029 | 2017-10-06 14:32:12 +0200 | [diff] [blame] | 56 | branches = subprocess.check_output(['git', 'branch', '-r', '--contains', |
Rico Wind | 0c24ae7 | 2017-09-08 11:33:56 +0200 | [diff] [blame] | 57 | 'HEAD']) |
Rico Wind | d450ba1 | 2019-04-24 13:18:40 +0200 | [diff] [blame] | 58 | # CL runs from gerrit does not have a branch, we always treat them as master |
| 59 | # commits to archive these to the hash based location |
| 60 | if len(branches) == 0: |
| 61 | return True |
Rico Wind | 0c24ae7 | 2017-09-08 11:33:56 +0200 | [diff] [blame] | 62 | if not version.endswith('-dev'): |
| 63 | # Sanity check, we don't want to archive on top of release builds EVER |
| 64 | # Note that even though we branch, we never push the bots to build the same |
| 65 | # commit as master on a branch since we always change the version to |
| 66 | # not have dev (or we crash here :-)). |
| 67 | if 'origin/master' in branches: |
| 68 | raise Exception('We are seeing origin/master in a commit that ' |
| 69 | 'don\'t have -dev in version') |
Mads Ager | ac79413 | 2017-11-09 11:38:45 +0100 | [diff] [blame] | 70 | return False |
Rico Wind | 0c24ae7 | 2017-09-08 11:33:56 +0200 | [diff] [blame] | 71 | if not 'origin/master' in branches: |
| 72 | raise Exception('We are not seeing origin/master ' |
| 73 | 'in a commit that have -dev in version') |
Mads Ager | ac79413 | 2017-11-09 11:38:45 +0100 | [diff] [blame] | 74 | return True |
Rico Wind | 0c24ae7 | 2017-09-08 11:33:56 +0200 | [diff] [blame] | 75 | |
Rico Wind | c0b1638 | 2018-05-17 13:23:43 +0200 | [diff] [blame] | 76 | def GetStorageDestination(storage_prefix, |
| 77 | version_or_path, |
| 78 | file_name, |
| 79 | is_master): |
Rico Wind | 0c24ae7 | 2017-09-08 11:33:56 +0200 | [diff] [blame] | 80 | # We archive master commits under raw/master instead of directly under raw |
Rico Wind | c0b1638 | 2018-05-17 13:23:43 +0200 | [diff] [blame] | 81 | version_dir = GetVersionDestination(storage_prefix, |
| 82 | version_or_path, |
| 83 | is_master) |
Rico Wind | 1a29c4f | 2018-01-25 08:43:08 +0100 | [diff] [blame] | 84 | return '%s/%s' % (version_dir, file_name) |
| 85 | |
Rico Wind | c0b1638 | 2018-05-17 13:23:43 +0200 | [diff] [blame] | 86 | def GetVersionDestination(storage_prefix, version_or_path, is_master): |
Rico Wind | 0c24ae7 | 2017-09-08 11:33:56 +0200 | [diff] [blame] | 87 | archive_dir = 'raw/master' if is_master else 'raw' |
Rico Wind | c0b1638 | 2018-05-17 13:23:43 +0200 | [diff] [blame] | 88 | return '%s%s/%s/%s' % (storage_prefix, ARCHIVE_BUCKET, |
| 89 | archive_dir, version_or_path) |
Rico Wind | 0c24ae7 | 2017-09-08 11:33:56 +0200 | [diff] [blame] | 90 | |
Rico Wind | c0b1638 | 2018-05-17 13:23:43 +0200 | [diff] [blame] | 91 | def GetUploadDestination(version_or_path, file_name, is_master): |
| 92 | return GetStorageDestination('gs://', version_or_path, file_name, is_master) |
Rico Wind | 0c24ae7 | 2017-09-08 11:33:56 +0200 | [diff] [blame] | 93 | |
Rico Wind | c0b1638 | 2018-05-17 13:23:43 +0200 | [diff] [blame] | 94 | def GetUrl(version_or_path, file_name, is_master): |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 95 | return GetStorageDestination('http://storage.googleapis.com/', |
Rico Wind | c0b1638 | 2018-05-17 13:23:43 +0200 | [diff] [blame] | 96 | version_or_path, file_name, is_master) |
| 97 | |
| 98 | def GetMavenUrl(is_master): |
| 99 | return GetVersionDestination('http://storage.googleapis.com/', '', is_master) |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 100 | |
Rico Wind | 7219bb0 | 2019-03-18 08:30:12 +0100 | [diff] [blame] | 101 | def SetRLimitToMax(): |
| 102 | (soft, hard) = resource.getrlimit(resource.RLIMIT_NOFILE) |
| 103 | resource.setrlimit(resource.RLIMIT_NOFILE, (hard, hard)) |
| 104 | |
Rico Wind | cea9ce0 | 2019-03-06 14:25:52 +0100 | [diff] [blame] | 105 | def PrintResourceInfo(): |
| 106 | (soft, hard) = resource.getrlimit(resource.RLIMIT_NOFILE) |
| 107 | print('INFO: Open files soft limit: %s' % soft) |
| 108 | print('INFO: Open files hard limit: %s' % hard) |
| 109 | |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 110 | def Main(): |
Rico Wind | 63a1356 | 2018-12-10 14:31:02 +0100 | [diff] [blame] | 111 | (options, args) = ParseOptions() |
Rico Wind | 089ca04 | 2019-03-06 13:27:25 +0000 | [diff] [blame] | 112 | if not utils.is_bot() and not options.dry_run: |
Søren Gjesse | c425e6a | 2019-06-28 11:41:14 +0200 | [diff] [blame] | 113 | raise Exception('You are not a bot, don\'t archive builds. ' |
| 114 | + 'Use --dry-run to test locally') |
Søren Gjesse | c425e6a | 2019-06-28 11:41:14 +0200 | [diff] [blame] | 115 | if (options.dry_run_output and |
| 116 | (not os.path.exists(options.dry_run_output) or |
| 117 | not os.path.isdir(options.dry_run_output))): |
| 118 | raise Exception(options.dry_run_output |
| 119 | + ' does not exist or is not a directory') |
Tamas Kenez | 180be09 | 2018-12-05 15:23:06 +0100 | [diff] [blame] | 120 | |
Rico Wind | 7219bb0 | 2019-03-18 08:30:12 +0100 | [diff] [blame] | 121 | if utils.is_bot(): |
| 122 | SetRLimitToMax() |
Rico Wind | cea9ce0 | 2019-03-06 14:25:52 +0100 | [diff] [blame] | 123 | PrintResourceInfo() |
Tamas Kenez | 180be09 | 2018-12-05 15:23:06 +0100 | [diff] [blame] | 124 | # Create maven release which uses a build that exclude dependencies. |
Rico Wind | 8fc8bfa | 2019-03-22 09:57:36 +0100 | [diff] [blame] | 125 | create_maven_release.run(utils.MAVEN_ZIP) |
| 126 | create_maven_release.run(utils.MAVEN_ZIP_LIB, is_r8lib=True) |
Mads Ager | a4911eb | 2017-11-22 13:19:36 +0100 | [diff] [blame] | 127 | |
Tamas Kenez | 180be09 | 2018-12-05 15:23:06 +0100 | [diff] [blame] | 128 | # Generate and copy a full build without dependencies. |
Mads Ager | b10c07f | 2017-11-27 13:25:52 +0100 | [diff] [blame] | 129 | gradle.RunGradleExcludeDeps([utils.R8, utils.R8_SRC]) |
Tamas Kenez | 180be09 | 2018-12-05 15:23:06 +0100 | [diff] [blame] | 130 | shutil.copyfile(utils.R8_JAR, utils.R8_FULL_EXCLUDE_DEPS_JAR) |
Mads Ager | 0bd1ebd | 2017-11-22 13:40:21 +0100 | [diff] [blame] | 131 | |
Mads Ager | ac79413 | 2017-11-09 11:38:45 +0100 | [diff] [blame] | 132 | # Ensure all archived artifacts has been built before archiving. |
Tamas Kenez | 03ab76f | 2018-12-07 14:33:25 +0100 | [diff] [blame] | 133 | # The target tasks postfixed by 'lib' depend on the actual target task so |
Tamas Kenez | f960e9c | 2018-12-03 16:13:29 +0100 | [diff] [blame] | 134 | # building it invokes the original task first. |
Morten Krogh-Jespersen | e28db46 | 2019-01-09 13:32:15 +0100 | [diff] [blame] | 135 | # The '-Pno_internal' flag is important because we generate the lib based on uses in tests. |
Tamas Kenez | 03ab76f | 2018-12-07 14:33:25 +0100 | [diff] [blame] | 136 | gradle.RunGradle([ |
| 137 | utils.R8, |
| 138 | utils.D8, |
| 139 | utils.COMPATDX, |
| 140 | utils.COMPATPROGUARD, |
| 141 | utils.R8LIB, |
Morten Krogh-Jespersen | cae32a7 | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 142 | utils.R8LIB_NO_DEPS, |
Tamas Kenez | 03ab76f | 2018-12-07 14:33:25 +0100 | [diff] [blame] | 143 | utils.COMPATDXLIB, |
| 144 | utils.COMPATPROGUARDLIB, |
Morten Krogh-Jespersen | e28db46 | 2019-01-09 13:32:15 +0100 | [diff] [blame] | 145 | '-Pno_internal' |
Tamas Kenez | 03ab76f | 2018-12-07 14:33:25 +0100 | [diff] [blame] | 146 | ]) |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 147 | version = GetVersion() |
Mads Ager | ac79413 | 2017-11-09 11:38:45 +0100 | [diff] [blame] | 148 | is_master = IsMaster(version) |
Rico Wind | 0c24ae7 | 2017-09-08 11:33:56 +0200 | [diff] [blame] | 149 | if is_master: |
| 150 | # On master we use the git hash to archive with |
| 151 | print 'On master, using git hash for archiving' |
| 152 | version = GetGitHash() |
| 153 | |
Rico Wind | 1a29c4f | 2018-01-25 08:43:08 +0100 | [diff] [blame] | 154 | destination = GetVersionDestination('gs://', version, is_master) |
Søren Gjesse | de7dd45 | 2019-03-06 16:03:17 +0100 | [diff] [blame] | 155 | if utils.cloud_storage_exists(destination) and not options.dry_run: |
Rico Wind | 1a29c4f | 2018-01-25 08:43:08 +0100 | [diff] [blame] | 156 | raise Exception('Target archive directory %s already exists' % destination) |
Yohann Roussel | 73f58e1 | 2017-10-13 17:33:14 +0200 | [diff] [blame] | 157 | with utils.TempDir() as temp: |
| 158 | version_file = os.path.join(temp, 'r8-version.properties') |
| 159 | with open(version_file,'w') as version_writer: |
| 160 | version_writer.write('version.sha=' + GetGitHash() + '\n') |
Søren Gjesse | c425e6a | 2019-06-28 11:41:14 +0200 | [diff] [blame] | 161 | if not os.environ.get('SWARMING_BOT_ID') and not options.dry_run: |
| 162 | raise Exception('Environment variable SWARMING_BOT_ID not set') |
| 163 | |
| 164 | releaser = \ |
| 165 | ("<local developer build>" if options.dry_run |
| 166 | else 'releaser=go/r8bot (' |
| 167 | + os.environ.get('SWARMING_BOT_ID') + ')\n') |
| 168 | version_writer.write(releaser) |
Yohann Roussel | 73f58e1 | 2017-10-13 17:33:14 +0200 | [diff] [blame] | 169 | version_writer.write('version-file.version.code=1\n') |
| 170 | |
Tamas Kenez | 03ab76f | 2018-12-07 14:33:25 +0100 | [diff] [blame] | 171 | for file in [ |
| 172 | utils.D8_JAR, |
| 173 | utils.R8_JAR, |
| 174 | utils.R8LIB_JAR, |
Tamas Kenez | 54b939a | 2018-12-07 15:55:26 +0100 | [diff] [blame] | 175 | utils.R8LIB_JAR + '.map', |
Tamas Kenez | 03ab76f | 2018-12-07 14:33:25 +0100 | [diff] [blame] | 176 | utils.R8_SRC_JAR, |
| 177 | utils.R8_FULL_EXCLUDE_DEPS_JAR, |
| 178 | utils.R8LIB_EXCLUDE_DEPS_JAR, |
Tamas Kenez | 54b939a | 2018-12-07 15:55:26 +0100 | [diff] [blame] | 179 | utils.R8LIB_EXCLUDE_DEPS_JAR + '.map', |
Tamas Kenez | 03ab76f | 2018-12-07 14:33:25 +0100 | [diff] [blame] | 180 | utils.COMPATDX_JAR, |
| 181 | utils.COMPATDXLIB_JAR, |
Tamas Kenez | 54b939a | 2018-12-07 15:55:26 +0100 | [diff] [blame] | 182 | utils.COMPATDXLIB_JAR + '.map', |
Tamas Kenez | 03ab76f | 2018-12-07 14:33:25 +0100 | [diff] [blame] | 183 | utils.COMPATPROGUARD_JAR, |
| 184 | utils.COMPATPROGUARDLIB_JAR, |
Tamas Kenez | 54b939a | 2018-12-07 15:55:26 +0100 | [diff] [blame] | 185 | utils.COMPATPROGUARDLIB_JAR + '.map', |
Tamas Kenez | 03ab76f | 2018-12-07 14:33:25 +0100 | [diff] [blame] | 186 | utils.MAVEN_ZIP, |
Rico Wind | 8fc8bfa | 2019-03-22 09:57:36 +0100 | [diff] [blame] | 187 | utils.MAVEN_ZIP_LIB, |
Tamas Kenez | 03ab76f | 2018-12-07 14:33:25 +0100 | [diff] [blame] | 188 | utils.GENERATED_LICENSE, |
| 189 | ]: |
Mads Ager | ac79413 | 2017-11-09 11:38:45 +0100 | [diff] [blame] | 190 | file_name = os.path.basename(file) |
Yohann Roussel | 73f58e1 | 2017-10-13 17:33:14 +0200 | [diff] [blame] | 191 | tagged_jar = os.path.join(temp, file_name) |
Mads Ager | ac79413 | 2017-11-09 11:38:45 +0100 | [diff] [blame] | 192 | shutil.copyfile(file, tagged_jar) |
Mads Ager | b10c07f | 2017-11-27 13:25:52 +0100 | [diff] [blame] | 193 | if file_name.endswith('.jar') and not file_name.endswith('-src.jar'): |
Mads Ager | afc0cda | 2017-11-27 13:04:27 +0100 | [diff] [blame] | 194 | with zipfile.ZipFile(tagged_jar, 'a') as zip: |
| 195 | zip.write(version_file, os.path.basename(version_file)) |
Yohann Roussel | 73f58e1 | 2017-10-13 17:33:14 +0200 | [diff] [blame] | 196 | destination = GetUploadDestination(version, file_name, is_master) |
| 197 | print('Uploading %s to %s' % (tagged_jar, destination)) |
Rico Wind | 63a1356 | 2018-12-10 14:31:02 +0100 | [diff] [blame] | 198 | if options.dry_run: |
Søren Gjesse | c425e6a | 2019-06-28 11:41:14 +0200 | [diff] [blame] | 199 | if options.dry_run_output: |
| 200 | dry_run_destination = os.path.join(options.dry_run_output, file_name) |
| 201 | print('Dry run, not actually uploading. Copying to ' |
| 202 | + dry_run_destination) |
| 203 | shutil.copyfile(tagged_jar, dry_run_destination) |
| 204 | else: |
| 205 | print('Dry run, not actually uploading') |
Rico Wind | 63a1356 | 2018-12-10 14:31:02 +0100 | [diff] [blame] | 206 | else: |
| 207 | utils.upload_file_to_cloud_storage(tagged_jar, destination) |
| 208 | print('File available at: %s' % GetUrl(version, file_name, is_master)) |
Rico Wind | c0b1638 | 2018-05-17 13:23:43 +0200 | [diff] [blame] | 209 | if file == utils.R8_JAR: |
| 210 | # Upload R8 to a maven compatible location. |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 211 | maven_dst = GetUploadDestination(utils.get_maven_path('r8', version), |
Rico Wind | c0b1638 | 2018-05-17 13:23:43 +0200 | [diff] [blame] | 212 | 'r8-%s.jar' % version, is_master) |
Rico Wind | 63a1356 | 2018-12-10 14:31:02 +0100 | [diff] [blame] | 213 | if options.dry_run: |
| 214 | print('Dry run, not actually creating maven repo') |
| 215 | else: |
| 216 | utils.upload_file_to_cloud_storage(tagged_jar, maven_dst) |
| 217 | print('Maven repo root available at: %s' % GetMavenUrl(is_master)) |
Rico Wind | c0b1638 | 2018-05-17 13:23:43 +0200 | [diff] [blame] | 218 | |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 219 | |
| 220 | if __name__ == '__main__': |
| 221 | sys.exit(Main()) |