blob: 5958ea77cd1c69f8c871f9b863fae801b276cd4c [file] [log] [blame]
Primiano Tucciae2879e2017-09-27 11:02:09 +09001#!/usr/bin/env python
2# Copyright (C) 2017 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import argparse
17import hashlib
18import logging
19import os
20import shutil
Primiano Tucci0825bc82017-09-28 18:50:23 +010021import subprocess
Primiano Tucciae2879e2017-09-27 11:02:09 +090022import sys
23import urllib
24import zipfile
25
26PREBUILTS = (
27 # GN
28 ('buildtools/mac/gn',
29 'https://storage.googleapis.com/chromium-gn/c2c934d4dda1f470a6511b1015dda9a9fb1ce50b',
30 'c2c934d4dda1f470a6511b1015dda9a9fb1ce50b',
31 'darwin'
32 ),
33 ('buildtools/linux64/gn',
34 'https://storage.googleapis.com/chromium-gn/b53fa13e950948c6f9a062189b76b34a9610281f',
35 'b53fa13e950948c6f9a062189b76b34a9610281f',
36 'linux2'
37 ),
38
39 # Ninja
40 ('buildtools/mac/ninja',
41 'https://storage.googleapis.com/fuchsia-build/fuchsia/ninja/mac/a1db595e824c50cf565fbf0af2437fd91b7babf4',
42 'a1db595e824c50cf565fbf0af2437fd91b7babf4',
43 'darwin'
44 ),
45 ('buildtools/linux64/ninja',
46 'https://storage.googleapis.com/fuchsia-build/fuchsia/ninja/linux64/d35b36c84a09f7e38b25947cafada10e8bf835bc',
47 'd35b36c84a09f7e38b25947cafada10e8bf835bc',
48 'linux2'
49 ),
50
51 # Android NDK
52 ('buildtools/ndk.zip',
53 'https://dl.google.com/android/repository/android-ndk-r15c-darwin-x86_64.zip',
54 'ea4b5d76475db84745aa8828000d009625fc1f98',
55 'darwin'
56 ),
57 ('buildtools/ndk.zip',
58 'https://dl.google.com/android/repository/android-ndk-r15c-linux-x86_64.zip',
59 '0bf02d4e8b85fd770fd7b9b2cdec57f9441f27a2',
60 'linux2'
61 ),
62
63 # Keep in sync with Android's //external/googletest/README.version .
64 ('buildtools/googletest.zip',
65 'https://github.com/google/googletest/archive/ff07a5de0e81580547f1685e101194ed1a4fcd56.zip',
66 'c7edec7d7e6db1fc37a20710de9c4d89e3a3893b',
67 'all'
68 ),
69
70 # Keep in sync with Android's //external/protobuf/README.version .
71 ('buildtools/protobuf.zip',
72 'https://github.com/google/protobuf/releases/download/v3.0.0-beta-3/protobuf-cpp-3.0.0-beta-3.zip',
73 '3caec60aa9d8eefc8c3c3201b6b8ca19935edb89',
74 'all'
75 ),
Primiano Tucci0825bc82017-09-28 18:50:23 +010076
77 # libc++ and libc++abi, for clang msan that require rebuilding the C++ lib
78 # from sources. Keep the SHA1s in sync with Chrome's src/buildtools/DEPS .
79 ('buildtools/libcxx',
80 'https://chromium.googlesource.com/chromium/llvm-project/libcxx.git',
81 '3a07dd740be63878167a0ea19fe81869954badd7',
82 'all'
83 ),
84 ('buildtools/libcxxabi',
85 'https://chromium.googlesource.com/chromium/llvm-project/libcxxabi.git',
86 '4072e8fd76febee37f60aeda76d6d9f5e3791daa',
87 'all'
88 ),
89
90
Primiano Tucciae2879e2017-09-27 11:02:09 +090091)
92
93ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
94
95
96def ReadFile(path):
97 if not os.path.exists(path):
98 return None
99 with open(path) as f:
100 return f.read().strip()
101
102
Primiano Tucci0825bc82017-09-28 18:50:23 +0100103def MkdirRecursive(path):
104 # Works with both relative and absolute paths
105 cwd = '/' if path.startswith('/') else ROOT_DIR
106 for part in path.split('/'):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900107 cwd = os.path.join(cwd, part)
108 if not os.path.exists(cwd):
109 os.makedirs(cwd)
110 else:
111 assert(os.path.isdir(cwd))
112
113
114def HashLocalFile(path):
115 if not os.path.exists(path):
116 return None
117 with open(path, 'rb') as f:
118 return hashlib.sha1(f.read()).hexdigest()
119
120
121def ExtractZipfilePreservePermissions(zf, info, path):
122 zf.extract(info.filename, path=path)
123 target_path = os.path.join(path, info.filename)
124 min_acls = 0o755 if info.filename.endswith('/') else 0o644
125 os.chmod(target_path, (info.external_attr >> 16L) | min_acls)
126
127
Primiano Tucci0825bc82017-09-28 18:50:23 +0100128def IsGitRepoCheckoutOutAtRevision(path, revision):
129 return ReadFile(os.path.join(path, '.git', 'HEAD')) == revision
130
131
132def CheckoutGitRepo(path, git_url, revision):
133 if IsGitRepoCheckoutOutAtRevision(path, revision):
134 return
135 if os.path.exists(path):
136 shutil.rmtree(path)
137 MkdirRecursive(path)
138 logging.info('Fetching %s @ %s into %s', git_url, revision, path)
139 subprocess.check_call(['git', 'clone', git_url, path], cwd=path)
140 subprocess.check_call(['git', 'checkout', revision, '--quiet'], cwd=path)
141 assert(IsGitRepoCheckoutOutAtRevision(path, revision))
142
143
Primiano Tucciae2879e2017-09-27 11:02:09 +0900144def Main():
145 parser = argparse.ArgumentParser()
146 parser.add_argument('--skip', action='append', default=[])
147 args = parser.parse_args()
148 skip_set = set(args.skip)
149 for rel_path, url, expected_sha1, platform in PREBUILTS:
150 if platform != 'all' and platform != sys.platform:
151 continue
152 if os.path.basename(rel_path) in skip_set:
153 logging.info('Skipping %s because of --skip cmdline arg', rel_path)
154 continue
155 local_path = os.path.join(ROOT_DIR, rel_path)
Primiano Tucci0825bc82017-09-28 18:50:23 +0100156 if url.endswith('.git'):
157 CheckoutGitRepo(local_path, url, expected_sha1)
158 continue
Primiano Tucciae2879e2017-09-27 11:02:09 +0900159 is_zip = local_path.lower().endswith('.zip')
160 zip_target_dir = local_path[:-4] if is_zip else None
161 zip_dir_stamp = os.path.join(zip_target_dir, '.stamp') if is_zip else None
162
163 if ((not is_zip and HashLocalFile(local_path) == expected_sha1) or
164 (is_zip and ReadFile(zip_dir_stamp) == expected_sha1)):
165 continue
166 MkdirRecursive(os.path.dirname(rel_path))
167 if HashLocalFile(local_path) != expected_sha1:
168 download_path = local_path + '.tmp'
169 logging.info('Downloading %s from %s', local_path, url)
170 urllib.urlretrieve(url, download_path)
171 os.chmod(download_path, 0o755)
172 if (HashLocalFile(download_path) != expected_sha1):
173 os.remove(download_path)
174 logging.fatal('SHA1 mismatch for %s', download_path)
175 return 1
176 os.rename(download_path, local_path)
177 assert(HashLocalFile(local_path) == expected_sha1)
178
179 if is_zip:
180 logging.info('Extracting %s into %s' % (local_path, zip_target_dir))
181 assert(os.path.commonprefix((ROOT_DIR, zip_target_dir)) == ROOT_DIR)
182 if os.path.exists(zip_target_dir):
183 logging.info('Deleting stale dir %s' % zip_target_dir)
184 shutil.rmtree(zip_target_dir)
185 with zipfile.ZipFile(local_path, 'r') as zf:
186 for info in zf.infolist():
187 ExtractZipfilePreservePermissions(zf, info, zip_target_dir)
188
189 # If the zip contains one root folder, rebase one level up moving all
190 # its sub files and folders inside |target_dir|.
191 subdir = os.listdir(zip_target_dir)
192 if len(subdir) == 1:
193 subdir = os.path.join(zip_target_dir, subdir[0])
194 if os.path.isdir(subdir):
195 for subf in os.listdir(subdir):
196 shutil.move(os.path.join(subdir,subf), zip_target_dir)
197 os.rmdir(subdir)
198 with open(zip_dir_stamp, 'w') as stamp_file:
199 stamp_file.write(expected_sha1)
200 os.remove(local_path)
201
202
203if __name__ == '__main__':
204 logging.basicConfig(level=logging.INFO)
205 sys.exit(Main())