blob: 2a8b5dc9c9515b1f2db1c740c7ef37f0928eeb8c [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
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +010029
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).
Matthew Clarkson40e48192019-10-28 12:35:01 +000039# |target_platform| is either 'darwin', 'linux' or 'all' and applies the dep
40# only on the given platform
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -070041Dependency = namedtuple('Dependency', ['target_folder', 'source_url',
42 'sha1', 'target_platform'])
Primiano Tuccib60d4b02017-11-10 11:03:00 +000043
Primiano Tuccid7750452017-09-29 14:38:51 +010044# Dependencies required to build code on the host or when targeting desktop OS.
45BUILD_DEPS_HOST = [
Matthew Clarkson63028d62019-10-10 15:48:23 +010046 # GN
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -070047 Dependency('buildtools/mac/gn',
Primiano Tucci2925e9d2020-01-27 10:15:58 +000048 'https://storage.googleapis.com/perfetto/gn-mac-1695-83dad00a',
49 '4c0d45772aea4146699772165e8112fa76ceb295', 'darwin'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -070050 Dependency('buildtools/linux64/gn',
Primiano Tucci2925e9d2020-01-27 10:15:58 +000051 '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
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -070055 Dependency('buildtools/mac/clang-format',
Matthew Clarkson63028d62019-10-10 15:48:23 +010056 'https://storage.googleapis.com/chromium-clang-format/025ca7c75f37ef4a40f3a67d81ddd11d7d0cdb9b',
57 '025ca7c75f37ef4a40f3a67d81ddd11d7d0cdb9b', 'darwin'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -070058 Dependency('buildtools/linux64/clang-format',
Matthew Clarkson63028d62019-10-10 15:48:23 +010059 'https://storage.googleapis.com/chromium-clang-format/942fc8b1789144b8071d3fc03ff0fcbe1cf81ac8',
Matthew Clarkson40e48192019-10-28 12:35:01 +000060 '942fc8b1789144b8071d3fc03ff0fcbe1cf81ac8', 'linux'),
Matthew Clarkson63028d62019-10-10 15:48:23 +010061 # Keep the SHA1 in sync with |clang_format_rev| in chromium //buildtools/DEPS.
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -070062 Dependency('buildtools/clang_format/script',
Matthew Clarkson63028d62019-10-10 15:48:23 +010063 'https://chromium.googlesource.com/chromium/llvm-project/cfe/tools/clang-format.git',
64 '96636aa0e9f047f17447f2d45a094d0b59ed7917', 'all'),
Primiano Tucci104bd6d2017-10-12 00:10:24 +020065
Matthew Clarkson63028d62019-10-10 15:48:23 +010066 # Ninja
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -070067 Dependency('buildtools/mac/ninja',
Matthew Clarkson63028d62019-10-10 15:48:23 +010068 'https://storage.googleapis.com/perfetto/ninja-mac-c15b0698da038b2bd2e8970c14c75fadc06b1add',
69 'c15b0698da038b2bd2e8970c14c75fadc06b1add', 'darwin'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -070070 Dependency('buildtools/linux64/ninja',
Matthew Clarkson63028d62019-10-10 15:48:23 +010071 'https://storage.googleapis.com/perfetto/ninja-linux64-c866952bda50c29a669222477309287119bbb7e8',
Matthew Clarkson40e48192019-10-28 12:35:01 +000072 'c866952bda50c29a669222477309287119bbb7e8', 'linux'),
Primiano Tucciae2879e2017-09-27 11:02:09 +090073
Matthew Clarkson63028d62019-10-10 15:48:23 +010074 # Keep in sync with Android's //external/googletest/README.version.
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -070075 Dependency('buildtools/googletest.zip',
Ryan Savitskib08b9cb2019-11-01 17:36:19 +000076 'https://github.com/google/googletest/archive/3f05f651ae3621db58468153e32016bc1397800b.zip',
77 '86384688f7c533ad325a505efc917e0cdf39a0ce', 'all'),
Primiano Tuccid7750452017-09-29 14:38:51 +010078
Primiano Tuccif550b252019-12-03 11:06:02 +000079 # Keep in sync with Chromium's //third_party/protobuf.
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -070080 Dependency('buildtools/protobuf.zip',
Primiano Tuccif550b252019-12-03 11:06:02 +000081 'https://github.com/protocolbuffers/protobuf/releases/download/v3.9.0/protobuf-cpp-3.9.0.zip',
82 'c975536dffe9d9a3d362928aef4fb9f199012b98', 'all'),
Primiano Tuccid7750452017-09-29 14:38:51 +010083
Matthew Clarkson63028d62019-10-10 15:48:23 +010084 # libc++, libc++abi and libunwind for Linux where we need to rebuild the C++
85 # lib from sources. Keep the SHA1s in sync with Chrome's src/buildtools/DEPS.
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -070086 Dependency('buildtools/libcxx',
Matthew Clarkson63028d62019-10-10 15:48:23 +010087 'https://chromium.googlesource.com/chromium/llvm-project/libcxx.git',
Primiano Tucci5e2dbac2020-01-27 10:35:27 +000088 '78d6a7767ed57b50122a161b91f59f19c9bd0d19', 'all'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -070089 Dependency('buildtools/libcxxabi',
Matthew Clarkson63028d62019-10-10 15:48:23 +010090 'https://chromium.googlesource.com/chromium/llvm-project/libcxxabi.git',
91 '0d529660e32d77d9111912d73f2c74fc5fa2a858', 'all'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -070092 Dependency('buildtools/libunwind',
Matthew Clarkson63028d62019-10-10 15:48:23 +010093 'https://chromium.googlesource.com/external/llvm.org/libunwind.git',
94 '69d9b84cca8354117b9fe9705a4430d789ee599b', 'all'),
Hector Dearman88a10112017-10-12 11:07:10 +010095
Matthew Clarkson63028d62019-10-10 15:48:23 +010096 # Keep the revision in sync with Chrome's PACKAGE_VERSION in
97 # tools/clang/scripts/update.py.
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -070098 Dependency('buildtools/clang.tgz',
Primiano Tucci5e2dbac2020-01-27 10:35:27 +000099 'https://commondatastorage.googleapis.com/chromium-browser-clang/Linux_x64/clang-n332890-c2443155-2.tgz',
Ryan Savitskib1cec092020-01-27 21:03:30 +0000100 'd6501ffdb5dbb0ffe8a4b873cc092a9929e661ec', 'linux'),
Primiano Tuccia65497e2018-09-26 19:53:58 +0100101
Matthew Clarkson63028d62019-10-10 15:48:23 +0100102 # Keep in sync with chromium DEPS.
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700103 Dependency('buildtools/libfuzzer',
Matthew Clarkson63028d62019-10-10 15:48:23 +0100104 'https://chromium.googlesource.com/chromium/llvm-project/compiler-rt/lib/fuzzer.git',
Primiano Tucci5e2dbac2020-01-27 10:35:27 +0000105 'debe7d2d1982e540fbd6bd78604bf001753f9e74', 'linux'),
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000106
Matthew Clarkson63028d62019-10-10 15:48:23 +0100107 # Benchmarking tool.
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700108 Dependency('buildtools/benchmark.zip',
Primiano Tucci5acece02020-06-04 10:39:53 +0100109 'https://github.com/google/benchmark/archive/v1.5.0.zip',
110 'a9c9bd8a28db82f5ba02998197cfcc4db5a67507', 'all'),
Primiano Tucci38faa6f2018-04-01 20:12:08 +0200111
Matthew Clarkson63028d62019-10-10 15:48:23 +0100112 # Libbacktrace, for stacktraces in Linux/Android debug builds.
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700113 Dependency('buildtools/libbacktrace.zip',
Matthew Clarkson63028d62019-10-10 15:48:23 +0100114 'https://github.com/ianlancetaylor/libbacktrace/archive/177940370e4a6b2509e92a0aaa9749184e64af43.zip',
115 'b723fe9d671d1ab54df1297f6afbf2893a41c3ea', 'all'),
Lalit Maganti6fe26e62018-05-23 12:14:38 +0100116
Matthew Clarkson63028d62019-10-10 15:48:23 +0100117 # Sqlite for the trace processing library.
118 # This is the amalgamated source whose compiled output is meant to be faster.
119 # We still pull the full source for the extensions (not amalgamated).
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700120 Dependency('buildtools/sqlite.zip',
Lalit Maganti2d4ee9b2020-06-30 18:29:20 +0100121 'https://storage.googleapis.com/perfetto/sqlite-amalgamation-3320300.zip',
122 '0c805bea134712a903290a26b2a61c3a8a3bd8cc', 'all'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700123 Dependency('buildtools/sqlite_src.zip',
Lalit Maganti2d4ee9b2020-06-30 18:29:20 +0100124 'https://storage.googleapis.com/perfetto/sqlite-src-3320300.zip',
125 'd46f60e0fb2b1a959ae59bfa881fc95a510c4d21', 'all'),
Primiano Tucci0d72a312018-08-07 14:42:45 +0100126
Matthew Clarkson63028d62019-10-10 15:48:23 +0100127 # JsonCpp for legacy json import. Used only by the trace processor in
128 # standalone builds.
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700129 Dependency('buildtools/jsoncpp.zip',
130 'https://github.com/open-source-parsers/jsoncpp/archive/1.9.3.zip',
131 'ec1cf26bf4e60822dbb31576e1a83ce1b9fbc36a', 'all'),
Florian Mayer475bd7e2018-08-07 20:04:03 +0100132
Matthew Clarkson63028d62019-10-10 15:48:23 +0100133 # These dependencies are for libunwindstack, which is used by src/profiling.
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700134 Dependency('buildtools/android-core',
Matthew Clarkson63028d62019-10-10 15:48:23 +0100135 'https://android.googlesource.com/platform/system/core.git',
Florian Mayer961274c2020-01-23 18:31:49 +0000136 '8bf4e29e44098e3232ff646331675fb113064162', 'all'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700137 Dependency('buildtools/lzma',
Matthew Clarkson63028d62019-10-10 15:48:23 +0100138 'https://android.googlesource.com/platform/external/lzma.git',
139 '7851dce6f4ca17f5caa1c93a4e0a45686b1d56c3', 'all'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700140 Dependency('buildtools/zlib',
Matthew Clarkson63028d62019-10-10 15:48:23 +0100141 'https://android.googlesource.com/platform/external/zlib.git',
142 'dfa0646a03b4e1707469e04dc931b09774968fe6', 'all'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700143 Dependency('buildtools/bionic',
Matthew Clarkson63028d62019-10-10 15:48:23 +0100144 'https://android.googlesource.com/platform/bionic.git',
145 'a60488109cda997dfd83832731c8527feaa2825e', 'all'),
Florian Mayer475bd7e2018-08-07 20:04:03 +0100146
Matthew Clarkson63028d62019-10-10 15:48:23 +0100147 # Example traces for regression tests.
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700148 Dependency(
Matthew Clarkson63028d62019-10-10 15:48:23 +0100149 'buildtools/test_data.zip',
Lalit Magantic8e27652020-07-16 23:58:18 +0100150 'https://storage.googleapis.com/perfetto/test-data-20200716-213947.zip',
151 'ed6a14fd5196479bb9e0c18805fd45b79fcbee8f',
Matthew Clarkson63028d62019-10-10 15:48:23 +0100152 'all',
153 ),
Hector Dearman7f71d0e2018-08-09 11:26:10 +0100154
Matthew Clarkson63028d62019-10-10 15:48:23 +0100155 # Linenoise, used only by trace_processor in standalone builds.
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700156 Dependency('buildtools/linenoise',
Matthew Clarkson63028d62019-10-10 15:48:23 +0100157 'https://fuchsia.googlesource.com/third_party/linenoise.git',
158 'c894b9e59f02203dbe4e2be657572cf88c4230c3', 'all'),
Primiano Tuccid7750452017-09-29 14:38:51 +0100159]
160
161# Dependencies required to build Android code.
162# URLs and SHA1s taken from:
163# - https://dl.google.com/android/repository/repository-11.xml
164# - https://dl.google.com/android/repository/sys-img/android/sys-img.xml
165BUILD_DEPS_ANDROID = [
Matthew Clarkson63028d62019-10-10 15:48:23 +0100166 # Android NDK
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700167 Dependency('buildtools/ndk.zip',
Matthew Clarkson63028d62019-10-10 15:48:23 +0100168 'https://dl.google.com/android/repository/android-ndk-r17b-darwin-x86_64.zip',
169 'f990aafaffec0b583d2c5420bfa622e52ac14248', 'darwin'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700170 Dependency('buildtools/ndk.zip',
Matthew Clarkson63028d62019-10-10 15:48:23 +0100171 'https://dl.google.com/android/repository/android-ndk-r17b-linux-x86_64.zip',
Matthew Clarkson40e48192019-10-28 12:35:01 +0000172 'dd5762ee7ef4995ad04fe0c45a608c344d99ca9f', 'linux'),
Primiano Tuccid7750452017-09-29 14:38:51 +0100173]
Primiano Tucciae2879e2017-09-27 11:02:09 +0900174
Primiano Tuccid7750452017-09-29 14:38:51 +0100175# Dependencies required to run Android tests.
176TEST_DEPS_ANDROID = [
Matthew Clarkson63028d62019-10-10 15:48:23 +0100177 # Android emulator images.
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700178 Dependency('buildtools/aosp-arm.zip',
Matthew Clarkson63028d62019-10-10 15:48:23 +0100179 'https://storage.googleapis.com/perfetto/aosp-02022018-arm.zip',
180 'a480d5e7d3ca888b0a58fe15ce76b1791537429a', 'all'),
Primiano Tucciae2879e2017-09-27 11:02:09 +0900181
Matthew Clarkson63028d62019-10-10 15:48:23 +0100182 # platform-tools.zip contains adb binaries.
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700183 Dependency('buildtools/android_sdk/platform-tools.zip',
Matthew Clarkson63028d62019-10-10 15:48:23 +0100184 'https://dl.google.com/android/repository/platform-tools_r26.0.0-darwin.zip',
185 'e75b6137dc444f777eb02f44a6d9819b3aabff82', 'darwin'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700186 Dependency('buildtools/android_sdk/platform-tools.zip',
Matthew Clarkson63028d62019-10-10 15:48:23 +0100187 'https://dl.google.com/android/repository/platform-tools_r26.0.0-linux.zip',
Matthew Clarkson40e48192019-10-28 12:35:01 +0000188 '00de8a6631405b617c10f68cd11ff2e1cd528e23', 'linux'),
Primiano Tucci0825bc82017-09-28 18:50:23 +0100189
Matthew Clarkson63028d62019-10-10 15:48:23 +0100190 # Android emulator binaries.
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700191 Dependency('buildtools/emulator',
Matthew Clarkson63028d62019-10-10 15:48:23 +0100192 'https://android.googlesource.com/platform/prebuilts/android-emulator.git',
193 '4b260028dc27bc92c39bee9129cb2ba839970956', 'all'),
Primiano Tuccid7750452017-09-29 14:38:51 +0100194]
Primiano Tucciae2879e2017-09-27 11:02:09 +0900195
Primiano Tucci1c752c12018-10-23 09:27:19 +0100196# This variable is updated by tools/roll-catapult-trace-viewer.
Isabelle Taylor4a69bd82020-06-08 09:24:58 +0100197CATAPULT_SHA1 = '5f77256e1b24851a05e8580438b532e2480dd7fd'
Primiano Tucci1c752c12018-10-23 09:27:19 +0100198
Primiano Tucci32ea1032020-01-07 13:53:23 +0000199TYPEFACES_SHA1 = '4fb455de506f8a2859dc5264b8448c2559b08ab8'
Hector Dearman6177b752019-01-24 10:17:32 +0000200
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100201UI_DEPS = [
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700202 Dependency('buildtools/nodejs.tgz',
Deepanjan Roy74ed9ca2020-07-27 13:06:29 -0400203 'https://storage.googleapis.com/perfetto/node-v12.18.3-darwin-x64.tar.gz',
204 '8a7a901b7c1447235bfe8575a1c8a78eb14afbcf', 'darwin'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700205 Dependency('buildtools/nodejs.tgz',
Deepanjan Roy74ed9ca2020-07-27 13:06:29 -0400206 'https://storage.googleapis.com/perfetto/node-v12.18.3-linux-x64.tar.gz',
207 'c02138c2f9a813f88e81d31c4f9e5652dd7a9684', 'linux'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700208 Dependency('buildtools/emsdk/emscripten.tgz',
Matthew Clarkson63028d62019-10-10 15:48:23 +0100209 'https://storage.googleapis.com/perfetto/emscripten-1.37.40.tar.gz',
210 '588c28221321ebbdfc8e3a6f47ea6106f589669b', 'all'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700211 Dependency('buildtools/emsdk/llvm.tgz',
Matthew Clarkson63028d62019-10-10 15:48:23 +0100212 'https://storage.googleapis.com/perfetto/emscripten-llvm-e1.37.40-darwin.tar.gz',
213 '7a894ef0a52821c62f6abaac552dc4ce5d424607', 'darwin'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700214 Dependency('buildtools/emsdk/llvm.tgz',
Matthew Clarkson63028d62019-10-10 15:48:23 +0100215 'https://storage.googleapis.com/perfetto/emscripten-llvm-e1.37.40-static-linux.tar.gz',
Matthew Clarkson40e48192019-10-28 12:35:01 +0000216 '478501b9b7a14884e546c84efe209a90052cbb07', 'linux'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700217 Dependency('buildtools/d8.tgz',
Matthew Clarkson40e48192019-10-28 12:35:01 +0000218 'https://storage.googleapis.com/perfetto/d8-linux-5.7.492.65.tar.gz',
219 '95e82ad7faf0a6f74d950c2aa65e3858b7bdb6c6', 'linux'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700220 Dependency('buildtools/d8.tgz',
Matthew Clarkson63028d62019-10-10 15:48:23 +0100221 'https://storage.googleapis.com/perfetto/d8-darwin-6.6.346.32.tar.gz',
222 '1abd630619bb1977ab62095570a113d782a1545d', 'darwin'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700223 Dependency('buildtools/catapult_trace_viewer.tgz',
Matthew Clarkson63028d62019-10-10 15:48:23 +0100224 'https://storage.googleapis.com/perfetto/catapult_trace_viewer-%s.tar.gz' %
225 CATAPULT_SHA1, CATAPULT_SHA1, 'all'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700226 Dependency('buildtools/typefaces.tgz',
Matthew Clarkson63028d62019-10-10 15:48:23 +0100227 'https://storage.googleapis.com/perfetto/typefaces-%s.tar.gz' %
228 TYPEFACES_SHA1, TYPEFACES_SHA1, 'all')
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100229]
230
Primiano Tucciae2879e2017-09-27 11:02:09 +0900231ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Primiano Tucci69132a12020-02-07 22:33:06 +0000232UI_DIR = os.path.join(ROOT_DIR, 'ui')
233NODE_MODULES_STATUS_FILE = os.path.join(UI_DIR, 'node_modules', '.last_install')
Primiano Tucciae2879e2017-09-27 11:02:09 +0900234
235
Primiano Tucci585b7e82020-05-14 11:19:10 +0100236def DownloadURL(url, out_file):
Primiano Tucci9ff392c2020-05-15 22:35:38 +0100237 subprocess.check_call(['curl', '-L', '-#', '-o', out_file, url])
Primiano Tucci585b7e82020-05-14 11:19:10 +0100238
239
Primiano Tucciae2879e2017-09-27 11:02:09 +0900240def ReadFile(path):
241 if not os.path.exists(path):
242 return None
243 with open(path) as f:
Matthew Clarkson63028d62019-10-10 15:48:23 +0100244 return f.read().strip()
Primiano Tucciae2879e2017-09-27 11:02:09 +0900245
246
Primiano Tucci0825bc82017-09-28 18:50:23 +0100247def MkdirRecursive(path):
248 # Works with both relative and absolute paths
249 cwd = '/' if path.startswith('/') else ROOT_DIR
250 for part in path.split('/'):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900251 cwd = os.path.join(cwd, part)
252 if not os.path.exists(cwd):
253 os.makedirs(cwd)
254 else:
Matthew Clarkson63028d62019-10-10 15:48:23 +0100255 assert (os.path.isdir(cwd))
Primiano Tucciae2879e2017-09-27 11:02:09 +0900256
257
258def HashLocalFile(path):
259 if not os.path.exists(path):
260 return None
261 with open(path, 'rb') as f:
262 return hashlib.sha1(f.read()).hexdigest()
263
264
265def ExtractZipfilePreservePermissions(zf, info, path):
266 zf.extract(info.filename, path=path)
267 target_path = os.path.join(path, info.filename)
268 min_acls = 0o755 if info.filename.endswith('/') else 0o644
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +0100269 os.chmod(target_path, (info.external_attr >> 16) | min_acls)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900270
271
Primiano Tucci0825bc82017-09-28 18:50:23 +0100272def IsGitRepoCheckoutOutAtRevision(path, revision):
273 return ReadFile(os.path.join(path, '.git', 'HEAD')) == revision
274
275
Primiano Tucci69132a12020-02-07 22:33:06 +0000276def CheckoutGitRepo(path, git_url, revision, check_only):
Primiano Tucci0825bc82017-09-28 18:50:23 +0100277 if IsGitRepoCheckoutOutAtRevision(path, revision):
Eric Seckler676421f2019-02-12 14:43:31 +0000278 return False
Primiano Tucci69132a12020-02-07 22:33:06 +0000279 if check_only:
280 return True
Primiano Tucci0825bc82017-09-28 18:50:23 +0100281 if os.path.exists(path):
282 shutil.rmtree(path)
283 MkdirRecursive(path)
284 logging.info('Fetching %s @ %s into %s', git_url, revision, path)
Lalit Maganti367fcd52018-02-05 16:06:13 +0000285 subprocess.check_call(['git', 'init', path], cwd=path)
286 subprocess.check_call(
Matthew Clarkson63028d62019-10-10 15:48:23 +0100287 ['git', 'fetch', '--quiet', '--depth', '1', git_url, revision], cwd=path)
Primiano Tucci0825bc82017-09-28 18:50:23 +0100288 subprocess.check_call(['git', 'checkout', revision, '--quiet'], cwd=path)
Matthew Clarkson63028d62019-10-10 15:48:23 +0100289 assert (IsGitRepoCheckoutOutAtRevision(path, revision))
Eric Seckler676421f2019-02-12 14:43:31 +0000290 return True
Primiano Tucci0825bc82017-09-28 18:50:23 +0100291
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100292
Deepanjan Royacf8c072018-07-13 11:37:04 -0400293def InstallNodeModules():
Primiano Tucci69132a12020-02-07 22:33:06 +0000294 logging.info("Running npm install in {0}".format(UI_DIR))
295 subprocess.check_call([os.path.join(UI_DIR, 'npm'), 'install', '--no-save'],
296 cwd=UI_DIR)
297 with open(NODE_MODULES_STATUS_FILE, 'w') as f:
298 f.write(HashLocalFile(os.path.join(UI_DIR, 'package-lock.json')))
299
300
301def CheckNodeModules():
302 """Returns True if the modules are up-to-date.
303
304 There doesn't seem to be an easy way to check node modules versions. Instead
305 just check if package-lock.json changed since the last `npm install` call.
306 """
307 if not os.path.exists(NODE_MODULES_STATUS_FILE):
308 return False
309 with open(NODE_MODULES_STATUS_FILE, 'r') as f:
310 actual = f.read()
311 expected = HashLocalFile(os.path.join(UI_DIR, 'package-lock.json'))
312 return expected == actual
Primiano Tucci0825bc82017-09-28 18:50:23 +0100313
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100314
315def CheckHashes():
Matthew Clarkson63028d62019-10-10 15:48:23 +0100316 for deps in [BUILD_DEPS_HOST, BUILD_DEPS_ANDROID, TEST_DEPS_ANDROID, UI_DEPS]:
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700317 for dep in deps:
318 if dep.source_url.endswith('.git'):
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100319 continue
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700320 logging.info('Downloading %s from %s', dep.target_platform,
321 dep.source_url)
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100322 with tempfile.NamedTemporaryFile(delete=False) as f:
323 f.close()
Primiano Tucci585b7e82020-05-14 11:19:10 +0100324 DownloadURL(url, f.name)
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100325 actual_sha1 = HashLocalFile(f.name)
326 os.unlink(f.name)
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700327 if (actual_sha1 != dep.sha1):
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100328 logging.fatal('SHA1 mismatch for {} expected {} was {}'.format(
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700329 dep.source_url, dep.sha1, actual_sha1))
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100330
331
Primiano Tucciae2879e2017-09-27 11:02:09 +0900332def Main():
333 parser = argparse.ArgumentParser()
Primiano Tucci69132a12020-02-07 22:33:06 +0000334 parser.add_argument('--android', action='store_true')
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100335 parser.add_argument('--ui', action='store_true')
Primiano Tucci69132a12020-02-07 22:33:06 +0000336 parser.add_argument('--check-only')
337 parser.add_argument('--verify', help='Check all URLs', action='store_true')
Primiano Tucciae2879e2017-09-27 11:02:09 +0900338 args = parser.parse_args()
Primiano Tucci69132a12020-02-07 22:33:06 +0000339 if args.verify:
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100340 CheckHashes()
341 return 0
Primiano Tuccid7750452017-09-29 14:38:51 +0100342 deps = BUILD_DEPS_HOST
Primiano Tucci69132a12020-02-07 22:33:06 +0000343 if args.android:
Primiano Tuccid7750452017-09-29 14:38:51 +0100344 deps += BUILD_DEPS_ANDROID + TEST_DEPS_ANDROID
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100345 if args.ui:
346 deps += UI_DEPS
Eric Seckler6e828f32019-01-02 11:10:56 +0000347 deps_updated = False
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700348
349 for dep in deps:
350 if (dep.target_platform != 'all' and
351 dep.target_platform != system().lower()):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900352 continue
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700353 local_path = os.path.join(ROOT_DIR, dep.target_folder)
354 if dep.source_url.endswith('.git'):
355 deps_updated |= CheckoutGitRepo(local_path, dep.source_url, dep.sha1,
Primiano Tucci69132a12020-02-07 22:33:06 +0000356 args.check_only)
Primiano Tucci0825bc82017-09-28 18:50:23 +0100357 continue
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000358 is_zip = local_path.endswith('.zip') or local_path.endswith('.tgz')
Primiano Tucciae2879e2017-09-27 11:02:09 +0900359 zip_target_dir = local_path[:-4] if is_zip else None
360 zip_dir_stamp = os.path.join(zip_target_dir, '.stamp') if is_zip else None
361
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700362 if ((not is_zip and HashLocalFile(local_path) == dep.sha1) or
363 (is_zip and ReadFile(zip_dir_stamp) == dep.sha1)):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900364 continue
Eric Seckler6e828f32019-01-02 11:10:56 +0000365 deps_updated = True
Primiano Tucci69132a12020-02-07 22:33:06 +0000366 if args.check_only:
367 continue
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700368 MkdirRecursive(os.path.dirname(dep.target_folder))
369 if HashLocalFile(local_path) != dep.sha1:
Primiano Tucciae2879e2017-09-27 11:02:09 +0900370 download_path = local_path + '.tmp'
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700371 logging.info('Downloading %s from %s', local_path, dep.source_url)
372 DownloadURL(dep.source_url, download_path)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900373 os.chmod(download_path, 0o755)
Hector Dearmand9628d32018-10-17 14:38:26 +0100374 actual_sha1 = HashLocalFile(download_path)
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700375 if (actual_sha1 != dep.sha1):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900376 os.remove(download_path)
Hector Dearmand9628d32018-10-17 14:38:26 +0100377 logging.fatal('SHA1 mismatch for {} expected {} was {}'.format(
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700378 download_path, dep.sha1, actual_sha1))
Primiano Tucciae2879e2017-09-27 11:02:09 +0900379 return 1
380 os.rename(download_path, local_path)
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700381 assert (HashLocalFile(local_path) == dep.sha1)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900382
383 if is_zip:
384 logging.info('Extracting %s into %s' % (local_path, zip_target_dir))
Matthew Clarkson63028d62019-10-10 15:48:23 +0100385 assert (os.path.commonprefix((ROOT_DIR, zip_target_dir)) == ROOT_DIR)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900386 if os.path.exists(zip_target_dir):
387 logging.info('Deleting stale dir %s' % zip_target_dir)
388 shutil.rmtree(zip_target_dir)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900389
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000390 # Decompress the archive.
391 if local_path.endswith('.tgz'):
392 MkdirRecursive(zip_target_dir)
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100393 subprocess.check_call(['tar', '-xf', local_path], cwd=zip_target_dir)
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000394 elif local_path.endswith('.zip'):
395 with zipfile.ZipFile(local_path, 'r') as zf:
396 for info in zf.infolist():
397 ExtractZipfilePreservePermissions(zf, info, zip_target_dir)
398
399 # If the zip contains one root folder, rebase one level up moving all
400 # its sub files and folders inside |target_dir|.
401 subdir = os.listdir(zip_target_dir)
402 if len(subdir) == 1:
403 subdir = os.path.join(zip_target_dir, subdir[0])
404 if os.path.isdir(subdir):
405 for subf in os.listdir(subdir):
Matthew Clarkson63028d62019-10-10 15:48:23 +0100406 shutil.move(os.path.join(subdir, subf), zip_target_dir)
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000407 os.rmdir(subdir)
408
409 # Create stamp and remove the archive.
410 with open(zip_dir_stamp, 'w') as stamp_file:
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700411 stamp_file.write(dep.sha1)
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000412 os.remove(local_path)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900413
Deepanjan Royacf8c072018-07-13 11:37:04 -0400414 if args.ui:
415 # Needs to happen after nodejs is installed above.
Primiano Tucci69132a12020-02-07 22:33:06 +0000416 if args.check_only:
417 deps_updated = not CheckNodeModules()
418 else:
419 InstallNodeModules()
420
421 if args.check_only:
422 if not deps_updated:
423 with open(args.check_only, 'w') as f:
424 f.write('OK') # The content is irrelevant, just keep GN happy.
425 return 0
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700426 argz = ' '.join([x for x in sys.argv[1:] if '--check-only' not in x])
Primiano Tucci69132a12020-02-07 22:33:06 +0000427 sys.stderr.write('\033[91mBuild deps are stale. ' +
428 'Please run tools/install-build-deps %s\033[0m' % argz)
429 return 1
Primiano Tucciae2879e2017-09-27 11:02:09 +0900430
Eric Seckler6e828f32019-01-02 11:10:56 +0000431 if deps_updated:
432 # Stale binary files may be compiled against old sysroot headers that aren't
433 # tracked by gn.
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +0100434 logging.warning('Remember to run "gn clean <output_directory>" ' +
435 'to avoid stale binary files.')
Eric Seckler6e828f32019-01-02 11:10:56 +0000436
Matthew Clarkson63028d62019-10-10 15:48:23 +0100437
Primiano Tucciae2879e2017-09-27 11:02:09 +0900438if __name__ == '__main__':
439 logging.basicConfig(level=logging.INFO)
440 sys.exit(Main())