blob: 923210c385e849539ff75806743594838d894159 [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 = [
Primiano Tucciae2879e2017-09-27 11:02:09 +090044 # GN
45 ('buildtools/mac/gn',
Primiano Tucci335412d2019-05-30 16:29:16 +010046 'https://storage.googleapis.com/perfetto/gn-mac-b5b65ca39d93a7cde9fa713be31b114755252f28',
47 'b5b65ca39d93a7cde9fa713be31b114755252f28',
Primiano Tucciae2879e2017-09-27 11:02:09 +090048 'darwin'
49 ),
50 ('buildtools/linux64/gn',
Primiano Tucci335412d2019-05-30 16:29:16 +010051 'https://storage.googleapis.com/perfetto/gn-linux64-1370d9c5358868b7b66292821b6fe61950826870',
52 '1370d9c5358868b7b66292821b6fe61950826870',
Primiano Tucciae2879e2017-09-27 11:02:09 +090053 'linux2'
54 ),
55
Primiano Tucci104bd6d2017-10-12 00:10:24 +020056 # clang-format
57 ('buildtools/mac/clang-format',
Primiano Tucci335412d2019-05-30 16:29:16 +010058 'https://storage.googleapis.com/chromium-clang-format/025ca7c75f37ef4a40f3a67d81ddd11d7d0cdb9b',
59 '025ca7c75f37ef4a40f3a67d81ddd11d7d0cdb9b',
Primiano Tucci104bd6d2017-10-12 00:10:24 +020060 'darwin'
61 ),
62 ('buildtools/linux64/clang-format',
Primiano Tucci335412d2019-05-30 16:29:16 +010063 'https://storage.googleapis.com/chromium-clang-format/942fc8b1789144b8071d3fc03ff0fcbe1cf81ac8',
64 '942fc8b1789144b8071d3fc03ff0fcbe1cf81ac8',
Primiano Tucci104bd6d2017-10-12 00:10:24 +020065 'linux2'
66 ),
67 # Keep the SHA1 in sync with |clang_format_rev| in chromium //buildtools/DEPS.
68 ('buildtools/clang_format/script',
69 'https://chromium.googlesource.com/chromium/llvm-project/cfe/tools/clang-format.git',
Primiano Tucci335412d2019-05-30 16:29:16 +010070 '96636aa0e9f047f17447f2d45a094d0b59ed7917',
Primiano Tucci104bd6d2017-10-12 00:10:24 +020071 'all'
72 ),
73
Primiano Tucciae2879e2017-09-27 11:02:09 +090074 # Ninja
75 ('buildtools/mac/ninja',
Primiano Tucci335412d2019-05-30 16:29:16 +010076 'https://storage.googleapis.com/perfetto/ninja-mac-c15b0698da038b2bd2e8970c14c75fadc06b1add',
77 'c15b0698da038b2bd2e8970c14c75fadc06b1add',
Primiano Tucciae2879e2017-09-27 11:02:09 +090078 'darwin'
79 ),
80 ('buildtools/linux64/ninja',
Primiano Tucci335412d2019-05-30 16:29:16 +010081 'https://storage.googleapis.com/perfetto/ninja-linux64-c866952bda50c29a669222477309287119bbb7e8',
82 'c866952bda50c29a669222477309287119bbb7e8',
Primiano Tucciae2879e2017-09-27 11:02:09 +090083 'linux2'
84 ),
85
Primiano Tuccid7750452017-09-29 14:38:51 +010086 # Keep in sync with Android's //external/googletest/README.version.
87 ('buildtools/googletest.zip',
88 'https://github.com/google/googletest/archive/ff07a5de0e81580547f1685e101194ed1a4fcd56.zip',
89 'c7edec7d7e6db1fc37a20710de9c4d89e3a3893b',
90 'all'
91 ),
92
Primiano Tucci1191e182019-08-25 06:36:56 +020093 # Keep in sync with Android's //external/protobuf/README.version.
Primiano Tuccid7750452017-09-29 14:38:51 +010094 ('buildtools/protobuf.zip',
Primiano Tucci1191e182019-08-25 06:36:56 +020095 'https://github.com/google/protobuf/releases/download/v3.0.0-beta-3/protobuf-cpp-3.0.0-beta-3.zip',
96 '3caec60aa9d8eefc8c3c3201b6b8ca19935edb89',
Primiano Tuccid7750452017-09-29 14:38:51 +010097 'all'
98 ),
99
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000100 # libc++, libc++abi and libunwind for Linux where we need to rebuild the C++
101 # lib from sources. Keep the SHA1s in sync with Chrome's src/buildtools/DEPS.
Primiano Tuccid7750452017-09-29 14:38:51 +0100102 ('buildtools/libcxx',
103 'https://chromium.googlesource.com/chromium/llvm-project/libcxx.git',
Primiano Tucci335412d2019-05-30 16:29:16 +0100104 '5938e0582bac570a41edb3d6a2217c299adc1bc6',
Primiano Tuccid7750452017-09-29 14:38:51 +0100105 'all'
106 ),
107 ('buildtools/libcxxabi',
108 'https://chromium.googlesource.com/chromium/llvm-project/libcxxabi.git',
Primiano Tucci335412d2019-05-30 16:29:16 +0100109 '0d529660e32d77d9111912d73f2c74fc5fa2a858',
Primiano Tuccid7750452017-09-29 14:38:51 +0100110 'all'
111 ),
Primiano Tucci7278dea2017-10-31 11:50:32 +0000112 ('buildtools/libunwind',
113 'https://chromium.googlesource.com/external/llvm.org/libunwind.git',
Primiano Tucci335412d2019-05-30 16:29:16 +0100114 '69d9b84cca8354117b9fe9705a4430d789ee599b',
Primiano Tucci7278dea2017-10-31 11:50:32 +0000115 'all'
116 ),
Hector Dearman88a10112017-10-12 11:07:10 +0100117
Primiano Tucci335412d2019-05-30 16:29:16 +0100118 # Keep the revision in sync with Chrome's PACKAGE_VERSION in
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000119 # tools/clang/scripts/update.py.
120 ('buildtools/clang.tgz',
Primiano Tuccid4aa37b2019-08-21 13:38:06 +0200121 'https://commondatastorage.googleapis.com/chromium-browser-clang/Linux_x64/clang-365097-f7e52fbd-8.tgz',
122 'fe1b1e5bd7381ae655661cb9658487389561568d',
Primiano Tuccia65497e2018-09-26 19:53:58 +0100123 'linux2'
124 ),
125
126 # Keep in sync with chromium DEPS.
127 ('buildtools/libfuzzer',
128 'https://chromium.googlesource.com/chromium/llvm-project/compiler-rt/lib/fuzzer.git',
Florian Mayera85b8fa2019-07-11 11:00:48 +0100129 'b9f51dc8c98065df0c8da13c051046f5bab833db',
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000130 'linux2'
131 ),
132
Hector Dearman88a10112017-10-12 11:07:10 +0100133 # Benchmarking tool.
134 ('buildtools/benchmark.zip',
Lalit Magantic99d93c2018-03-22 15:09:30 +0000135 'https://github.com/google/benchmark/archive/v1.3.0.zip',
136 'f387e0df37d54bfd5be239e8d0d3ea2e2c3e34f4',
Hector Dearman88a10112017-10-12 11:07:10 +0100137 'all'
138 ),
Primiano Tucci38faa6f2018-04-01 20:12:08 +0200139
140 # Libbacktrace, for stacktraces in Linux/Android debug builds.
141 ('buildtools/libbacktrace.zip',
142 'https://github.com/ianlancetaylor/libbacktrace/archive/177940370e4a6b2509e92a0aaa9749184e64af43.zip',
143 'b723fe9d671d1ab54df1297f6afbf2893a41c3ea',
144 'all'
145 ),
Lalit Maganti6fe26e62018-05-23 12:14:38 +0100146
147 # Sqlite for the trace processing library.
Ioannis Ilkos178535e2018-11-05 17:32:45 +0000148 # This is the amalgamated source whose compiled output is meant to be faster.
149 # We still pull the full source for the extensions (not amalgamated).
Lalit Maganti6fe26e62018-05-23 12:14:38 +0100150 ('buildtools/sqlite.zip',
Lalit Maganti10d6deb2018-11-20 15:15:50 +0000151 'https://storage.googleapis.com/perfetto/sqlite-amalgamation-3250300.zip',
152 'b78c2cb0d2c9182686c582312479f96a82bf5380',
Lalit Maganti6fe26e62018-05-23 12:14:38 +0100153 'all'
154 ),
Ioannis Ilkos178535e2018-11-05 17:32:45 +0000155 ('buildtools/sqlite_src.zip',
Lalit Maganti10d6deb2018-11-20 15:15:50 +0000156 'https://storage.googleapis.com/perfetto/sqlite-src-3250300.zip',
157 'd1af2883bb800852946f9bf8ab6055e7698e18ee',
Ioannis Ilkos178535e2018-11-05 17:32:45 +0000158 'all'
159 ),
Primiano Tucci0d72a312018-08-07 14:42:45 +0100160
161 # JsonCpp for legacy json import. Used only by the trace processor in
162 # standalone builds.
163 ('buildtools/jsoncpp.zip',
Lalit Maganti36d229a2019-07-12 10:09:39 +0000164 'https://github.com/open-source-parsers/jsoncpp/archive/1.0.0.zip',
165 '3219e26f2e249bb46b7d688478208c7ec138fea4',
Primiano Tucci0d72a312018-08-07 14:42:45 +0100166 'all'
167 ),
Florian Mayer475bd7e2018-08-07 20:04:03 +0100168
Florian Mayerf8335662018-08-08 11:30:32 +0100169 # These dependencies are for libunwindstack, which is used by src/profiling.
170 ('buildtools/android-core',
Florian Mayer475bd7e2018-08-07 20:04:03 +0100171 'https://android.googlesource.com/platform/system/core.git',
Florian Mayer96e1f082019-07-10 10:42:01 +0100172 '3f407fcc37b401c91784700c0a691ba8b1f7ef15',
Florian Mayer475bd7e2018-08-07 20:04:03 +0100173 'all'
174 ),
175
176 ('buildtools/lzma',
177 'https://android.googlesource.com/platform/external/lzma.git',
178 '7851dce6f4ca17f5caa1c93a4e0a45686b1d56c3',
179 'all'
180 ),
Hector Dearman7f71d0e2018-08-09 11:26:10 +0100181
Hector Dearmane0b993f2019-05-24 18:48:16 +0100182 ('buildtools/zlib',
183 'https://android.googlesource.com/platform/external/zlib.git',
184 'dfa0646a03b4e1707469e04dc931b09774968fe6',
185 'all'
186 ),
187
Florian Mayerb64d6b12018-08-30 10:46:30 -0700188 ('buildtools/bionic',
189 'https://android.googlesource.com/platform/bionic.git',
Primiano Tucci1721b1a2019-02-17 14:18:07 +0000190 'a60488109cda997dfd83832731c8527feaa2825e',
Florian Mayerb64d6b12018-08-30 10:46:30 -0700191 'all'
192 ),
193
Hector Dearman7f71d0e2018-08-09 11:26:10 +0100194 # Example traces for regression tests.
195 ('buildtools/test_data.zip',
Ryan Savitski9f5e33a2019-09-24 16:39:51 +0100196 'https://storage.googleapis.com/perfetto/test-data-20190920-011709.zip',
197 '75fc970bf2778d506c9d77f94a6381b04f10b692',
Hector Dearman7f71d0e2018-08-09 11:26:10 +0100198 'all',
199 ),
Hector Dearmane44ad452018-09-21 11:51:57 +0100200
Primiano Tucci21d41a72018-09-24 23:12:04 +0100201 # Linenoise, used only by trace_processor in standalone builds.
Hector Dearmane44ad452018-09-21 11:51:57 +0100202 ('buildtools/linenoise',
Primiano Tucci21d41a72018-09-24 23:12:04 +0100203 'https://fuchsia.googlesource.com/third_party/linenoise.git',
204 'c894b9e59f02203dbe4e2be657572cf88c4230c3',
Hector Dearmane44ad452018-09-21 11:51:57 +0100205 'all'
206 ),
Primiano Tuccid7750452017-09-29 14:38:51 +0100207]
208
209# Dependencies required to build Android code.
210# URLs and SHA1s taken from:
211# - https://dl.google.com/android/repository/repository-11.xml
212# - https://dl.google.com/android/repository/sys-img/android/sys-img.xml
213BUILD_DEPS_ANDROID = [
Primiano Tucciae2879e2017-09-27 11:02:09 +0900214 # Android NDK
215 ('buildtools/ndk.zip',
Florian Mayeraa5316b2018-08-20 17:45:12 -0700216 'https://dl.google.com/android/repository/android-ndk-r17b-darwin-x86_64.zip',
217 'f990aafaffec0b583d2c5420bfa622e52ac14248',
Primiano Tucciae2879e2017-09-27 11:02:09 +0900218 'darwin'
219 ),
220 ('buildtools/ndk.zip',
Florian Mayeraa5316b2018-08-20 17:45:12 -0700221 'https://dl.google.com/android/repository/android-ndk-r17b-linux-x86_64.zip',
222 'dd5762ee7ef4995ad04fe0c45a608c344d99ca9f',
Primiano Tucciae2879e2017-09-27 11:02:09 +0900223 'linux2'
224 ),
Primiano Tuccid7750452017-09-29 14:38:51 +0100225]
Primiano Tucciae2879e2017-09-27 11:02:09 +0900226
Primiano Tuccid7750452017-09-29 14:38:51 +0100227# Dependencies required to run Android tests.
228TEST_DEPS_ANDROID = [
Lalit Maganti367fcd52018-02-05 16:06:13 +0000229 # Android emulator images.
230 ('buildtools/aosp-arm.zip',
231 'https://storage.googleapis.com/perfetto/aosp-02022018-arm.zip',
232 'a480d5e7d3ca888b0a58fe15ce76b1791537429a',
Primiano Tuccie7ca7c62018-04-07 08:28:03 +0200233 'all'
Primiano Tucciae2879e2017-09-27 11:02:09 +0900234 ),
235
Primiano Tuccid7750452017-09-29 14:38:51 +0100236 # platform-tools.zip contains adb binaries.
237 ('buildtools/android_sdk/platform-tools.zip',
238 'https://dl.google.com/android/repository/platform-tools_r26.0.0-darwin.zip',
239 'e75b6137dc444f777eb02f44a6d9819b3aabff82',
240 'darwin'
241 ),
242 ('buildtools/android_sdk/platform-tools.zip',
243 'https://dl.google.com/android/repository/platform-tools_r26.0.0-linux.zip',
244 '00de8a6631405b617c10f68cd11ff2e1cd528e23',
245 'linux2'
Primiano Tucciae2879e2017-09-27 11:02:09 +0900246 ),
Primiano Tucci0825bc82017-09-28 18:50:23 +0100247
Lalit Maganti367fcd52018-02-05 16:06:13 +0000248 # Android emulator binaries.
249 ('buildtools/emulator',
250 'https://android.googlesource.com/platform/prebuilts/android-emulator.git',
251 '4b260028dc27bc92c39bee9129cb2ba839970956',
252 'all'
Primiano Tucci0825bc82017-09-28 18:50:23 +0100253 ),
Primiano Tuccid7750452017-09-29 14:38:51 +0100254]
Primiano Tucciae2879e2017-09-27 11:02:09 +0900255
Primiano Tucci1c752c12018-10-23 09:27:19 +0100256# This variable is updated by tools/roll-catapult-trace-viewer.
257CATAPULT_SHA1 = 'ff5d8fd7244680b4d4456c25d5fdc04c76f9ef66'
258
Hector Dearman6177b752019-01-24 10:17:32 +0000259TYPEFACES_SHA1 = '756b0a015b8f99f5718f7fdf967d052c1ec55ab3'
260
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100261UI_DEPS = [
262 ('buildtools/nodejs.tgz',
Primiano Tuccidca96092018-05-31 11:47:16 +0100263 'https://storage.googleapis.com/perfetto/node-v10.3.0-darwin-x64.tar.gz',
264 '6d9a122785f38c256add3b25f74adf125497861a',
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100265 'darwin'
266 ),
267 ('buildtools/nodejs.tgz',
Primiano Tuccidca96092018-05-31 11:47:16 +0100268 'https://storage.googleapis.com/perfetto/node-v10.3.0-linux-x64.tar.xz',
269 '118f6ea19f75089b3f12ac2ddfce357bff872b5e',
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100270 'linux2'
271 ),
272 ('buildtools/emsdk/emscripten.tgz',
273 'https://storage.googleapis.com/perfetto/emscripten-1.37.40.tar.gz',
274 '588c28221321ebbdfc8e3a6f47ea6106f589669b',
275 'all'
276 ),
277 ('buildtools/emsdk/llvm.tgz',
278 'https://storage.googleapis.com/perfetto/emscripten-llvm-e1.37.40-darwin.tar.gz',
279 '7a894ef0a52821c62f6abaac552dc4ce5d424607',
280 'darwin'
281 ),
282 ('buildtools/emsdk/llvm.tgz',
Primiano Tuccife199902018-06-04 12:33:04 +0200283 'https://storage.googleapis.com/perfetto/emscripten-llvm-e1.37.40-static-linux.tar.gz',
284 '478501b9b7a14884e546c84efe209a90052cbb07',
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100285 'linux2'
286 ),
Hector Dearmand9628d32018-10-17 14:38:26 +0100287 ('buildtools/d8.tgz',
288 'https://storage.googleapis.com/perfetto/d8-linux2-5.7.492.65.tar.gz',
289 '95e82ad7faf0a6f74d950c2aa65e3858b7bdb6c6',
290 'linux2'
291 ),
292 ('buildtools/d8.tgz',
293 'https://storage.googleapis.com/perfetto/d8-darwin-6.6.346.32.tar.gz',
294 '1abd630619bb1977ab62095570a113d782a1545d',
295 'darwin'
296 ),
Primiano Tucci1c752c12018-10-23 09:27:19 +0100297 ('buildtools/catapult_trace_viewer.tgz',
298 'https://storage.googleapis.com/perfetto/catapult_trace_viewer-%s.tar.gz' % CATAPULT_SHA1,
299 CATAPULT_SHA1,
300 'all'
301 ),
Hector Dearman6177b752019-01-24 10:17:32 +0000302 ('buildtools/typefaces.tgz',
303 'https://storage.googleapis.com/perfetto/typefaces-%s.tar.gz' % TYPEFACES_SHA1,
304 TYPEFACES_SHA1,
305 'all'
306 )
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100307]
308
Primiano Tucciae2879e2017-09-27 11:02:09 +0900309ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
310
311
312def ReadFile(path):
313 if not os.path.exists(path):
314 return None
315 with open(path) as f:
316 return f.read().strip()
317
318
Primiano Tucci0825bc82017-09-28 18:50:23 +0100319def MkdirRecursive(path):
320 # Works with both relative and absolute paths
321 cwd = '/' if path.startswith('/') else ROOT_DIR
322 for part in path.split('/'):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900323 cwd = os.path.join(cwd, part)
324 if not os.path.exists(cwd):
325 os.makedirs(cwd)
326 else:
327 assert(os.path.isdir(cwd))
328
329
330def HashLocalFile(path):
331 if not os.path.exists(path):
332 return None
333 with open(path, 'rb') as f:
334 return hashlib.sha1(f.read()).hexdigest()
335
336
337def ExtractZipfilePreservePermissions(zf, info, path):
338 zf.extract(info.filename, path=path)
339 target_path = os.path.join(path, info.filename)
340 min_acls = 0o755 if info.filename.endswith('/') else 0o644
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +0100341 os.chmod(target_path, (info.external_attr >> 16) | min_acls)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900342
343
Primiano Tucci0825bc82017-09-28 18:50:23 +0100344def IsGitRepoCheckoutOutAtRevision(path, revision):
345 return ReadFile(os.path.join(path, '.git', 'HEAD')) == revision
346
347
348def CheckoutGitRepo(path, git_url, revision):
349 if IsGitRepoCheckoutOutAtRevision(path, revision):
Eric Seckler676421f2019-02-12 14:43:31 +0000350 return False
Primiano Tucci0825bc82017-09-28 18:50:23 +0100351 if os.path.exists(path):
352 shutil.rmtree(path)
353 MkdirRecursive(path)
354 logging.info('Fetching %s @ %s into %s', git_url, revision, path)
Lalit Maganti367fcd52018-02-05 16:06:13 +0000355 subprocess.check_call(['git', 'init', path], cwd=path)
356 subprocess.check_call(
Ryan Savitskie957ce82018-11-06 14:53:33 +0000357 ['git', 'fetch', '--quiet', '--depth', '1', git_url, revision], cwd=path)
Primiano Tucci0825bc82017-09-28 18:50:23 +0100358 subprocess.check_call(['git', 'checkout', revision, '--quiet'], cwd=path)
359 assert(IsGitRepoCheckoutOutAtRevision(path, revision))
Eric Seckler676421f2019-02-12 14:43:31 +0000360 return True
Primiano Tucci0825bc82017-09-28 18:50:23 +0100361
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100362
Deepanjan Royacf8c072018-07-13 11:37:04 -0400363def InstallNodeModules():
364 ui_dir = os.path.join(ROOT_DIR, 'ui')
365 logging.info("Running npm install in {0}".format(ui_dir))
366 subprocess.check_call(
367 [os.path.join(ui_dir, 'npm'), 'install', '--no-save'],
368 cwd=os.path.join(ROOT_DIR, 'ui'))
Primiano Tucci0825bc82017-09-28 18:50:23 +0100369
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100370
371def CheckHashes():
372 for deps in [BUILD_DEPS_HOST,
373 BUILD_DEPS_ANDROID,
374 TEST_DEPS_ANDROID,
375 UI_DEPS]:
376 for rel_path, url, expected_sha1, platform in deps:
377 if url.endswith('.git'):
378 continue
379 logging.info('Downloading %s from %s', rel_path, url)
380 with tempfile.NamedTemporaryFile(delete=False) as f:
381 f.close()
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +0100382 urlretrieve(url, f.name)
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100383 actual_sha1 = HashLocalFile(f.name)
384 os.unlink(f.name)
385 if (actual_sha1 != expected_sha1):
386 logging.fatal('SHA1 mismatch for {} expected {} was {}'.format(
387 url, expected_sha1, actual_sha1))
388
389
Primiano Tucciae2879e2017-09-27 11:02:09 +0900390def Main():
391 parser = argparse.ArgumentParser()
Primiano Tuccid7750452017-09-29 14:38:51 +0100392 parser.add_argument('--no-android', action='store_true')
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100393 parser.add_argument('--ui', action='store_true')
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100394 parser.add_argument('--check-hashes', help='Check hashes for all URLs',
395 action='store_true')
Primiano Tucciae2879e2017-09-27 11:02:09 +0900396 args = parser.parse_args()
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100397 if args.check_hashes:
398 CheckHashes()
399 return 0
Primiano Tuccid7750452017-09-29 14:38:51 +0100400 deps = BUILD_DEPS_HOST
401 if not args.no_android:
402 deps += BUILD_DEPS_ANDROID + TEST_DEPS_ANDROID
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100403 if args.ui:
404 deps += UI_DEPS
Eric Seckler6e828f32019-01-02 11:10:56 +0000405 deps_updated = False
Primiano Tuccid7750452017-09-29 14:38:51 +0100406 for rel_path, url, expected_sha1, platform in deps:
Lalit Maganti367fcd52018-02-05 16:06:13 +0000407 if (platform != 'all' and platform != sys.platform):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900408 continue
Primiano Tucciae2879e2017-09-27 11:02:09 +0900409 local_path = os.path.join(ROOT_DIR, rel_path)
Primiano Tucci0825bc82017-09-28 18:50:23 +0100410 if url.endswith('.git'):
Eric Seckler676421f2019-02-12 14:43:31 +0000411 deps_updated |= CheckoutGitRepo(local_path, url, expected_sha1)
Primiano Tucci0825bc82017-09-28 18:50:23 +0100412 continue
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000413 is_zip = local_path.endswith('.zip') or local_path.endswith('.tgz')
Primiano Tucciae2879e2017-09-27 11:02:09 +0900414 zip_target_dir = local_path[:-4] if is_zip else None
415 zip_dir_stamp = os.path.join(zip_target_dir, '.stamp') if is_zip else None
416
417 if ((not is_zip and HashLocalFile(local_path) == expected_sha1) or
418 (is_zip and ReadFile(zip_dir_stamp) == expected_sha1)):
419 continue
Eric Seckler6e828f32019-01-02 11:10:56 +0000420 deps_updated = True
Primiano Tucciae2879e2017-09-27 11:02:09 +0900421 MkdirRecursive(os.path.dirname(rel_path))
422 if HashLocalFile(local_path) != expected_sha1:
423 download_path = local_path + '.tmp'
424 logging.info('Downloading %s from %s', local_path, url)
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +0100425 urlretrieve(url, download_path)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900426 os.chmod(download_path, 0o755)
Hector Dearmand9628d32018-10-17 14:38:26 +0100427 actual_sha1 = HashLocalFile(download_path)
428 if (actual_sha1 != expected_sha1):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900429 os.remove(download_path)
Hector Dearmand9628d32018-10-17 14:38:26 +0100430 logging.fatal('SHA1 mismatch for {} expected {} was {}'.format(
431 download_path, expected_sha1, actual_sha1))
Primiano Tucciae2879e2017-09-27 11:02:09 +0900432 return 1
433 os.rename(download_path, local_path)
434 assert(HashLocalFile(local_path) == expected_sha1)
435
436 if is_zip:
437 logging.info('Extracting %s into %s' % (local_path, zip_target_dir))
438 assert(os.path.commonprefix((ROOT_DIR, zip_target_dir)) == ROOT_DIR)
439 if os.path.exists(zip_target_dir):
440 logging.info('Deleting stale dir %s' % zip_target_dir)
441 shutil.rmtree(zip_target_dir)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900442
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000443 # Decompress the archive.
444 if local_path.endswith('.tgz'):
445 MkdirRecursive(zip_target_dir)
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100446 subprocess.check_call(['tar', '-xf', local_path], cwd=zip_target_dir)
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000447 elif local_path.endswith('.zip'):
448 with zipfile.ZipFile(local_path, 'r') as zf:
449 for info in zf.infolist():
450 ExtractZipfilePreservePermissions(zf, info, zip_target_dir)
451
452 # If the zip contains one root folder, rebase one level up moving all
453 # its sub files and folders inside |target_dir|.
454 subdir = os.listdir(zip_target_dir)
455 if len(subdir) == 1:
456 subdir = os.path.join(zip_target_dir, subdir[0])
457 if os.path.isdir(subdir):
458 for subf in os.listdir(subdir):
459 shutil.move(os.path.join(subdir,subf), zip_target_dir)
460 os.rmdir(subdir)
461
462 # Create stamp and remove the archive.
463 with open(zip_dir_stamp, 'w') as stamp_file:
464 stamp_file.write(expected_sha1)
465 os.remove(local_path)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900466
Deepanjan Royacf8c072018-07-13 11:37:04 -0400467 if args.ui:
468 # Needs to happen after nodejs is installed above.
469 InstallNodeModules()
Primiano Tucciae2879e2017-09-27 11:02:09 +0900470
Eric Seckler6e828f32019-01-02 11:10:56 +0000471 if deps_updated:
472 # Stale binary files may be compiled against old sysroot headers that aren't
473 # tracked by gn.
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +0100474 logging.warning('Remember to run "gn clean <output_directory>" ' +
475 'to avoid stale binary files.')
Eric Seckler6e828f32019-01-02 11:10:56 +0000476
Primiano Tucciae2879e2017-09-27 11:02:09 +0900477if __name__ == '__main__':
478 logging.basicConfig(level=logging.INFO)
479 sys.exit(Main())