blob: 99db737f00a9cbabdcc4171147b296cbae6891e9 [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
Sami Kyostilac7ddac72019-06-05 21:43:40 +010023import tempfile
Primiano Tucciae2879e2017-09-27 11:02:09 +090024import zipfile
25
Primiano Tuccid7750452017-09-29 14:38:51 +010026from collections import namedtuple
27
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +010028from compat import urlretrieve
29
Primiano Tuccib60d4b02017-11-10 11:03:00 +000030# The format for the deps below is the following:
31# (target_folder, source_url, sha1, target_platform)
32# |source_url| can be either a git repo or a http url.
33# If a git repo, |sha1| is the committish that will be checked out.
34# If a http url, |sha1| is the shasum of the original file.
35# If the url is a .zip or .tgz file it will be automatically deflated under
36# |target_folder|, taking care of stripping the root folder if it's a single
37# root (to avoid ending up with buildtools/protobuf/protobuf-1.2.3/... and have
38# instead just buildtools/protobuf).
39# |target_platform| is either 'darwin', 'linux2' or 'all' and applies the dep
40# only on the given platform (ask python why linux2 and not just linux).
41
Primiano Tuccid7750452017-09-29 14:38:51 +010042# Dependencies required to build code on the host or when targeting desktop OS.
43BUILD_DEPS_HOST = [
Matthew Clarkson63028d62019-10-10 15:48:23 +010044 # GN
45 ('buildtools/mac/gn',
46 'https://storage.googleapis.com/perfetto/gn-mac-b5b65ca39d93a7cde9fa713be31b114755252f28',
47 'b5b65ca39d93a7cde9fa713be31b114755252f28', 'darwin'),
48 ('buildtools/linux64/gn',
49 'https://storage.googleapis.com/perfetto/gn-linux64-1370d9c5358868b7b66292821b6fe61950826870',
50 '1370d9c5358868b7b66292821b6fe61950826870', 'linux2'),
Primiano Tucciae2879e2017-09-27 11:02:09 +090051
Matthew Clarkson63028d62019-10-10 15:48:23 +010052 # clang-format
53 ('buildtools/mac/clang-format',
54 'https://storage.googleapis.com/chromium-clang-format/025ca7c75f37ef4a40f3a67d81ddd11d7d0cdb9b',
55 '025ca7c75f37ef4a40f3a67d81ddd11d7d0cdb9b', 'darwin'),
56 ('buildtools/linux64/clang-format',
57 'https://storage.googleapis.com/chromium-clang-format/942fc8b1789144b8071d3fc03ff0fcbe1cf81ac8',
58 '942fc8b1789144b8071d3fc03ff0fcbe1cf81ac8', 'linux2'),
59 # Keep the SHA1 in sync with |clang_format_rev| in chromium //buildtools/DEPS.
60 ('buildtools/clang_format/script',
61 'https://chromium.googlesource.com/chromium/llvm-project/cfe/tools/clang-format.git',
62 '96636aa0e9f047f17447f2d45a094d0b59ed7917', 'all'),
Primiano Tucci104bd6d2017-10-12 00:10:24 +020063
Matthew Clarkson63028d62019-10-10 15:48:23 +010064 # Ninja
65 ('buildtools/mac/ninja',
66 'https://storage.googleapis.com/perfetto/ninja-mac-c15b0698da038b2bd2e8970c14c75fadc06b1add',
67 'c15b0698da038b2bd2e8970c14c75fadc06b1add', 'darwin'),
68 ('buildtools/linux64/ninja',
69 'https://storage.googleapis.com/perfetto/ninja-linux64-c866952bda50c29a669222477309287119bbb7e8',
70 'c866952bda50c29a669222477309287119bbb7e8', 'linux2'),
Primiano Tucciae2879e2017-09-27 11:02:09 +090071
Matthew Clarkson63028d62019-10-10 15:48:23 +010072 # Keep in sync with Android's //external/googletest/README.version.
73 ('buildtools/googletest.zip',
74 'https://github.com/google/googletest/archive/ff07a5de0e81580547f1685e101194ed1a4fcd56.zip',
75 'c7edec7d7e6db1fc37a20710de9c4d89e3a3893b', 'all'),
Primiano Tuccid7750452017-09-29 14:38:51 +010076
Matthew Clarkson63028d62019-10-10 15:48:23 +010077 # Keep in sync with Android's //external/protobuf/README.version.
78 ('buildtools/protobuf.zip',
79 'https://github.com/google/protobuf/releases/download/v3.0.0-beta-3/protobuf-cpp-3.0.0-beta-3.zip',
80 '3caec60aa9d8eefc8c3c3201b6b8ca19935edb89', 'all'),
Primiano Tuccid7750452017-09-29 14:38:51 +010081
Matthew Clarkson63028d62019-10-10 15:48:23 +010082 # libc++, libc++abi and libunwind for Linux where we need to rebuild the C++
83 # lib from sources. Keep the SHA1s in sync with Chrome's src/buildtools/DEPS.
84 ('buildtools/libcxx',
85 'https://chromium.googlesource.com/chromium/llvm-project/libcxx.git',
86 '5938e0582bac570a41edb3d6a2217c299adc1bc6', 'all'),
87 ('buildtools/libcxxabi',
88 'https://chromium.googlesource.com/chromium/llvm-project/libcxxabi.git',
89 '0d529660e32d77d9111912d73f2c74fc5fa2a858', 'all'),
90 ('buildtools/libunwind',
91 'https://chromium.googlesource.com/external/llvm.org/libunwind.git',
92 '69d9b84cca8354117b9fe9705a4430d789ee599b', 'all'),
Hector Dearman88a10112017-10-12 11:07:10 +010093
Matthew Clarkson63028d62019-10-10 15:48:23 +010094 # Keep the revision in sync with Chrome's PACKAGE_VERSION in
95 # tools/clang/scripts/update.py.
96 ('buildtools/clang.tgz',
97 'https://commondatastorage.googleapis.com/chromium-browser-clang/Linux_x64/clang-365097-f7e52fbd-8.tgz',
98 'fe1b1e5bd7381ae655661cb9658487389561568d', 'linux2'),
Primiano Tuccia65497e2018-09-26 19:53:58 +010099
Matthew Clarkson63028d62019-10-10 15:48:23 +0100100 # Keep in sync with chromium DEPS.
101 ('buildtools/libfuzzer',
102 'https://chromium.googlesource.com/chromium/llvm-project/compiler-rt/lib/fuzzer.git',
103 'b9f51dc8c98065df0c8da13c051046f5bab833db', 'linux2'),
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000104
Matthew Clarkson63028d62019-10-10 15:48:23 +0100105 # Benchmarking tool.
106 ('buildtools/benchmark.zip',
107 'https://github.com/google/benchmark/archive/v1.3.0.zip',
108 'f387e0df37d54bfd5be239e8d0d3ea2e2c3e34f4', 'all'),
Primiano Tucci38faa6f2018-04-01 20:12:08 +0200109
Matthew Clarkson63028d62019-10-10 15:48:23 +0100110 # Libbacktrace, for stacktraces in Linux/Android debug builds.
111 ('buildtools/libbacktrace.zip',
112 'https://github.com/ianlancetaylor/libbacktrace/archive/177940370e4a6b2509e92a0aaa9749184e64af43.zip',
113 'b723fe9d671d1ab54df1297f6afbf2893a41c3ea', 'all'),
Lalit Maganti6fe26e62018-05-23 12:14:38 +0100114
Matthew Clarkson63028d62019-10-10 15:48:23 +0100115 # Sqlite for the trace processing library.
116 # This is the amalgamated source whose compiled output is meant to be faster.
117 # We still pull the full source for the extensions (not amalgamated).
118 ('buildtools/sqlite.zip',
119 'https://storage.googleapis.com/perfetto/sqlite-amalgamation-3250300.zip',
120 'b78c2cb0d2c9182686c582312479f96a82bf5380', 'all'),
121 ('buildtools/sqlite_src.zip',
122 'https://storage.googleapis.com/perfetto/sqlite-src-3250300.zip',
123 'd1af2883bb800852946f9bf8ab6055e7698e18ee', 'all'),
Primiano Tucci0d72a312018-08-07 14:42:45 +0100124
Matthew Clarkson63028d62019-10-10 15:48:23 +0100125 # JsonCpp for legacy json import. Used only by the trace processor in
126 # standalone builds.
127 ('buildtools/jsoncpp.zip',
128 'https://github.com/open-source-parsers/jsoncpp/archive/1.0.0.zip',
129 '3219e26f2e249bb46b7d688478208c7ec138fea4', 'all'),
Florian Mayer475bd7e2018-08-07 20:04:03 +0100130
Matthew Clarkson63028d62019-10-10 15:48:23 +0100131 # These dependencies are for libunwindstack, which is used by src/profiling.
132 ('buildtools/android-core',
133 'https://android.googlesource.com/platform/system/core.git',
134 '3f407fcc37b401c91784700c0a691ba8b1f7ef15', 'all'),
135 ('buildtools/lzma',
136 'https://android.googlesource.com/platform/external/lzma.git',
137 '7851dce6f4ca17f5caa1c93a4e0a45686b1d56c3', 'all'),
138 ('buildtools/zlib',
139 'https://android.googlesource.com/platform/external/zlib.git',
140 'dfa0646a03b4e1707469e04dc931b09774968fe6', 'all'),
141 ('buildtools/bionic',
142 'https://android.googlesource.com/platform/bionic.git',
143 'a60488109cda997dfd83832731c8527feaa2825e', 'all'),
Florian Mayer475bd7e2018-08-07 20:04:03 +0100144
Matthew Clarkson63028d62019-10-10 15:48:23 +0100145 # Example traces for regression tests.
146 (
147 'buildtools/test_data.zip',
148 'https://storage.googleapis.com/perfetto/test-data-20191008-115945.zip',
149 '4738c53cec52dc2bf98d7c3a1b6f0bccfaaf26f8',
150 'all',
151 ),
Hector Dearman7f71d0e2018-08-09 11:26:10 +0100152
Matthew Clarkson63028d62019-10-10 15:48:23 +0100153 # Linenoise, used only by trace_processor in standalone builds.
154 ('buildtools/linenoise',
155 'https://fuchsia.googlesource.com/third_party/linenoise.git',
156 'c894b9e59f02203dbe4e2be657572cf88c4230c3', 'all'),
Primiano Tuccid7750452017-09-29 14:38:51 +0100157]
158
159# Dependencies required to build Android code.
160# URLs and SHA1s taken from:
161# - https://dl.google.com/android/repository/repository-11.xml
162# - https://dl.google.com/android/repository/sys-img/android/sys-img.xml
163BUILD_DEPS_ANDROID = [
Matthew Clarkson63028d62019-10-10 15:48:23 +0100164 # Android NDK
165 ('buildtools/ndk.zip',
166 'https://dl.google.com/android/repository/android-ndk-r17b-darwin-x86_64.zip',
167 'f990aafaffec0b583d2c5420bfa622e52ac14248', 'darwin'),
168 ('buildtools/ndk.zip',
169 'https://dl.google.com/android/repository/android-ndk-r17b-linux-x86_64.zip',
170 'dd5762ee7ef4995ad04fe0c45a608c344d99ca9f', 'linux2'),
Primiano Tuccid7750452017-09-29 14:38:51 +0100171]
Primiano Tucciae2879e2017-09-27 11:02:09 +0900172
Primiano Tuccid7750452017-09-29 14:38:51 +0100173# Dependencies required to run Android tests.
174TEST_DEPS_ANDROID = [
Matthew Clarkson63028d62019-10-10 15:48:23 +0100175 # Android emulator images.
176 ('buildtools/aosp-arm.zip',
177 'https://storage.googleapis.com/perfetto/aosp-02022018-arm.zip',
178 'a480d5e7d3ca888b0a58fe15ce76b1791537429a', 'all'),
Primiano Tucciae2879e2017-09-27 11:02:09 +0900179
Matthew Clarkson63028d62019-10-10 15:48:23 +0100180 # platform-tools.zip contains adb binaries.
181 ('buildtools/android_sdk/platform-tools.zip',
182 'https://dl.google.com/android/repository/platform-tools_r26.0.0-darwin.zip',
183 'e75b6137dc444f777eb02f44a6d9819b3aabff82', 'darwin'),
184 ('buildtools/android_sdk/platform-tools.zip',
185 'https://dl.google.com/android/repository/platform-tools_r26.0.0-linux.zip',
186 '00de8a6631405b617c10f68cd11ff2e1cd528e23', 'linux2'),
Primiano Tucci0825bc82017-09-28 18:50:23 +0100187
Matthew Clarkson63028d62019-10-10 15:48:23 +0100188 # Android emulator binaries.
189 ('buildtools/emulator',
190 'https://android.googlesource.com/platform/prebuilts/android-emulator.git',
191 '4b260028dc27bc92c39bee9129cb2ba839970956', 'all'),
Primiano Tuccid7750452017-09-29 14:38:51 +0100192]
Primiano Tucciae2879e2017-09-27 11:02:09 +0900193
Primiano Tucci1c752c12018-10-23 09:27:19 +0100194# This variable is updated by tools/roll-catapult-trace-viewer.
195CATAPULT_SHA1 = 'ff5d8fd7244680b4d4456c25d5fdc04c76f9ef66'
196
Hector Dearman6177b752019-01-24 10:17:32 +0000197TYPEFACES_SHA1 = '756b0a015b8f99f5718f7fdf967d052c1ec55ab3'
198
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100199UI_DEPS = [
Matthew Clarkson63028d62019-10-10 15:48:23 +0100200 ('buildtools/nodejs.tgz',
201 'https://storage.googleapis.com/perfetto/node-v10.3.0-darwin-x64.tar.gz',
202 '6d9a122785f38c256add3b25f74adf125497861a', 'darwin'),
203 ('buildtools/nodejs.tgz',
204 'https://storage.googleapis.com/perfetto/node-v10.3.0-linux-x64.tar.xz',
205 '118f6ea19f75089b3f12ac2ddfce357bff872b5e', 'linux2'),
206 ('buildtools/emsdk/emscripten.tgz',
207 'https://storage.googleapis.com/perfetto/emscripten-1.37.40.tar.gz',
208 '588c28221321ebbdfc8e3a6f47ea6106f589669b', 'all'),
209 ('buildtools/emsdk/llvm.tgz',
210 'https://storage.googleapis.com/perfetto/emscripten-llvm-e1.37.40-darwin.tar.gz',
211 '7a894ef0a52821c62f6abaac552dc4ce5d424607', 'darwin'),
212 ('buildtools/emsdk/llvm.tgz',
213 'https://storage.googleapis.com/perfetto/emscripten-llvm-e1.37.40-static-linux.tar.gz',
214 '478501b9b7a14884e546c84efe209a90052cbb07', 'linux2'),
215 ('buildtools/d8.tgz',
216 'https://storage.googleapis.com/perfetto/d8-linux2-5.7.492.65.tar.gz',
217 '95e82ad7faf0a6f74d950c2aa65e3858b7bdb6c6', 'linux2'),
218 ('buildtools/d8.tgz',
219 'https://storage.googleapis.com/perfetto/d8-darwin-6.6.346.32.tar.gz',
220 '1abd630619bb1977ab62095570a113d782a1545d', 'darwin'),
221 ('buildtools/catapult_trace_viewer.tgz',
222 'https://storage.googleapis.com/perfetto/catapult_trace_viewer-%s.tar.gz' %
223 CATAPULT_SHA1, CATAPULT_SHA1, 'all'),
224 ('buildtools/typefaces.tgz',
225 'https://storage.googleapis.com/perfetto/typefaces-%s.tar.gz' %
226 TYPEFACES_SHA1, TYPEFACES_SHA1, 'all')
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100227]
228
Primiano Tucciae2879e2017-09-27 11:02:09 +0900229ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
230
231
232def ReadFile(path):
233 if not os.path.exists(path):
234 return None
235 with open(path) as f:
Matthew Clarkson63028d62019-10-10 15:48:23 +0100236 return f.read().strip()
Primiano Tucciae2879e2017-09-27 11:02:09 +0900237
238
Primiano Tucci0825bc82017-09-28 18:50:23 +0100239def MkdirRecursive(path):
240 # Works with both relative and absolute paths
241 cwd = '/' if path.startswith('/') else ROOT_DIR
242 for part in path.split('/'):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900243 cwd = os.path.join(cwd, part)
244 if not os.path.exists(cwd):
245 os.makedirs(cwd)
246 else:
Matthew Clarkson63028d62019-10-10 15:48:23 +0100247 assert (os.path.isdir(cwd))
Primiano Tucciae2879e2017-09-27 11:02:09 +0900248
249
250def HashLocalFile(path):
251 if not os.path.exists(path):
252 return None
253 with open(path, 'rb') as f:
254 return hashlib.sha1(f.read()).hexdigest()
255
256
257def ExtractZipfilePreservePermissions(zf, info, path):
258 zf.extract(info.filename, path=path)
259 target_path = os.path.join(path, info.filename)
260 min_acls = 0o755 if info.filename.endswith('/') else 0o644
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +0100261 os.chmod(target_path, (info.external_attr >> 16) | min_acls)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900262
263
Primiano Tucci0825bc82017-09-28 18:50:23 +0100264def IsGitRepoCheckoutOutAtRevision(path, revision):
265 return ReadFile(os.path.join(path, '.git', 'HEAD')) == revision
266
267
268def CheckoutGitRepo(path, git_url, revision):
269 if IsGitRepoCheckoutOutAtRevision(path, revision):
Eric Seckler676421f2019-02-12 14:43:31 +0000270 return False
Primiano Tucci0825bc82017-09-28 18:50:23 +0100271 if os.path.exists(path):
272 shutil.rmtree(path)
273 MkdirRecursive(path)
274 logging.info('Fetching %s @ %s into %s', git_url, revision, path)
Lalit Maganti367fcd52018-02-05 16:06:13 +0000275 subprocess.check_call(['git', 'init', path], cwd=path)
276 subprocess.check_call(
Matthew Clarkson63028d62019-10-10 15:48:23 +0100277 ['git', 'fetch', '--quiet', '--depth', '1', git_url, revision], cwd=path)
Primiano Tucci0825bc82017-09-28 18:50:23 +0100278 subprocess.check_call(['git', 'checkout', revision, '--quiet'], cwd=path)
Matthew Clarkson63028d62019-10-10 15:48:23 +0100279 assert (IsGitRepoCheckoutOutAtRevision(path, revision))
Eric Seckler676421f2019-02-12 14:43:31 +0000280 return True
Primiano Tucci0825bc82017-09-28 18:50:23 +0100281
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100282
Deepanjan Royacf8c072018-07-13 11:37:04 -0400283def InstallNodeModules():
284 ui_dir = os.path.join(ROOT_DIR, 'ui')
285 logging.info("Running npm install in {0}".format(ui_dir))
Matthew Clarkson63028d62019-10-10 15:48:23 +0100286 subprocess.check_call([os.path.join(ui_dir, 'npm'), 'install', '--no-save'],
287 cwd=os.path.join(ROOT_DIR, 'ui'))
Primiano Tucci0825bc82017-09-28 18:50:23 +0100288
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100289
290def CheckHashes():
Matthew Clarkson63028d62019-10-10 15:48:23 +0100291 for deps in [BUILD_DEPS_HOST, BUILD_DEPS_ANDROID, TEST_DEPS_ANDROID, UI_DEPS]:
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100292 for rel_path, url, expected_sha1, platform in deps:
293 if url.endswith('.git'):
294 continue
295 logging.info('Downloading %s from %s', rel_path, url)
296 with tempfile.NamedTemporaryFile(delete=False) as f:
297 f.close()
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +0100298 urlretrieve(url, f.name)
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100299 actual_sha1 = HashLocalFile(f.name)
300 os.unlink(f.name)
301 if (actual_sha1 != expected_sha1):
302 logging.fatal('SHA1 mismatch for {} expected {} was {}'.format(
303 url, expected_sha1, actual_sha1))
304
305
Primiano Tucciae2879e2017-09-27 11:02:09 +0900306def Main():
307 parser = argparse.ArgumentParser()
Primiano Tuccid7750452017-09-29 14:38:51 +0100308 parser.add_argument('--no-android', action='store_true')
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100309 parser.add_argument('--ui', action='store_true')
Matthew Clarkson63028d62019-10-10 15:48:23 +0100310 parser.add_argument(
311 '--check-hashes', help='Check hashes for all URLs', action='store_true')
Primiano Tucciae2879e2017-09-27 11:02:09 +0900312 args = parser.parse_args()
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100313 if args.check_hashes:
314 CheckHashes()
315 return 0
Primiano Tuccid7750452017-09-29 14:38:51 +0100316 deps = BUILD_DEPS_HOST
317 if not args.no_android:
318 deps += BUILD_DEPS_ANDROID + TEST_DEPS_ANDROID
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100319 if args.ui:
320 deps += UI_DEPS
Eric Seckler6e828f32019-01-02 11:10:56 +0000321 deps_updated = False
Primiano Tuccid7750452017-09-29 14:38:51 +0100322 for rel_path, url, expected_sha1, platform in deps:
Lalit Maganti367fcd52018-02-05 16:06:13 +0000323 if (platform != 'all' and platform != sys.platform):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900324 continue
Primiano Tucciae2879e2017-09-27 11:02:09 +0900325 local_path = os.path.join(ROOT_DIR, rel_path)
Primiano Tucci0825bc82017-09-28 18:50:23 +0100326 if url.endswith('.git'):
Eric Seckler676421f2019-02-12 14:43:31 +0000327 deps_updated |= CheckoutGitRepo(local_path, url, expected_sha1)
Primiano Tucci0825bc82017-09-28 18:50:23 +0100328 continue
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000329 is_zip = local_path.endswith('.zip') or local_path.endswith('.tgz')
Primiano Tucciae2879e2017-09-27 11:02:09 +0900330 zip_target_dir = local_path[:-4] if is_zip else None
331 zip_dir_stamp = os.path.join(zip_target_dir, '.stamp') if is_zip else None
332
333 if ((not is_zip and HashLocalFile(local_path) == expected_sha1) or
334 (is_zip and ReadFile(zip_dir_stamp) == expected_sha1)):
335 continue
Eric Seckler6e828f32019-01-02 11:10:56 +0000336 deps_updated = True
Primiano Tucciae2879e2017-09-27 11:02:09 +0900337 MkdirRecursive(os.path.dirname(rel_path))
338 if HashLocalFile(local_path) != expected_sha1:
339 download_path = local_path + '.tmp'
340 logging.info('Downloading %s from %s', local_path, url)
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +0100341 urlretrieve(url, download_path)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900342 os.chmod(download_path, 0o755)
Hector Dearmand9628d32018-10-17 14:38:26 +0100343 actual_sha1 = HashLocalFile(download_path)
344 if (actual_sha1 != expected_sha1):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900345 os.remove(download_path)
Hector Dearmand9628d32018-10-17 14:38:26 +0100346 logging.fatal('SHA1 mismatch for {} expected {} was {}'.format(
347 download_path, expected_sha1, actual_sha1))
Primiano Tucciae2879e2017-09-27 11:02:09 +0900348 return 1
349 os.rename(download_path, local_path)
Matthew Clarkson63028d62019-10-10 15:48:23 +0100350 assert (HashLocalFile(local_path) == expected_sha1)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900351
352 if is_zip:
353 logging.info('Extracting %s into %s' % (local_path, zip_target_dir))
Matthew Clarkson63028d62019-10-10 15:48:23 +0100354 assert (os.path.commonprefix((ROOT_DIR, zip_target_dir)) == ROOT_DIR)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900355 if os.path.exists(zip_target_dir):
356 logging.info('Deleting stale dir %s' % zip_target_dir)
357 shutil.rmtree(zip_target_dir)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900358
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000359 # Decompress the archive.
360 if local_path.endswith('.tgz'):
361 MkdirRecursive(zip_target_dir)
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100362 subprocess.check_call(['tar', '-xf', local_path], cwd=zip_target_dir)
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000363 elif local_path.endswith('.zip'):
364 with zipfile.ZipFile(local_path, 'r') as zf:
365 for info in zf.infolist():
366 ExtractZipfilePreservePermissions(zf, info, zip_target_dir)
367
368 # If the zip contains one root folder, rebase one level up moving all
369 # its sub files and folders inside |target_dir|.
370 subdir = os.listdir(zip_target_dir)
371 if len(subdir) == 1:
372 subdir = os.path.join(zip_target_dir, subdir[0])
373 if os.path.isdir(subdir):
374 for subf in os.listdir(subdir):
Matthew Clarkson63028d62019-10-10 15:48:23 +0100375 shutil.move(os.path.join(subdir, subf), zip_target_dir)
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000376 os.rmdir(subdir)
377
378 # Create stamp and remove the archive.
379 with open(zip_dir_stamp, 'w') as stamp_file:
380 stamp_file.write(expected_sha1)
381 os.remove(local_path)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900382
Deepanjan Royacf8c072018-07-13 11:37:04 -0400383 if args.ui:
384 # Needs to happen after nodejs is installed above.
385 InstallNodeModules()
Primiano Tucciae2879e2017-09-27 11:02:09 +0900386
Eric Seckler6e828f32019-01-02 11:10:56 +0000387 if deps_updated:
388 # Stale binary files may be compiled against old sysroot headers that aren't
389 # tracked by gn.
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +0100390 logging.warning('Remember to run "gn clean <output_directory>" ' +
391 'to avoid stale binary files.')
Eric Seckler6e828f32019-01-02 11:10:56 +0000392
Matthew Clarkson63028d62019-10-10 15:48:23 +0100393
Primiano Tucciae2879e2017-09-27 11:02:09 +0900394if __name__ == '__main__':
395 logging.basicConfig(level=logging.INFO)
396 sys.exit(Main())