blob: 27a29a5dedf4d1683d0841869dc21110368b16c8 [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 urllib
25import zipfile
26
Primiano Tuccid7750452017-09-29 14:38:51 +010027from collections import namedtuple
28
Primiano Tucci636ede12017-10-30 16:03:07 +000029# When adding a new git dependency here please also add a corresponding entry in
30# .travis.yml under the "cache:" section.
31
Primiano Tuccib60d4b02017-11-10 11:03:00 +000032# The format for the deps below is the following:
33# (target_folder, source_url, sha1, target_platform)
34# |source_url| can be either a git repo or a http url.
35# If a git repo, |sha1| is the committish that will be checked out.
36# If a http url, |sha1| is the shasum of the original file.
37# If the url is a .zip or .tgz file it will be automatically deflated under
38# |target_folder|, taking care of stripping the root folder if it's a single
39# root (to avoid ending up with buildtools/protobuf/protobuf-1.2.3/... and have
40# instead just buildtools/protobuf).
41# |target_platform| is either 'darwin', 'linux2' or 'all' and applies the dep
42# only on the given platform (ask python why linux2 and not just linux).
43
Primiano Tuccid7750452017-09-29 14:38:51 +010044# Dependencies required to build code on the host or when targeting desktop OS.
45BUILD_DEPS_HOST = [
Primiano Tucciae2879e2017-09-27 11:02:09 +090046 # GN
47 ('buildtools/mac/gn',
Primiano Tucci335412d2019-05-30 16:29:16 +010048 'https://storage.googleapis.com/perfetto/gn-mac-b5b65ca39d93a7cde9fa713be31b114755252f28',
49 'b5b65ca39d93a7cde9fa713be31b114755252f28',
Primiano Tucciae2879e2017-09-27 11:02:09 +090050 'darwin'
51 ),
52 ('buildtools/linux64/gn',
Primiano Tucci335412d2019-05-30 16:29:16 +010053 'https://storage.googleapis.com/perfetto/gn-linux64-1370d9c5358868b7b66292821b6fe61950826870',
54 '1370d9c5358868b7b66292821b6fe61950826870',
Primiano Tucciae2879e2017-09-27 11:02:09 +090055 'linux2'
56 ),
57
Primiano Tucci104bd6d2017-10-12 00:10:24 +020058 # clang-format
59 ('buildtools/mac/clang-format',
Primiano Tucci335412d2019-05-30 16:29:16 +010060 'https://storage.googleapis.com/chromium-clang-format/025ca7c75f37ef4a40f3a67d81ddd11d7d0cdb9b',
61 '025ca7c75f37ef4a40f3a67d81ddd11d7d0cdb9b',
Primiano Tucci104bd6d2017-10-12 00:10:24 +020062 'darwin'
63 ),
64 ('buildtools/linux64/clang-format',
Primiano Tucci335412d2019-05-30 16:29:16 +010065 'https://storage.googleapis.com/chromium-clang-format/942fc8b1789144b8071d3fc03ff0fcbe1cf81ac8',
66 '942fc8b1789144b8071d3fc03ff0fcbe1cf81ac8',
Primiano Tucci104bd6d2017-10-12 00:10:24 +020067 'linux2'
68 ),
69 # Keep the SHA1 in sync with |clang_format_rev| in chromium //buildtools/DEPS.
70 ('buildtools/clang_format/script',
71 'https://chromium.googlesource.com/chromium/llvm-project/cfe/tools/clang-format.git',
Primiano Tucci335412d2019-05-30 16:29:16 +010072 '96636aa0e9f047f17447f2d45a094d0b59ed7917',
Primiano Tucci104bd6d2017-10-12 00:10:24 +020073 'all'
74 ),
75
Primiano Tucciae2879e2017-09-27 11:02:09 +090076 # Ninja
77 ('buildtools/mac/ninja',
Primiano Tucci335412d2019-05-30 16:29:16 +010078 'https://storage.googleapis.com/perfetto/ninja-mac-c15b0698da038b2bd2e8970c14c75fadc06b1add',
79 'c15b0698da038b2bd2e8970c14c75fadc06b1add',
Primiano Tucciae2879e2017-09-27 11:02:09 +090080 'darwin'
81 ),
82 ('buildtools/linux64/ninja',
Primiano Tucci335412d2019-05-30 16:29:16 +010083 'https://storage.googleapis.com/perfetto/ninja-linux64-c866952bda50c29a669222477309287119bbb7e8',
84 'c866952bda50c29a669222477309287119bbb7e8',
Primiano Tucciae2879e2017-09-27 11:02:09 +090085 'linux2'
86 ),
87
Primiano Tuccid7750452017-09-29 14:38:51 +010088 # Keep in sync with Android's //external/googletest/README.version.
89 ('buildtools/googletest.zip',
90 'https://github.com/google/googletest/archive/ff07a5de0e81580547f1685e101194ed1a4fcd56.zip',
91 'c7edec7d7e6db1fc37a20710de9c4d89e3a3893b',
92 'all'
93 ),
94
95 # Keep in sync with Android's //external/protobuf/README.version.
96 ('buildtools/protobuf.zip',
97 'https://github.com/google/protobuf/releases/download/v3.0.0-beta-3/protobuf-cpp-3.0.0-beta-3.zip',
98 '3caec60aa9d8eefc8c3c3201b6b8ca19935edb89',
99 'all'
100 ),
101
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000102 # libc++, libc++abi and libunwind for Linux where we need to rebuild the C++
103 # lib from sources. Keep the SHA1s in sync with Chrome's src/buildtools/DEPS.
Primiano Tuccid7750452017-09-29 14:38:51 +0100104 ('buildtools/libcxx',
105 'https://chromium.googlesource.com/chromium/llvm-project/libcxx.git',
Primiano Tucci335412d2019-05-30 16:29:16 +0100106 '5938e0582bac570a41edb3d6a2217c299adc1bc6',
Primiano Tuccid7750452017-09-29 14:38:51 +0100107 'all'
108 ),
109 ('buildtools/libcxxabi',
110 'https://chromium.googlesource.com/chromium/llvm-project/libcxxabi.git',
Primiano Tucci335412d2019-05-30 16:29:16 +0100111 '0d529660e32d77d9111912d73f2c74fc5fa2a858',
Primiano Tuccid7750452017-09-29 14:38:51 +0100112 'all'
113 ),
Primiano Tucci7278dea2017-10-31 11:50:32 +0000114 ('buildtools/libunwind',
115 'https://chromium.googlesource.com/external/llvm.org/libunwind.git',
Primiano Tucci335412d2019-05-30 16:29:16 +0100116 '69d9b84cca8354117b9fe9705a4430d789ee599b',
Primiano Tucci7278dea2017-10-31 11:50:32 +0000117 'all'
118 ),
Hector Dearman88a10112017-10-12 11:07:10 +0100119
Primiano Tucci335412d2019-05-30 16:29:16 +0100120 # Keep the revision in sync with Chrome's PACKAGE_VERSION in
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000121 # tools/clang/scripts/update.py.
122 ('buildtools/clang.tgz',
Primiano Tucci335412d2019-05-30 16:29:16 +0100123 'https://commondatastorage.googleapis.com/chromium-browser-clang/Linux_x64/clang-361212-67510fac-4.tgz',
124 '5bdd995f13a8b67c98062394d0869c6a409e02b8',
Primiano Tuccia65497e2018-09-26 19:53:58 +0100125 'linux2'
126 ),
127
128 # Keep in sync with chromium DEPS.
129 ('buildtools/libfuzzer',
130 'https://chromium.googlesource.com/chromium/llvm-project/compiler-rt/lib/fuzzer.git',
Primiano Tucci5403e4f2018-11-27 10:07:03 +0000131 '2a53098584c48af50aec3fb51febe5e651489774',
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000132 'linux2'
133 ),
134
Hector Dearman88a10112017-10-12 11:07:10 +0100135 # Benchmarking tool.
136 ('buildtools/benchmark.zip',
Lalit Magantic99d93c2018-03-22 15:09:30 +0000137 'https://github.com/google/benchmark/archive/v1.3.0.zip',
138 'f387e0df37d54bfd5be239e8d0d3ea2e2c3e34f4',
Hector Dearman88a10112017-10-12 11:07:10 +0100139 'all'
140 ),
Primiano Tucci38faa6f2018-04-01 20:12:08 +0200141
142 # Libbacktrace, for stacktraces in Linux/Android debug builds.
143 ('buildtools/libbacktrace.zip',
144 'https://github.com/ianlancetaylor/libbacktrace/archive/177940370e4a6b2509e92a0aaa9749184e64af43.zip',
145 'b723fe9d671d1ab54df1297f6afbf2893a41c3ea',
146 'all'
147 ),
Lalit Maganti6fe26e62018-05-23 12:14:38 +0100148
149 # Sqlite for the trace processing library.
Ioannis Ilkos178535e2018-11-05 17:32:45 +0000150 # This is the amalgamated source whose compiled output is meant to be faster.
151 # We still pull the full source for the extensions (not amalgamated).
Lalit Maganti6fe26e62018-05-23 12:14:38 +0100152 ('buildtools/sqlite.zip',
Lalit Maganti10d6deb2018-11-20 15:15:50 +0000153 'https://storage.googleapis.com/perfetto/sqlite-amalgamation-3250300.zip',
154 'b78c2cb0d2c9182686c582312479f96a82bf5380',
Lalit Maganti6fe26e62018-05-23 12:14:38 +0100155 'all'
156 ),
Ioannis Ilkos178535e2018-11-05 17:32:45 +0000157 ('buildtools/sqlite_src.zip',
Lalit Maganti10d6deb2018-11-20 15:15:50 +0000158 'https://storage.googleapis.com/perfetto/sqlite-src-3250300.zip',
159 'd1af2883bb800852946f9bf8ab6055e7698e18ee',
Ioannis Ilkos178535e2018-11-05 17:32:45 +0000160 'all'
161 ),
Primiano Tucci0d72a312018-08-07 14:42:45 +0100162
163 # JsonCpp for legacy json import. Used only by the trace processor in
164 # standalone builds.
165 ('buildtools/jsoncpp.zip',
166 'https://github.com/open-source-parsers/jsoncpp/archive/1.0.0.zip',
167 '3219e26f2e249bb46b7d688478208c7ec138fea4',
168 'all'
169 ),
Florian Mayer475bd7e2018-08-07 20:04:03 +0100170
Florian Mayerf8335662018-08-08 11:30:32 +0100171 # These dependencies are for libunwindstack, which is used by src/profiling.
172 ('buildtools/android-core',
Florian Mayer475bd7e2018-08-07 20:04:03 +0100173 'https://android.googlesource.com/platform/system/core.git',
Florian Mayerb3110de2019-05-10 10:52:40 +0100174 '09070afd38466fc0e84aa58b90a336fd4b3f56a0',
Florian Mayer475bd7e2018-08-07 20:04:03 +0100175 'all'
176 ),
177
178 ('buildtools/lzma',
179 'https://android.googlesource.com/platform/external/lzma.git',
180 '7851dce6f4ca17f5caa1c93a4e0a45686b1d56c3',
181 'all'
182 ),
Hector Dearman7f71d0e2018-08-09 11:26:10 +0100183
Hector Dearmane0b993f2019-05-24 18:48:16 +0100184 ('buildtools/zlib',
185 'https://android.googlesource.com/platform/external/zlib.git',
186 'dfa0646a03b4e1707469e04dc931b09774968fe6',
187 'all'
188 ),
189
Florian Mayerb64d6b12018-08-30 10:46:30 -0700190 ('buildtools/bionic',
191 'https://android.googlesource.com/platform/bionic.git',
Primiano Tucci1721b1a2019-02-17 14:18:07 +0000192 'a60488109cda997dfd83832731c8527feaa2825e',
Florian Mayerb64d6b12018-08-30 10:46:30 -0700193 'all'
194 ),
195
Hector Dearman7f71d0e2018-08-09 11:26:10 +0100196 # Example traces for regression tests.
197 ('buildtools/test_data.zip',
Brian Hamrickd57e1332019-04-24 11:25:36 -0700198 'https://storage.googleapis.com/perfetto/test-data-20190423-131328.zip',
199 '263db97612203fd0dd047edd54eaa7007e32bf91',
Hector Dearman7f71d0e2018-08-09 11:26:10 +0100200 'all',
201 ),
Hector Dearmane44ad452018-09-21 11:51:57 +0100202
Primiano Tucci21d41a72018-09-24 23:12:04 +0100203 # Linenoise, used only by trace_processor in standalone builds.
Hector Dearmane44ad452018-09-21 11:51:57 +0100204 ('buildtools/linenoise',
Primiano Tucci21d41a72018-09-24 23:12:04 +0100205 'https://fuchsia.googlesource.com/third_party/linenoise.git',
206 'c894b9e59f02203dbe4e2be657572cf88c4230c3',
Hector Dearmane44ad452018-09-21 11:51:57 +0100207 'all'
208 ),
Primiano Tuccid7750452017-09-29 14:38:51 +0100209]
210
211# Dependencies required to build Android code.
212# URLs and SHA1s taken from:
213# - https://dl.google.com/android/repository/repository-11.xml
214# - https://dl.google.com/android/repository/sys-img/android/sys-img.xml
215BUILD_DEPS_ANDROID = [
Primiano Tucciae2879e2017-09-27 11:02:09 +0900216 # Android NDK
217 ('buildtools/ndk.zip',
Florian Mayeraa5316b2018-08-20 17:45:12 -0700218 'https://dl.google.com/android/repository/android-ndk-r17b-darwin-x86_64.zip',
219 'f990aafaffec0b583d2c5420bfa622e52ac14248',
Primiano Tucciae2879e2017-09-27 11:02:09 +0900220 'darwin'
221 ),
222 ('buildtools/ndk.zip',
Florian Mayeraa5316b2018-08-20 17:45:12 -0700223 'https://dl.google.com/android/repository/android-ndk-r17b-linux-x86_64.zip',
224 'dd5762ee7ef4995ad04fe0c45a608c344d99ca9f',
Primiano Tucciae2879e2017-09-27 11:02:09 +0900225 'linux2'
226 ),
Primiano Tuccid7750452017-09-29 14:38:51 +0100227]
Primiano Tucciae2879e2017-09-27 11:02:09 +0900228
Primiano Tuccid7750452017-09-29 14:38:51 +0100229# Dependencies required to run Android tests.
230TEST_DEPS_ANDROID = [
Lalit Maganti367fcd52018-02-05 16:06:13 +0000231 # Android emulator images.
232 ('buildtools/aosp-arm.zip',
233 'https://storage.googleapis.com/perfetto/aosp-02022018-arm.zip',
234 'a480d5e7d3ca888b0a58fe15ce76b1791537429a',
Primiano Tuccie7ca7c62018-04-07 08:28:03 +0200235 'all'
Primiano Tucciae2879e2017-09-27 11:02:09 +0900236 ),
237
Primiano Tuccid7750452017-09-29 14:38:51 +0100238 # platform-tools.zip contains adb binaries.
239 ('buildtools/android_sdk/platform-tools.zip',
240 'https://dl.google.com/android/repository/platform-tools_r26.0.0-darwin.zip',
241 'e75b6137dc444f777eb02f44a6d9819b3aabff82',
242 'darwin'
243 ),
244 ('buildtools/android_sdk/platform-tools.zip',
245 'https://dl.google.com/android/repository/platform-tools_r26.0.0-linux.zip',
246 '00de8a6631405b617c10f68cd11ff2e1cd528e23',
247 'linux2'
Primiano Tucciae2879e2017-09-27 11:02:09 +0900248 ),
Primiano Tucci0825bc82017-09-28 18:50:23 +0100249
Lalit Maganti367fcd52018-02-05 16:06:13 +0000250 # Android emulator binaries.
251 ('buildtools/emulator',
252 'https://android.googlesource.com/platform/prebuilts/android-emulator.git',
253 '4b260028dc27bc92c39bee9129cb2ba839970956',
254 'all'
Primiano Tucci0825bc82017-09-28 18:50:23 +0100255 ),
Primiano Tuccid7750452017-09-29 14:38:51 +0100256]
Primiano Tucciae2879e2017-09-27 11:02:09 +0900257
Primiano Tucci1c752c12018-10-23 09:27:19 +0100258# This variable is updated by tools/roll-catapult-trace-viewer.
259CATAPULT_SHA1 = 'ff5d8fd7244680b4d4456c25d5fdc04c76f9ef66'
260
Hector Dearman6177b752019-01-24 10:17:32 +0000261TYPEFACES_SHA1 = '756b0a015b8f99f5718f7fdf967d052c1ec55ab3'
262
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100263UI_DEPS = [
264 ('buildtools/nodejs.tgz',
Primiano Tuccidca96092018-05-31 11:47:16 +0100265 'https://storage.googleapis.com/perfetto/node-v10.3.0-darwin-x64.tar.gz',
266 '6d9a122785f38c256add3b25f74adf125497861a',
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100267 'darwin'
268 ),
269 ('buildtools/nodejs.tgz',
Primiano Tuccidca96092018-05-31 11:47:16 +0100270 'https://storage.googleapis.com/perfetto/node-v10.3.0-linux-x64.tar.xz',
271 '118f6ea19f75089b3f12ac2ddfce357bff872b5e',
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100272 'linux2'
273 ),
274 ('buildtools/emsdk/emscripten.tgz',
275 'https://storage.googleapis.com/perfetto/emscripten-1.37.40.tar.gz',
276 '588c28221321ebbdfc8e3a6f47ea6106f589669b',
277 'all'
278 ),
279 ('buildtools/emsdk/llvm.tgz',
280 'https://storage.googleapis.com/perfetto/emscripten-llvm-e1.37.40-darwin.tar.gz',
281 '7a894ef0a52821c62f6abaac552dc4ce5d424607',
282 'darwin'
283 ),
284 ('buildtools/emsdk/llvm.tgz',
Primiano Tuccife199902018-06-04 12:33:04 +0200285 'https://storage.googleapis.com/perfetto/emscripten-llvm-e1.37.40-static-linux.tar.gz',
286 '478501b9b7a14884e546c84efe209a90052cbb07',
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100287 'linux2'
288 ),
Hector Dearmand9628d32018-10-17 14:38:26 +0100289 ('buildtools/d8.tgz',
290 'https://storage.googleapis.com/perfetto/d8-linux2-5.7.492.65.tar.gz',
291 '95e82ad7faf0a6f74d950c2aa65e3858b7bdb6c6',
292 'linux2'
293 ),
294 ('buildtools/d8.tgz',
295 'https://storage.googleapis.com/perfetto/d8-darwin-6.6.346.32.tar.gz',
296 '1abd630619bb1977ab62095570a113d782a1545d',
297 'darwin'
298 ),
Primiano Tucci1c752c12018-10-23 09:27:19 +0100299 ('buildtools/catapult_trace_viewer.tgz',
300 'https://storage.googleapis.com/perfetto/catapult_trace_viewer-%s.tar.gz' % CATAPULT_SHA1,
301 CATAPULT_SHA1,
302 'all'
303 ),
Hector Dearman6177b752019-01-24 10:17:32 +0000304 ('buildtools/typefaces.tgz',
305 'https://storage.googleapis.com/perfetto/typefaces-%s.tar.gz' % TYPEFACES_SHA1,
306 TYPEFACES_SHA1,
307 'all'
308 )
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100309]
310
Primiano Tucciae2879e2017-09-27 11:02:09 +0900311ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
312
313
314def ReadFile(path):
315 if not os.path.exists(path):
316 return None
317 with open(path) as f:
318 return f.read().strip()
319
320
Primiano Tucci0825bc82017-09-28 18:50:23 +0100321def MkdirRecursive(path):
322 # Works with both relative and absolute paths
323 cwd = '/' if path.startswith('/') else ROOT_DIR
324 for part in path.split('/'):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900325 cwd = os.path.join(cwd, part)
326 if not os.path.exists(cwd):
327 os.makedirs(cwd)
328 else:
329 assert(os.path.isdir(cwd))
330
331
332def HashLocalFile(path):
333 if not os.path.exists(path):
334 return None
335 with open(path, 'rb') as f:
336 return hashlib.sha1(f.read()).hexdigest()
337
338
339def ExtractZipfilePreservePermissions(zf, info, path):
340 zf.extract(info.filename, path=path)
341 target_path = os.path.join(path, info.filename)
342 min_acls = 0o755 if info.filename.endswith('/') else 0o644
343 os.chmod(target_path, (info.external_attr >> 16L) | min_acls)
344
345
Primiano Tucci0825bc82017-09-28 18:50:23 +0100346def IsGitRepoCheckoutOutAtRevision(path, revision):
347 return ReadFile(os.path.join(path, '.git', 'HEAD')) == revision
348
349
350def CheckoutGitRepo(path, git_url, revision):
351 if IsGitRepoCheckoutOutAtRevision(path, revision):
Eric Seckler676421f2019-02-12 14:43:31 +0000352 return False
Primiano Tucci0825bc82017-09-28 18:50:23 +0100353 if os.path.exists(path):
354 shutil.rmtree(path)
355 MkdirRecursive(path)
356 logging.info('Fetching %s @ %s into %s', git_url, revision, path)
Lalit Maganti367fcd52018-02-05 16:06:13 +0000357 subprocess.check_call(['git', 'init', path], cwd=path)
358 subprocess.check_call(
Ryan Savitskie957ce82018-11-06 14:53:33 +0000359 ['git', 'fetch', '--quiet', '--depth', '1', git_url, revision], cwd=path)
Primiano Tucci0825bc82017-09-28 18:50:23 +0100360 subprocess.check_call(['git', 'checkout', revision, '--quiet'], cwd=path)
361 assert(IsGitRepoCheckoutOutAtRevision(path, revision))
Eric Seckler676421f2019-02-12 14:43:31 +0000362 return True
Primiano Tucci0825bc82017-09-28 18:50:23 +0100363
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100364
Deepanjan Royacf8c072018-07-13 11:37:04 -0400365def InstallNodeModules():
366 ui_dir = os.path.join(ROOT_DIR, 'ui')
367 logging.info("Running npm install in {0}".format(ui_dir))
368 subprocess.check_call(
369 [os.path.join(ui_dir, 'npm'), 'install', '--no-save'],
370 cwd=os.path.join(ROOT_DIR, 'ui'))
Primiano Tucci0825bc82017-09-28 18:50:23 +0100371
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100372
373def CheckHashes():
374 for deps in [BUILD_DEPS_HOST,
375 BUILD_DEPS_ANDROID,
376 TEST_DEPS_ANDROID,
377 UI_DEPS]:
378 for rel_path, url, expected_sha1, platform in deps:
379 if url.endswith('.git'):
380 continue
381 logging.info('Downloading %s from %s', rel_path, url)
382 with tempfile.NamedTemporaryFile(delete=False) as f:
383 f.close()
384 urllib.urlretrieve(url, f.name)
385 actual_sha1 = HashLocalFile(f.name)
386 os.unlink(f.name)
387 if (actual_sha1 != expected_sha1):
388 logging.fatal('SHA1 mismatch for {} expected {} was {}'.format(
389 url, expected_sha1, actual_sha1))
390
391
Primiano Tucciae2879e2017-09-27 11:02:09 +0900392def Main():
393 parser = argparse.ArgumentParser()
Primiano Tuccid7750452017-09-29 14:38:51 +0100394 parser.add_argument('--no-android', action='store_true')
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100395 parser.add_argument('--ui', action='store_true')
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100396 parser.add_argument('--check-hashes', help='Check hashes for all URLs',
397 action='store_true')
Primiano Tucciae2879e2017-09-27 11:02:09 +0900398 args = parser.parse_args()
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100399 if args.check_hashes:
400 CheckHashes()
401 return 0
Primiano Tuccid7750452017-09-29 14:38:51 +0100402 deps = BUILD_DEPS_HOST
403 if not args.no_android:
404 deps += BUILD_DEPS_ANDROID + TEST_DEPS_ANDROID
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100405 if args.ui:
406 deps += UI_DEPS
Eric Seckler6e828f32019-01-02 11:10:56 +0000407 deps_updated = False
Primiano Tuccid7750452017-09-29 14:38:51 +0100408 for rel_path, url, expected_sha1, platform in deps:
Lalit Maganti367fcd52018-02-05 16:06:13 +0000409 if (platform != 'all' and platform != sys.platform):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900410 continue
Primiano Tucciae2879e2017-09-27 11:02:09 +0900411 local_path = os.path.join(ROOT_DIR, rel_path)
Primiano Tucci0825bc82017-09-28 18:50:23 +0100412 if url.endswith('.git'):
Eric Seckler676421f2019-02-12 14:43:31 +0000413 deps_updated |= CheckoutGitRepo(local_path, url, expected_sha1)
Primiano Tucci0825bc82017-09-28 18:50:23 +0100414 continue
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000415 is_zip = local_path.endswith('.zip') or local_path.endswith('.tgz')
Primiano Tucciae2879e2017-09-27 11:02:09 +0900416 zip_target_dir = local_path[:-4] if is_zip else None
417 zip_dir_stamp = os.path.join(zip_target_dir, '.stamp') if is_zip else None
418
419 if ((not is_zip and HashLocalFile(local_path) == expected_sha1) or
420 (is_zip and ReadFile(zip_dir_stamp) == expected_sha1)):
421 continue
Eric Seckler6e828f32019-01-02 11:10:56 +0000422 deps_updated = True
Primiano Tucciae2879e2017-09-27 11:02:09 +0900423 MkdirRecursive(os.path.dirname(rel_path))
424 if HashLocalFile(local_path) != expected_sha1:
425 download_path = local_path + '.tmp'
426 logging.info('Downloading %s from %s', local_path, url)
427 urllib.urlretrieve(url, download_path)
428 os.chmod(download_path, 0o755)
Hector Dearmand9628d32018-10-17 14:38:26 +0100429 actual_sha1 = HashLocalFile(download_path)
430 if (actual_sha1 != expected_sha1):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900431 os.remove(download_path)
Hector Dearmand9628d32018-10-17 14:38:26 +0100432 logging.fatal('SHA1 mismatch for {} expected {} was {}'.format(
433 download_path, expected_sha1, actual_sha1))
Primiano Tucciae2879e2017-09-27 11:02:09 +0900434 return 1
435 os.rename(download_path, local_path)
436 assert(HashLocalFile(local_path) == expected_sha1)
437
438 if is_zip:
439 logging.info('Extracting %s into %s' % (local_path, zip_target_dir))
440 assert(os.path.commonprefix((ROOT_DIR, zip_target_dir)) == ROOT_DIR)
441 if os.path.exists(zip_target_dir):
442 logging.info('Deleting stale dir %s' % zip_target_dir)
443 shutil.rmtree(zip_target_dir)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900444
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000445 # Decompress the archive.
446 if local_path.endswith('.tgz'):
447 MkdirRecursive(zip_target_dir)
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100448 subprocess.check_call(['tar', '-xf', local_path], cwd=zip_target_dir)
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000449 elif local_path.endswith('.zip'):
450 with zipfile.ZipFile(local_path, 'r') as zf:
451 for info in zf.infolist():
452 ExtractZipfilePreservePermissions(zf, info, zip_target_dir)
453
454 # If the zip contains one root folder, rebase one level up moving all
455 # its sub files and folders inside |target_dir|.
456 subdir = os.listdir(zip_target_dir)
457 if len(subdir) == 1:
458 subdir = os.path.join(zip_target_dir, subdir[0])
459 if os.path.isdir(subdir):
460 for subf in os.listdir(subdir):
461 shutil.move(os.path.join(subdir,subf), zip_target_dir)
462 os.rmdir(subdir)
463
464 # Create stamp and remove the archive.
465 with open(zip_dir_stamp, 'w') as stamp_file:
466 stamp_file.write(expected_sha1)
467 os.remove(local_path)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900468
Deepanjan Royacf8c072018-07-13 11:37:04 -0400469 if args.ui:
470 # Needs to happen after nodejs is installed above.
471 InstallNodeModules()
Primiano Tucciae2879e2017-09-27 11:02:09 +0900472
Eric Seckler6e828f32019-01-02 11:10:56 +0000473 if deps_updated:
474 # Stale binary files may be compiled against old sysroot headers that aren't
475 # tracked by gn.
476 logging.warn('Remember to run "gn clean <output_directory>" ' +
477 'to avoid stale binary files.')
478
Primiano Tucciae2879e2017-09-27 11:02:09 +0900479if __name__ == '__main__':
480 logging.basicConfig(level=logging.INFO)
481 sys.exit(Main())