Søren Gjesse | 6d1934f | 2017-11-09 10:00:24 +0100 | [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 | |
| 6 | import argparse |
| 7 | import gradle |
| 8 | import os |
Søren Gjesse | 6d1934f | 2017-11-09 10:00:24 +0100 | [diff] [blame] | 9 | from shutil import copyfile |
Mads Ager | 14d9b07 | 2017-11-20 13:42:55 +0100 | [diff] [blame] | 10 | import sys |
| 11 | import tempfile |
| 12 | import utils |
| 13 | import urllib |
| 14 | |
Rico Wind | 70d614f | 2020-01-31 08:45:21 +0100 | [diff] [blame] | 15 | BUILD_ROOT = "https://storage.googleapis.com/r8-releases/raw/" |
Rico Wind | 03463d3 | 2018-01-22 10:44:00 +0100 | [diff] [blame] | 16 | MASTER_BUILD_ROOT = "%smaster/" % BUILD_ROOT |
| 17 | |
Tamas Kenez | 4f3fe5a | 2018-12-14 14:37:30 +0100 | [diff] [blame] | 18 | JAR_TARGETS_MAP = { |
| 19 | 'full': [ |
Søren Gjesse | fe640e5 | 2019-11-28 12:46:04 +0100 | [diff] [blame] | 20 | (utils.R8, 'r8'), |
Tamas Kenez | 4f3fe5a | 2018-12-14 14:37:30 +0100 | [diff] [blame] | 21 | ], |
| 22 | 'lib': [ |
Søren Gjesse | fe640e5 | 2019-11-28 12:46:04 +0100 | [diff] [blame] | 23 | (utils.R8LIB, 'r8'), |
Tamas Kenez | 4f3fe5a | 2018-12-14 14:37:30 +0100 | [diff] [blame] | 24 | ], |
| 25 | } |
| 26 | |
Mads Ager | 12a56bc | 2017-11-27 11:51:25 +0100 | [diff] [blame] | 27 | OTHER_TARGETS = ["LICENSE"] |
Søren Gjesse | 6d1934f | 2017-11-09 10:00:24 +0100 | [diff] [blame] | 28 | |
| 29 | def 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 Ager | 14d9b07 | 2017-11-20 13:42:55 +0100 | [diff] [blame] | 34 | parser.add_argument('--commit_hash', default=None, help='Commit hash') |
Rico Wind | 03463d3 | 2018-01-22 10:44:00 +0100 | [diff] [blame] | 35 | parser.add_argument('--version', default=None, help='The version to download') |
Tamas Kenez | 4f3fe5a | 2018-12-14 14:37:30 +0100 | [diff] [blame] | 36 | 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-Jespersen | 87da12f | 2019-01-22 10:32:32 +0100 | [diff] [blame] | 41 | " behaviour) and 'lib' for the R8-processed, optimized jars.", |
Tamas Kenez | 4f3fe5a | 2018-12-14 14:37:30 +0100 | [diff] [blame] | 42 | ) |
| 43 | parser.add_argument( |
| 44 | '--maps', |
| 45 | action='store_true', |
| 46 | help="Download proguard maps for jars, use only with '--target lib'.", |
| 47 | ) |
clementbera | 2bd06e8 | 2019-07-24 15:19:18 +0200 | [diff] [blame] | 48 | 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 Gjesse | 6d1934f | 2017-11-09 10:00:24 +0100 | [diff] [blame] | 52 | return parser.parse_args() |
| 53 | |
Tamas Kenez | 4f3fe5a | 2018-12-14 14:37:30 +0100 | [diff] [blame] | 54 | def copy_targets(root, target_root, srcs, dests, maps=False): |
Tamas Kenez | 03ab76f | 2018-12-07 14:33:25 +0100 | [diff] [blame] | 55 | assert len(srcs) == len(dests) |
Mads Ager | 12a56bc | 2017-11-27 11:51:25 +0100 | [diff] [blame] | 56 | 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 Gjesse | 50c2927 | 2019-11-27 15:37:12 +0100 | [diff] [blame] | 59 | 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 Gjesse | fe640e5 | 2019-11-28 12:46:04 +0100 | [diff] [blame] | 65 | else: |
| 66 | print ('WARNING: Not copying ' + src + ' -> ' + dest + |
| 67 | ', as' + dest + ' does not exist already') |
| 68 | |
Søren Gjesse | 50c2927 | 2019-11-27 15:37:12 +0100 | [diff] [blame] | 69 | |
Mads Ager | 14d9b07 | 2017-11-20 13:42:55 +0100 | [diff] [blame] | 70 | |
Tamas Kenez | 4f3fe5a | 2018-12-14 14:37:30 +0100 | [diff] [blame] | 71 | def 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 Ager | 12a56bc | 2017-11-27 11:51:25 +0100 | [diff] [blame] | 75 | |
| 76 | def copy_other_targets(root, target_root): |
| 77 | copy_targets(root, target_root, OTHER_TARGETS, OTHER_TARGETS) |
| 78 | |
Rico Wind | 03463d3 | 2018-01-22 10:44:00 +0100 | [diff] [blame] | 79 | def download_hash(root, commit_hash, target): |
Mads Ager | 12a56bc | 2017-11-27 11:51:25 +0100 | [diff] [blame] | 80 | url = MASTER_BUILD_ROOT + commit_hash + '/' + target |
Rico Wind | 03463d3 | 2018-01-22 10:44:00 +0100 | [diff] [blame] | 81 | download_target(root, url, target) |
| 82 | |
| 83 | def download_version(root, version, target): |
| 84 | url = BUILD_ROOT + version + '/' + target |
| 85 | download_target(root, url, target) |
| 86 | |
| 87 | def download_target(root, url, target): |
Mads Ager | 12a56bc | 2017-11-27 11:51:25 +0100 | [diff] [blame] | 88 | download_path = os.path.join(root, target) |
| 89 | print 'Downloading: ' + url + ' -> ' + download_path |
Tamas Kenez | 6b5d3b8 | 2018-02-05 12:02:20 +0100 | [diff] [blame] | 90 | 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 Ager | 12a56bc | 2017-11-27 11:51:25 +0100 | [diff] [blame] | 93 | |
Morten Krogh-Jespersen | 1947a86 | 2019-09-26 14:06:48 +0200 | [diff] [blame] | 94 | def 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 | |
| 111 | def 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 | |
| 120 | def main(args): |
Tamas Kenez | 4f3fe5a | 2018-12-14 14:37:30 +0100 | [diff] [blame] | 121 | if args.maps and args.targets != 'lib': |
| 122 | raise Exception("Use '--maps' only with '--targets lib.") |
Mads Ager | 14d9b07 | 2017-11-20 13:42:55 +0100 | [diff] [blame] | 123 | target_root = args.android_root[0] |
Rico Wind | 03463d3 | 2018-01-22 10:44:00 +0100 | [diff] [blame] | 124 | if args.commit_hash == None and args.version == None: |
Morten Krogh-Jespersen | 1947a86 | 2019-09-26 14:06:48 +0200 | [diff] [blame] | 125 | main_build(args.maps, args.java_max_memory_size, args.targets, target_root) |
Mads Ager | 14d9b07 | 2017-11-20 13:42:55 +0100 | [diff] [blame] | 126 | else: |
Rico Wind | 03463d3 | 2018-01-22 10:44:00 +0100 | [diff] [blame] | 127 | assert args.commit_hash == None or args.version == None |
Morten Krogh-Jespersen | 1947a86 | 2019-09-26 14:06:48 +0200 | [diff] [blame] | 128 | main_download(args.commit_hash, args.maps, args.targets, target_root, args.version) |
Søren Gjesse | 6d1934f | 2017-11-09 10:00:24 +0100 | [diff] [blame] | 129 | |
| 130 | if __name__ == '__main__': |
Morten Krogh-Jespersen | 1947a86 | 2019-09-26 14:06:48 +0200 | [diff] [blame] | 131 | sys.exit(main(parse_arguments())) |