blob: fd17b814dfb7153ed9b3ff94a4dec640c6ec469a [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
Lalit Magantibf99dd32023-02-07 23:50:48 +000017import dataclasses as dc
Primiano Tucciae2879e2017-09-27 11:02:09 +090018import hashlib
19import logging
20import os
21import shutil
Primiano Tucci0825bc82017-09-28 18:50:23 +010022import subprocess
Primiano Tucci857ed732020-12-19 18:11:42 +010023import stat
Primiano Tucciae2879e2017-09-27 11:02:09 +090024import sys
Sami Kyostilac7ddac72019-06-05 21:43:40 +010025import tempfile
Primiano Tucci16fed022023-05-05 17:11:32 +010026import time
Primiano Tucciae2879e2017-09-27 11:02:09 +090027import zipfile
Hector Dearmane1981602023-06-16 10:28:12 +010028import bz2
Primiano Tucciae2879e2017-09-27 11:02:09 +090029
Primiano Tuccid7750452017-09-29 14:38:51 +010030from collections import namedtuple
Hector Dearmanadf51172021-04-09 16:09:45 +010031from platform import system, machine
Primiano Tuccid7750452017-09-29 14:38:51 +010032
Lalit Magantibf99dd32023-02-07 23:50:48 +000033
Primiano Tuccib60d4b02017-11-10 11:03:00 +000034# The format for the deps below is the following:
Primiano Tuccib428d492021-08-02 19:01:29 +010035# (target_folder, source_url, sha1, target_os, target_arch)
Primiano Tuccib60d4b02017-11-10 11:03:00 +000036# |source_url| can be either a git repo or a http url.
Primiano Tucci15968b52020-10-28 15:30:40 +010037# If a git repo, |checksum| is the SHA1 committish that will be checked out.
38# If a http url, |checksum| is the SHA256 of the downloaded file.
Hector Dearmane1981602023-06-16 10:28:12 +010039# If the url is a .zip, .tgz, or .tbz2 file it will be automatically deflated under
Primiano Tuccib60d4b02017-11-10 11:03:00 +000040# |target_folder|, taking care of stripping the root folder if it's a single
41# root (to avoid ending up with buildtools/protobuf/protobuf-1.2.3/... and have
42# instead just buildtools/protobuf).
Hector Dearmanadf51172021-04-09 16:09:45 +010043# |target_os| is either 'darwin', 'linux', 'windows' or 'all'
Primiano Tucci0e7915d2022-02-08 15:25:49 +000044# |target_arch| is either 'x64', 'arm64' or 'all'
Hector Dearmanadf51172021-04-09 16:09:45 +010045# in both cases the dep only applies on matching platforms
46# |target_arch| can be 'all' when 'target_os' is not 'all' for example in the
47# case of MacOS universal binaries.
Lalit Magantibf99dd32023-02-07 23:50:48 +000048@dc.dataclass
49class Dependency:
50 target_folder: str
51 source_url: str
52 checksum: str
53 target_os: str
54 target_arch: str
55 submodules: bool = False
56
Primiano Tuccib60d4b02017-11-10 11:03:00 +000057
Primiano Tucci7e9865c2021-01-10 23:55:15 +010058# This is to remove old directories when build tools get {re,}moved. This is to
59# avoid accidentally referring to stale dir in custom user scripts.
60CLEANUP_OLD_DIRS = [
61 'buildtools/nodejs', # Moved to buildtools/{mac,linux64}/nodejs
62 'buildtools/emsdk', # Moved to buildtools/{mac,linux64}/emsdk
63 'buildtools/test_data', # Moved to test/data by r.android.com/1539381 .
64 'buildtools/d8', # Removed by r.android.com/1424334 .
Primiano Tuccia0b1bc22023-05-04 15:06:58 +010065
Hector Dearmand3109302023-12-07 20:32:05 +000066 # Build tools moved to third_party/ by r.android.com/2327602 .
Primiano Tuccia0b1bc22023-05-04 15:06:58 +010067 'buildtools/mac/clang-format',
68 'buildtools/mac/gn',
69 'buildtools/mac/ninja',
70 'buildtools/linux64/clang-format',
71 'buildtools/linux64/gn',
72 'buildtools/linux64/ninja',
73 'buildtools/win/clang-format.exe',
74 'buildtools/win/gn.exe',
75 'buildtools/win/ninja.exe',
Primiano Tucci7e9865c2021-01-10 23:55:15 +010076]
77
Primiano Tuccid7750452017-09-29 14:38:51 +010078# Dependencies required to build code on the host or when targeting desktop OS.
Florian Mayer54c4e382021-04-20 15:51:23 +010079BUILD_DEPS_TOOLCHAIN_HOST = [
Primiano Tucci15968b52020-10-28 15:30:40 +010080 # GN. From https://chrome-infra-packages.appspot.com/dl/gn/gn/.
Primiano Tuccid8afe872022-02-07 23:18:59 +000081 # git_revision:0725d7827575b239594fbc8fd5192873a1d62f44 .
Primiano Tucci15968b52020-10-28 15:30:40 +010082 Dependency(
Primiano Tuccia0b1bc22023-05-04 15:06:58 +010083 'third_party/gn/gn',
Primiano Tuccid8afe872022-02-07 23:18:59 +000084 'https://storage.googleapis.com/perfetto/gn-mac-1968-0725d782',
85 '9ced623a664560bba38bbadb9b91158ca4186358c847e17ab7d982b351373c2e',
Hector Dearmanadf51172021-04-09 16:09:45 +010086 'darwin', 'x64'),
Primiano Tucci43f111a2020-07-27 20:37:52 +020087 Dependency(
Primiano Tuccia0b1bc22023-05-04 15:06:58 +010088 'third_party/gn/gn',
Primiano Tucci0e7915d2022-02-08 15:25:49 +000089 'https://storage.googleapis.com/perfetto/gn-mac-arm64-1968-0725d782',
90 'd22336b5210b4dad5e36e8c28ce81187f491822cf4d8fd0a257b30d6bee3fd3f',
91 'darwin', 'arm64'),
92 Dependency(
Primiano Tuccia0b1bc22023-05-04 15:06:58 +010093 'third_party/gn/gn',
Primiano Tuccid8afe872022-02-07 23:18:59 +000094 'https://storage.googleapis.com/perfetto/gn-linux64-1968-0725d782',
95 'f706aaa0676e3e22f5fc9ca482295d7caee8535d1869f99efa2358177b64f5cd',
Hector Dearmanadf51172021-04-09 16:09:45 +010096 'linux', 'x64'),
Primiano Tuccie6440102020-12-01 12:41:03 +010097 Dependency(
Hector Dearmand3109302023-12-07 20:32:05 +000098 'third_party/gn/gn',
99 'https://storage.googleapis.com/perfetto/gn-linux-arm64-1968-0725d782',
100 'c2a372cd4f911028d8bc351fbf24835c9b1194fcc92beadf6c5a2b3addae973c',
101 'linux', 'arm64'),
102 Dependency(
Primiano Tuccia0b1bc22023-05-04 15:06:58 +0100103 'third_party/gn/gn.exe',
Primiano Tuccid8afe872022-02-07 23:18:59 +0000104 'https://storage.googleapis.com/perfetto/gn-win-1968-0725d782',
105 '001f777f023c7a6959c778fb3a6b6cfc63f6baef953410ecdeaec350fb12285b',
Hector Dearmanadf51172021-04-09 16:09:45 +0100106 'windows', 'x64'),
Primiano Tucciae2879e2017-09-27 11:02:09 +0900107
Matthew Clarkson63028d62019-10-10 15:48:23 +0100108 # clang-format
Daniele Di Proiettoc9630362023-06-15 13:28:03 +0000109 # From
110 # https://chromium.googlesource.com/chromium/src/buildtools/+/refs/heads/master/mac/clang-format.arm64.sha1
Primiano Tucci43f111a2020-07-27 20:37:52 +0200111 Dependency(
Primiano Tuccia0b1bc22023-05-04 15:06:58 +0100112 'third_party/clang-format/clang-format',
Daniele Di Proiettoc9630362023-06-15 13:28:03 +0000113 'https://storage.googleapis.com/chromium-clang-format/5553d7a3d912b7d49381ad68c9a56740601a57a0',
114 'e077990b2ea6807f6abc71b4cf1e487719d7e40574baddd2b51187fdcc8db803',
115 'darwin', 'arm64'),
116 # From
117 # https://chromium.googlesource.com/chromium/src/buildtools/+/refs/heads/master/mac/clang-format.x64.sha1
118 Dependency(
119 'third_party/clang-format/clang-format',
120 'https://storage.googleapis.com/chromium-clang-format/87d69aeff220c916b85b5d6d162fa5668aa9d64f',
121 '71179a8788a009cfd589636d50f0eb9f95f58b0cfda4351430bed7c0a48c080b',
122 'darwin', 'x64'),
Primiano Tucci15968b52020-10-28 15:30:40 +0100123 # From https://chromium.googlesource.com/chromium/src/buildtools/+/refs/heads/master/linux64/clang-format.sha1
Primiano Tucci43f111a2020-07-27 20:37:52 +0200124 Dependency(
Primiano Tuccia0b1bc22023-05-04 15:06:58 +0100125 'third_party/clang-format/clang-format',
Daniele Di Proiettoc9630362023-06-15 13:28:03 +0000126 'https://storage.googleapis.com/chromium-clang-format/1facab3101fc6da6b9467543f27f0622b966bc19',
127 '5e459118d8ac825452e9e1f2717e4de5a36399dc6cc6aec7ec483ad27a0c927e',
Hector Dearmanadf51172021-04-09 16:09:45 +0100128 'linux', 'x64'),
Primiano Tuccie6440102020-12-01 12:41:03 +0100129 # From https://chromium.googlesource.com/chromium/src/buildtools/+/refs/heads/master/win/clang-format.exe.sha1
130 Dependency(
Primiano Tuccia0b1bc22023-05-04 15:06:58 +0100131 'third_party/clang-format/clang-format.exe',
Daniele Di Proiettoc9630362023-06-15 13:28:03 +0000132 'https://storage.googleapis.com/chromium-clang-format/2e569921b9884daf157021d6ae9e21991cd6cf81',
133 '2227376ada89ea832995b832222b722a27c4d5d8d59e9c4d7842877f99a1e30d',
Hector Dearmanadf51172021-04-09 16:09:45 +0100134 'windows', 'x64'),
Primiano Tucci15968b52020-10-28 15:30:40 +0100135
Matthew Clarkson63028d62019-10-10 15:48:23 +0100136 # Keep the SHA1 in sync with |clang_format_rev| in chromium //buildtools/DEPS.
Primiano Tucci43f111a2020-07-27 20:37:52 +0200137 Dependency(
138 'buildtools/clang_format/script',
Daniele Di Proiettoc9630362023-06-15 13:28:03 +0000139 'https://chromium.googlesource.com/external/github.com/llvm/llvm-project/clang/tools/clang-format.git',
140 'f97059df7f8b205064625cdb5f97b56668a125ef', 'all', 'all'),
Primiano Tucci104bd6d2017-10-12 00:10:24 +0200141
Matthew Clarkson63028d62019-10-10 15:48:23 +0100142 # Ninja
Primiano Tucci43f111a2020-07-27 20:37:52 +0200143 Dependency(
Primiano Tuccia0b1bc22023-05-04 15:06:58 +0100144 'third_party/ninja/ninja',
Primiano Tucci0e7915d2022-02-08 15:25:49 +0000145 'https://storage.googleapis.com/perfetto/ninja-mac-x64_and_arm64-182',
146 '36e8b7aaa06911e1334feb664dd731a1cd69a15eb916a231a3d10ff65fca2c73',
147 'darwin', 'all'),
Primiano Tucci43f111a2020-07-27 20:37:52 +0200148 Dependency(
Primiano Tuccia0b1bc22023-05-04 15:06:58 +0100149 'third_party/ninja/ninja',
Primiano Tucci0e7915d2022-02-08 15:25:49 +0000150 'https://storage.googleapis.com/perfetto/ninja-linux64-182',
Primiano Tucci15968b52020-10-28 15:30:40 +0100151 '54ac6a01362190aaabf4cf276f9c8982cdf11b225438940fdde3339be0f2ecdc',
Hector Dearmanadf51172021-04-09 16:09:45 +0100152 'linux', 'x64'),
Primiano Tuccie6440102020-12-01 12:41:03 +0100153 Dependency(
Primiano Tuccia0b1bc22023-05-04 15:06:58 +0100154 'third_party/ninja/ninja.exe',
Primiano Tucci0e7915d2022-02-08 15:25:49 +0000155 'https://storage.googleapis.com/perfetto/ninja-win-182',
156 '09ced0fcd1a4dec7d1b798a2cf9ce5d20e5d2fbc2337343827f192ce47d0f491',
Hector Dearmanadf51172021-04-09 16:09:45 +0100157 'windows', 'x64'),
Hector Dearmand3109302023-12-07 20:32:05 +0000158 Dependency(
159 'third_party/ninja/ninja',
160 'https://storage.googleapis.com/perfetto/ninja-linux-arm64-1111',
161 '05031a734ec4310a51b2cfe9f0096b26fce25ab4ff19e5b51abe6371de066cc5',
162 'linux', 'arm64'),
Primiano Tucciae2879e2017-09-27 11:02:09 +0900163
Florian Mayer54c4e382021-04-20 15:51:23 +0100164 # Keep the revision in sync with Chrome's PACKAGE_VERSION in
165 # tools/clang/scripts/update.py.
166 Dependency(
167 'buildtools/linux64/clang.tgz',
Lalit Magantibdddf762022-11-02 20:23:03 +0000168 'https://commondatastorage.googleapis.com/chromium-browser-clang/Linux_x64/clang-llvmorg-16-init-8697-g60809cd2-1.tgz',
169 '5ae35f85e0d32136795c6b223bf64263d46678dd4a24fea4e9039e58a32670de',
Florian Mayer54c4e382021-04-20 15:51:23 +0100170 'linux', 'x64'),
171 Dependency(
172 'buildtools/win/clang.tgz',
Lalit Magantibdddf762022-11-02 20:23:03 +0000173 'https://commondatastorage.googleapis.com/chromium-browser-clang/Win/clang-llvmorg-16-init-8697-g60809cd2-1.tgz',
174 '086faec822acba5b9c0308c6a8be34424031027d757efa2b81805aed18ffc521',
Florian Mayer54c4e382021-04-20 15:51:23 +0100175 'windows', 'x64'),
176]
177
178BUILD_DEPS_HOST = [
Daniele Di Proiettoce01ef12022-05-19 11:46:53 +0100179 # Keep in sync with Android's //external/googletest/METADATA.
Primiano Tucci43f111a2020-07-27 20:37:52 +0200180 Dependency(
Primiano Tucci15968b52020-10-28 15:30:40 +0100181 'buildtools/googletest',
182 'https://android.googlesource.com/platform/external/googletest.git',
Daniele Di Proiettoce01ef12022-05-19 11:46:53 +0100183 '609281088cfefc76f9d0ce82e1ff6c30cc3591e5', 'all', 'all'),
Primiano Tuccid7750452017-09-29 14:38:51 +0100184
Primiano Tuccif550b252019-12-03 11:06:02 +0000185 # Keep in sync with Chromium's //third_party/protobuf.
Primiano Tucci43f111a2020-07-27 20:37:52 +0200186 Dependency(
Primiano Tucci15968b52020-10-28 15:30:40 +0100187 'buildtools/protobuf',
Lalit Maganti0ec3ec32023-02-07 23:54:01 +0000188 'https://chromium.googlesource.com/external/github.com/protocolbuffers/protobuf.git',
189 'fe271ab76f2ad2b2b28c10443865d2af21e27e0e', # refs/tags/v3.20.3
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000190 'all',
191 'all'),
Primiano Tuccid7750452017-09-29 14:38:51 +0100192
Matthew Clarkson63028d62019-10-10 15:48:23 +0100193 # libc++, libc++abi and libunwind for Linux where we need to rebuild the C++
194 # lib from sources. Keep the SHA1s in sync with Chrome's src/buildtools/DEPS.
Primiano Tucci43f111a2020-07-27 20:37:52 +0200195 Dependency(
196 'buildtools/libcxx',
Primiano Tucci4e8d6c92020-09-23 14:40:33 +0200197 'https://chromium.googlesource.com/external/github.com/llvm/llvm-project/libcxx.git',
Lalit Magantibdddf762022-11-02 20:23:03 +0000198 'f8571eaba606bde2eb8cd34b30104ca33e7c207e', 'all', 'all'),
Primiano Tucci43f111a2020-07-27 20:37:52 +0200199 Dependency(
200 'buildtools/libcxxabi',
Primiano Tucci4e8d6c92020-09-23 14:40:33 +0200201 'https://chromium.googlesource.com/external/github.com/llvm/llvm-project/libcxxabi.git',
Lalit Magantibdddf762022-11-02 20:23:03 +0000202 '8dd405113a4f3694e910b79785dd7fb7535a888a', 'all', 'all'),
Primiano Tucci43f111a2020-07-27 20:37:52 +0200203 Dependency(
204 'buildtools/libunwind',
Primiano Tucci4e8d6c92020-09-23 14:40:33 +0200205 'https://chromium.googlesource.com/external/github.com/llvm/llvm-project/libunwind.git',
Lalit Magantibdddf762022-11-02 20:23:03 +0000206 'aabcd8753678f1536e15eb6385a948470debdae4', 'all', 'all'),
Hector Dearman88a10112017-10-12 11:07:10 +0100207
Matthew Clarkson63028d62019-10-10 15:48:23 +0100208 # Keep in sync with chromium DEPS.
Primiano Tucci43f111a2020-07-27 20:37:52 +0200209 Dependency(
210 'buildtools/libfuzzer',
211 'https://chromium.googlesource.com/chromium/llvm-project/compiler-rt/lib/fuzzer.git',
Hector Dearmanadf51172021-04-09 16:09:45 +0100212 'debe7d2d1982e540fbd6bd78604bf001753f9e74', 'linux', 'all'),
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000213
Matthew Clarkson63028d62019-10-10 15:48:23 +0100214 # Benchmarking tool.
Primiano Tucci15968b52020-10-28 15:30:40 +0100215 Dependency(
216 'buildtools/benchmark',
217 'https://chromium.googlesource.com/external/github.com/google/benchmark.git',
Lalit Maganti4329f232022-04-25 14:49:02 +0100218 'e991355c02b93fe17713efe04cbc2e278e00fdbd', 'all', 'all'),
Primiano Tucci38faa6f2018-04-01 20:12:08 +0200219
Matthew Clarkson63028d62019-10-10 15:48:23 +0100220 # Libbacktrace, for stacktraces in Linux/Android debug builds.
Primiano Tucci15968b52020-10-28 15:30:40 +0100221 # From https://github.com/ianlancetaylor/libbacktrace/archive/177940370e4a6b2509e92a0aaa9749184e64af43.zip
Primiano Tucci43f111a2020-07-27 20:37:52 +0200222 Dependency(
223 'buildtools/libbacktrace.zip',
Primiano Tucci15968b52020-10-28 15:30:40 +0100224 'https://storage.googleapis.com/perfetto/libbacktrace-177940370e4a6b2509e92a0aaa9749184e64af43.zip',
225 '21ac9a4209f7aeef766c482be53a7fa365063c031c7077e2070b491202983b31',
Hector Dearmanadf51172021-04-09 16:09:45 +0100226 'all', 'all'),
Lalit Maganti6fe26e62018-05-23 12:14:38 +0100227
Matthew Clarkson63028d62019-10-10 15:48:23 +0100228 # Sqlite for the trace processing library.
229 # This is the amalgamated source whose compiled output is meant to be faster.
Primiano Tucci15968b52020-10-28 15:30:40 +0100230 # We still pull the full source for the extensions (which are not available
231 # in the amalgamation).
Ryan Savitski31751112022-04-02 00:49:07 +0100232 # If updating the version, also update bazel/deps.bzl.
Primiano Tucci43f111a2020-07-27 20:37:52 +0200233 Dependency(
234 'buildtools/sqlite.zip',
Lalit Magantib7069142022-09-02 15:43:20 +0100235 'https://storage.googleapis.com/perfetto/sqlite-amalgamation-3390200.zip',
236 '87775784f8b22d0d0f1d7811870d39feaa7896319c7c20b849a4181c5a50609b',
Hector Dearmanadf51172021-04-09 16:09:45 +0100237 'all', 'all'),
Primiano Tucci43f111a2020-07-27 20:37:52 +0200238 Dependency(
Primiano Tucci15968b52020-10-28 15:30:40 +0100239 'buildtools/sqlite_src',
240 'https://chromium.googlesource.com/external/github.com/sqlite/sqlite.git',
Lalit Magantib7069142022-09-02 15:43:20 +0100241 '202b2a7b54ea2dd13a8a5adfd75523abe4dcf17f', # refs/tags/version-3.39.2.
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000242 'all',
243 'all'),
Primiano Tucci0d72a312018-08-07 14:42:45 +0100244
Matthew Clarkson63028d62019-10-10 15:48:23 +0100245 # JsonCpp for legacy json import. Used only by the trace processor in
246 # standalone builds.
Ryan Savitski31751112022-04-02 00:49:07 +0100247 # If updating the version, also update bazel/deps.bzl.
Primiano Tucci43f111a2020-07-27 20:37:52 +0200248 Dependency(
Primiano Tucci15968b52020-10-28 15:30:40 +0100249 'buildtools/jsoncpp',
250 'https://chromium.googlesource.com/external/github.com/open-source-parsers/jsoncpp.git',
251 '6aba23f4a8628d599a9ef7fa4811c4ff6e4070e2', # refs/tags/1.9.3.
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000252 'all',
253 'all'),
Florian Mayer475bd7e2018-08-07 20:04:03 +0100254
Ryan Savitskie65c4052022-03-24 18:22:19 +0000255 # Archive with only the demangling sources from llvm-project.
256 # See tools/repackage_llvm_demangler.sh on how to update this.
257 # File suffix is the git reference to the commit at which we rearchived the
258 # sources, as hosted on https://llvm.googlesource.com/llvm-project.
259 # If updating the version, also update bazel/deps.bzl.
260 Dependency(
261 'buildtools/llvm-project.tgz',
262 'https://storage.googleapis.com/perfetto/llvm-project-3b4c59c156919902c785ce3cbae0eee2ee53064d.tgz',
263 'f4a52e7f36edd7cacc844d5ae0e5f60b6f57c5afc40683e99f295886c9ce8ff4',
264 'all', 'all'),
265
Matthew Clarkson63028d62019-10-10 15:48:23 +0100266 # These dependencies are for libunwindstack, which is used by src/profiling.
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700267 Dependency('buildtools/android-core',
Primiano Tucci43f111a2020-07-27 20:37:52 +0200268 'https://android.googlesource.com/platform/system/core.git',
Hector Dearmanadf51172021-04-09 16:09:45 +0100269 '9e6cef7f07d8c11b3ea820938aeb7ff2e9dbaa52', 'all', 'all'),
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100270 Dependency(
271 'buildtools/android-unwinding',
272 'https://android.googlesource.com/platform/system/unwinding.git',
Daniele Di Proiettof75abb02023-06-30 16:24:19 +0000273 'c0fa0c327e58eda604b2fd00b81f1c12817542d3', 'all', 'all'),
Florian Mayere02b2722020-11-12 10:50:43 +0000274 Dependency('buildtools/android-logging',
275 'https://android.googlesource.com/platform/system/logging.git',
Hector Dearmanadf51172021-04-09 16:09:45 +0100276 '7b36b566c9113fc703d68f76e8f40c0c2432481c', 'all', 'all'),
Florian Mayere02b2722020-11-12 10:50:43 +0000277 Dependency('buildtools/android-libbase',
Florian Mayerfd78c442020-08-14 12:48:29 +0100278 'https://android.googlesource.com/platform/system/libbase.git',
Hector Dearmanadf51172021-04-09 16:09:45 +0100279 '78f1c2f83e625bdf66d55b48bdb3a301c20d2fb3', 'all', 'all'),
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100280 Dependency(
281 'buildtools/android-libprocinfo',
282 'https://android.googlesource.com/platform/system/libprocinfo.git',
Hector Dearmanadf51172021-04-09 16:09:45 +0100283 'fd214c13ededecae97a3b15b5fccc8925a749a84', 'all', 'all'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700284 Dependency('buildtools/lzma',
Primiano Tucci43f111a2020-07-27 20:37:52 +0200285 'https://android.googlesource.com/platform/external/lzma.git',
Hector Dearmanadf51172021-04-09 16:09:45 +0100286 '7851dce6f4ca17f5caa1c93a4e0a45686b1d56c3', 'all', 'all'),
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700287 Dependency('buildtools/bionic',
Primiano Tucci43f111a2020-07-27 20:37:52 +0200288 'https://android.googlesource.com/platform/bionic.git',
Daniele Di Proietto62a22b62022-10-26 08:53:11 +0000289 '4b0e16bc72a82a63c699977376a7d6eadca1b206', 'all', 'all'),
Florian Mayer475bd7e2018-08-07 20:04:03 +0100290
Ryan Savitski31751112022-04-02 00:49:07 +0100291 # Zlib used both in the tracing binaries, as well as the trace processor and
292 # assorted tools.
293 # If updating the version, also update bazel/deps.bzl.
294 Dependency('buildtools/zlib',
295 'https://android.googlesource.com/platform/external/zlib.git',
Lalit Magantifa957e72023-03-16 18:22:23 +0000296 '6d3f6aa0f87c9791ca7724c279ef61384f331dfd', 'all', 'all'),
Ryan Savitski31751112022-04-02 00:49:07 +0100297
Matthew Clarkson63028d62019-10-10 15:48:23 +0100298 # Linenoise, used only by trace_processor in standalone builds.
Ryan Savitski31751112022-04-02 00:49:07 +0100299 # If updating the version, also update bazel/deps.bzl.
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700300 Dependency('buildtools/linenoise',
Primiano Tucci43f111a2020-07-27 20:37:52 +0200301 'https://fuchsia.googlesource.com/third_party/linenoise.git',
Hector Dearmanadf51172021-04-09 16:09:45 +0100302 'c894b9e59f02203dbe4e2be657572cf88c4230c3', 'all', 'all'),
Hector Dearman74572bd2021-07-07 15:34:13 +0100303
304 # Bloaty, used to investigate binary size
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000305 Dependency(
306 'buildtools/bloaty.zip',
307 'https://storage.googleapis.com/perfetto/bloaty-1.1-b3b829de35babc2fe831b9488ad2e50bca939412-mac.zip',
308 '2d301bd72a20e3f42888c9274ceb4dca76c103608053572322412c2c65ab8cb8',
Primiano Tucci0e7915d2022-02-08 15:25:49 +0000309 'darwin', 'all'),
Primiano Tuccid7750452017-09-29 14:38:51 +0100310]
311
312# Dependencies required to build Android code.
313# URLs and SHA1s taken from:
314# - https://dl.google.com/android/repository/repository-11.xml
315# - https://dl.google.com/android/repository/sys-img/android/sys-img.xml
316BUILD_DEPS_ANDROID = [
Matthew Clarkson63028d62019-10-10 15:48:23 +0100317 # Android NDK
Primiano Tucci43f111a2020-07-27 20:37:52 +0200318 Dependency(
319 'buildtools/ndk.zip',
Florian Mayer55926852021-02-17 14:42:32 +0000320 'https://dl.google.com/android/repository/android-ndk-r21e-darwin-x86_64.zip',
321 '437278103a3db12632c05b1be5c41bbb8522791a67e415cc54411a65366f499d',
Primiano Tucci0e7915d2022-02-08 15:25:49 +0000322 'darwin', 'all'),
Primiano Tucci43f111a2020-07-27 20:37:52 +0200323 Dependency(
324 'buildtools/ndk.zip',
Florian Mayer55926852021-02-17 14:42:32 +0000325 'https://dl.google.com/android/repository/android-ndk-r21e-linux-x86_64.zip',
326 'ad7ce5467e18d40050dc51b8e7affc3e635c85bd8c59be62de32352328ed467e',
Hector Dearmanadf51172021-04-09 16:09:45 +0100327 'linux', 'x64'),
Primiano Tuccid7750452017-09-29 14:38:51 +0100328]
Primiano Tucciae2879e2017-09-27 11:02:09 +0900329
Primiano Tuccid7750452017-09-29 14:38:51 +0100330# Dependencies required to run Android tests.
331TEST_DEPS_ANDROID = [
Matthew Clarkson63028d62019-10-10 15:48:23 +0100332 # Android emulator images.
Primiano Tucci15968b52020-10-28 15:30:40 +0100333 Dependency(
334 'buildtools/aosp-arm.zip',
335 'https://storage.googleapis.com/perfetto/aosp-02022018-arm.zip',
336 'f5c7a3a22ad7aa0bd14ba467e8697e1e917d306699bd25622aa4419a413b9b67',
Hector Dearmanadf51172021-04-09 16:09:45 +0100337 'all', 'all'),
Primiano Tucciae2879e2017-09-27 11:02:09 +0900338
Matthew Clarkson63028d62019-10-10 15:48:23 +0100339 # platform-tools.zip contains adb binaries.
Primiano Tucci43f111a2020-07-27 20:37:52 +0200340 Dependency(
341 'buildtools/android_sdk/platform-tools.zip',
342 'https://dl.google.com/android/repository/platform-tools_r26.0.0-darwin.zip',
Primiano Tucci15968b52020-10-28 15:30:40 +0100343 '98d392cbd21ca20d643c7e1605760cc49075611e317c534096b5564053f4ac8e',
Primiano Tucci0e7915d2022-02-08 15:25:49 +0000344 'darwin', 'all'),
Primiano Tucci43f111a2020-07-27 20:37:52 +0200345 Dependency(
346 'buildtools/android_sdk/platform-tools.zip',
347 'https://dl.google.com/android/repository/platform-tools_r26.0.0-linux.zip',
Primiano Tucci15968b52020-10-28 15:30:40 +0100348 '90208207521d85abf0d46e3374aa4e04b7aff74e4f355c792ac334de7a77e50b',
Hector Dearmanadf51172021-04-09 16:09:45 +0100349 'linux', 'x64'),
Primiano Tucci0825bc82017-09-28 18:50:23 +0100350
Matthew Clarkson63028d62019-10-10 15:48:23 +0100351 # Android emulator binaries.
Primiano Tucci43f111a2020-07-27 20:37:52 +0200352 Dependency(
353 'buildtools/emulator',
354 'https://android.googlesource.com/platform/prebuilts/android-emulator.git',
Hector Dearmanadf51172021-04-09 16:09:45 +0100355 '4b260028dc27bc92c39bee9129cb2ba839970956', 'all', 'x64'),
Primiano Tuccid7750452017-09-29 14:38:51 +0100356]
Primiano Tucciae2879e2017-09-27 11:02:09 +0900357
Primiano Tucci1c752c12018-10-23 09:27:19 +0100358# This variable is updated by tools/roll-catapult-trace-viewer.
Primiano Tucci15968b52020-10-28 15:30:40 +0100359CATAPULT_SHA256 = 'b30108e05268ce6c65bb4126b65f6bfac165d17f5c1fd285046e7e6fd76c209f'
Primiano Tucci1c752c12018-10-23 09:27:19 +0100360
Hector Dearman69bcd1b2023-03-28 10:35:00 +0100361TYPEFACES_SHA256 = '1065172aaf0e9c22bc4f206ed9fdf5f1b4355d233fb21f9f26a89cd66c941940'
Hector Dearman6177b752019-01-24 10:17:32 +0000362
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100363UI_DEPS = [
Primiano Tucci43f111a2020-07-27 20:37:52 +0200364 Dependency(
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100365 'buildtools/mac/nodejs.tgz',
Hector Dearman0b600da2022-05-16 19:33:32 +0100366 'https://storage.googleapis.com/chromium-nodejs/16.13.0/31859fc1fa0994a95f44f09c367d6ff63607cfde',
367 'd9cf1f36b16e08ce52cfbdef427c6596eed2bd2f3608a79112d25b4ec8f75753',
368 'darwin', 'arm64'),
369 Dependency(
370 'buildtools/mac/nodejs.tgz',
371 'https://storage.googleapis.com/chromium-nodejs/16.13.0/16dfd094763b71988933a31735f9dea966f9abd6',
372 '065ced7e93f0e26276cb74d9688706674323865c4a8fc89e4adfa6868d4ebb4d',
373 'darwin', 'x64'),
Primiano Tucci43f111a2020-07-27 20:37:52 +0200374 Dependency(
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100375 'buildtools/linux64/nodejs.tgz',
Hector Dearman0b600da2022-05-16 19:33:32 +0100376 'https://storage.googleapis.com/chromium-nodejs/16.13.0/ab9544e24e752d3d17f335fb7b2055062e582d11',
377 '3b5ca150a55f3aadfa18f3a10a4495aaf9653614ba3e460647170fef5287ec4f',
Hector Dearmanadf51172021-04-09 16:09:45 +0100378 'linux', 'x64'),
Primiano Tucci43f111a2020-07-27 20:37:52 +0200379 Dependency(
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100380 'buildtools/mac/emsdk.tgz',
381 'https://storage.googleapis.com/perfetto/emscripten-2.0.12-mac.tgz',
382 'aa125f8c8ff8a386d43e18c8ea0c98c875cc19160a899403e8967a5478f96f31',
Primiano Tucci0e7915d2022-02-08 15:25:49 +0000383 'darwin', 'all'),
Primiano Tucci43f111a2020-07-27 20:37:52 +0200384 Dependency(
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100385 'buildtools/linux64/emsdk.tgz',
386 'https://storage.googleapis.com/perfetto/emscripten-2.0.12-linux.tgz',
387 'bfff9fb0326363c12e19b542f27a5f12cedbfc310f30621dc497c9af51d2d2e3',
Hector Dearmanadf51172021-04-09 16:09:45 +0100388 'linux', 'x64'),
Primiano Tucci43f111a2020-07-27 20:37:52 +0200389 Dependency(
Primiano Tucci43f111a2020-07-27 20:37:52 +0200390 'buildtools/catapult_trace_viewer.tgz',
391 'https://storage.googleapis.com/perfetto/catapult_trace_viewer-%s.tar.gz'
Hector Dearmanadf51172021-04-09 16:09:45 +0100392 % CATAPULT_SHA256, CATAPULT_SHA256, 'all', 'all'),
Primiano Tucci43f111a2020-07-27 20:37:52 +0200393 Dependency(
394 'buildtools/typefaces.tgz',
395 'https://storage.googleapis.com/perfetto/typefaces-%s.tar.gz' %
Hector Dearman48d34082023-06-21 15:43:08 +0100396 TYPEFACES_SHA256, TYPEFACES_SHA256, 'all', 'all'),
397
398 Dependency(
399 'third_party/pnpm/pnpm',
400 'https://storage.googleapis.com/perfetto/pnpm-linux-arm64-8.6.3',
401 'ac76e9ab6a770479f93c1a2bf978d72636dbcb02608554378cf30075a78a22ac',
402 'linux', 'arm64'),
403 Dependency(
404 'third_party/pnpm/pnpm',
405 'https://storage.googleapis.com/perfetto/pnpm-linux-x64-8.6.3',
406 '5a58ccd78d44faac138d901976a7a8917c0f2a2f83743cfdd895fcd0bb6aa135',
407 'linux', 'x64'),
408 Dependency(
409 'third_party/pnpm/pnpm',
410 'https://storage.googleapis.com/perfetto/pnpm-macos-arm64-8.6.3',
411 'f527713d3183e30cfbfd7fd6403ceed730831c53649e50c979961eab3b2cf866',
412 'darwin', 'arm64'),
413 Dependency(
414 'third_party/pnpm/pnpm',
415 'https://storage.googleapis.com/perfetto/pnpm-macos-x64-8.6.3',
416 '6b425f7f0342341e9ee9427a9a2be2c89936c4a04efe6125f7af667eb02b10c1',
417 'darwin', 'x64'),
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100418]
419
Lalit Magantibf99dd32023-02-07 23:50:48 +0000420# Dependencies to build gRPC.
421GRPC_DEPS = [
422 Dependency(
423 'buildtools/grpc/src',
424 'https://chromium.googlesource.com/external/github.com/grpc/grpc.git',
425 '6943c1841f57cac4666b165aea4f618fe73b3ff1', 'all', 'all', True),
426]
427
Primiano Tuccib428d492021-08-02 19:01:29 +0100428# Sysroots required to cross-compile Linux targets (linux-arm{,64}).
429# These are taken from Chromium's build/linux/sysroot_scripts/sysroots.json.
430BUILD_DEPS_LINUX_CROSS_SYSROOTS = [
Primiano Tuccib428d492021-08-02 19:01:29 +0100431 Dependency(
432 'buildtools/debian_sid_arm-sysroot.tgz',
433 'https://commondatastorage.googleapis.com/chrome-linux-sysroot/toolchain/11d6f690ca49e8ba01a1d8c5346cedad2cf308fd/debian_sid_arm_sysroot.tar.xz',
434 'ff192fe073d140d836c9ca1e68f7200d558bb9aa6c5c8f4f76f794f82890f99a',
435 'linux', 'all'),
436 Dependency(
437 'buildtools/debian_sid_arm64-sysroot.tgz',
438 'https://commondatastorage.googleapis.com/chrome-linux-sysroot/toolchain/2befe8ce3e88be6080e4fb7e6d412278ea6a7625/debian_sid_arm64_sysroot.tar.xz',
439 'e4389eab2fe363f3fbdfa4d3ce9d94457d78fd2c0e62171a7534867623eadc90',
440 'linux', 'all'),
441]
442
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000443ALL_DEPS = (
444 BUILD_DEPS_HOST + BUILD_DEPS_ANDROID + BUILD_DEPS_LINUX_CROSS_SYSROOTS +
445 TEST_DEPS_ANDROID + UI_DEPS)
Primiano Tuccib428d492021-08-02 19:01:29 +0100446
Primiano Tucciae2879e2017-09-27 11:02:09 +0900447ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Primiano Tucci69132a12020-02-07 22:33:06 +0000448UI_DIR = os.path.join(ROOT_DIR, 'ui')
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100449TOOLS_DIR = os.path.join(ROOT_DIR, 'tools')
Primiano Tucci69132a12020-02-07 22:33:06 +0000450NODE_MODULES_STATUS_FILE = os.path.join(UI_DIR, 'node_modules', '.last_install')
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000451TEST_DATA_SCRIPT = os.path.join(TOOLS_DIR, 'test_data')
Primiano Tucciae2879e2017-09-27 11:02:09 +0900452
453
Primiano Tucci16fed022023-05-05 17:11:32 +0100454def CheckCallRetry(*args, **kwargs):
455 """ Like subprocess.check_call, with retries up to 5 times. """
456 MAX_ATTEMPTS = 5
457 for attempt in range(1, MAX_ATTEMPTS + 1):
458 try:
459 return subprocess.check_call(*args, **kwargs)
460 except subprocess.CalledProcessError as error:
461 if attempt == MAX_ATTEMPTS:
462 raise error
463 else:
464 logging.error(error)
465 time.sleep(attempt * 3)
466
467
Primiano Tucci585b7e82020-05-14 11:19:10 +0100468def DownloadURL(url, out_file):
Primiano Tucci16fed022023-05-05 17:11:32 +0100469 CheckCallRetry(['curl', '-L', '-#', '-o', out_file, url])
Primiano Tucci585b7e82020-05-14 11:19:10 +0100470
471
Hector Dearmanadf51172021-04-09 16:09:45 +0100472def GetArch():
473 arch = machine()
Primiano Tucci0e7915d2022-02-08 15:25:49 +0000474 if arch == 'arm64':
475 return 'arm64'
Hector Dearmand3109302023-12-07 20:32:05 +0000476 elif arch == 'aarch64':
477 return 'arm64'
Hector Dearmanadf51172021-04-09 16:09:45 +0100478 else:
479 # Assume everything else is x64 matching previous behaviour.
480 return 'x64'
481
482
Primiano Tucciae2879e2017-09-27 11:02:09 +0900483def ReadFile(path):
484 if not os.path.exists(path):
485 return None
486 with open(path) as f:
Matthew Clarkson63028d62019-10-10 15:48:23 +0100487 return f.read().strip()
Primiano Tucciae2879e2017-09-27 11:02:09 +0900488
489
Primiano Tucci0825bc82017-09-28 18:50:23 +0100490def MkdirRecursive(path):
491 # Works with both relative and absolute paths
492 cwd = '/' if path.startswith('/') else ROOT_DIR
493 for part in path.split('/'):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900494 cwd = os.path.join(cwd, part)
495 if not os.path.exists(cwd):
496 os.makedirs(cwd)
497 else:
Matthew Clarkson63028d62019-10-10 15:48:23 +0100498 assert (os.path.isdir(cwd))
Primiano Tucciae2879e2017-09-27 11:02:09 +0900499
500
501def HashLocalFile(path):
502 if not os.path.exists(path):
503 return None
504 with open(path, 'rb') as f:
Primiano Tucci15968b52020-10-28 15:30:40 +0100505 return hashlib.sha256(f.read()).hexdigest()
Primiano Tucciae2879e2017-09-27 11:02:09 +0900506
507
508def ExtractZipfilePreservePermissions(zf, info, path):
509 zf.extract(info.filename, path=path)
510 target_path = os.path.join(path, info.filename)
511 min_acls = 0o755 if info.filename.endswith('/') else 0o644
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +0100512 os.chmod(target_path, (info.external_attr >> 16) | min_acls)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900513
514
Primiano Tucci0825bc82017-09-28 18:50:23 +0100515def IsGitRepoCheckoutOutAtRevision(path, revision):
516 return ReadFile(os.path.join(path, '.git', 'HEAD')) == revision
517
518
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100519def RmtreeIfExists(path):
Primiano Tucci857ed732020-12-19 18:11:42 +0100520 # Git creates read-only files on windows, which cause failures with rmtree.
521 # This seems the socially accepted way to deal with it.
522 # See https://bugs.python.org/issue19643 .
523 def del_read_only_for_windows(_action, name, _exc):
524 os.chmod(name, stat.S_IWRITE)
525 os.remove(name)
526
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100527 if not os.path.exists(path):
528 return
Primiano Tuccia0b1bc22023-05-04 15:06:58 +0100529 third_party_path = os.path.abspath(os.path.join(ROOT_DIR, 'third_party'))
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100530 buildtools_path = os.path.abspath(os.path.join(ROOT_DIR, 'buildtools'))
Florian Mayer83d683f2021-01-29 16:57:16 +0000531 test_path = os.path.abspath(os.path.join(ROOT_DIR, 'test', 'data'))
532 if (not os.path.abspath(path).startswith(buildtools_path) and
Primiano Tuccia0b1bc22023-05-04 15:06:58 +0100533 not os.path.abspath(path).startswith(test_path) and
534 not os.path.abspath(path).startswith(third_party_path)):
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100535 # Safety check to prevent that some merge confilct ends up doing some
536 # rm -rf / or similar.
Primiano Tuccia0b1bc22023-05-04 15:06:58 +0100537 logging.fatal(
538 'Cannot remove %s: outside of {buildtools, test/data, third_party}',
539 path)
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100540 sys.exit(1)
541 logging.info('Removing %s' % path)
Primiano Tuccia0b1bc22023-05-04 15:06:58 +0100542 if os.path.isdir(path):
543 shutil.rmtree(path, onerror=del_read_only_for_windows)
544 else:
545 os.remove(path)
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100546
547
548def CheckoutGitRepo(path, git_url, revision, check_only):
549 if IsGitRepoCheckoutOutAtRevision(path, revision):
550 return False
551 if check_only:
552 return True
553 path = path.replace('/', os.sep)
554 RmtreeIfExists(path)
Primiano Tucci0825bc82017-09-28 18:50:23 +0100555 MkdirRecursive(path)
556 logging.info('Fetching %s @ %s into %s', git_url, revision, path)
Lalit Maganti367fcd52018-02-05 16:06:13 +0000557 subprocess.check_call(['git', 'init', path], cwd=path)
Primiano Tucci16fed022023-05-05 17:11:32 +0100558 CheckCallRetry(['git', 'fetch', '--quiet', '--depth', '1', git_url, revision],
559 cwd=path)
Primiano Tucci0825bc82017-09-28 18:50:23 +0100560 subprocess.check_call(['git', 'checkout', revision, '--quiet'], cwd=path)
Primiano Tucci16fed022023-05-05 17:11:32 +0100561 CheckCallRetry(
Lalit Magantibf99dd32023-02-07 23:50:48 +0000562 ['git', 'submodule', 'update', '--init', '--recursive', '--quiet'],
563 cwd=path)
Matthew Clarkson63028d62019-10-10 15:48:23 +0100564 assert (IsGitRepoCheckoutOutAtRevision(path, revision))
Eric Seckler676421f2019-02-12 14:43:31 +0000565 return True
Primiano Tucci0825bc82017-09-28 18:50:23 +0100566
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100567
Primiano Tucci43f111a2020-07-27 20:37:52 +0200568def InstallNodeModules(force_clean=False):
569 if force_clean:
570 node_modules = os.path.join(UI_DIR, 'node_modules')
571 logging.info('Clearing %s', node_modules)
572 subprocess.check_call(['git', 'clean', '-qxffd', node_modules],
573 cwd=ROOT_DIR)
Hector Dearman69c23df2023-06-27 12:51:04 +0100574 logging.info("Running `pnpm install --shamefully-hoist --frozen-lockfile` in {0}".format(UI_DIR))
Hector Dearman48d34082023-06-21 15:43:08 +0100575
576 # Some node modules have postinstall scripts (already bad) but worse
577 # sometimes they are in the form: "postinstall: 'node ./scripts/foo'"
578 # so here we need to ensure that our hermetic node is available in
579 # PATH.
580 env = os.environ.copy()
581 env['PATH'] = TOOLS_DIR + ':' + env['PATH']
582
Hector Dearman69c23df2023-06-27 12:51:04 +0100583 subprocess.check_call([
584 os.path.join(TOOLS_DIR, 'pnpm'),
585 'install',
586 '--shamefully-hoist',
587 '--frozen-lockfile'],
588 cwd=UI_DIR,
589 env=env)
590 # pbjs has the bad habit of installing extra packages on its first
591 # run. Run it here, so we avoid fetches while building.
Hector Dearman48d34082023-06-21 15:43:08 +0100592 pbjs = ['node_modules/.bin/pbjs', '/dev/null', '-o', '/dev/null']
593 subprocess.call(pbjs, cwd=UI_DIR, env=env)
Primiano Tucci69132a12020-02-07 22:33:06 +0000594 with open(NODE_MODULES_STATUS_FILE, 'w') as f:
Hector Dearman48d34082023-06-21 15:43:08 +0100595 f.write(HashLocalFile(os.path.join(UI_DIR, 'pnpm-lock.yaml')))
Primiano Tucci69132a12020-02-07 22:33:06 +0000596
597
598def CheckNodeModules():
599 """Returns True if the modules are up-to-date.
600
601 There doesn't seem to be an easy way to check node modules versions. Instead
Hector Dearman48d34082023-06-21 15:43:08 +0100602 just check if pnpm-lock.json changed since the last `pnpm install` call.
Primiano Tucci69132a12020-02-07 22:33:06 +0000603 """
604 if not os.path.exists(NODE_MODULES_STATUS_FILE):
605 return False
606 with open(NODE_MODULES_STATUS_FILE, 'r') as f:
607 actual = f.read()
Hector Dearman48d34082023-06-21 15:43:08 +0100608 expected = HashLocalFile(os.path.join(UI_DIR, 'pnpm-lock.yaml'))
Primiano Tucci69132a12020-02-07 22:33:06 +0000609 return expected == actual
Primiano Tucci0825bc82017-09-28 18:50:23 +0100610
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100611
612def CheckHashes():
Primiano Tuccib428d492021-08-02 19:01:29 +0100613 for dep in ALL_DEPS:
614 if dep.source_url.endswith('.git'):
615 continue
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000616 logging.info('Downloading %s for %s-%s', dep.source_url, dep.target_os,
617 dep.target_arch)
Primiano Tuccib428d492021-08-02 19:01:29 +0100618 with tempfile.NamedTemporaryFile(delete=False) as f:
619 f.close()
620 DownloadURL(dep.source_url, f.name)
621 actual_checksum = HashLocalFile(f.name)
622 os.unlink(f.name)
623 if (actual_checksum != dep.checksum):
624 logging.fatal('SHA-256 mismatch for {} expected {} was {}'.format(
625 dep.source_url, dep.checksum, actual_checksum))
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100626
627
Primiano Tuccia0b1bc22023-05-04 15:06:58 +0100628def CheckDepotToolsIsRecent():
629 gn_py_path = shutil.which('gn.py')
630 if gn_py_path is None:
631 return True # depot_tools doesn't seem to be installed in the PATH.
632 dt_dir = os.path.abspath(os.path.dirname(gn_py_path))
633 cmd = ['git', '-C', dt_dir, 'merge-base', '--is-ancestor', 'a0cf4321', 'HEAD']
634 git_ret = subprocess.call(cmd, stderr=subprocess.DEVNULL)
635 if git_ret == 0:
636 return True
637 print('\033[91mYour depot_tools revision is too old. Please run:\033[0m')
638 print('git -C %s fetch origin && git -C %s checkout -B main -t origin/main' %
639 (dt_dir, dt_dir))
640 return False
641
642
Primiano Tucciae2879e2017-09-27 11:02:09 +0900643def Main():
644 parser = argparse.ArgumentParser()
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000645 parser.add_argument(
646 '--android',
647 action='store_true',
Primiano Tuccib428d492021-08-02 19:01:29 +0100648 help='NDK and emulator images target_os="android"')
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000649 parser.add_argument(
650 '--linux-arm',
651 action='store_true',
Primiano Tuccib428d492021-08-02 19:01:29 +0100652 help='Debian sysroots for target_os="linux" target_cpu="arm|arm64"')
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000653 parser.add_argument(
654 '--ui',
655 action='store_true',
Primiano Tuccib428d492021-08-02 19:01:29 +0100656 help='Node and NPM packages to Build the Web-based UI via ./ui/build')
Lalit Magantibf99dd32023-02-07 23:50:48 +0000657 parser.add_argument(
Lalit Maganti7d418712023-04-13 13:45:14 +0100658 '--grpc', action='store_true', help='Packages to build gRPC')
Primiano Tucci69132a12020-02-07 22:33:06 +0000659 parser.add_argument('--check-only')
Daniele Di Proietto99687bd2022-11-18 11:18:12 +0000660 parser.add_argument('--filter', action='append')
Primiano Tucci69132a12020-02-07 22:33:06 +0000661 parser.add_argument('--verify', help='Check all URLs', action='store_true')
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000662 parser.add_argument(
663 '--no-toolchain', help='Do not download toolchain', action='store_true')
Chris Phlipot3d44ed92023-10-10 17:48:07 -0700664 parser.add_argument(
665 '--build-os',
666 default=system().lower(),
667 choices=['windows', 'darwin', 'linux'],
668 help='Override the autodetected build operating system')
669 parser.add_argument(
670 '--build-arch',
671 default=GetArch(),
672 choices=['arm64', 'x64'],
673 help='Override the autodetected build CPU architecture')
Primiano Tucciae2879e2017-09-27 11:02:09 +0900674 args = parser.parse_args()
Primiano Tucci69132a12020-02-07 22:33:06 +0000675 if args.verify:
Sami Kyostilac7ddac72019-06-05 21:43:40 +0100676 CheckHashes()
677 return 0
Lalit Maganti0830b602021-06-11 15:11:42 +0100678
Chris Phlipot3d44ed92023-10-10 17:48:07 -0700679 target_os = args.build_os
Lalit Maganti0830b602021-06-11 15:11:42 +0100680 if args.ui and target_os == 'windows':
681 print('Building the UI on Windows is unsupported')
682 return 1
683
Primiano Tuccia0b1bc22023-05-04 15:06:58 +0100684 if not CheckDepotToolsIsRecent():
685 return 1
686
Primiano Tuccid7750452017-09-29 14:38:51 +0100687 deps = BUILD_DEPS_HOST
Florian Mayer54c4e382021-04-20 15:51:23 +0100688 if not args.no_toolchain:
689 deps += BUILD_DEPS_TOOLCHAIN_HOST
Primiano Tucci69132a12020-02-07 22:33:06 +0000690 if args.android:
Primiano Tuccid7750452017-09-29 14:38:51 +0100691 deps += BUILD_DEPS_ANDROID + TEST_DEPS_ANDROID
Primiano Tuccib428d492021-08-02 19:01:29 +0100692 if args.linux_arm:
693 deps += BUILD_DEPS_LINUX_CROSS_SYSROOTS
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100694 if args.ui:
695 deps += UI_DEPS
Lalit Magantibf99dd32023-02-07 23:50:48 +0000696 if args.grpc:
697 deps += GRPC_DEPS
Eric Seckler6e828f32019-01-02 11:10:56 +0000698 deps_updated = False
Primiano Tucci43f111a2020-07-27 20:37:52 +0200699 nodejs_updated = False
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700700
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100701 for old_dir in CLEANUP_OLD_DIRS:
702 RmtreeIfExists(os.path.join(ROOT_DIR, old_dir))
703
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700704 for dep in deps:
Chris Phlipot3d44ed92023-10-10 17:48:07 -0700705 target_arch = args.build_arch
Hector Dearmanadf51172021-04-09 16:09:45 +0100706 matches_os = dep.target_os == 'all' or target_os == dep.target_os
707 matches_arch = dep.target_arch == 'all' or target_arch == dep.target_arch
708 if not matches_os or not matches_arch:
Primiano Tucciae2879e2017-09-27 11:02:09 +0900709 continue
Daniele Di Proietto99687bd2022-11-18 11:18:12 +0000710 if args.filter and not any(f in dep.target_folder for f in args.filter):
Primiano Tuccicde1a8c2021-02-15 19:18:10 +0100711 continue
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700712 local_path = os.path.join(ROOT_DIR, dep.target_folder)
713 if dep.source_url.endswith('.git'):
Primiano Tucci15968b52020-10-28 15:30:40 +0100714 deps_updated |= CheckoutGitRepo(local_path, dep.source_url, dep.checksum,
Primiano Tucci69132a12020-02-07 22:33:06 +0000715 args.check_only)
Primiano Tucci0825bc82017-09-28 18:50:23 +0100716 continue
Hector Dearmane1981602023-06-16 10:28:12 +0100717 is_compressed = any([local_path.endswith(ext) for ext in ['.zip', '.tgz', '.tbz2']])
718 compressed_target_dir = os.path.splitext(local_path)[0] if is_compressed else None
719 compressed_dir_stamp = os.path.join(compressed_target_dir, '.stamp') if is_compressed else None
Primiano Tucciae2879e2017-09-27 11:02:09 +0900720
Hector Dearmane1981602023-06-16 10:28:12 +0100721 if ((not is_compressed and HashLocalFile(local_path) == dep.checksum) or
722 (is_compressed and ReadFile(compressed_dir_stamp) == dep.checksum)):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900723 continue
Eric Seckler6e828f32019-01-02 11:10:56 +0000724 deps_updated = True
Primiano Tucci69132a12020-02-07 22:33:06 +0000725 if args.check_only:
726 continue
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700727 MkdirRecursive(os.path.dirname(dep.target_folder))
Primiano Tucci15968b52020-10-28 15:30:40 +0100728 if HashLocalFile(local_path) != dep.checksum:
Primiano Tucciae2879e2017-09-27 11:02:09 +0900729 download_path = local_path + '.tmp'
Jordan Bayles8c6a4bc2020-07-16 20:20:48 -0700730 logging.info('Downloading %s from %s', local_path, dep.source_url)
731 DownloadURL(dep.source_url, download_path)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900732 os.chmod(download_path, 0o755)
Primiano Tucci15968b52020-10-28 15:30:40 +0100733 actual_checksum = HashLocalFile(download_path)
734 if (actual_checksum != dep.checksum):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900735 os.remove(download_path)
Primiano Tucci15968b52020-10-28 15:30:40 +0100736 logging.fatal('SHA-256 mismatch for {} expected {} was {}'.format(
737 download_path, dep.checksum, actual_checksum))
Primiano Tucciae2879e2017-09-27 11:02:09 +0900738 return 1
Primiano Tuccie6440102020-12-01 12:41:03 +0100739 shutil.move(download_path, local_path)
Primiano Tucci43f111a2020-07-27 20:37:52 +0200740 if 'nodejs' in dep.target_folder:
741 nodejs_updated = True
742
Primiano Tucci15968b52020-10-28 15:30:40 +0100743 assert (HashLocalFile(local_path) == dep.checksum)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900744
Hector Dearmane1981602023-06-16 10:28:12 +0100745 if is_compressed:
746 logging.info('Extracting %s into %s' % (local_path, compressed_target_dir))
747 assert (os.path.commonprefix((ROOT_DIR, compressed_target_dir)) == ROOT_DIR)
748 RmtreeIfExists(compressed_target_dir)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900749
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000750 # Decompress the archive.
751 if local_path.endswith('.tgz'):
Hector Dearmane1981602023-06-16 10:28:12 +0100752 MkdirRecursive(compressed_target_dir)
753 subprocess.check_call(['tar', '-oxf', local_path], cwd=compressed_target_dir)
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000754 elif local_path.endswith('.zip'):
755 with zipfile.ZipFile(local_path, 'r') as zf:
756 for info in zf.infolist():
Hector Dearmane1981602023-06-16 10:28:12 +0100757 ExtractZipfilePreservePermissions(zf, info, compressed_target_dir)
758 elif local_path.endswith('.tbz2'):
759 tar_path = '{}.tar.tmp'.format(local_path)
760 with open(tar_path, 'w') as f:
761 with bz2.open(local_path, 'r') as bf:
762 f.write(bf.read())
763 MkdirRecursive(compressed_target_dir)
764 subprocess.check_call(['tar', '-oxf', tar_path], cwd=compressed_target_dir)
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000765
766 # If the zip contains one root folder, rebase one level up moving all
767 # its sub files and folders inside |target_dir|.
Hector Dearmane1981602023-06-16 10:28:12 +0100768 subdir = os.listdir(compressed_target_dir)
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000769 if len(subdir) == 1:
Hector Dearmane1981602023-06-16 10:28:12 +0100770 subdir = os.path.join(compressed_target_dir, subdir[0])
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000771 if os.path.isdir(subdir):
772 for subf in os.listdir(subdir):
Hector Dearmane1981602023-06-16 10:28:12 +0100773 shutil.move(os.path.join(subdir, subf), compressed_target_dir)
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000774 os.rmdir(subdir)
775
776 # Create stamp and remove the archive.
Hector Dearmane1981602023-06-16 10:28:12 +0100777 with open(compressed_dir_stamp, 'w') as stamp_file:
Primiano Tucci15968b52020-10-28 15:30:40 +0100778 stamp_file.write(dep.checksum)
Primiano Tuccib60d4b02017-11-10 11:03:00 +0000779 os.remove(local_path)
Primiano Tucciae2879e2017-09-27 11:02:09 +0900780
Deepanjan Royacf8c072018-07-13 11:37:04 -0400781 if args.ui:
782 # Needs to happen after nodejs is installed above.
Primiano Tucci69132a12020-02-07 22:33:06 +0000783 if args.check_only:
Primiano Tuccic1aecf42021-04-13 22:22:19 +0100784 deps_updated |= not CheckNodeModules()
Primiano Tucci69132a12020-02-07 22:33:06 +0000785 else:
Primiano Tucci43f111a2020-07-27 20:37:52 +0200786 InstallNodeModules(force_clean=nodejs_updated)
Primiano Tucci69132a12020-02-07 22:33:06 +0000787
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000788 cur_python_interpreter = sys.executable
Primiano Tucci856f2be2022-03-17 17:25:12 +0000789 test_data_synced = 0 == subprocess.call([
790 cur_python_interpreter, TEST_DATA_SCRIPT, 'status', '--quiet',
791 '--ignore-new'
792 ])
Primiano Tucci69132a12020-02-07 22:33:06 +0000793 if args.check_only:
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000794 if not deps_updated and test_data_synced:
Primiano Tucci69132a12020-02-07 22:33:06 +0000795 with open(args.check_only, 'w') as f:
796 f.write('OK') # The content is irrelevant, just keep GN happy.
797 return 0
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000798 argz = ' '.join(
799 [x for x in sys.argv[1:] if not x.startswith('--check-only')])
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100800 print('\033[91mBuild deps are stale. ' +
801 'Please run tools/install-build-deps %s\033[0m' % argz)
Primiano Tucci856f2be2022-03-17 17:25:12 +0000802 if not test_data_synced:
803 print('//test/data/ is out of sync. `tools/test_data status` for details')
Primiano Tucci69132a12020-02-07 22:33:06 +0000804 return 1
Primiano Tucciae2879e2017-09-27 11:02:09 +0900805
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000806 if not test_data_synced:
Primiano Tucci54418902021-11-06 12:09:48 +0000807 cmd = [cur_python_interpreter, TEST_DATA_SCRIPT, 'download', '--overwrite']
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000808 if not sys.stdout.isatty():
809 cmd += ['--verbose'] # For CI bots
810 subprocess.check_call(cmd)
811
Eric Seckler6e828f32019-01-02 11:10:56 +0000812 if deps_updated:
813 # Stale binary files may be compiled against old sysroot headers that aren't
814 # tracked by gn.
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +0100815 logging.warning('Remember to run "gn clean <output_directory>" ' +
816 'to avoid stale binary files.')
Eric Seckler6e828f32019-01-02 11:10:56 +0000817
Matthew Clarkson63028d62019-10-10 15:48:23 +0100818
Primiano Tucciae2879e2017-09-27 11:02:09 +0900819if __name__ == '__main__':
820 logging.basicConfig(level=logging.INFO)
821 sys.exit(Main())