blob: 07f138a7bbd9c9b50394de2dfc056efff1b834ed [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 Windb4621c12017-08-28 12:48:53 +02008import os
Mathias Ravdd6a6de2018-05-18 10:18:33 +02009import shutil
Rico Wind0c24ae72017-09-08 11:33:56 +020010import subprocess
Rico Windb4621c12017-08-28 12:48:53 +020011import sys
Mathias Ravdd6a6de2018-05-18 10:18:33 +020012import toolhelper
Rico Windb4621c12017-08-28 12:48:53 +020013import utils
Yohann Roussel73f58e12017-10-13 17:33:14 +020014import zipfile
Rico Windb4621c12017-08-28 12:48:53 +020015
Rico Wind792e8c72017-08-30 09:43:46 +020016ARCHIVE_BUCKET = 'r8-releases'
Rico Windb4621c12017-08-28 12:48:53 +020017
Mathias Ravdd6a6de2018-05-18 10:18:33 +020018def GetToolVersion(jar_path):
19 output = subprocess.check_output(['java', '-jar', jar_path, '--version'])
20 return output.splitlines()[0].strip()
21
Rico Windb4621c12017-08-28 12:48:53 +020022def GetVersion():
Mathias Ravdd6a6de2018-05-18 10:18:33 +020023 r8_version = GetToolVersion(utils.R8_JAR)
24 d8_version = GetToolVersion(utils.D8_JAR)
Rico Windb4621c12017-08-28 12:48:53 +020025 # The version printed is "D8 vVERSION_NUMBER" and "R8 vVERSION_NUMBER"
26 # Sanity check that versions match.
27 if d8_version.split()[1] != r8_version.split()[1]:
28 raise Exception(
29 'Version mismatch: \n%s\n%s' % (d8_version, r8_version))
30 return d8_version.split()[1]
31
Rico Wind0c24ae72017-09-08 11:33:56 +020032def GetGitBranches():
33 return subprocess.check_output(['git', 'show', '-s', '--pretty=%d', 'HEAD'])
Rico Windb4621c12017-08-28 12:48:53 +020034
Rico Wind0c24ae72017-09-08 11:33:56 +020035def GetGitHash():
36 return subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip()
Rico Windb4621c12017-08-28 12:48:53 +020037
Rico Wind0c24ae72017-09-08 11:33:56 +020038def IsMaster(version):
Rico Wind2f720292017-10-06 14:32:12 +020039 branches = subprocess.check_output(['git', 'branch', '-r', '--contains',
Rico Wind0c24ae72017-09-08 11:33:56 +020040 'HEAD'])
41 if not version.endswith('-dev'):
42 # Sanity check, we don't want to archive on top of release builds EVER
43 # Note that even though we branch, we never push the bots to build the same
44 # commit as master on a branch since we always change the version to
45 # not have dev (or we crash here :-)).
46 if 'origin/master' in branches:
47 raise Exception('We are seeing origin/master in a commit that '
48 'don\'t have -dev in version')
Mads Agerac794132017-11-09 11:38:45 +010049 return False
Rico Wind0c24ae72017-09-08 11:33:56 +020050 if not 'origin/master' in branches:
51 raise Exception('We are not seeing origin/master '
52 'in a commit that have -dev in version')
Mads Agerac794132017-11-09 11:38:45 +010053 return True
Rico Wind0c24ae72017-09-08 11:33:56 +020054
Rico Windc0b16382018-05-17 13:23:43 +020055def GetStorageDestination(storage_prefix,
56 version_or_path,
57 file_name,
58 is_master):
Rico Wind0c24ae72017-09-08 11:33:56 +020059 # We archive master commits under raw/master instead of directly under raw
Rico Windc0b16382018-05-17 13:23:43 +020060 version_dir = GetVersionDestination(storage_prefix,
61 version_or_path,
62 is_master)
Rico Wind1a29c4f2018-01-25 08:43:08 +010063 return '%s/%s' % (version_dir, file_name)
64
Rico Windc0b16382018-05-17 13:23:43 +020065def GetVersionDestination(storage_prefix, version_or_path, is_master):
Rico Wind0c24ae72017-09-08 11:33:56 +020066 archive_dir = 'raw/master' if is_master else 'raw'
Rico Windc0b16382018-05-17 13:23:43 +020067 return '%s%s/%s/%s' % (storage_prefix, ARCHIVE_BUCKET,
68 archive_dir, version_or_path)
Rico Wind0c24ae72017-09-08 11:33:56 +020069
Rico Windc0b16382018-05-17 13:23:43 +020070def GetUploadDestination(version_or_path, file_name, is_master):
71 return GetStorageDestination('gs://', version_or_path, file_name, is_master)
Rico Wind0c24ae72017-09-08 11:33:56 +020072
Rico Windc0b16382018-05-17 13:23:43 +020073def GetUrl(version_or_path, file_name, is_master):
Rico Windb4621c12017-08-28 12:48:53 +020074 return GetStorageDestination('http://storage.googleapis.com/',
Rico Windc0b16382018-05-17 13:23:43 +020075 version_or_path, file_name, is_master)
76
77def GetMavenUrl(is_master):
78 return GetVersionDestination('http://storage.googleapis.com/', '', is_master)
Rico Windb4621c12017-08-28 12:48:53 +020079
80def Main():
81 if not 'BUILDBOT_BUILDERNAME' in os.environ:
82 raise Exception('You are not a bot, don\'t archive builds')
Mads Ager0bd1ebd2017-11-22 13:40:21 +010083 # Create maven release first which uses a build that exclude dependencies.
Mads Agera4911eb2017-11-22 13:19:36 +010084 create_maven_release.main(["--out", utils.LIBS])
85
Mads Ager0bd1ebd2017-11-22 13:40:21 +010086 # Generate and copy the build that exclude dependencies.
Mads Agerb10c07f2017-11-27 13:25:52 +010087 gradle.RunGradleExcludeDeps([utils.R8, utils.R8_SRC])
Mads Ager0bd1ebd2017-11-22 13:40:21 +010088 shutil.copyfile(utils.R8_JAR, utils.R8_EXCLUDE_DEPS_JAR)
89
Mads Agerac794132017-11-09 11:38:45 +010090 # Ensure all archived artifacts has been built before archiving.
91 gradle.RunGradle([utils.D8, utils.R8, utils.COMPATDX, utils.COMPATPROGUARD])
Rico Windb4621c12017-08-28 12:48:53 +020092 version = GetVersion()
Mads Agerac794132017-11-09 11:38:45 +010093 is_master = IsMaster(version)
Rico Wind0c24ae72017-09-08 11:33:56 +020094 if is_master:
95 # On master we use the git hash to archive with
96 print 'On master, using git hash for archiving'
97 version = GetGitHash()
98
Rico Wind1a29c4f2018-01-25 08:43:08 +010099 destination = GetVersionDestination('gs://', version, is_master)
100 if utils.cloud_storage_exists(destination):
101 raise Exception('Target archive directory %s already exists' % destination)
Yohann Roussel73f58e12017-10-13 17:33:14 +0200102 with utils.TempDir() as temp:
103 version_file = os.path.join(temp, 'r8-version.properties')
104 with open(version_file,'w') as version_writer:
105 version_writer.write('version.sha=' + GetGitHash() + '\n')
Mads Agerac794132017-11-09 11:38:45 +0100106 version_writer.write(
107 'releaser=go/r8bot (' + os.environ.get('BUILDBOT_SLAVENAME') + ')\n')
Yohann Roussel73f58e12017-10-13 17:33:14 +0200108 version_writer.write('version-file.version.code=1\n')
109
Mads Agerac794132017-11-09 11:38:45 +0100110 for file in [utils.D8_JAR,
111 utils.R8_JAR,
Mads Agerb10c07f2017-11-27 13:25:52 +0100112 utils.R8_SRC_JAR,
Mads Ager0bd1ebd2017-11-22 13:40:21 +0100113 utils.R8_EXCLUDE_DEPS_JAR,
Mads Agerac794132017-11-09 11:38:45 +0100114 utils.COMPATDX_JAR,
115 utils.COMPATPROGUARD_JAR,
Mads Ager12a56bc2017-11-27 11:51:25 +0100116 utils.MAVEN_ZIP,
117 utils.GENERATED_LICENSE]:
Mads Agerac794132017-11-09 11:38:45 +0100118 file_name = os.path.basename(file)
Yohann Roussel73f58e12017-10-13 17:33:14 +0200119 tagged_jar = os.path.join(temp, file_name)
Mads Agerac794132017-11-09 11:38:45 +0100120 shutil.copyfile(file, tagged_jar)
Mads Agerb10c07f2017-11-27 13:25:52 +0100121 if file_name.endswith('.jar') and not file_name.endswith('-src.jar'):
Mads Agerafc0cda2017-11-27 13:04:27 +0100122 with zipfile.ZipFile(tagged_jar, 'a') as zip:
123 zip.write(version_file, os.path.basename(version_file))
Yohann Roussel73f58e12017-10-13 17:33:14 +0200124 destination = GetUploadDestination(version, file_name, is_master)
125 print('Uploading %s to %s' % (tagged_jar, destination))
126 utils.upload_file_to_cloud_storage(tagged_jar, destination)
127 print('File available at: %s' % GetUrl(version, file_name, is_master))
Rico Windc0b16382018-05-17 13:23:43 +0200128 if file == utils.R8_JAR:
129 # Upload R8 to a maven compatible location.
130 maven_dst = GetUploadDestination(utils.get_maven_path(version),
131 'r8-%s.jar' % version, is_master)
132 utils.upload_file_to_cloud_storage(tagged_jar, maven_dst)
133 print('Maven repo root available at: %s' % GetMavenUrl(is_master))
134
Rico Windb4621c12017-08-28 12:48:53 +0200135
136if __name__ == '__main__':
137 sys.exit(Main())