blob: 909faa2ad7d944eda198281c04fd08495cbd142c [file] [log] [blame]
Rico Windb4621c12017-08-28 12:48:53 +02001#!/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 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
Rico Windcea9ce02019-03-06 14:25:52 +010011import resource
Mathias Ravdd6a6de2018-05-18 10:18:33 +020012import shutil
Rico Wind0c24ae72017-09-08 11:33:56 +020013import subprocess
Rico Windb4621c12017-08-28 12:48:53 +020014import sys
Mathias Ravdd6a6de2018-05-18 10:18:33 +020015import toolhelper
Rico Windb4621c12017-08-28 12:48:53 +020016import utils
Yohann Roussel73f58e12017-10-13 17:33:14 +020017import zipfile
Tamas Kenez180be092018-12-05 15:23:06 +010018from build_r8lib import build_r8lib
Rico Windb4621c12017-08-28 12:48:53 +020019
Rico Wind792e8c72017-08-30 09:43:46 +020020ARCHIVE_BUCKET = 'r8-releases'
Rico Windb4621c12017-08-28 12:48:53 +020021
Rico Wind63a13562018-12-10 14:31:02 +010022def 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')
27 return result.parse_args()
28
Mathias Ravdd6a6de2018-05-18 10:18:33 +020029def GetToolVersion(jar_path):
Morten Krogh-Jespersen0de13732019-03-01 08:56:39 +010030 # TODO(mkroghj) This would not work for r8-lib, maybe use utils.getR8Version.
Ian Zerny3f54e222019-02-12 10:51:17 +010031 output = subprocess.check_output([
32 jdk.GetJavaExecutable(), '-jar', jar_path, '--version'
33 ])
Mathias Ravdd6a6de2018-05-18 10:18:33 +020034 return output.splitlines()[0].strip()
35
Rico Windb4621c12017-08-28 12:48:53 +020036def GetVersion():
Mathias Ravdd6a6de2018-05-18 10:18:33 +020037 r8_version = GetToolVersion(utils.R8_JAR)
38 d8_version = GetToolVersion(utils.D8_JAR)
Rico Windb4621c12017-08-28 12:48:53 +020039 # The version printed is "D8 vVERSION_NUMBER" and "R8 vVERSION_NUMBER"
40 # Sanity check that versions match.
41 if d8_version.split()[1] != r8_version.split()[1]:
42 raise Exception(
43 'Version mismatch: \n%s\n%s' % (d8_version, r8_version))
44 return d8_version.split()[1]
45
Rico Wind0c24ae72017-09-08 11:33:56 +020046def GetGitBranches():
47 return subprocess.check_output(['git', 'show', '-s', '--pretty=%d', 'HEAD'])
Rico Windb4621c12017-08-28 12:48:53 +020048
Rico Wind0c24ae72017-09-08 11:33:56 +020049def GetGitHash():
50 return subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip()
Rico Windb4621c12017-08-28 12:48:53 +020051
Rico Wind0c24ae72017-09-08 11:33:56 +020052def IsMaster(version):
Rico Wind2f720292017-10-06 14:32:12 +020053 branches = subprocess.check_output(['git', 'branch', '-r', '--contains',
Rico Wind0c24ae72017-09-08 11:33:56 +020054 'HEAD'])
Rico Windd450ba12019-04-24 13:18:40 +020055 # CL runs from gerrit does not have a branch, we always treat them as master
56 # commits to archive these to the hash based location
57 if len(branches) == 0:
58 return True
Rico Wind0c24ae72017-09-08 11:33:56 +020059 if not version.endswith('-dev'):
60 # Sanity check, we don't want to archive on top of release builds EVER
61 # Note that even though we branch, we never push the bots to build the same
62 # commit as master on a branch since we always change the version to
63 # not have dev (or we crash here :-)).
64 if 'origin/master' in branches:
65 raise Exception('We are seeing origin/master in a commit that '
66 'don\'t have -dev in version')
Mads Agerac794132017-11-09 11:38:45 +010067 return False
Rico Wind0c24ae72017-09-08 11:33:56 +020068 if not 'origin/master' in branches:
69 raise Exception('We are not seeing origin/master '
70 'in a commit that have -dev in version')
Mads Agerac794132017-11-09 11:38:45 +010071 return True
Rico Wind0c24ae72017-09-08 11:33:56 +020072
Rico Windc0b16382018-05-17 13:23:43 +020073def GetStorageDestination(storage_prefix,
74 version_or_path,
75 file_name,
76 is_master):
Rico Wind0c24ae72017-09-08 11:33:56 +020077 # We archive master commits under raw/master instead of directly under raw
Rico Windc0b16382018-05-17 13:23:43 +020078 version_dir = GetVersionDestination(storage_prefix,
79 version_or_path,
80 is_master)
Rico Wind1a29c4f2018-01-25 08:43:08 +010081 return '%s/%s' % (version_dir, file_name)
82
Rico Windc0b16382018-05-17 13:23:43 +020083def GetVersionDestination(storage_prefix, version_or_path, is_master):
Rico Wind0c24ae72017-09-08 11:33:56 +020084 archive_dir = 'raw/master' if is_master else 'raw'
Rico Windc0b16382018-05-17 13:23:43 +020085 return '%s%s/%s/%s' % (storage_prefix, ARCHIVE_BUCKET,
86 archive_dir, version_or_path)
Rico Wind0c24ae72017-09-08 11:33:56 +020087
Rico Windc0b16382018-05-17 13:23:43 +020088def GetUploadDestination(version_or_path, file_name, is_master):
89 return GetStorageDestination('gs://', version_or_path, file_name, is_master)
Rico Wind0c24ae72017-09-08 11:33:56 +020090
Rico Windc0b16382018-05-17 13:23:43 +020091def GetUrl(version_or_path, file_name, is_master):
Rico Windb4621c12017-08-28 12:48:53 +020092 return GetStorageDestination('http://storage.googleapis.com/',
Rico Windc0b16382018-05-17 13:23:43 +020093 version_or_path, file_name, is_master)
94
95def GetMavenUrl(is_master):
96 return GetVersionDestination('http://storage.googleapis.com/', '', is_master)
Rico Windb4621c12017-08-28 12:48:53 +020097
Rico Wind7219bb02019-03-18 08:30:12 +010098def SetRLimitToMax():
99 (soft, hard) = resource.getrlimit(resource.RLIMIT_NOFILE)
100 resource.setrlimit(resource.RLIMIT_NOFILE, (hard, hard))
101
Rico Windcea9ce02019-03-06 14:25:52 +0100102def PrintResourceInfo():
103 (soft, hard) = resource.getrlimit(resource.RLIMIT_NOFILE)
104 print('INFO: Open files soft limit: %s' % soft)
105 print('INFO: Open files hard limit: %s' % hard)
106
Rico Windb4621c12017-08-28 12:48:53 +0200107def Main():
Rico Wind63a13562018-12-10 14:31:02 +0100108 (options, args) = ParseOptions()
Rico Wind089ca042019-03-06 13:27:25 +0000109 if not utils.is_bot() and not options.dry_run:
110 raise Exception('You are not a bot, don\'t archive builds')
Tamas Kenez180be092018-12-05 15:23:06 +0100111
Rico Wind7219bb02019-03-18 08:30:12 +0100112 if utils.is_bot():
113 SetRLimitToMax()
Rico Windcea9ce02019-03-06 14:25:52 +0100114 PrintResourceInfo()
Tamas Kenez180be092018-12-05 15:23:06 +0100115 # Create maven release which uses a build that exclude dependencies.
Rico Wind8fc8bfa2019-03-22 09:57:36 +0100116 create_maven_release.run(utils.MAVEN_ZIP)
117 create_maven_release.run(utils.MAVEN_ZIP_LIB, is_r8lib=True)
Mads Agera4911eb2017-11-22 13:19:36 +0100118
Tamas Kenez180be092018-12-05 15:23:06 +0100119 # Generate and copy a full build without dependencies.
Mads Agerb10c07f2017-11-27 13:25:52 +0100120 gradle.RunGradleExcludeDeps([utils.R8, utils.R8_SRC])
Tamas Kenez180be092018-12-05 15:23:06 +0100121 shutil.copyfile(utils.R8_JAR, utils.R8_FULL_EXCLUDE_DEPS_JAR)
Mads Ager0bd1ebd2017-11-22 13:40:21 +0100122
Mads Agerac794132017-11-09 11:38:45 +0100123 # Ensure all archived artifacts has been built before archiving.
Tamas Kenez03ab76f2018-12-07 14:33:25 +0100124 # The target tasks postfixed by 'lib' depend on the actual target task so
Tamas Kenezf960e9c2018-12-03 16:13:29 +0100125 # building it invokes the original task first.
Morten Krogh-Jespersene28db462019-01-09 13:32:15 +0100126 # The '-Pno_internal' flag is important because we generate the lib based on uses in tests.
Tamas Kenez03ab76f2018-12-07 14:33:25 +0100127 gradle.RunGradle([
128 utils.R8,
129 utils.D8,
130 utils.COMPATDX,
131 utils.COMPATPROGUARD,
132 utils.R8LIB,
Morten Krogh-Jespersencae32a72019-01-11 11:02:19 +0100133 utils.R8LIB_NO_DEPS,
Tamas Kenez03ab76f2018-12-07 14:33:25 +0100134 utils.COMPATDXLIB,
135 utils.COMPATPROGUARDLIB,
Morten Krogh-Jespersene28db462019-01-09 13:32:15 +0100136 '-Pno_internal'
Tamas Kenez03ab76f2018-12-07 14:33:25 +0100137 ])
Rico Windb4621c12017-08-28 12:48:53 +0200138 version = GetVersion()
Mads Agerac794132017-11-09 11:38:45 +0100139 is_master = IsMaster(version)
Rico Wind0c24ae72017-09-08 11:33:56 +0200140 if is_master:
141 # On master we use the git hash to archive with
142 print 'On master, using git hash for archiving'
143 version = GetGitHash()
144
Rico Wind1a29c4f2018-01-25 08:43:08 +0100145 destination = GetVersionDestination('gs://', version, is_master)
Søren Gjessede7dd452019-03-06 16:03:17 +0100146 if utils.cloud_storage_exists(destination) and not options.dry_run:
Rico Wind1a29c4f2018-01-25 08:43:08 +0100147 raise Exception('Target archive directory %s already exists' % destination)
Yohann Roussel73f58e12017-10-13 17:33:14 +0200148 with utils.TempDir() as temp:
149 version_file = os.path.join(temp, 'r8-version.properties')
150 with open(version_file,'w') as version_writer:
151 version_writer.write('version.sha=' + GetGitHash() + '\n')
Mads Agerac794132017-11-09 11:38:45 +0100152 version_writer.write(
Rico Windf462b432019-02-28 13:53:47 +0100153 'releaser=go/r8bot (' + os.environ.get('SWARMING_BOT_ID') + ')\n')
Yohann Roussel73f58e12017-10-13 17:33:14 +0200154 version_writer.write('version-file.version.code=1\n')
155
Tamas Kenez03ab76f2018-12-07 14:33:25 +0100156 for file in [
157 utils.D8_JAR,
158 utils.R8_JAR,
159 utils.R8LIB_JAR,
Tamas Kenez54b939a2018-12-07 15:55:26 +0100160 utils.R8LIB_JAR + '.map',
Tamas Kenez03ab76f2018-12-07 14:33:25 +0100161 utils.R8_SRC_JAR,
162 utils.R8_FULL_EXCLUDE_DEPS_JAR,
163 utils.R8LIB_EXCLUDE_DEPS_JAR,
Tamas Kenez54b939a2018-12-07 15:55:26 +0100164 utils.R8LIB_EXCLUDE_DEPS_JAR + '.map',
Tamas Kenez03ab76f2018-12-07 14:33:25 +0100165 utils.COMPATDX_JAR,
166 utils.COMPATDXLIB_JAR,
Tamas Kenez54b939a2018-12-07 15:55:26 +0100167 utils.COMPATDXLIB_JAR + '.map',
Tamas Kenez03ab76f2018-12-07 14:33:25 +0100168 utils.COMPATPROGUARD_JAR,
169 utils.COMPATPROGUARDLIB_JAR,
Tamas Kenez54b939a2018-12-07 15:55:26 +0100170 utils.COMPATPROGUARDLIB_JAR + '.map',
Tamas Kenez03ab76f2018-12-07 14:33:25 +0100171 utils.MAVEN_ZIP,
Rico Wind8fc8bfa2019-03-22 09:57:36 +0100172 utils.MAVEN_ZIP_LIB,
Tamas Kenez03ab76f2018-12-07 14:33:25 +0100173 utils.GENERATED_LICENSE,
174 ]:
Mads Agerac794132017-11-09 11:38:45 +0100175 file_name = os.path.basename(file)
Yohann Roussel73f58e12017-10-13 17:33:14 +0200176 tagged_jar = os.path.join(temp, file_name)
Mads Agerac794132017-11-09 11:38:45 +0100177 shutil.copyfile(file, tagged_jar)
Mads Agerb10c07f2017-11-27 13:25:52 +0100178 if file_name.endswith('.jar') and not file_name.endswith('-src.jar'):
Mads Agerafc0cda2017-11-27 13:04:27 +0100179 with zipfile.ZipFile(tagged_jar, 'a') as zip:
180 zip.write(version_file, os.path.basename(version_file))
Yohann Roussel73f58e12017-10-13 17:33:14 +0200181 destination = GetUploadDestination(version, file_name, is_master)
182 print('Uploading %s to %s' % (tagged_jar, destination))
Rico Wind63a13562018-12-10 14:31:02 +0100183 if options.dry_run:
184 print('Dry run, not actually uploading')
185 else:
186 utils.upload_file_to_cloud_storage(tagged_jar, destination)
187 print('File available at: %s' % GetUrl(version, file_name, is_master))
Rico Windc0b16382018-05-17 13:23:43 +0200188 if file == utils.R8_JAR:
189 # Upload R8 to a maven compatible location.
190 maven_dst = GetUploadDestination(utils.get_maven_path(version),
191 'r8-%s.jar' % version, is_master)
Rico Wind63a13562018-12-10 14:31:02 +0100192 if options.dry_run:
193 print('Dry run, not actually creating maven repo')
194 else:
195 utils.upload_file_to_cloud_storage(tagged_jar, maven_dst)
196 print('Maven repo root available at: %s' % GetMavenUrl(is_master))
Rico Windc0b16382018-05-17 13:23:43 +0200197
Rico Windb4621c12017-08-28 12:48:53 +0200198
199if __name__ == '__main__':
200 sys.exit(Main())