blob: e9233c7130886cc9d9014370a2d229f3de2ed088 [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
Rico Wind63a13562018-12-10 14:31:02 +01008import optparse
Rico Windb4621c12017-08-28 12:48:53 +02009import os
Mathias Ravdd6a6de2018-05-18 10:18:33 +020010import shutil
Rico Wind0c24ae72017-09-08 11:33:56 +020011import subprocess
Rico Windb4621c12017-08-28 12:48:53 +020012import sys
Mathias Ravdd6a6de2018-05-18 10:18:33 +020013import toolhelper
Rico Windb4621c12017-08-28 12:48:53 +020014import utils
Yohann Roussel73f58e12017-10-13 17:33:14 +020015import zipfile
Tamas Kenez180be092018-12-05 15:23:06 +010016from build_r8lib import build_r8lib
Rico Windb4621c12017-08-28 12:48:53 +020017
Rico Wind792e8c72017-08-30 09:43:46 +020018ARCHIVE_BUCKET = 'r8-releases'
Rico Windb4621c12017-08-28 12:48:53 +020019
Rico Wind63a13562018-12-10 14:31:02 +010020def ParseOptions():
21 result = optparse.OptionParser()
22 result.add_option('--dry-run', '--dry_run',
23 help='Build only, no upload.',
24 default=False, action='store_true')
25 return result.parse_args()
26
Mathias Ravdd6a6de2018-05-18 10:18:33 +020027def GetToolVersion(jar_path):
28 output = subprocess.check_output(['java', '-jar', jar_path, '--version'])
29 return output.splitlines()[0].strip()
30
Rico Windb4621c12017-08-28 12:48:53 +020031def GetVersion():
Mathias Ravdd6a6de2018-05-18 10:18:33 +020032 r8_version = GetToolVersion(utils.R8_JAR)
33 d8_version = GetToolVersion(utils.D8_JAR)
Rico Windb4621c12017-08-28 12:48:53 +020034 # The version printed is "D8 vVERSION_NUMBER" and "R8 vVERSION_NUMBER"
35 # Sanity check that versions match.
36 if d8_version.split()[1] != r8_version.split()[1]:
37 raise Exception(
38 'Version mismatch: \n%s\n%s' % (d8_version, r8_version))
39 return d8_version.split()[1]
40
Rico Wind0c24ae72017-09-08 11:33:56 +020041def GetGitBranches():
42 return subprocess.check_output(['git', 'show', '-s', '--pretty=%d', 'HEAD'])
Rico Windb4621c12017-08-28 12:48:53 +020043
Rico Wind0c24ae72017-09-08 11:33:56 +020044def GetGitHash():
45 return subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip()
Rico Windb4621c12017-08-28 12:48:53 +020046
Rico Wind0c24ae72017-09-08 11:33:56 +020047def IsMaster(version):
Rico Wind2f720292017-10-06 14:32:12 +020048 branches = subprocess.check_output(['git', 'branch', '-r', '--contains',
Rico Wind0c24ae72017-09-08 11:33:56 +020049 'HEAD'])
Christoffer Quist Adamsen6159fb62019-04-24 16:24:16 +020050 # CL runs from gerrit does not have a branch, we always treat them as master
51 # commits to archive these to the hash based location
52 if len(branches) == 0:
53 return True
Rico Wind0c24ae72017-09-08 11:33:56 +020054 if not version.endswith('-dev'):
55 # Sanity check, we don't want to archive on top of release builds EVER
56 # Note that even though we branch, we never push the bots to build the same
57 # commit as master on a branch since we always change the version to
58 # not have dev (or we crash here :-)).
59 if 'origin/master' in branches:
60 raise Exception('We are seeing origin/master in a commit that '
61 'don\'t have -dev in version')
Mads Agerac794132017-11-09 11:38:45 +010062 return False
Rico Wind0c24ae72017-09-08 11:33:56 +020063 if not 'origin/master' in branches:
64 raise Exception('We are not seeing origin/master '
65 'in a commit that have -dev in version')
Mads Agerac794132017-11-09 11:38:45 +010066 return True
Rico Wind0c24ae72017-09-08 11:33:56 +020067
Rico Windc0b16382018-05-17 13:23:43 +020068def GetStorageDestination(storage_prefix,
69 version_or_path,
70 file_name,
71 is_master):
Rico Wind0c24ae72017-09-08 11:33:56 +020072 # We archive master commits under raw/master instead of directly under raw
Rico Windc0b16382018-05-17 13:23:43 +020073 version_dir = GetVersionDestination(storage_prefix,
74 version_or_path,
75 is_master)
Rico Wind1a29c4f2018-01-25 08:43:08 +010076 return '%s/%s' % (version_dir, file_name)
77
Rico Windc0b16382018-05-17 13:23:43 +020078def GetVersionDestination(storage_prefix, version_or_path, is_master):
Rico Wind0c24ae72017-09-08 11:33:56 +020079 archive_dir = 'raw/master' if is_master else 'raw'
Rico Windc0b16382018-05-17 13:23:43 +020080 return '%s%s/%s/%s' % (storage_prefix, ARCHIVE_BUCKET,
81 archive_dir, version_or_path)
Rico Wind0c24ae72017-09-08 11:33:56 +020082
Rico Windc0b16382018-05-17 13:23:43 +020083def GetUploadDestination(version_or_path, file_name, is_master):
84 return GetStorageDestination('gs://', version_or_path, file_name, is_master)
Rico Wind0c24ae72017-09-08 11:33:56 +020085
Rico Windc0b16382018-05-17 13:23:43 +020086def GetUrl(version_or_path, file_name, is_master):
Rico Windb4621c12017-08-28 12:48:53 +020087 return GetStorageDestination('http://storage.googleapis.com/',
Rico Windc0b16382018-05-17 13:23:43 +020088 version_or_path, file_name, is_master)
89
90def GetMavenUrl(is_master):
91 return GetVersionDestination('http://storage.googleapis.com/', '', is_master)
Rico Windb4621c12017-08-28 12:48:53 +020092
93def Main():
Rico Wind63a13562018-12-10 14:31:02 +010094 (options, args) = ParseOptions()
Søren Gjessee9e86c62019-03-04 13:26:47 +010095 # TODO(126871526): Fix the is_bot check.
96 # if not utils.is_bot() and not options.dry_run:
97 # raise Exception('You are not a bot, don\'t archive builds')
Tamas Kenez180be092018-12-05 15:23:06 +010098
99 # Generate an r8-ed build without dependencies.
Morten Krogh-Jespersene28db462019-01-09 13:32:15 +0100100 # The '-Pno_internal' flag is important because we generate the lib based on uses in tests.
Søren Gjesse190e0f12019-03-04 14:59:26 +0100101 # TODO(127264123): Remove commented out lines.
102 # gradle.RunGradleExcludeDeps([utils.R8LIB_NO_DEPS, '-Pno_internal'])
103 # shutil.copyfile(utils.R8LIB_JAR, utils.R8LIB_EXCLUDE_DEPS_JAR)
104 # shutil.copyfile(utils.R8LIB_JAR + '.map', utils.R8LIB_EXCLUDE_DEPS_JAR + '.map')
Tamas Kenez180be092018-12-05 15:23:06 +0100105
106 # Create maven release which uses a build that exclude dependencies.
Mads Agera4911eb2017-11-22 13:19:36 +0100107 create_maven_release.main(["--out", utils.LIBS])
108
Tamas Kenez180be092018-12-05 15:23:06 +0100109 # Generate and copy a full build without dependencies.
Mads Agerb10c07f2017-11-27 13:25:52 +0100110 gradle.RunGradleExcludeDeps([utils.R8, utils.R8_SRC])
Tamas Kenez180be092018-12-05 15:23:06 +0100111 shutil.copyfile(utils.R8_JAR, utils.R8_FULL_EXCLUDE_DEPS_JAR)
Mads Ager0bd1ebd2017-11-22 13:40:21 +0100112
Mads Agerac794132017-11-09 11:38:45 +0100113 # Ensure all archived artifacts has been built before archiving.
Tamas Kenez03ab76f2018-12-07 14:33:25 +0100114 # The target tasks postfixed by 'lib' depend on the actual target task so
Tamas Kenezf960e9c2018-12-03 16:13:29 +0100115 # building it invokes the original task first.
Morten Krogh-Jespersene28db462019-01-09 13:32:15 +0100116 # The '-Pno_internal' flag is important because we generate the lib based on uses in tests.
Søren Gjesse190e0f12019-03-04 14:59:26 +0100117 # TODO(127264123): Remove commented out targets.
Tamas Kenez03ab76f2018-12-07 14:33:25 +0100118 gradle.RunGradle([
119 utils.R8,
120 utils.D8,
121 utils.COMPATDX,
122 utils.COMPATPROGUARD,
Søren Gjesse190e0f12019-03-04 14:59:26 +0100123 # utils.R8LIB,
124 # utils.COMPATDXLIB,
125 # utils.COMPATPROGUARDLIB,
Morten Krogh-Jespersene28db462019-01-09 13:32:15 +0100126 '-Pno_internal'
Tamas Kenez03ab76f2018-12-07 14:33:25 +0100127 ])
Rico Windb4621c12017-08-28 12:48:53 +0200128 version = GetVersion()
Mads Agerac794132017-11-09 11:38:45 +0100129 is_master = IsMaster(version)
Rico Wind0c24ae72017-09-08 11:33:56 +0200130 if is_master:
131 # On master we use the git hash to archive with
132 print 'On master, using git hash for archiving'
133 version = GetGitHash()
134
Rico Wind1a29c4f2018-01-25 08:43:08 +0100135 destination = GetVersionDestination('gs://', version, is_master)
136 if utils.cloud_storage_exists(destination):
137 raise Exception('Target archive directory %s already exists' % destination)
Yohann Roussel73f58e12017-10-13 17:33:14 +0200138 with utils.TempDir() as temp:
139 version_file = os.path.join(temp, 'r8-version.properties')
140 with open(version_file,'w') as version_writer:
141 version_writer.write('version.sha=' + GetGitHash() + '\n')
Mads Agerac794132017-11-09 11:38:45 +0100142 version_writer.write(
Søren Gjesse60d500a2019-03-04 17:11:34 +0100143 'releaser=go/r8bot (' + os.environ.get('SWARMING_BOT_ID') + ')\n')
Yohann Roussel73f58e12017-10-13 17:33:14 +0200144 version_writer.write('version-file.version.code=1\n')
145
Søren Gjesse190e0f12019-03-04 14:59:26 +0100146 # TODO(127264123): Remove commented out files.
Tamas Kenez03ab76f2018-12-07 14:33:25 +0100147 for file in [
148 utils.D8_JAR,
149 utils.R8_JAR,
Søren Gjesse190e0f12019-03-04 14:59:26 +0100150 # utils.R8LIB_JAR,
151 # utils.R8LIB_JAR + '.map',
Tamas Kenez03ab76f2018-12-07 14:33:25 +0100152 utils.R8_SRC_JAR,
153 utils.R8_FULL_EXCLUDE_DEPS_JAR,
Søren Gjesse190e0f12019-03-04 14:59:26 +0100154 # utils.R8LIB_EXCLUDE_DEPS_JAR,
155 # utils.R8LIB_EXCLUDE_DEPS_JAR + '.map',
Tamas Kenez03ab76f2018-12-07 14:33:25 +0100156 utils.COMPATDX_JAR,
Søren Gjesse190e0f12019-03-04 14:59:26 +0100157 # utils.COMPATDXLIB_JAR,
158 # utils.COMPATDXLIB_JAR + '.map',
Tamas Kenez03ab76f2018-12-07 14:33:25 +0100159 utils.COMPATPROGUARD_JAR,
Søren Gjesse190e0f12019-03-04 14:59:26 +0100160 # utils.COMPATPROGUARDLIB_JAR,
161 # utils.COMPATPROGUARDLIB_JAR + '.map',
Tamas Kenez03ab76f2018-12-07 14:33:25 +0100162 utils.MAVEN_ZIP,
163 utils.GENERATED_LICENSE,
164 ]:
Mads Agerac794132017-11-09 11:38:45 +0100165 file_name = os.path.basename(file)
Yohann Roussel73f58e12017-10-13 17:33:14 +0200166 tagged_jar = os.path.join(temp, file_name)
Mads Agerac794132017-11-09 11:38:45 +0100167 shutil.copyfile(file, tagged_jar)
Mads Agerb10c07f2017-11-27 13:25:52 +0100168 if file_name.endswith('.jar') and not file_name.endswith('-src.jar'):
Mads Agerafc0cda2017-11-27 13:04:27 +0100169 with zipfile.ZipFile(tagged_jar, 'a') as zip:
170 zip.write(version_file, os.path.basename(version_file))
Yohann Roussel73f58e12017-10-13 17:33:14 +0200171 destination = GetUploadDestination(version, file_name, is_master)
172 print('Uploading %s to %s' % (tagged_jar, destination))
Rico Wind63a13562018-12-10 14:31:02 +0100173 if options.dry_run:
174 print('Dry run, not actually uploading')
175 else:
176 utils.upload_file_to_cloud_storage(tagged_jar, destination)
177 print('File available at: %s' % GetUrl(version, file_name, is_master))
Rico Windc0b16382018-05-17 13:23:43 +0200178 if file == utils.R8_JAR:
179 # Upload R8 to a maven compatible location.
180 maven_dst = GetUploadDestination(utils.get_maven_path(version),
181 'r8-%s.jar' % version, is_master)
Rico Wind63a13562018-12-10 14:31:02 +0100182 if options.dry_run:
183 print('Dry run, not actually creating maven repo')
184 else:
185 utils.upload_file_to_cloud_storage(tagged_jar, maven_dst)
186 print('Maven repo root available at: %s' % GetMavenUrl(is_master))
Rico Windc0b16382018-05-17 13:23:43 +0200187
Rico Windb4621c12017-08-28 12:48:53 +0200188
189if __name__ == '__main__':
190 sys.exit(Main())