blob: 96a2f65482eda04620108d9cf6fcf51c5b32a901 [file] [log] [blame]
Primiano Tucci34bc5592021-02-19 17:53:36 +01001#!/usr/bin/env python3
Primiano Tucciae2879e2017-09-27 11:02:09 +09002# 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 Tucci857ed732020-12-19 18:11:42 +010022import stat
Primiano Tucciae2879e2017-09-27 11:02:09 +090023import sys
Sami Kyostilac7ddac72019-06-05 21:43:40 +010024import tempfile
Primiano Tucciae2879e2017-09-27 11:02:09 +090025import zipfile
26
Primiano Tuccid7750452017-09-29 14:38:51 +010027from collections import namedtuple
Hector Dearmanadf51172021-04-09 16:09:45 +010028from platform import system, machine
Primiano Tuccid7750452017-09-29 14:38:51 +010029
Primiano Tuccib60d4b02017-11-10 11:03:00 +000030# The format for the deps below is the following:
Primiano Tuccib428d492021-08-02 19:01:29 +010031# (target_folder, source_url, sha1, target_os, target_arch)
Primiano Tuccib60d4b02017-11-10 11:03:00 +000032# |source_url| can be either a git repo or a http url.
Primiano Tucci15968b52020-10-28 15:30:40 +010033# If a git repo, |checksum| is the SHA1 committish that will be checked out.
34# If a http url, |checksum| is the SHA256 of the downloaded file.
Primiano Tuccib60d4b02017-11-10 11:03:00 +000035# 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).
Hector Dearmanadf51172021-04-09 16:09:45 +010039# |target_os| is either 'darwin', 'linux', 'windows' or 'all'
Primiano Tucci0e7915d2022-02-08 15:25:49 +000040# |target_arch| is either 'x64', 'arm64' or 'all'
Hector Dearmanadf51172021-04-09 16:09:45 +010041# in both cases the dep only applies on matching platforms
42# |target_arch| can be 'all' when 'target_os' is not 'all' for example in the
43# case of MacOS universal binaries.
Primiano Tucci43f111a2020-07-27 20:37:52 +020044Dependency = namedtuple(
Primiano Tucci15968b52020-10-28 15:30:40 +010045 'Dependency',
Hector Dearmanadf51172021-04-09 16:09:45 +010046 ['target_folder', 'source_url', 'checksum', 'target_os', 'target_arch'])
Primiano Tuccib60d4b02017-11-10 11:03:00 +000047
Primiano Tucci7e9865c2021-01-10 23:55:15 +010048# This is to remove old directories when build tools get {re,}moved. This is to
49# avoid accidentally referring to stale dir in custom user scripts.
50CLEANUP_OLD_DIRS = [
51 'buildtools/nodejs', # Moved to buildtools/{mac,linux64}/nodejs
52 'buildtools/emsdk', # Moved to buildtools/{mac,linux64}/emsdk
53 'buildtools/test_data', # Moved to test/data by r.android.com/1539381 .
54 'buildtools/d8', # Removed by r.android.com/1424334 .
55]
56
Primiano Tuccid7750452017-09-29 14:38:51 +010057# Dependencies required to build code on the host or when targeting desktop OS.
Florian Mayer54c4e382021-04-20 15:51:23 +010058BUILD_DEPS_TOOLCHAIN_HOST = [
Primiano Tucci15968b52020-10-28 15:30:40 +010059 # GN. From https://chrome-infra-packages.appspot.com/dl/gn/gn/.
Primiano Tuccid8afe872022-02-07 23:18:59 +000060 # git_revision:0725d7827575b239594fbc8fd5192873a1d62f44 .
Primiano Tucci15968b52020-10-28 15:30:40 +010061 Dependency(
62 'buildtools/mac/gn',
Primiano Tuccid8afe872022-02-07 23:18:59 +000063 'https://storage.googleapis.com/perfetto/gn-mac-1968-0725d782',
64 '9ced623a664560bba38bbadb9b91158ca4186358c847e17ab7d982b351373c2e',
Hector Dearmanadf51172021-04-09 16:09:45 +010065 'darwin', 'x64'),
Primiano Tucci43f111a2020-07-27 20:37:52 +020066 Dependency(
Primiano Tucci0e7915d2022-02-08 15:25:49 +000067 'buildtools/mac/gn',
68 'https://storage.googleapis.com/perfetto/gn-mac-arm64-1968-0725d782',
69 'd22336b5210b4dad5e36e8c28ce81187f491822cf4d8fd0a257b30d6bee3fd3f',
70 'darwin', 'arm64'),
71 Dependency(
Primiano Tucci43f111a2020-07-27 20:37:52 +020072 'buildtools/linux64/gn',
Primiano Tuccid8afe872022-02-07 23:18:59 +000073 'https://storage.googleapis.com/perfetto/gn-linux64-1968-0725d782',
74 'f706aaa0676e3e22f5fc9ca482295d7caee8535d1869f99efa2358177b64f5cd',
Hector Dearmanadf51172021-04-09 16:09:45 +010075 'linux', 'x64'),
Primiano Tuccie6440102020-12-01 12:41:03 +010076 Dependency(
77 'buildtools/win/gn.exe',
Primiano Tuccid8afe872022-02-07 23:18:59 +000078 'https://storage.googleapis.com/perfetto/gn-win-1968-0725d782',
79 '001f777f023c7a6959c778fb3a6b6cfc63f6baef953410ecdeaec350fb12285b',
Hector Dearmanadf51172021-04-09 16:09:45 +010080 'windows', 'x64'),
Primiano Tucciae2879e2017-09-27 11:02:09 +090081
Matthew Clarkson63028d62019-10-10 15:48:23 +010082 # clang-format
Primiano Tucci15968b52020-10-28 15:30:40 +010083 # From https://chromium.googlesource.com/chromium/src/buildtools/+/refs/heads/master/mac/clang-format.sha1
Primiano Tucci43f111a2020-07-27 20:37:52 +020084 Dependency(
85 'buildtools/mac/clang-format',
Primiano Tucci4e8d6c92020-09-23 14:40:33 +020086 'https://storage.googleapis.com/chromium-clang-format/62bde1baa7196ad9df969fc1f06b66360b1a927b',
Primiano Tucci15968b52020-10-28 15:30:40 +010087 '6df686a937443cbe6efc013467a7ba5f98d3f187eb7765bb7abc6ce47626cf66',
Primiano Tucci0e7915d2022-02-08 15:25:49 +000088 'darwin', 'all'),
Primiano Tucci15968b52020-10-28 15:30:40 +010089 # From https://chromium.googlesource.com/chromium/src/buildtools/+/refs/heads/master/linux64/clang-format.sha1
Primiano Tucci43f111a2020-07-27 20:37:52 +020090 Dependency(
91 'buildtools/linux64/clang-format',
Primiano Tucci4e8d6c92020-09-23 14:40:33 +020092 'https://storage.googleapis.com/chromium-clang-format/1baf0089e895c989a311b6a38ed94d0e8be4c0a7',
Primiano Tucci15968b52020-10-28 15:30:40 +010093 'd02a97a87e8c28898033aaf5986967b24dc47ebd5b376e1cd93e5009f22cd75e',
Hector Dearmanadf51172021-04-09 16:09:45 +010094 'linux', 'x64'),
Primiano Tuccie6440102020-12-01 12:41:03 +010095 # From https://chromium.googlesource.com/chromium/src/buildtools/+/refs/heads/master/win/clang-format.exe.sha1
96 Dependency(
97 'buildtools/win/clang-format.exe',
98 'https://storage.googleapis.com/chromium-clang-format/d4afd4eba27022f5f6d518133aebde57281677c9',
99 '2ba1b4d3ade90ea80316890b598ab5fc16777572be26afec6ce23117da121b80',
Hector Dearmanadf51172021-04-09 16:09:45 +0100100 'windows', 'x64'),
Primiano Tucci15968b52020-10-28 15:30:40 +0100101
Matthew Clarkson63028d62019-10-10 15:48:23 +0100102 # Keep the SHA1 in sync with |clang_format_rev| in chromium //buildtools/DEPS.
Primiano Tucci43f111a2020-07-27 20:37:52 +0200103 Dependency(
104 'buildtools/clang_format/script',
105 'https://chromium.googlesource.com/chromium/llvm-project/cfe/tools/clang-format.git',
Hector Dearmanadf51172021-04-09 16:09:45 +0100106 '96636aa0e9f047f17447f2d45a094d0b59ed7917', 'all', 'all'),
Primiano Tucci104bd6d2017-10-12 00:10:24 +0200107
Matthew Clarkson63028d62019-10-10 15:48:23 +0100108 # Ninja
Primiano Tucci43f111a2020-07-27 20:37:52 +0200109 Dependency(
110 'buildtools/mac/ninja',
Primiano Tucci0e7915d2022-02-08 15:25:49 +0000111 'https://storage.googleapis.com/perfetto/ninja-mac-x64_and_arm64-182',
112 '36e8b7aaa06911e1334feb664dd731a1cd69a15eb916a231a3d10ff65fca2c73',
113 'darwin', 'all'),
Primiano Tucci43f111a2020-07-27 20:37:52 +0200114 Dependency(
115 'buildtools/linux64/ninja',
Primiano Tucci0e7915d2022-02-08 15:25:49 +0000116 'https://storage.googleapis.com/perfetto/ninja-linux64-182',
Primiano Tucci15968b52020-10-28 15:30:40 +0100117 '54ac6a01362190aaabf4cf276f9c8982cdf11b225438940fdde3339be0f2ecdc',
Hector Dearmanadf51172021-04-09 16:09:45 +0100118 'linux', 'x64'),
Primiano Tuccie6440102020-12-01 12:41:03 +0100119 Dependency(
120 'buildtools/win/ninja.exe',
Primiano Tucci0e7915d2022-02-08 15:25:49 +0000121 'https://storage.googleapis.com/perfetto/ninja-win-182',
122 '09ced0fcd1a4dec7d1b798a2cf9ce5d20e5d2fbc2337343827f192ce47d0f491',
Hector Dearmanadf51172021-04-09 16:09:45 +0100123 'windows', 'x64'),
Primiano Tucciae2879e2017-09-27 11:02:09 +0900124
Florian Mayer54c4e382021-04-20 15:51:23 +0100125 # Keep the revision in sync with Chrome's PACKAGE_VERSION in
126 # tools/clang/scripts/update.py.
127 Dependency(
128 'buildtools/linux64/clang.tgz',
Primiano Tucci8652a852021-09-08 14:56:19 +0100129 'https://commondatastorage.googleapis.com/chromium-browser-clang/Linux_x64/clang-llvmorg-14-init-3191-g0e03450a-1.tgz',
130 'dd7479d43ce61401e057a5dee8b7e32bc2bd0d0e15d4f46c6858daf9170c9978',
Florian Mayer54c4e382021-04-20 15:51:23 +0100131 'linux', 'x64'),
132 Dependency(
133 'buildtools/win/clang.tgz',
Primiano Tucci434f1f82021-10-05 10:17:22 +0100134 'https://commondatastorage.googleapis.com/chromium-browser-clang/Win/clang-llvmorg-14-init-3191-g0e03450a-1.tgz',
135 '4292d191742e7120200c63224f02e1f2f2a7be8b57c0f18edf6ca0955bdd43df',
Florian Mayer54c4e382021-04-20 15:51:23 +0100136 'windows', 'x64'),
137]
138
139BUILD_DEPS_HOST = [
Matthew Clarkson63028d62019-10-10 15:48:23 +0100140 # Keep in sync with Android's //external/googletest/README.version.
Primiano Tucci43f111a2020-07-27 20:37:52 +0200141 Dependency(
Primiano Tucci15968b52020-10-28 15:30:40 +0100142 'buildtools/googletest',
143 'https://android.googlesource.com/platform/external/googletest.git',
Hector Dearmanadf51172021-04-09 16:09:45 +0100144 '3f05f651ae3621db58468153e32016bc1397800b', 'all', 'all'),
Primiano Tuccid7750452017-09-29 14:38:51 +0100145
Primiano Tuccif550b252019-12-03 11:06:02 +0000146 # Keep in sync with Chromium's //third_party/protobuf.
Primiano Tucci43f111a2020-07-27 20:37:52 +0200147 Dependency(
Primiano Tucci15968b52020-10-28 15:30:40 +0100148 'buildtools/protobuf',
149 'https://chromium.googlesource.com/external/github.com/google/protobuf.git',
150 '6a59a2ad1f61d9696092f79b6d74368b4d7970a3', # refs/tags/v3.9.0
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000151 'all',
152 'all'),
Primiano Tuccid7750452017-09-29 14:38:51 +0100153
Matthew Clarkson63028d62019-10-10 15:48:23 +0100154 # libc++, libc++abi and libunwind for Linux where we need to rebuild the C++
155 # lib from sources. Keep the SHA1s in sync with Chrome's src/buildtools/DEPS.
Primiano Tucci43f111a2020-07-27 20:37:52 +0200156 Dependency(
157 'buildtools/libcxx',
Primiano Tucci4e8d6c92020-09-23 14:40:33 +0200158 'https://chromium.googlesource.com/external/github.com/llvm/llvm-project/libcxx.git',
Hector Dearmanadf51172021-04-09 16:09:45 +0100159 'd9040c75cfea5928c804ab7c235fed06a63f743a', 'all', 'all'),
Primiano Tucci43f111a2020-07-27 20:37:52 +0200160 Dependency(
161 'buildtools/libcxxabi',
Primiano Tucci4e8d6c92020-09-23 14:40:33 +0200162 'https://chromium.googlesource.com/external/github.com/llvm/llvm-project/libcxxabi.git',
Hector Dearmanadf51172021-04-09 16:09:45 +0100163 '196ba1aaa8ac285d94f4ea8d9836390a45360533', 'all', 'all'),
Primiano Tucci43f111a2020-07-27 20:37:52 +0200164 Dependency(
165 'buildtools/libunwind',
Primiano Tucci4e8d6c92020-09-23 14:40:33 +0200166 'https://chromium.googlesource.com/external/github.com/llvm/llvm-project/libunwind.git',
Hector Dearmanadf51172021-04-09 16:09:45 +0100167 'd999d54f4bca789543a2eb6c995af2d9b5a1f3ed', 'all', 'all'),
Hector Dearman88a10112017-10-12 11:07:10 +0100168
Matthew Clarkson63028d62019-10-10 15:48:23 +0100169 # Keep in sync with chromium DEPS.
Primiano Tucci43f111a2020-07-27 20:37:52 +0200170 Dependency(
171 'buildtools/libfuzzer',
172 'https://chromium.googlesource.com/chromium/llvm-project/compiler-rt/lib/fuzzer.git',
Hector Dearmanadf51172021-04-09 16:09:45 +0100173 'debe7d2d1982e540fbd6bd78604bf001753f9e74', 'linux', 'all'),
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000174
Matthew Clarkson63028d62019-10-10 15:48:23 +0100175 # Benchmarking tool.
Primiano Tucci15968b52020-10-28 15:30:40 +0100176 Dependency(
177 'buildtools/benchmark',
178 'https://chromium.googlesource.com/external/github.com/google/benchmark.git',
Hector Dearmanadf51172021-04-09 16:09:45 +0100179 '090faecb454fbd6e6e17a75ef8146acb037118d4', 'all', 'all'),
Primiano Tucci38faa6f2018-04-01 20:12:08 +0200180
Matthew Clarkson63028d62019-10-10 15:48:23 +0100181 # Libbacktrace, for stacktraces in Linux/Android debug builds.
Primiano Tucci15968b52020-10-28 15:30:40 +0100182 # From https://github.com/ianlancetaylor/libbacktrace/archive/177940370e4a6b2509e92a0aaa9749184e64af43.zip
Primiano Tucci43f111a2020-07-27 20:37:52 +0200183 Dependency(
184 'buildtools/libbacktrace.zip',
Primiano Tucci15968b52020-10-28 15:30:40 +0100185 'https://storage.googleapis.com/perfetto/libbacktrace-177940370e4a6b2509e92a0aaa9749184e64af43.zip',
186 '21ac9a4209f7aeef766c482be53a7fa365063c031c7077e2070b491202983b31',
Hector Dearmanadf51172021-04-09 16:09:45 +0100187 'all', 'all'),
Lalit Maganti6fe26e62018-05-23 12:14:38 +0100188
Matthew Clarkson63028d62019-10-10 15:48:23 +0100189 # Sqlite for the trace processing library.
190 # This is the amalgamated source whose compiled output is meant to be faster.
Primiano Tucci15968b52020-10-28 15:30:40 +0100191 # We still pull the full source for the extensions (which are not available
192 # in the amalgamation).
Primiano Tucci43f111a2020-07-27 20:37:52 +0200193 Dependency(
194 'buildtools/sqlite.zip',
Lalit Magantif9a98e62021-04-09 14:17:37 +0100195 'https://storage.googleapis.com/perfetto/sqlite-amalgamation-3350400.zip',
196 'f3bf0df69f5de0675196f4644e05d07dbc698d674dc563a12eff17d5b215cdf5',
Hector Dearmanadf51172021-04-09 16:09:45 +0100197 'all', 'all'),
Primiano Tucci43f111a2020-07-27 20:37:52 +0200198 Dependency(
Primiano Tucci15968b52020-10-28 15:30:40 +0100199 'buildtools/sqlite_src',
200 'https://chromium.googlesource.com/external/github.com/sqlite/sqlite.git',
201 'ee3686eb50c0e3dbb087c9a0976f7e37e1b014ae', # refs/tags/version-3.32.3.
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000202 'all',
203 'all'),
Primiano Tucci0d72a312018-08-07 14:42:45 +0100204
Matthew Clarkson63028d62019-10-10 15:48:23 +0100205 # JsonCpp for legacy json import. Used only by the trace processor in
206 # standalone builds.
Primiano Tucci43f111a2020-07-27 20:37:52 +0200207 Dependency(
Primiano Tucci15968b52020-10-28 15:30:40 +0100208 'buildtools/jsoncpp',
209 'https://chromium.googlesource.com/external/github.com/open-source-parsers/jsoncpp.git',
210 '6aba23f4a8628d599a9ef7fa4811c4ff6e4070e2', # refs/tags/1.9.3.
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000211 'all',
212 'all'),
Florian Mayer475bd7e2018-08-07 20:04:03 +0100213
Ryan Savitskie65c4052022-03-24 18:22:19 +0000214 # Archive with only the demangling sources from llvm-project.
215 # See tools/repackage_llvm_demangler.sh on how to update this.
216 # File suffix is the git reference to the commit at which we rearchived the
217 # sources, as hosted on https://llvm.googlesource.com/llvm-project.
218 # If updating the version, also update bazel/deps.bzl.
219 Dependency(
220 'buildtools/llvm-project.tgz',
221 'https://storage.googleapis.com/perfetto/llvm-project-3b4c59c156919902c785ce3cbae0eee2ee53064d.tgz',
222 'f4a52e7f36edd7cacc844d5ae0e5f60b6f57c5afc40683e99f295886c9ce8ff4',
223 'all', 'all'),
224
Matthew Clarkson63028d62019-10-10 15:48:23 +0100225 # These dependencies are for libunwindstack, which is used by src/profiling.
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700226 Dependency('buildtools/android-core',
Primiano Tucci43f111a2020-07-27 20:37:52 +0200227 'https://android.googlesource.com/platform/system/core.git',
Hector Dearmanadf51172021-04-09 16:09:45 +0100228 '9e6cef7f07d8c11b3ea820938aeb7ff2e9dbaa52', 'all', 'all'),
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100229 Dependency(
230 'buildtools/android-unwinding',
231 'https://android.googlesource.com/platform/system/unwinding.git',
Daniele Di Proietto06d11772022-01-14 11:19:34 +0000232 'b61aebb7f7afaad9b775a59dcd0ea6289fc4325a', 'all', 'all'),
Florian Mayere02b2722020-11-12 10:50:43 +0000233 Dependency('buildtools/android-logging',
234 'https://android.googlesource.com/platform/system/logging.git',
Hector Dearmanadf51172021-04-09 16:09:45 +0100235 '7b36b566c9113fc703d68f76e8f40c0c2432481c', 'all', 'all'),
Florian Mayere02b2722020-11-12 10:50:43 +0000236 Dependency('buildtools/android-libbase',
Florian Mayerfd78c442020-08-14 12:48:29 +0100237 'https://android.googlesource.com/platform/system/libbase.git',
Hector Dearmanadf51172021-04-09 16:09:45 +0100238 '78f1c2f83e625bdf66d55b48bdb3a301c20d2fb3', 'all', 'all'),
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100239 Dependency(
240 'buildtools/android-libprocinfo',
241 'https://android.googlesource.com/platform/system/libprocinfo.git',
Hector Dearmanadf51172021-04-09 16:09:45 +0100242 'fd214c13ededecae97a3b15b5fccc8925a749a84', 'all', 'all'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700243 Dependency('buildtools/lzma',
Primiano Tucci43f111a2020-07-27 20:37:52 +0200244 'https://android.googlesource.com/platform/external/lzma.git',
Hector Dearmanadf51172021-04-09 16:09:45 +0100245 '7851dce6f4ca17f5caa1c93a4e0a45686b1d56c3', 'all', 'all'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700246 Dependency('buildtools/zlib',
Primiano Tucci43f111a2020-07-27 20:37:52 +0200247 'https://android.googlesource.com/platform/external/zlib.git',
Hector Dearmanadf51172021-04-09 16:09:45 +0100248 '5c85a2da4c13eda07f69d81a1579a5afddd35f59', 'all', 'all'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700249 Dependency('buildtools/bionic',
Primiano Tucci43f111a2020-07-27 20:37:52 +0200250 'https://android.googlesource.com/platform/bionic.git',
Hector Dearmanadf51172021-04-09 16:09:45 +0100251 '332065d57e734b65f56474d136d22d767e36cbcd', 'all', 'all'),
Florian Mayer475bd7e2018-08-07 20:04:03 +0100252
Matthew Clarkson63028d62019-10-10 15:48:23 +0100253 # Linenoise, used only by trace_processor in standalone builds.
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700254 Dependency('buildtools/linenoise',
Primiano Tucci43f111a2020-07-27 20:37:52 +0200255 'https://fuchsia.googlesource.com/third_party/linenoise.git',
Hector Dearmanadf51172021-04-09 16:09:45 +0100256 'c894b9e59f02203dbe4e2be657572cf88c4230c3', 'all', 'all'),
Hector Dearman74572bd2021-07-07 15:34:13 +0100257
258 # Bloaty, used to investigate binary size
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000259 Dependency(
260 'buildtools/bloaty.zip',
261 'https://storage.googleapis.com/perfetto/bloaty-1.1-b3b829de35babc2fe831b9488ad2e50bca939412-mac.zip',
262 '2d301bd72a20e3f42888c9274ceb4dca76c103608053572322412c2c65ab8cb8',
Primiano Tucci0e7915d2022-02-08 15:25:49 +0000263 'darwin', 'all'),
Primiano Tuccid7750452017-09-29 14:38:51 +0100264]
265
266# Dependencies required to build Android code.
267# URLs and SHA1s taken from:
268# - https://dl.google.com/android/repository/repository-11.xml
269# - https://dl.google.com/android/repository/sys-img/android/sys-img.xml
270BUILD_DEPS_ANDROID = [
Matthew Clarkson63028d62019-10-10 15:48:23 +0100271 # Android NDK
Primiano Tucci43f111a2020-07-27 20:37:52 +0200272 Dependency(
273 'buildtools/ndk.zip',
Florian Mayer55926852021-02-17 14:42:32 +0000274 'https://dl.google.com/android/repository/android-ndk-r21e-darwin-x86_64.zip',
275 '437278103a3db12632c05b1be5c41bbb8522791a67e415cc54411a65366f499d',
Primiano Tucci0e7915d2022-02-08 15:25:49 +0000276 'darwin', 'all'),
Primiano Tucci43f111a2020-07-27 20:37:52 +0200277 Dependency(
278 'buildtools/ndk.zip',
Florian Mayer55926852021-02-17 14:42:32 +0000279 'https://dl.google.com/android/repository/android-ndk-r21e-linux-x86_64.zip',
280 'ad7ce5467e18d40050dc51b8e7affc3e635c85bd8c59be62de32352328ed467e',
Hector Dearmanadf51172021-04-09 16:09:45 +0100281 'linux', 'x64'),
Primiano Tuccid7750452017-09-29 14:38:51 +0100282]
Primiano Tucciae2879e2017-09-27 11:02:09 +0900283
Primiano Tuccid7750452017-09-29 14:38:51 +0100284# Dependencies required to run Android tests.
285TEST_DEPS_ANDROID = [
Matthew Clarkson63028d62019-10-10 15:48:23 +0100286 # Android emulator images.
Primiano Tucci15968b52020-10-28 15:30:40 +0100287 Dependency(
288 'buildtools/aosp-arm.zip',
289 'https://storage.googleapis.com/perfetto/aosp-02022018-arm.zip',
290 'f5c7a3a22ad7aa0bd14ba467e8697e1e917d306699bd25622aa4419a413b9b67',
Hector Dearmanadf51172021-04-09 16:09:45 +0100291 'all', 'all'),
Primiano Tucciae2879e2017-09-27 11:02:09 +0900292
Matthew Clarkson63028d62019-10-10 15:48:23 +0100293 # platform-tools.zip contains adb binaries.
Primiano Tucci43f111a2020-07-27 20:37:52 +0200294 Dependency(
295 'buildtools/android_sdk/platform-tools.zip',
296 'https://dl.google.com/android/repository/platform-tools_r26.0.0-darwin.zip',
Primiano Tucci15968b52020-10-28 15:30:40 +0100297 '98d392cbd21ca20d643c7e1605760cc49075611e317c534096b5564053f4ac8e',
Primiano Tucci0e7915d2022-02-08 15:25:49 +0000298 'darwin', 'all'),
Primiano Tucci43f111a2020-07-27 20:37:52 +0200299 Dependency(
300 'buildtools/android_sdk/platform-tools.zip',
301 'https://dl.google.com/android/repository/platform-tools_r26.0.0-linux.zip',
Primiano Tucci15968b52020-10-28 15:30:40 +0100302 '90208207521d85abf0d46e3374aa4e04b7aff74e4f355c792ac334de7a77e50b',
Hector Dearmanadf51172021-04-09 16:09:45 +0100303 'linux', 'x64'),
Primiano Tucci0825bc82017-09-28 18:50:23 +0100304
Matthew Clarkson63028d62019-10-10 15:48:23 +0100305 # Android emulator binaries.
Primiano Tucci43f111a2020-07-27 20:37:52 +0200306 Dependency(
307 'buildtools/emulator',
308 'https://android.googlesource.com/platform/prebuilts/android-emulator.git',
Hector Dearmanadf51172021-04-09 16:09:45 +0100309 '4b260028dc27bc92c39bee9129cb2ba839970956', 'all', 'x64'),
Primiano Tuccid7750452017-09-29 14:38:51 +0100310]
Primiano Tucciae2879e2017-09-27 11:02:09 +0900311
Primiano Tucci1c752c12018-10-23 09:27:19 +0100312# This variable is updated by tools/roll-catapult-trace-viewer.
Primiano Tucci15968b52020-10-28 15:30:40 +0100313CATAPULT_SHA256 = 'b30108e05268ce6c65bb4126b65f6bfac165d17f5c1fd285046e7e6fd76c209f'
Primiano Tucci1c752c12018-10-23 09:27:19 +0100314
Primiano Tuccib82f3572021-11-19 12:01:39 +0000315TYPEFACES_SHA256 = 'f5f78f8f4395db65cdf5fdc1bf51da65f2161e6d61a305091d6ea54d3094a1f0'
Hector Dearman6177b752019-01-24 10:17:32 +0000316
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100317UI_DEPS = [
Primiano Tucci43f111a2020-07-27 20:37:52 +0200318 Dependency(
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100319 'buildtools/mac/nodejs.tgz',
320 'https://storage.googleapis.com/chromium-nodejs/14.15.4/17ba7216e09de1bffb9dc80b7ec617a1cee40330',
321 'b81a466347d2ae34b1370b6681ba173e9fb082338170a41624b37be7a2052b7e',
Primiano Tucci0e7915d2022-02-08 15:25:49 +0000322 'darwin', 'all'),
Primiano Tucci43f111a2020-07-27 20:37:52 +0200323 Dependency(
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100324 'buildtools/linux64/nodejs.tgz',
325 'https://storage.googleapis.com/chromium-nodejs/14.15.4/b2e40ddbac04d05baafbb007f203c6663c9d4ca9',
326 '5aa88f1e2bf036950790277f3431634f64044ec78362f3e4f0dc8da28d61e9a4',
Hector Dearmanadf51172021-04-09 16:09:45 +0100327 'linux', 'x64'),
Primiano Tucci43f111a2020-07-27 20:37:52 +0200328 Dependency(
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100329 'buildtools/mac/emsdk.tgz',
330 'https://storage.googleapis.com/perfetto/emscripten-2.0.12-mac.tgz',
331 'aa125f8c8ff8a386d43e18c8ea0c98c875cc19160a899403e8967a5478f96f31',
Primiano Tucci0e7915d2022-02-08 15:25:49 +0000332 'darwin', 'all'),
Primiano Tucci43f111a2020-07-27 20:37:52 +0200333 Dependency(
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100334 'buildtools/linux64/emsdk.tgz',
335 'https://storage.googleapis.com/perfetto/emscripten-2.0.12-linux.tgz',
336 'bfff9fb0326363c12e19b542f27a5f12cedbfc310f30621dc497c9af51d2d2e3',
Hector Dearmanadf51172021-04-09 16:09:45 +0100337 'linux', 'x64'),
Primiano Tucci43f111a2020-07-27 20:37:52 +0200338 Dependency(
Primiano Tucci43f111a2020-07-27 20:37:52 +0200339 'buildtools/catapult_trace_viewer.tgz',
340 'https://storage.googleapis.com/perfetto/catapult_trace_viewer-%s.tar.gz'
Hector Dearmanadf51172021-04-09 16:09:45 +0100341 % CATAPULT_SHA256, CATAPULT_SHA256, 'all', 'all'),
Primiano Tucci43f111a2020-07-27 20:37:52 +0200342 Dependency(
343 'buildtools/typefaces.tgz',
344 'https://storage.googleapis.com/perfetto/typefaces-%s.tar.gz' %
Hector Dearmanadf51172021-04-09 16:09:45 +0100345 TYPEFACES_SHA256, TYPEFACES_SHA256, 'all', 'all')
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100346]
347
Primiano Tuccib428d492021-08-02 19:01:29 +0100348# Sysroots required to cross-compile Linux targets (linux-arm{,64}).
349# These are taken from Chromium's build/linux/sysroot_scripts/sysroots.json.
350BUILD_DEPS_LINUX_CROSS_SYSROOTS = [
Primiano Tuccib428d492021-08-02 19:01:29 +0100351 Dependency(
352 'buildtools/debian_sid_arm-sysroot.tgz',
353 'https://commondatastorage.googleapis.com/chrome-linux-sysroot/toolchain/11d6f690ca49e8ba01a1d8c5346cedad2cf308fd/debian_sid_arm_sysroot.tar.xz',
354 'ff192fe073d140d836c9ca1e68f7200d558bb9aa6c5c8f4f76f794f82890f99a',
355 'linux', 'all'),
356 Dependency(
357 'buildtools/debian_sid_arm64-sysroot.tgz',
358 'https://commondatastorage.googleapis.com/chrome-linux-sysroot/toolchain/2befe8ce3e88be6080e4fb7e6d412278ea6a7625/debian_sid_arm64_sysroot.tar.xz',
359 'e4389eab2fe363f3fbdfa4d3ce9d94457d78fd2c0e62171a7534867623eadc90',
360 'linux', 'all'),
361]
362
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000363ALL_DEPS = (
364 BUILD_DEPS_HOST + BUILD_DEPS_ANDROID + BUILD_DEPS_LINUX_CROSS_SYSROOTS +
365 TEST_DEPS_ANDROID + UI_DEPS)
Primiano Tuccib428d492021-08-02 19:01:29 +0100366
Primiano Tucciae2879e2017-09-27 11:02:09 +0900367ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Primiano Tucci69132a12020-02-07 22:33:06 +0000368UI_DIR = os.path.join(ROOT_DIR, 'ui')
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100369TOOLS_DIR = os.path.join(ROOT_DIR, 'tools')
Primiano Tucci69132a12020-02-07 22:33:06 +0000370NODE_MODULES_STATUS_FILE = os.path.join(UI_DIR, 'node_modules', '.last_install')
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000371TEST_DATA_SCRIPT = os.path.join(TOOLS_DIR, 'test_data')
Primiano Tucciae2879e2017-09-27 11:02:09 +0900372
373
Primiano Tucci585b7e82020-05-14 11:19:10 +0100374def DownloadURL(url, out_file):
Primiano Tucci9ff392c2020-05-15 22:35:38 +0100375 subprocess.check_call(['curl', '-L', '-#', '-o', out_file, url])
Primiano Tucci585b7e82020-05-14 11:19:10 +0100376
377
Hector Dearmanadf51172021-04-09 16:09:45 +0100378def GetArch():
379 arch = machine()
Primiano Tucci0e7915d2022-02-08 15:25:49 +0000380 if arch == 'arm64':
381 return 'arm64'
Hector Dearmanadf51172021-04-09 16:09:45 +0100382 else:
383 # Assume everything else is x64 matching previous behaviour.
384 return 'x64'
385
386
Primiano Tucciae2879e2017-09-27 11:02:09 +0900387def ReadFile(path):
388 if not os.path.exists(path):
389 return None
390 with open(path) as f:
Matthew Clarkson63028d62019-10-10 15:48:23 +0100391 return f.read().strip()
Primiano Tucciae2879e2017-09-27 11:02:09 +0900392
393
Primiano Tucci0825bc82017-09-28 18:50:23 +0100394def MkdirRecursive(path):
395 # Works with both relative and absolute paths
396 cwd = '/' if path.startswith('/') else ROOT_DIR
397 for part in path.split('/'):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900398 cwd = os.path.join(cwd, part)
399 if not os.path.exists(cwd):
400 os.makedirs(cwd)
401 else:
Matthew Clarkson63028d62019-10-10 15:48:23 +0100402 assert (os.path.isdir(cwd))
Primiano Tucciae2879e2017-09-27 11:02:09 +0900403
404
405def HashLocalFile(path):
406 if not os.path.exists(path):
407 return None
408 with open(path, 'rb') as f:
Primiano Tucci15968b52020-10-28 15:30:40 +0100409 return hashlib.sha256(f.read()).hexdigest()
Primiano Tucciae2879e2017-09-27 11:02:09 +0900410
411
412def ExtractZipfilePreservePermissions(zf, info, path):
413 zf.extract(info.filename, path=path)
414 target_path = os.path.join(path, info.filename)
415 min_acls = 0o755 if info.filename.endswith('/') else 0o644
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +0100416 os.chmod(target_path, (info.external_attr >> 16) | min_acls)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900417
418
Primiano Tucci0825bc82017-09-28 18:50:23 +0100419def IsGitRepoCheckoutOutAtRevision(path, revision):
420 return ReadFile(os.path.join(path, '.git', 'HEAD')) == revision
421
422
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100423def RmtreeIfExists(path):
Primiano Tucci857ed732020-12-19 18:11:42 +0100424 # Git creates read-only files on windows, which cause failures with rmtree.
425 # This seems the socially accepted way to deal with it.
426 # See https://bugs.python.org/issue19643 .
427 def del_read_only_for_windows(_action, name, _exc):
428 os.chmod(name, stat.S_IWRITE)
429 os.remove(name)
430
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100431 if not os.path.exists(path):
432 return
433 buildtools_path = os.path.abspath(os.path.join(ROOT_DIR, 'buildtools'))
Florian Mayer83d683f2021-01-29 16:57:16 +0000434 test_path = os.path.abspath(os.path.join(ROOT_DIR, 'test', 'data'))
435 if (not os.path.abspath(path).startswith(buildtools_path) and
436 not os.path.abspath(path).startswith(test_path)):
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100437 # Safety check to prevent that some merge confilct ends up doing some
438 # rm -rf / or similar.
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000439 logging.fatal('Cannot remove %s: outside of buildtools and test/data', path)
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100440 sys.exit(1)
441 logging.info('Removing %s' % path)
442 shutil.rmtree(path, onerror=del_read_only_for_windows)
443
444
445def CheckoutGitRepo(path, git_url, revision, check_only):
446 if IsGitRepoCheckoutOutAtRevision(path, revision):
447 return False
448 if check_only:
449 return True
450 path = path.replace('/', os.sep)
451 RmtreeIfExists(path)
Primiano Tucci0825bc82017-09-28 18:50:23 +0100452 MkdirRecursive(path)
453 logging.info('Fetching %s @ %s into %s', git_url, revision, path)
Lalit Maganti367fcd52018-02-05 16:06:13 +0000454 subprocess.check_call(['git', 'init', path], cwd=path)
455 subprocess.check_call(
Matthew Clarkson63028d62019-10-10 15:48:23 +0100456 ['git', 'fetch', '--quiet', '--depth', '1', git_url, revision], cwd=path)
Primiano Tucci0825bc82017-09-28 18:50:23 +0100457 subprocess.check_call(['git', 'checkout', revision, '--quiet'], cwd=path)
Matthew Clarkson63028d62019-10-10 15:48:23 +0100458 assert (IsGitRepoCheckoutOutAtRevision(path, revision))
Eric Seckler676421f2019-02-12 14:43:31 +0000459 return True
Primiano Tucci0825bc82017-09-28 18:50:23 +0100460
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100461
Primiano Tucci43f111a2020-07-27 20:37:52 +0200462def InstallNodeModules(force_clean=False):
463 if force_clean:
464 node_modules = os.path.join(UI_DIR, 'node_modules')
465 logging.info('Clearing %s', node_modules)
466 subprocess.check_call(['git', 'clean', '-qxffd', node_modules],
467 cwd=ROOT_DIR)
Primiano Tucci73933a72021-06-04 00:14:38 +0100468 logging.info("Running `npm ci` in {0}".format(UI_DIR))
469 # `npm ci` is like `npm install` but respects package-lock.json.
470 subprocess.check_call([os.path.join(TOOLS_DIR, 'npm'), 'ci'], cwd=UI_DIR)
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100471 # pbjs has the bad habit of installing extra packages on its first run. Run
472 # it here, so we avoid fetches while building.
473 node_bin = os.path.join(TOOLS_DIR, 'node')
474 pbjs = [node_bin, 'node_modules/.bin/pbjs', '/dev/null', '-o', '/dev/null']
475 subprocess.call(pbjs, cwd=UI_DIR)
Primiano Tucci69132a12020-02-07 22:33:06 +0000476 with open(NODE_MODULES_STATUS_FILE, 'w') as f:
477 f.write(HashLocalFile(os.path.join(UI_DIR, 'package-lock.json')))
478
479
480def CheckNodeModules():
481 """Returns True if the modules are up-to-date.
482
483 There doesn't seem to be an easy way to check node modules versions. Instead
484 just check if package-lock.json changed since the last `npm install` call.
485 """
486 if not os.path.exists(NODE_MODULES_STATUS_FILE):
487 return False
488 with open(NODE_MODULES_STATUS_FILE, 'r') as f:
489 actual = f.read()
490 expected = HashLocalFile(os.path.join(UI_DIR, 'package-lock.json'))
491 return expected == actual
Primiano Tucci0825bc82017-09-28 18:50:23 +0100492
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100493
494def CheckHashes():
Primiano Tuccib428d492021-08-02 19:01:29 +0100495 for dep in ALL_DEPS:
496 if dep.source_url.endswith('.git'):
497 continue
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000498 logging.info('Downloading %s for %s-%s', dep.source_url, dep.target_os,
499 dep.target_arch)
Primiano Tuccib428d492021-08-02 19:01:29 +0100500 with tempfile.NamedTemporaryFile(delete=False) as f:
501 f.close()
502 DownloadURL(dep.source_url, f.name)
503 actual_checksum = HashLocalFile(f.name)
504 os.unlink(f.name)
505 if (actual_checksum != dep.checksum):
506 logging.fatal('SHA-256 mismatch for {} expected {} was {}'.format(
507 dep.source_url, dep.checksum, actual_checksum))
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100508
509
Primiano Tucciae2879e2017-09-27 11:02:09 +0900510def Main():
511 parser = argparse.ArgumentParser()
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000512 parser.add_argument(
513 '--android',
514 action='store_true',
Primiano Tuccib428d492021-08-02 19:01:29 +0100515 help='NDK and emulator images target_os="android"')
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000516 parser.add_argument(
517 '--linux-arm',
518 action='store_true',
Primiano Tuccib428d492021-08-02 19:01:29 +0100519 help='Debian sysroots for target_os="linux" target_cpu="arm|arm64"')
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000520 parser.add_argument(
521 '--ui',
522 action='store_true',
Primiano Tuccib428d492021-08-02 19:01:29 +0100523 help='Node and NPM packages to Build the Web-based UI via ./ui/build')
Primiano Tucci69132a12020-02-07 22:33:06 +0000524 parser.add_argument('--check-only')
Primiano Tuccicde1a8c2021-02-15 19:18:10 +0100525 parser.add_argument('--filter', default='')
Primiano Tucci69132a12020-02-07 22:33:06 +0000526 parser.add_argument('--verify', help='Check all URLs', action='store_true')
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000527 parser.add_argument(
528 '--no-toolchain', help='Do not download toolchain', action='store_true')
Primiano Tucciae2879e2017-09-27 11:02:09 +0900529 args = parser.parse_args()
Primiano Tucci69132a12020-02-07 22:33:06 +0000530 if args.verify:
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100531 CheckHashes()
532 return 0
Lalit Maganti0830b602021-06-11 15:11:42 +0100533
534 target_os = system().lower()
535 if args.ui and target_os == 'windows':
536 print('Building the UI on Windows is unsupported')
537 return 1
538
Primiano Tuccid7750452017-09-29 14:38:51 +0100539 deps = BUILD_DEPS_HOST
Florian Mayer54c4e382021-04-20 15:51:23 +0100540 if not args.no_toolchain:
541 deps += BUILD_DEPS_TOOLCHAIN_HOST
Primiano Tucci69132a12020-02-07 22:33:06 +0000542 if args.android:
Primiano Tuccid7750452017-09-29 14:38:51 +0100543 deps += BUILD_DEPS_ANDROID + TEST_DEPS_ANDROID
Primiano Tuccib428d492021-08-02 19:01:29 +0100544 if args.linux_arm:
545 deps += BUILD_DEPS_LINUX_CROSS_SYSROOTS
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100546 if args.ui:
547 deps += UI_DEPS
Eric Seckler6e828f32019-01-02 11:10:56 +0000548 deps_updated = False
Primiano Tucci43f111a2020-07-27 20:37:52 +0200549 nodejs_updated = False
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700550
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100551 for old_dir in CLEANUP_OLD_DIRS:
552 RmtreeIfExists(os.path.join(ROOT_DIR, old_dir))
553
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700554 for dep in deps:
Hector Dearmanadf51172021-04-09 16:09:45 +0100555 target_arch = GetArch()
556 matches_os = dep.target_os == 'all' or target_os == dep.target_os
557 matches_arch = dep.target_arch == 'all' or target_arch == dep.target_arch
558 if not matches_os or not matches_arch:
Primiano Tucciae2879e2017-09-27 11:02:09 +0900559 continue
Primiano Tuccicde1a8c2021-02-15 19:18:10 +0100560 if args.filter and args.filter not in dep.target_folder:
561 continue
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700562 local_path = os.path.join(ROOT_DIR, dep.target_folder)
563 if dep.source_url.endswith('.git'):
Primiano Tucci15968b52020-10-28 15:30:40 +0100564 deps_updated |= CheckoutGitRepo(local_path, dep.source_url, dep.checksum,
Primiano Tucci69132a12020-02-07 22:33:06 +0000565 args.check_only)
Primiano Tucci0825bc82017-09-28 18:50:23 +0100566 continue
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000567 is_zip = local_path.endswith('.zip') or local_path.endswith('.tgz')
Primiano Tucciae2879e2017-09-27 11:02:09 +0900568 zip_target_dir = local_path[:-4] if is_zip else None
569 zip_dir_stamp = os.path.join(zip_target_dir, '.stamp') if is_zip else None
570
Primiano Tucci15968b52020-10-28 15:30:40 +0100571 if ((not is_zip and HashLocalFile(local_path) == dep.checksum) or
572 (is_zip and ReadFile(zip_dir_stamp) == dep.checksum)):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900573 continue
Eric Seckler6e828f32019-01-02 11:10:56 +0000574 deps_updated = True
Primiano Tucci69132a12020-02-07 22:33:06 +0000575 if args.check_only:
576 continue
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700577 MkdirRecursive(os.path.dirname(dep.target_folder))
Primiano Tucci15968b52020-10-28 15:30:40 +0100578 if HashLocalFile(local_path) != dep.checksum:
Primiano Tucciae2879e2017-09-27 11:02:09 +0900579 download_path = local_path + '.tmp'
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700580 logging.info('Downloading %s from %s', local_path, dep.source_url)
581 DownloadURL(dep.source_url, download_path)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900582 os.chmod(download_path, 0o755)
Primiano Tucci15968b52020-10-28 15:30:40 +0100583 actual_checksum = HashLocalFile(download_path)
584 if (actual_checksum != dep.checksum):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900585 os.remove(download_path)
Primiano Tucci15968b52020-10-28 15:30:40 +0100586 logging.fatal('SHA-256 mismatch for {} expected {} was {}'.format(
587 download_path, dep.checksum, actual_checksum))
Primiano Tucciae2879e2017-09-27 11:02:09 +0900588 return 1
Primiano Tuccie6440102020-12-01 12:41:03 +0100589 shutil.move(download_path, local_path)
Primiano Tucci43f111a2020-07-27 20:37:52 +0200590 if 'nodejs' in dep.target_folder:
591 nodejs_updated = True
592
Primiano Tucci15968b52020-10-28 15:30:40 +0100593 assert (HashLocalFile(local_path) == dep.checksum)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900594
595 if is_zip:
596 logging.info('Extracting %s into %s' % (local_path, zip_target_dir))
Matthew Clarkson63028d62019-10-10 15:48:23 +0100597 assert (os.path.commonprefix((ROOT_DIR, zip_target_dir)) == ROOT_DIR)
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100598 RmtreeIfExists(zip_target_dir)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900599
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000600 # Decompress the archive.
601 if local_path.endswith('.tgz'):
602 MkdirRecursive(zip_target_dir)
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100603 subprocess.check_call(['tar', '-xf', local_path], cwd=zip_target_dir)
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000604 elif local_path.endswith('.zip'):
605 with zipfile.ZipFile(local_path, 'r') as zf:
606 for info in zf.infolist():
607 ExtractZipfilePreservePermissions(zf, info, zip_target_dir)
608
609 # If the zip contains one root folder, rebase one level up moving all
610 # its sub files and folders inside |target_dir|.
611 subdir = os.listdir(zip_target_dir)
612 if len(subdir) == 1:
613 subdir = os.path.join(zip_target_dir, subdir[0])
614 if os.path.isdir(subdir):
615 for subf in os.listdir(subdir):
Matthew Clarkson63028d62019-10-10 15:48:23 +0100616 shutil.move(os.path.join(subdir, subf), zip_target_dir)
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000617 os.rmdir(subdir)
618
619 # Create stamp and remove the archive.
620 with open(zip_dir_stamp, 'w') as stamp_file:
Primiano Tucci15968b52020-10-28 15:30:40 +0100621 stamp_file.write(dep.checksum)
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000622 os.remove(local_path)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900623
Deepanjan Royacf8c072018-07-13 11:37:04 -0400624 if args.ui:
625 # Needs to happen after nodejs is installed above.
Primiano Tucci69132a12020-02-07 22:33:06 +0000626 if args.check_only:
Primiano Tuccic1aecf42021-04-13 22:22:19 +0100627 deps_updated |= not CheckNodeModules()
Primiano Tucci69132a12020-02-07 22:33:06 +0000628 else:
Primiano Tucci43f111a2020-07-27 20:37:52 +0200629 InstallNodeModules(force_clean=nodejs_updated)
Primiano Tucci69132a12020-02-07 22:33:06 +0000630
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000631 cur_python_interpreter = sys.executable
Primiano Tucci856f2be2022-03-17 17:25:12 +0000632 test_data_synced = 0 == subprocess.call([
633 cur_python_interpreter, TEST_DATA_SCRIPT, 'status', '--quiet',
634 '--ignore-new'
635 ])
Primiano Tucci69132a12020-02-07 22:33:06 +0000636 if args.check_only:
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000637 if not deps_updated and test_data_synced:
Primiano Tucci69132a12020-02-07 22:33:06 +0000638 with open(args.check_only, 'w') as f:
639 f.write('OK') # The content is irrelevant, just keep GN happy.
640 return 0
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000641 argz = ' '.join(
642 [x for x in sys.argv[1:] if not x.startswith('--check-only')])
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100643 print('\033[91mBuild deps are stale. ' +
644 'Please run tools/install-build-deps %s\033[0m' % argz)
Primiano Tucci856f2be2022-03-17 17:25:12 +0000645 if not test_data_synced:
646 print('//test/data/ is out of sync. `tools/test_data status` for details')
Primiano Tucci69132a12020-02-07 22:33:06 +0000647 return 1
Primiano Tucciae2879e2017-09-27 11:02:09 +0900648
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000649 if not test_data_synced:
Primiano Tucci54418902021-11-06 12:09:48 +0000650 cmd = [cur_python_interpreter, TEST_DATA_SCRIPT, 'download', '--overwrite']
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000651 if not sys.stdout.isatty():
652 cmd += ['--verbose'] # For CI bots
653 subprocess.check_call(cmd)
654
Eric Seckler6e828f32019-01-02 11:10:56 +0000655 if deps_updated:
656 # Stale binary files may be compiled against old sysroot headers that aren't
657 # tracked by gn.
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +0100658 logging.warning('Remember to run "gn clean <output_directory>" ' +
659 'to avoid stale binary files.')
Eric Seckler6e828f32019-01-02 11:10:56 +0000660
Matthew Clarkson63028d62019-10-10 15:48:23 +0100661
Primiano Tucciae2879e2017-09-27 11:02:09 +0900662if __name__ == '__main__':
663 logging.basicConfig(level=logging.INFO)
664 sys.exit(Main())