blob: d3bf211bc82eef9f1271c1fd206e2620dc04b4f0 [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
Primiano Tuccid7750452017-09-29 14:38:51 +010026from collections import namedtuple
27
Primiano Tucci636ede12017-10-30 16:03:07 +000028# When adding a new git dependency here please also add a corresponding entry in
29# .travis.yml under the "cache:" section.
30
Primiano Tuccid7750452017-09-29 14:38:51 +010031# Dependencies required to build code on the host or when targeting desktop OS.
32BUILD_DEPS_HOST = [
Primiano Tucciae2879e2017-09-27 11:02:09 +090033 # GN
34 ('buildtools/mac/gn',
35 'https://storage.googleapis.com/chromium-gn/c2c934d4dda1f470a6511b1015dda9a9fb1ce50b',
36 'c2c934d4dda1f470a6511b1015dda9a9fb1ce50b',
37 'darwin'
38 ),
39 ('buildtools/linux64/gn',
40 'https://storage.googleapis.com/chromium-gn/b53fa13e950948c6f9a062189b76b34a9610281f',
41 'b53fa13e950948c6f9a062189b76b34a9610281f',
42 'linux2'
43 ),
44
Primiano Tucci104bd6d2017-10-12 00:10:24 +020045 # clang-format
46 ('buildtools/mac/clang-format',
47 'https://storage.googleapis.com/chromium-clang-format/0679b295e2ce2fce7919d1e8d003e497475f24a3',
48 '0679b295e2ce2fce7919d1e8d003e497475f24a3',
49 'darwin'
50 ),
51 ('buildtools/linux64/clang-format',
52 'https://storage.googleapis.com/chromium-clang-format/5349d1954e17f6ccafb6e6663b0f13cdb2bb33c8',
53 '5349d1954e17f6ccafb6e6663b0f13cdb2bb33c8',
54 'linux2'
55 ),
56 # Keep the SHA1 in sync with |clang_format_rev| in chromium //buildtools/DEPS.
57 ('buildtools/clang_format/script',
58 'https://chromium.googlesource.com/chromium/llvm-project/cfe/tools/clang-format.git',
59 '0653eee0c81ea04715c635dd0885e8096ff6ba6d',
60 'all'
61 ),
62
Primiano Tucciae2879e2017-09-27 11:02:09 +090063 # Ninja
64 ('buildtools/mac/ninja',
65 'https://storage.googleapis.com/fuchsia-build/fuchsia/ninja/mac/a1db595e824c50cf565fbf0af2437fd91b7babf4',
66 'a1db595e824c50cf565fbf0af2437fd91b7babf4',
67 'darwin'
68 ),
69 ('buildtools/linux64/ninja',
70 'https://storage.googleapis.com/fuchsia-build/fuchsia/ninja/linux64/d35b36c84a09f7e38b25947cafada10e8bf835bc',
71 'd35b36c84a09f7e38b25947cafada10e8bf835bc',
72 'linux2'
73 ),
74
Primiano Tuccid7750452017-09-29 14:38:51 +010075 # Keep in sync with Android's //external/googletest/README.version.
76 ('buildtools/googletest.zip',
77 'https://github.com/google/googletest/archive/ff07a5de0e81580547f1685e101194ed1a4fcd56.zip',
78 'c7edec7d7e6db1fc37a20710de9c4d89e3a3893b',
79 'all'
80 ),
81
82 # Keep in sync with Android's //external/protobuf/README.version.
83 ('buildtools/protobuf.zip',
84 'https://github.com/google/protobuf/releases/download/v3.0.0-beta-3/protobuf-cpp-3.0.0-beta-3.zip',
85 '3caec60aa9d8eefc8c3c3201b6b8ca19935edb89',
86 'all'
87 ),
88
89 # libc++ and libc++abi, for clang msan that require rebuilding the C++ lib
90 # from sources. Keep the SHA1s in sync with Chrome's src/buildtools/DEPS.
91 ('buildtools/libcxx',
92 'https://chromium.googlesource.com/chromium/llvm-project/libcxx.git',
93 '3a07dd740be63878167a0ea19fe81869954badd7',
94 'all'
95 ),
96 ('buildtools/libcxxabi',
97 'https://chromium.googlesource.com/chromium/llvm-project/libcxxabi.git',
98 '4072e8fd76febee37f60aeda76d6d9f5e3791daa',
99 'all'
100 ),
Hector Dearman88a10112017-10-12 11:07:10 +0100101
102 # Benchmarking tool.
103 ('buildtools/benchmark.zip',
104 'https://github.com/google/benchmark/archive/v1.2.0.zip',
105 '553b6bfa59c63f15c54cfd8c5122b0f7f84469af',
106 'all'
107 ),
Primiano Tuccid7750452017-09-29 14:38:51 +0100108]
109
110# Dependencies required to build Android code.
111# URLs and SHA1s taken from:
112# - https://dl.google.com/android/repository/repository-11.xml
113# - https://dl.google.com/android/repository/sys-img/android/sys-img.xml
114BUILD_DEPS_ANDROID = [
Primiano Tucciae2879e2017-09-27 11:02:09 +0900115 # Android NDK
116 ('buildtools/ndk.zip',
117 'https://dl.google.com/android/repository/android-ndk-r15c-darwin-x86_64.zip',
118 'ea4b5d76475db84745aa8828000d009625fc1f98',
119 'darwin'
120 ),
121 ('buildtools/ndk.zip',
122 'https://dl.google.com/android/repository/android-ndk-r15c-linux-x86_64.zip',
123 '0bf02d4e8b85fd770fd7b9b2cdec57f9441f27a2',
124 'linux2'
125 ),
Primiano Tuccid7750452017-09-29 14:38:51 +0100126]
Primiano Tucciae2879e2017-09-27 11:02:09 +0900127
Primiano Tuccid7750452017-09-29 14:38:51 +0100128# Dependencies required to run Android tests.
129TEST_DEPS_ANDROID = [
130 # tools.zip contains the emulator binaries.
131 ('buildtools/android_sdk/tools.zip',
132 'https://dl.google.com/android/repository/tools_r25.2.5-macosx.zip',
133 'd2168d963ac5b616e3d3ddaf21511d084baf3659',
134 'darwin'
135 ),
136 ('buildtools/android_sdk/tools.zip',
137 'https://dl.google.com/android/repository/tools_r25.2.5-linux.zip',
138 '72df3aa1988c0a9003ccdfd7a13a7b8bd0f47fc1',
139 'linux2'
Primiano Tucciae2879e2017-09-27 11:02:09 +0900140 ),
141
Primiano Tuccid7750452017-09-29 14:38:51 +0100142 # platform-tools.zip contains adb binaries.
143 ('buildtools/android_sdk/platform-tools.zip',
144 'https://dl.google.com/android/repository/platform-tools_r26.0.0-darwin.zip',
145 'e75b6137dc444f777eb02f44a6d9819b3aabff82',
146 'darwin'
147 ),
148 ('buildtools/android_sdk/platform-tools.zip',
149 'https://dl.google.com/android/repository/platform-tools_r26.0.0-linux.zip',
150 '00de8a6631405b617c10f68cd11ff2e1cd528e23',
151 'linux2'
Primiano Tucciae2879e2017-09-27 11:02:09 +0900152 ),
Primiano Tucci0825bc82017-09-28 18:50:23 +0100153
Primiano Tuccid7750452017-09-29 14:38:51 +0100154 # Android emulator images.
155 ('buildtools/android_sdk/system-images/android-24/default/armeabi-v7a.zip',
156 'https://dl.google.com/android/repository/sys-img/android/armeabi-v7a-24_r07.zip',
157 '3454546b4eed2d6c3dd06d47757d6da9f4176033',
Primiano Tucci0825bc82017-09-28 18:50:23 +0100158 'all'
159 ),
Primiano Tuccid7750452017-09-29 14:38:51 +0100160 ('buildtools/android_sdk/system-images/android-24/default/arm64-v8a.zip',
161 'https://dl.google.com/android/repository/sys-img/android/arm64-v8a-24_r07.zip',
162 'e8ab2e49e4efe4b064232b33b5eeaded61437d7f',
Primiano Tucci0825bc82017-09-28 18:50:23 +0100163 'all'
164 ),
Primiano Tuccid7750452017-09-29 14:38:51 +0100165]
Primiano Tucciae2879e2017-09-27 11:02:09 +0900166
167ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
168
169
170def ReadFile(path):
171 if not os.path.exists(path):
172 return None
173 with open(path) as f:
174 return f.read().strip()
175
176
Primiano Tucci0825bc82017-09-28 18:50:23 +0100177def MkdirRecursive(path):
178 # Works with both relative and absolute paths
179 cwd = '/' if path.startswith('/') else ROOT_DIR
180 for part in path.split('/'):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900181 cwd = os.path.join(cwd, part)
182 if not os.path.exists(cwd):
183 os.makedirs(cwd)
184 else:
185 assert(os.path.isdir(cwd))
186
187
188def HashLocalFile(path):
189 if not os.path.exists(path):
190 return None
191 with open(path, 'rb') as f:
192 return hashlib.sha1(f.read()).hexdigest()
193
194
195def ExtractZipfilePreservePermissions(zf, info, path):
196 zf.extract(info.filename, path=path)
197 target_path = os.path.join(path, info.filename)
198 min_acls = 0o755 if info.filename.endswith('/') else 0o644
199 os.chmod(target_path, (info.external_attr >> 16L) | min_acls)
200
201
Primiano Tucci0825bc82017-09-28 18:50:23 +0100202def IsGitRepoCheckoutOutAtRevision(path, revision):
203 return ReadFile(os.path.join(path, '.git', 'HEAD')) == revision
204
205
206def CheckoutGitRepo(path, git_url, revision):
207 if IsGitRepoCheckoutOutAtRevision(path, revision):
208 return
209 if os.path.exists(path):
210 shutil.rmtree(path)
211 MkdirRecursive(path)
212 logging.info('Fetching %s @ %s into %s', git_url, revision, path)
213 subprocess.check_call(['git', 'clone', git_url, path], cwd=path)
214 subprocess.check_call(['git', 'checkout', revision, '--quiet'], cwd=path)
215 assert(IsGitRepoCheckoutOutAtRevision(path, revision))
216
217
Primiano Tucciae2879e2017-09-27 11:02:09 +0900218def Main():
219 parser = argparse.ArgumentParser()
Primiano Tuccid7750452017-09-29 14:38:51 +0100220 parser.add_argument('--no-android', action='store_true')
Primiano Tucciae2879e2017-09-27 11:02:09 +0900221 args = parser.parse_args()
Primiano Tuccid7750452017-09-29 14:38:51 +0100222 deps = BUILD_DEPS_HOST
223 if not args.no_android:
224 deps += BUILD_DEPS_ANDROID + TEST_DEPS_ANDROID
225 for rel_path, url, expected_sha1, platform in deps:
Primiano Tucciae2879e2017-09-27 11:02:09 +0900226 if platform != 'all' and platform != sys.platform:
227 continue
Primiano Tucciae2879e2017-09-27 11:02:09 +0900228 local_path = os.path.join(ROOT_DIR, rel_path)
Primiano Tucci0825bc82017-09-28 18:50:23 +0100229 if url.endswith('.git'):
230 CheckoutGitRepo(local_path, url, expected_sha1)
231 continue
Primiano Tucciae2879e2017-09-27 11:02:09 +0900232 is_zip = local_path.lower().endswith('.zip')
233 zip_target_dir = local_path[:-4] if is_zip else None
234 zip_dir_stamp = os.path.join(zip_target_dir, '.stamp') if is_zip else None
235
236 if ((not is_zip and HashLocalFile(local_path) == expected_sha1) or
237 (is_zip and ReadFile(zip_dir_stamp) == expected_sha1)):
238 continue
239 MkdirRecursive(os.path.dirname(rel_path))
240 if HashLocalFile(local_path) != expected_sha1:
241 download_path = local_path + '.tmp'
242 logging.info('Downloading %s from %s', local_path, url)
243 urllib.urlretrieve(url, download_path)
244 os.chmod(download_path, 0o755)
245 if (HashLocalFile(download_path) != expected_sha1):
246 os.remove(download_path)
247 logging.fatal('SHA1 mismatch for %s', download_path)
248 return 1
249 os.rename(download_path, local_path)
250 assert(HashLocalFile(local_path) == expected_sha1)
251
252 if is_zip:
253 logging.info('Extracting %s into %s' % (local_path, zip_target_dir))
254 assert(os.path.commonprefix((ROOT_DIR, zip_target_dir)) == ROOT_DIR)
255 if os.path.exists(zip_target_dir):
256 logging.info('Deleting stale dir %s' % zip_target_dir)
257 shutil.rmtree(zip_target_dir)
258 with zipfile.ZipFile(local_path, 'r') as zf:
259 for info in zf.infolist():
260 ExtractZipfilePreservePermissions(zf, info, zip_target_dir)
261
262 # If the zip contains one root folder, rebase one level up moving all
263 # its sub files and folders inside |target_dir|.
264 subdir = os.listdir(zip_target_dir)
265 if len(subdir) == 1:
266 subdir = os.path.join(zip_target_dir, subdir[0])
267 if os.path.isdir(subdir):
268 for subf in os.listdir(subdir):
269 shutil.move(os.path.join(subdir,subf), zip_target_dir)
270 os.rmdir(subdir)
271 with open(zip_dir_stamp, 'w') as stamp_file:
272 stamp_file.write(expected_sha1)
273 os.remove(local_path)
274
275
276if __name__ == '__main__':
277 logging.basicConfig(level=logging.INFO)
278 sys.exit(Main())