blob: 73a537a93761288d779c814b5638a0fdffd7ad92 [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
Matthew Clarkson40e48192019-10-28 12:35:01 +000027from platform import system
Primiano Tuccid7750452017-09-29 14:38:51 +010028
Primiano Tuccib60d4b02017-11-10 11:03:00 +000029# The format for the deps below is the following:
30# (target_folder, source_url, sha1, target_platform)
31# |source_url| can be either a git repo or a http url.
32# If a git repo, |sha1| is the committish that will be checked out.
33# If a http url, |sha1| is the shasum of the original file.
34# If the url is a .zip or .tgz file it will be automatically deflated under
35# |target_folder|, taking care of stripping the root folder if it's a single
36# root (to avoid ending up with buildtools/protobuf/protobuf-1.2.3/... and have
37# instead just buildtools/protobuf).
Matthew Clarkson40e48192019-10-28 12:35:01 +000038# |target_platform| is either 'darwin', 'linux' or 'all' and applies the dep
39# only on the given platform
Primiano Tucci43f111a2020-07-27 20:37:52 +020040Dependency = namedtuple(
41 'Dependency', ['target_folder', 'source_url', 'sha1', 'target_platform'])
Primiano Tuccib60d4b02017-11-10 11:03:00 +000042
Primiano Tuccid7750452017-09-29 14:38:51 +010043# Dependencies required to build code on the host or when targeting desktop OS.
44BUILD_DEPS_HOST = [
Matthew Clarkson63028d62019-10-10 15:48:23 +010045 # GN
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -070046 Dependency('buildtools/mac/gn',
Primiano Tucci43f111a2020-07-27 20:37:52 +020047 'https://storage.googleapis.com/perfetto/gn-mac-1695-83dad00a',
48 '4c0d45772aea4146699772165e8112fa76ceb295', 'darwin'),
49 Dependency(
50 'buildtools/linux64/gn',
51 'https://storage.googleapis.com/perfetto/gn-linux64-1695-83dad00a',
52 'fcabfc379bccaa65b4e2fc791594ba124dafc7d0', 'linux'),
Primiano Tucciae2879e2017-09-27 11:02:09 +090053
Matthew Clarkson63028d62019-10-10 15:48:23 +010054 # clang-format
Primiano Tucci43f111a2020-07-27 20:37:52 +020055 Dependency(
56 'buildtools/mac/clang-format',
57 'https://storage.googleapis.com/chromium-clang-format/025ca7c75f37ef4a40f3a67d81ddd11d7d0cdb9b',
58 '025ca7c75f37ef4a40f3a67d81ddd11d7d0cdb9b', 'darwin'),
59 Dependency(
60 'buildtools/linux64/clang-format',
61 'https://storage.googleapis.com/chromium-clang-format/942fc8b1789144b8071d3fc03ff0fcbe1cf81ac8',
62 '942fc8b1789144b8071d3fc03ff0fcbe1cf81ac8', 'linux'),
Matthew Clarkson63028d62019-10-10 15:48:23 +010063 # Keep the SHA1 in sync with |clang_format_rev| in chromium //buildtools/DEPS.
Primiano Tucci43f111a2020-07-27 20:37:52 +020064 Dependency(
65 'buildtools/clang_format/script',
66 'https://chromium.googlesource.com/chromium/llvm-project/cfe/tools/clang-format.git',
67 '96636aa0e9f047f17447f2d45a094d0b59ed7917', 'all'),
Primiano Tucci104bd6d2017-10-12 00:10:24 +020068
Matthew Clarkson63028d62019-10-10 15:48:23 +010069 # Ninja
Primiano Tucci43f111a2020-07-27 20:37:52 +020070 Dependency(
71 'buildtools/mac/ninja',
72 'https://storage.googleapis.com/perfetto/ninja-mac-c15b0698da038b2bd2e8970c14c75fadc06b1add',
73 'c15b0698da038b2bd2e8970c14c75fadc06b1add', 'darwin'),
74 Dependency(
75 'buildtools/linux64/ninja',
76 'https://storage.googleapis.com/perfetto/ninja-linux64-c866952bda50c29a669222477309287119bbb7e8',
77 'c866952bda50c29a669222477309287119bbb7e8', 'linux'),
Primiano Tucciae2879e2017-09-27 11:02:09 +090078
Matthew Clarkson63028d62019-10-10 15:48:23 +010079 # Keep in sync with Android's //external/googletest/README.version.
Primiano Tucci43f111a2020-07-27 20:37:52 +020080 Dependency(
81 'buildtools/googletest.zip',
82 'https://github.com/google/googletest/archive/3f05f651ae3621db58468153e32016bc1397800b.zip',
83 '86384688f7c533ad325a505efc917e0cdf39a0ce', 'all'),
Primiano Tuccid7750452017-09-29 14:38:51 +010084
Primiano Tuccif550b252019-12-03 11:06:02 +000085 # Keep in sync with Chromium's //third_party/protobuf.
Primiano Tucci43f111a2020-07-27 20:37:52 +020086 Dependency(
87 'buildtools/protobuf.zip',
88 'https://github.com/protocolbuffers/protobuf/releases/download/v3.9.0/protobuf-cpp-3.9.0.zip',
89 'c975536dffe9d9a3d362928aef4fb9f199012b98', 'all'),
Primiano Tuccid7750452017-09-29 14:38:51 +010090
Matthew Clarkson63028d62019-10-10 15:48:23 +010091 # libc++, libc++abi and libunwind for Linux where we need to rebuild the C++
92 # lib from sources. Keep the SHA1s in sync with Chrome's src/buildtools/DEPS.
Primiano Tucci43f111a2020-07-27 20:37:52 +020093 Dependency(
94 'buildtools/libcxx',
95 'https://chromium.googlesource.com/chromium/llvm-project/libcxx.git',
96 '78d6a7767ed57b50122a161b91f59f19c9bd0d19', 'all'),
97 Dependency(
98 'buildtools/libcxxabi',
99 'https://chromium.googlesource.com/chromium/llvm-project/libcxxabi.git',
100 '0d529660e32d77d9111912d73f2c74fc5fa2a858', 'all'),
101 Dependency(
102 'buildtools/libunwind',
103 'https://chromium.googlesource.com/external/llvm.org/libunwind.git',
104 '69d9b84cca8354117b9fe9705a4430d789ee599b', 'all'),
Hector Dearman88a10112017-10-12 11:07:10 +0100105
Matthew Clarkson63028d62019-10-10 15:48:23 +0100106 # Keep the revision in sync with Chrome's PACKAGE_VERSION in
107 # tools/clang/scripts/update.py.
Primiano Tucci43f111a2020-07-27 20:37:52 +0200108 Dependency(
109 'buildtools/clang.tgz',
110 'https://commondatastorage.googleapis.com/chromium-browser-clang/Linux_x64/clang-n332890-c2443155-2.tgz',
111 'd6501ffdb5dbb0ffe8a4b873cc092a9929e661ec', 'linux'),
Primiano Tuccia65497e2018-09-26 19:53:58 +0100112
Matthew Clarkson63028d62019-10-10 15:48:23 +0100113 # Keep in sync with chromium DEPS.
Primiano Tucci43f111a2020-07-27 20:37:52 +0200114 Dependency(
115 'buildtools/libfuzzer',
116 'https://chromium.googlesource.com/chromium/llvm-project/compiler-rt/lib/fuzzer.git',
117 'debe7d2d1982e540fbd6bd78604bf001753f9e74', 'linux'),
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000118
Matthew Clarkson63028d62019-10-10 15:48:23 +0100119 # Benchmarking tool.
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700120 Dependency('buildtools/benchmark.zip',
Primiano Tucci43f111a2020-07-27 20:37:52 +0200121 'https://github.com/google/benchmark/archive/v1.5.0.zip',
122 'a9c9bd8a28db82f5ba02998197cfcc4db5a67507', 'all'),
Primiano Tucci38faa6f2018-04-01 20:12:08 +0200123
Matthew Clarkson63028d62019-10-10 15:48:23 +0100124 # Libbacktrace, for stacktraces in Linux/Android debug builds.
Primiano Tucci43f111a2020-07-27 20:37:52 +0200125 Dependency(
126 'buildtools/libbacktrace.zip',
127 'https://github.com/ianlancetaylor/libbacktrace/archive/177940370e4a6b2509e92a0aaa9749184e64af43.zip',
128 'b723fe9d671d1ab54df1297f6afbf2893a41c3ea', 'all'),
Lalit Maganti6fe26e62018-05-23 12:14:38 +0100129
Matthew Clarkson63028d62019-10-10 15:48:23 +0100130 # Sqlite for the trace processing library.
131 # This is the amalgamated source whose compiled output is meant to be faster.
132 # We still pull the full source for the extensions (not amalgamated).
Primiano Tucci43f111a2020-07-27 20:37:52 +0200133 Dependency(
134 'buildtools/sqlite.zip',
135 'https://storage.googleapis.com/perfetto/sqlite-amalgamation-3320300.zip',
136 '0c805bea134712a903290a26b2a61c3a8a3bd8cc', 'all'),
137 Dependency(
138 'buildtools/sqlite_src.zip',
139 'https://storage.googleapis.com/perfetto/sqlite-src-3320300.zip',
140 'd46f60e0fb2b1a959ae59bfa881fc95a510c4d21', 'all'),
Primiano Tucci0d72a312018-08-07 14:42:45 +0100141
Matthew Clarkson63028d62019-10-10 15:48:23 +0100142 # JsonCpp for legacy json import. Used only by the trace processor in
143 # standalone builds.
Primiano Tucci43f111a2020-07-27 20:37:52 +0200144 Dependency(
145 'buildtools/jsoncpp.zip',
146 'https://github.com/open-source-parsers/jsoncpp/archive/1.9.3.zip',
147 'ec1cf26bf4e60822dbb31576e1a83ce1b9fbc36a', 'all'),
Florian Mayer475bd7e2018-08-07 20:04:03 +0100148
Matthew Clarkson63028d62019-10-10 15:48:23 +0100149 # These dependencies are for libunwindstack, which is used by src/profiling.
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700150 Dependency('buildtools/android-core',
Primiano Tucci43f111a2020-07-27 20:37:52 +0200151 'https://android.googlesource.com/platform/system/core.git',
152 '8bf4e29e44098e3232ff646331675fb113064162', 'all'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700153 Dependency('buildtools/lzma',
Primiano Tucci43f111a2020-07-27 20:37:52 +0200154 'https://android.googlesource.com/platform/external/lzma.git',
155 '7851dce6f4ca17f5caa1c93a4e0a45686b1d56c3', 'all'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700156 Dependency('buildtools/zlib',
Primiano Tucci43f111a2020-07-27 20:37:52 +0200157 'https://android.googlesource.com/platform/external/zlib.git',
158 'dfa0646a03b4e1707469e04dc931b09774968fe6', 'all'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700159 Dependency('buildtools/bionic',
Primiano Tucci43f111a2020-07-27 20:37:52 +0200160 'https://android.googlesource.com/platform/bionic.git',
161 'a60488109cda997dfd83832731c8527feaa2825e', 'all'),
Florian Mayer475bd7e2018-08-07 20:04:03 +0100162
Matthew Clarkson63028d62019-10-10 15:48:23 +0100163 # Example traces for regression tests.
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700164 Dependency(
Matthew Clarkson63028d62019-10-10 15:48:23 +0100165 'buildtools/test_data.zip',
Florian Mayer192c15f2020-07-31 10:44:27 +0100166 'https://storage.googleapis.com/perfetto/test-data-20200730-163541.zip',
167 '21531fb582e98c8c459f3e7a90bce5b6dedd1035',
Matthew Clarkson63028d62019-10-10 15:48:23 +0100168 'all',
169 ),
Hector Dearman7f71d0e2018-08-09 11:26:10 +0100170
Matthew Clarkson63028d62019-10-10 15:48:23 +0100171 # Linenoise, used only by trace_processor in standalone builds.
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700172 Dependency('buildtools/linenoise',
Primiano Tucci43f111a2020-07-27 20:37:52 +0200173 'https://fuchsia.googlesource.com/third_party/linenoise.git',
174 'c894b9e59f02203dbe4e2be657572cf88c4230c3', 'all'),
Primiano Tuccid7750452017-09-29 14:38:51 +0100175]
176
177# Dependencies required to build Android code.
178# URLs and SHA1s taken from:
179# - https://dl.google.com/android/repository/repository-11.xml
180# - https://dl.google.com/android/repository/sys-img/android/sys-img.xml
181BUILD_DEPS_ANDROID = [
Matthew Clarkson63028d62019-10-10 15:48:23 +0100182 # Android NDK
Primiano Tucci43f111a2020-07-27 20:37:52 +0200183 Dependency(
184 'buildtools/ndk.zip',
185 'https://dl.google.com/android/repository/android-ndk-r17b-darwin-x86_64.zip',
186 'f990aafaffec0b583d2c5420bfa622e52ac14248', 'darwin'),
187 Dependency(
188 'buildtools/ndk.zip',
189 'https://dl.google.com/android/repository/android-ndk-r17b-linux-x86_64.zip',
190 'dd5762ee7ef4995ad04fe0c45a608c344d99ca9f', 'linux'),
Primiano Tuccid7750452017-09-29 14:38:51 +0100191]
Primiano Tucciae2879e2017-09-27 11:02:09 +0900192
Primiano Tuccid7750452017-09-29 14:38:51 +0100193# Dependencies required to run Android tests.
194TEST_DEPS_ANDROID = [
Matthew Clarkson63028d62019-10-10 15:48:23 +0100195 # Android emulator images.
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700196 Dependency('buildtools/aosp-arm.zip',
Primiano Tucci43f111a2020-07-27 20:37:52 +0200197 'https://storage.googleapis.com/perfetto/aosp-02022018-arm.zip',
198 'a480d5e7d3ca888b0a58fe15ce76b1791537429a', 'all'),
Primiano Tucciae2879e2017-09-27 11:02:09 +0900199
Matthew Clarkson63028d62019-10-10 15:48:23 +0100200 # platform-tools.zip contains adb binaries.
Primiano Tucci43f111a2020-07-27 20:37:52 +0200201 Dependency(
202 'buildtools/android_sdk/platform-tools.zip',
203 'https://dl.google.com/android/repository/platform-tools_r26.0.0-darwin.zip',
204 'e75b6137dc444f777eb02f44a6d9819b3aabff82', 'darwin'),
205 Dependency(
206 'buildtools/android_sdk/platform-tools.zip',
207 'https://dl.google.com/android/repository/platform-tools_r26.0.0-linux.zip',
208 '00de8a6631405b617c10f68cd11ff2e1cd528e23', 'linux'),
Primiano Tucci0825bc82017-09-28 18:50:23 +0100209
Matthew Clarkson63028d62019-10-10 15:48:23 +0100210 # Android emulator binaries.
Primiano Tucci43f111a2020-07-27 20:37:52 +0200211 Dependency(
212 'buildtools/emulator',
213 'https://android.googlesource.com/platform/prebuilts/android-emulator.git',
214 '4b260028dc27bc92c39bee9129cb2ba839970956', 'all'),
Primiano Tuccid7750452017-09-29 14:38:51 +0100215]
Primiano Tucciae2879e2017-09-27 11:02:09 +0900216
Primiano Tucci1c752c12018-10-23 09:27:19 +0100217# This variable is updated by tools/roll-catapult-trace-viewer.
Isabelle Taylor4a69bd82020-06-08 09:24:58 +0100218CATAPULT_SHA1 = '5f77256e1b24851a05e8580438b532e2480dd7fd'
Primiano Tucci1c752c12018-10-23 09:27:19 +0100219
Primiano Tucci32ea1032020-01-07 13:53:23 +0000220TYPEFACES_SHA1 = '4fb455de506f8a2859dc5264b8448c2559b08ab8'
Hector Dearman6177b752019-01-24 10:17:32 +0000221
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100222UI_DEPS = [
Primiano Tucci43f111a2020-07-27 20:37:52 +0200223 Dependency(
224 'buildtools/nodejs.tgz',
225 'https://storage.googleapis.com/perfetto/node-v12.18.3-darwin-x64.tar.gz',
226 '8a7a901b7c1447235bfe8575a1c8a78eb14afbcf', 'darwin'),
227 Dependency(
228 'buildtools/nodejs.tgz',
229 'https://storage.googleapis.com/perfetto/node-v12.18.3-linux-x64.tar.gz',
230 'c02138c2f9a813f88e81d31c4f9e5652dd7a9684', 'linux'),
231 Dependency(
232 'buildtools/emsdk/emscripten.tgz',
233 'https://storage.googleapis.com/perfetto/emscripten-1.37.40.tar.gz',
234 '588c28221321ebbdfc8e3a6f47ea6106f589669b', 'all'),
235 Dependency(
236 'buildtools/emsdk/llvm.tgz',
237 'https://storage.googleapis.com/perfetto/emscripten-llvm-e1.37.40-darwin.tar.gz',
238 '7a894ef0a52821c62f6abaac552dc4ce5d424607', 'darwin'),
239 Dependency(
240 'buildtools/emsdk/llvm.tgz',
241 'https://storage.googleapis.com/perfetto/emscripten-llvm-e1.37.40-static-linux.tar.gz',
242 '478501b9b7a14884e546c84efe209a90052cbb07', 'linux'),
243 Dependency(
244 'buildtools/d8.tgz',
245 'https://storage.googleapis.com/perfetto/d8-linux-5.7.492.65.tar.gz',
246 '95e82ad7faf0a6f74d950c2aa65e3858b7bdb6c6', 'linux'),
247 Dependency(
248 'buildtools/d8.tgz',
249 'https://storage.googleapis.com/perfetto/d8-darwin-6.6.346.32.tar.gz',
250 '1abd630619bb1977ab62095570a113d782a1545d', 'darwin'),
251 Dependency(
252 'buildtools/catapult_trace_viewer.tgz',
253 'https://storage.googleapis.com/perfetto/catapult_trace_viewer-%s.tar.gz'
254 % CATAPULT_SHA1, CATAPULT_SHA1, 'all'),
255 Dependency(
256 'buildtools/typefaces.tgz',
257 'https://storage.googleapis.com/perfetto/typefaces-%s.tar.gz' %
258 TYPEFACES_SHA1, TYPEFACES_SHA1, 'all')
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100259]
260
Primiano Tucciae2879e2017-09-27 11:02:09 +0900261ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Primiano Tucci69132a12020-02-07 22:33:06 +0000262UI_DIR = os.path.join(ROOT_DIR, 'ui')
263NODE_MODULES_STATUS_FILE = os.path.join(UI_DIR, 'node_modules', '.last_install')
Primiano Tucciae2879e2017-09-27 11:02:09 +0900264
265
Primiano Tucci585b7e82020-05-14 11:19:10 +0100266def DownloadURL(url, out_file):
Primiano Tucci9ff392c2020-05-15 22:35:38 +0100267 subprocess.check_call(['curl', '-L', '-#', '-o', out_file, url])
Primiano Tucci585b7e82020-05-14 11:19:10 +0100268
269
Primiano Tucciae2879e2017-09-27 11:02:09 +0900270def ReadFile(path):
271 if not os.path.exists(path):
272 return None
273 with open(path) as f:
Matthew Clarkson63028d62019-10-10 15:48:23 +0100274 return f.read().strip()
Primiano Tucciae2879e2017-09-27 11:02:09 +0900275
276
Primiano Tucci0825bc82017-09-28 18:50:23 +0100277def MkdirRecursive(path):
278 # Works with both relative and absolute paths
279 cwd = '/' if path.startswith('/') else ROOT_DIR
280 for part in path.split('/'):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900281 cwd = os.path.join(cwd, part)
282 if not os.path.exists(cwd):
283 os.makedirs(cwd)
284 else:
Matthew Clarkson63028d62019-10-10 15:48:23 +0100285 assert (os.path.isdir(cwd))
Primiano Tucciae2879e2017-09-27 11:02:09 +0900286
287
288def HashLocalFile(path):
289 if not os.path.exists(path):
290 return None
291 with open(path, 'rb') as f:
292 return hashlib.sha1(f.read()).hexdigest()
293
294
295def ExtractZipfilePreservePermissions(zf, info, path):
296 zf.extract(info.filename, path=path)
297 target_path = os.path.join(path, info.filename)
298 min_acls = 0o755 if info.filename.endswith('/') else 0o644
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +0100299 os.chmod(target_path, (info.external_attr >> 16) | min_acls)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900300
301
Primiano Tucci0825bc82017-09-28 18:50:23 +0100302def IsGitRepoCheckoutOutAtRevision(path, revision):
303 return ReadFile(os.path.join(path, '.git', 'HEAD')) == revision
304
305
Primiano Tucci69132a12020-02-07 22:33:06 +0000306def CheckoutGitRepo(path, git_url, revision, check_only):
Primiano Tucci0825bc82017-09-28 18:50:23 +0100307 if IsGitRepoCheckoutOutAtRevision(path, revision):
Eric Seckler676421f2019-02-12 14:43:31 +0000308 return False
Primiano Tucci69132a12020-02-07 22:33:06 +0000309 if check_only:
310 return True
Primiano Tucci0825bc82017-09-28 18:50:23 +0100311 if os.path.exists(path):
312 shutil.rmtree(path)
313 MkdirRecursive(path)
314 logging.info('Fetching %s @ %s into %s', git_url, revision, path)
Lalit Maganti367fcd52018-02-05 16:06:13 +0000315 subprocess.check_call(['git', 'init', path], cwd=path)
316 subprocess.check_call(
Matthew Clarkson63028d62019-10-10 15:48:23 +0100317 ['git', 'fetch', '--quiet', '--depth', '1', git_url, revision], cwd=path)
Primiano Tucci0825bc82017-09-28 18:50:23 +0100318 subprocess.check_call(['git', 'checkout', revision, '--quiet'], cwd=path)
Matthew Clarkson63028d62019-10-10 15:48:23 +0100319 assert (IsGitRepoCheckoutOutAtRevision(path, revision))
Eric Seckler676421f2019-02-12 14:43:31 +0000320 return True
Primiano Tucci0825bc82017-09-28 18:50:23 +0100321
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100322
Primiano Tucci43f111a2020-07-27 20:37:52 +0200323def InstallNodeModules(force_clean=False):
324 if force_clean:
325 node_modules = os.path.join(UI_DIR, 'node_modules')
326 logging.info('Clearing %s', node_modules)
327 subprocess.check_call(['git', 'clean', '-qxffd', node_modules],
328 cwd=ROOT_DIR)
Primiano Tucci69132a12020-02-07 22:33:06 +0000329 logging.info("Running npm install in {0}".format(UI_DIR))
330 subprocess.check_call([os.path.join(UI_DIR, 'npm'), 'install', '--no-save'],
331 cwd=UI_DIR)
332 with open(NODE_MODULES_STATUS_FILE, 'w') as f:
333 f.write(HashLocalFile(os.path.join(UI_DIR, 'package-lock.json')))
334
335
336def CheckNodeModules():
337 """Returns True if the modules are up-to-date.
338
339 There doesn't seem to be an easy way to check node modules versions. Instead
340 just check if package-lock.json changed since the last `npm install` call.
341 """
342 if not os.path.exists(NODE_MODULES_STATUS_FILE):
343 return False
344 with open(NODE_MODULES_STATUS_FILE, 'r') as f:
345 actual = f.read()
346 expected = HashLocalFile(os.path.join(UI_DIR, 'package-lock.json'))
347 return expected == actual
Primiano Tucci0825bc82017-09-28 18:50:23 +0100348
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100349
350def CheckHashes():
Matthew Clarkson63028d62019-10-10 15:48:23 +0100351 for deps in [BUILD_DEPS_HOST, BUILD_DEPS_ANDROID, TEST_DEPS_ANDROID, UI_DEPS]:
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700352 for dep in deps:
353 if dep.source_url.endswith('.git'):
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100354 continue
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700355 logging.info('Downloading %s from %s', dep.target_platform,
Primiano Tucci43f111a2020-07-27 20:37:52 +0200356 dep.source_url)
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100357 with tempfile.NamedTemporaryFile(delete=False) as f:
358 f.close()
Primiano Tucci585b7e82020-05-14 11:19:10 +0100359 DownloadURL(url, f.name)
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100360 actual_sha1 = HashLocalFile(f.name)
361 os.unlink(f.name)
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700362 if (actual_sha1 != dep.sha1):
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100363 logging.fatal('SHA1 mismatch for {} expected {} was {}'.format(
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700364 dep.source_url, dep.sha1, actual_sha1))
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100365
366
Primiano Tucciae2879e2017-09-27 11:02:09 +0900367def Main():
368 parser = argparse.ArgumentParser()
Primiano Tucci69132a12020-02-07 22:33:06 +0000369 parser.add_argument('--android', action='store_true')
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100370 parser.add_argument('--ui', action='store_true')
Primiano Tucci69132a12020-02-07 22:33:06 +0000371 parser.add_argument('--check-only')
372 parser.add_argument('--verify', help='Check all URLs', action='store_true')
Primiano Tucciae2879e2017-09-27 11:02:09 +0900373 args = parser.parse_args()
Primiano Tucci69132a12020-02-07 22:33:06 +0000374 if args.verify:
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100375 CheckHashes()
376 return 0
Primiano Tuccid7750452017-09-29 14:38:51 +0100377 deps = BUILD_DEPS_HOST
Primiano Tucci69132a12020-02-07 22:33:06 +0000378 if args.android:
Primiano Tuccid7750452017-09-29 14:38:51 +0100379 deps += BUILD_DEPS_ANDROID + TEST_DEPS_ANDROID
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100380 if args.ui:
381 deps += UI_DEPS
Eric Seckler6e828f32019-01-02 11:10:56 +0000382 deps_updated = False
Primiano Tucci43f111a2020-07-27 20:37:52 +0200383 nodejs_updated = False
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700384
385 for dep in deps:
386 if (dep.target_platform != 'all' and
387 dep.target_platform != system().lower()):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900388 continue
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700389 local_path = os.path.join(ROOT_DIR, dep.target_folder)
390 if dep.source_url.endswith('.git'):
391 deps_updated |= CheckoutGitRepo(local_path, dep.source_url, dep.sha1,
Primiano Tucci69132a12020-02-07 22:33:06 +0000392 args.check_only)
Primiano Tucci0825bc82017-09-28 18:50:23 +0100393 continue
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000394 is_zip = local_path.endswith('.zip') or local_path.endswith('.tgz')
Primiano Tucciae2879e2017-09-27 11:02:09 +0900395 zip_target_dir = local_path[:-4] if is_zip else None
396 zip_dir_stamp = os.path.join(zip_target_dir, '.stamp') if is_zip else None
397
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700398 if ((not is_zip and HashLocalFile(local_path) == dep.sha1) or
399 (is_zip and ReadFile(zip_dir_stamp) == dep.sha1)):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900400 continue
Eric Seckler6e828f32019-01-02 11:10:56 +0000401 deps_updated = True
Primiano Tucci69132a12020-02-07 22:33:06 +0000402 if args.check_only:
403 continue
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700404 MkdirRecursive(os.path.dirname(dep.target_folder))
405 if HashLocalFile(local_path) != dep.sha1:
Primiano Tucciae2879e2017-09-27 11:02:09 +0900406 download_path = local_path + '.tmp'
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700407 logging.info('Downloading %s from %s', local_path, dep.source_url)
408 DownloadURL(dep.source_url, download_path)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900409 os.chmod(download_path, 0o755)
Hector Dearmand9628d32018-10-17 14:38:26 +0100410 actual_sha1 = HashLocalFile(download_path)
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700411 if (actual_sha1 != dep.sha1):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900412 os.remove(download_path)
Hector Dearmand9628d32018-10-17 14:38:26 +0100413 logging.fatal('SHA1 mismatch for {} expected {} was {}'.format(
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700414 download_path, dep.sha1, actual_sha1))
Primiano Tucciae2879e2017-09-27 11:02:09 +0900415 return 1
416 os.rename(download_path, local_path)
Primiano Tucci43f111a2020-07-27 20:37:52 +0200417 if 'nodejs' in dep.target_folder:
418 nodejs_updated = True
419
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700420 assert (HashLocalFile(local_path) == dep.sha1)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900421
422 if is_zip:
423 logging.info('Extracting %s into %s' % (local_path, zip_target_dir))
Matthew Clarkson63028d62019-10-10 15:48:23 +0100424 assert (os.path.commonprefix((ROOT_DIR, zip_target_dir)) == ROOT_DIR)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900425 if os.path.exists(zip_target_dir):
426 logging.info('Deleting stale dir %s' % zip_target_dir)
427 shutil.rmtree(zip_target_dir)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900428
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000429 # Decompress the archive.
430 if local_path.endswith('.tgz'):
431 MkdirRecursive(zip_target_dir)
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100432 subprocess.check_call(['tar', '-xf', local_path], cwd=zip_target_dir)
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000433 elif local_path.endswith('.zip'):
434 with zipfile.ZipFile(local_path, 'r') as zf:
435 for info in zf.infolist():
436 ExtractZipfilePreservePermissions(zf, info, zip_target_dir)
437
438 # If the zip contains one root folder, rebase one level up moving all
439 # its sub files and folders inside |target_dir|.
440 subdir = os.listdir(zip_target_dir)
441 if len(subdir) == 1:
442 subdir = os.path.join(zip_target_dir, subdir[0])
443 if os.path.isdir(subdir):
444 for subf in os.listdir(subdir):
Matthew Clarkson63028d62019-10-10 15:48:23 +0100445 shutil.move(os.path.join(subdir, subf), zip_target_dir)
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000446 os.rmdir(subdir)
447
448 # Create stamp and remove the archive.
449 with open(zip_dir_stamp, 'w') as stamp_file:
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700450 stamp_file.write(dep.sha1)
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000451 os.remove(local_path)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900452
Deepanjan Royacf8c072018-07-13 11:37:04 -0400453 if args.ui:
454 # Needs to happen after nodejs is installed above.
Primiano Tucci69132a12020-02-07 22:33:06 +0000455 if args.check_only:
456 deps_updated = not CheckNodeModules()
457 else:
Primiano Tucci43f111a2020-07-27 20:37:52 +0200458 InstallNodeModules(force_clean=nodejs_updated)
Primiano Tucci69132a12020-02-07 22:33:06 +0000459
460 if args.check_only:
461 if not deps_updated:
462 with open(args.check_only, 'w') as f:
463 f.write('OK') # The content is irrelevant, just keep GN happy.
464 return 0
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700465 argz = ' '.join([x for x in sys.argv[1:] if '--check-only' not in x])
Primiano Tucci69132a12020-02-07 22:33:06 +0000466 sys.stderr.write('\033[91mBuild deps are stale. ' +
467 'Please run tools/install-build-deps %s\033[0m' % argz)
468 return 1
Primiano Tucciae2879e2017-09-27 11:02:09 +0900469
Eric Seckler6e828f32019-01-02 11:10:56 +0000470 if deps_updated:
471 # Stale binary files may be compiled against old sysroot headers that aren't
472 # tracked by gn.
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +0100473 logging.warning('Remember to run "gn clean <output_directory>" ' +
474 'to avoid stale binary files.')
Eric Seckler6e828f32019-01-02 11:10:56 +0000475
Matthew Clarkson63028d62019-10-10 15:48:23 +0100476
Primiano Tucciae2879e2017-09-27 11:02:09 +0900477if __name__ == '__main__':
478 logging.basicConfig(level=logging.INFO)
479 sys.exit(Main())