blob: f3a09236409391444349c92a6ee53c655b36998d [file] [log] [blame]
Søren Gjesse6d1934f2017-11-09 10:00:24 +01001#!/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
6import argparse
7import gradle
8import os
Søren Gjesse6d1934f2017-11-09 10:00:24 +01009from shutil import copyfile
Mads Ager14d9b072017-11-20 13:42:55 +010010import sys
11import tempfile
12import utils
13import urllib
14
Rico Wind70d614f2020-01-31 08:45:21 +010015BUILD_ROOT = "https://storage.googleapis.com/r8-releases/raw/"
Rico Wind03463d32018-01-22 10:44:00 +010016MASTER_BUILD_ROOT = "%smaster/" % BUILD_ROOT
17
Tamas Kenez4f3fe5a2018-12-14 14:37:30 +010018JAR_TARGETS_MAP = {
19 'full': [
Søren Gjessefe640e52019-11-28 12:46:04 +010020 (utils.R8, 'r8'),
Tamas Kenez4f3fe5a2018-12-14 14:37:30 +010021 ],
22 'lib': [
Søren Gjessefe640e52019-11-28 12:46:04 +010023 (utils.R8LIB, 'r8'),
Tamas Kenez4f3fe5a2018-12-14 14:37:30 +010024 ],
25}
26
Mads Ager12a56bc2017-11-27 11:51:25 +010027OTHER_TARGETS = ["LICENSE"]
Søren Gjesse6d1934f2017-11-09 10:00:24 +010028
29def parse_arguments():
30 parser = argparse.ArgumentParser(
31 description = 'Build and copy jars to an Android tree.')
32 parser.add_argument('android_root', nargs=1,
33 help='Android checkout root.')
Mads Ager14d9b072017-11-20 13:42:55 +010034 parser.add_argument('--commit_hash', default=None, help='Commit hash')
Rico Wind03463d32018-01-22 10:44:00 +010035 parser.add_argument('--version', default=None, help='The version to download')
Tamas Kenez4f3fe5a2018-12-14 14:37:30 +010036 parser.add_argument(
37 '--targets',
38 required=True,
39 choices=['full', 'lib'],
40 help="Use 'full' to download the full, non-optimized jars (legacy" +
Morten Krogh-Jespersen87da12f2019-01-22 10:32:32 +010041 " behaviour) and 'lib' for the R8-processed, optimized jars.",
Tamas Kenez4f3fe5a2018-12-14 14:37:30 +010042 )
43 parser.add_argument(
44 '--maps',
45 action='store_true',
46 help="Download proguard maps for jars, use only with '--target lib'.",
47 )
clementbera2bd06e82019-07-24 15:19:18 +020048 parser.add_argument(
49 '--java-max-memory-size',
50 '--java_max_memory_size',
51 help='Use a custom max memory size for the gradle java instance, eg, 4g')
Søren Gjesse6d1934f2017-11-09 10:00:24 +010052 return parser.parse_args()
53
Tamas Kenez4f3fe5a2018-12-14 14:37:30 +010054def copy_targets(root, target_root, srcs, dests, maps=False):
Tamas Kenez03ab76f2018-12-07 14:33:25 +010055 assert len(srcs) == len(dests)
Mads Ager12a56bc2017-11-27 11:51:25 +010056 for i in range(len(srcs)):
57 src = os.path.join(root, srcs[i])
58 dest = os.path.join(target_root, 'prebuilts', 'r8', dests[i])
Søren Gjesse50c29272019-11-27 15:37:12 +010059 if os.path.exists(dest):
60 print 'Copying: ' + src + ' -> ' + dest
61 copyfile(src, dest)
62 if maps:
63 print 'Copying: ' + src + '.map -> ' + dest + '.map'
64 copyfile(src + '.map', dest + '.map')
Søren Gjessefe640e52019-11-28 12:46:04 +010065 else:
66 print ('WARNING: Not copying ' + src + ' -> ' + dest +
67 ', as' + dest + ' does not exist already')
68
Søren Gjesse50c29272019-11-27 15:37:12 +010069
Mads Ager14d9b072017-11-20 13:42:55 +010070
Tamas Kenez4f3fe5a2018-12-14 14:37:30 +010071def copy_jar_targets(root, target_root, jar_targets, maps):
72 srcs = map((lambda t: t[0] + '.jar'), jar_targets)
73 dests = map((lambda t: t[1] + '.jar'), jar_targets)
74 copy_targets(root, target_root, srcs, dests, maps=maps)
Mads Ager12a56bc2017-11-27 11:51:25 +010075
76def copy_other_targets(root, target_root):
77 copy_targets(root, target_root, OTHER_TARGETS, OTHER_TARGETS)
78
Rico Wind03463d32018-01-22 10:44:00 +010079def download_hash(root, commit_hash, target):
Mads Ager12a56bc2017-11-27 11:51:25 +010080 url = MASTER_BUILD_ROOT + commit_hash + '/' + target
Rico Wind03463d32018-01-22 10:44:00 +010081 download_target(root, url, target)
82
83def download_version(root, version, target):
84 url = BUILD_ROOT + version + '/' + target
85 download_target(root, url, target)
86
87def download_target(root, url, target):
Mads Ager12a56bc2017-11-27 11:51:25 +010088 download_path = os.path.join(root, target)
89 print 'Downloading: ' + url + ' -> ' + download_path
Tamas Kenez6b5d3b82018-02-05 12:02:20 +010090 result = urllib.urlretrieve(url, download_path)
91 if 'X-GUploader-Request-Result: success' not in str(result[1]):
92 raise IOError('Failed to download ' + url)
Mads Ager12a56bc2017-11-27 11:51:25 +010093
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +020094def main_download(hash, maps, targets, target_root, version):
95 jar_targets = JAR_TARGETS_MAP[targets]
96 final_targets = map((lambda t: t[0] + '.jar'), jar_targets) + OTHER_TARGETS
97 with utils.TempDir() as root:
98 for target in final_targets:
99 if hash:
100 download_hash(root, hash, target)
101 if maps and target not in OTHER_TARGETS:
102 download_hash(root, hash, target + '.map')
103 else:
104 assert version
105 download_version(root, version, target)
106 if maps and target not in OTHER_TARGETS:
107 download_version(root, version, target + '.map')
108 copy_jar_targets(root, target_root, jar_targets, maps)
109 copy_other_targets(root, target_root)
110
111def main_build(maps, max_memory_size, targets, target_root):
112 jar_targets = JAR_TARGETS_MAP[targets]
113 gradle_args = map(lambda t: t[0], jar_targets)
114 if max_memory_size:
115 gradle_args.append('-Dorg.gradle.jvmargs=-Xmx' + max_memory_size)
116 gradle.RunGradle(gradle_args)
117 copy_jar_targets(utils.LIBS, target_root, jar_targets, maps)
118 copy_other_targets(utils.GENERATED_LICENSE_DIR, target_root)
119
120def main(args):
Tamas Kenez4f3fe5a2018-12-14 14:37:30 +0100121 if args.maps and args.targets != 'lib':
122 raise Exception("Use '--maps' only with '--targets lib.")
Mads Ager14d9b072017-11-20 13:42:55 +0100123 target_root = args.android_root[0]
Rico Wind03463d32018-01-22 10:44:00 +0100124 if args.commit_hash == None and args.version == None:
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200125 main_build(args.maps, args.java_max_memory_size, args.targets, target_root)
Mads Ager14d9b072017-11-20 13:42:55 +0100126 else:
Rico Wind03463d32018-01-22 10:44:00 +0100127 assert args.commit_hash == None or args.version == None
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200128 main_download(args.commit_hash, args.maps, args.targets, target_root, args.version)
Søren Gjesse6d1934f2017-11-09 10:00:24 +0100129
130if __name__ == '__main__':
Morten Krogh-Jespersen1947a862019-09-26 14:06:48 +0200131 sys.exit(main(parse_arguments()))