blob: 78907a0efd4757f750ef72eb8b317c6fef721c8c [file] [log] [blame]
Primiano Tuccid3e40bd2021-08-03 10:52:39 +01001#!/usr/bin/env python3
2# Copyright (C) 2021 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
16# This file should do the same thing when being invoked in any of these ways:
17# ./tracebox
18# python tracebox
19# bash tracebox
20# cat ./tracebox | bash
21# cat ./tracebox | python -
22
23BASH_FALLBACK = """ "
24exec python3 - "$@" <<'#'EOF
25#"""
26
27TOOL_NAME = 'tracebox'
28
29# BEGIN_SECTION_GENERATED_BY(roll-prebuilts)
Primiano Tucci45646b62021-11-04 11:18:03 +000030# Revision: v21.0
Primiano Tuccid3e40bd2021-08-03 10:52:39 +010031PERFETTO_PREBUILT_MANIFEST = [{
32 'tool':
33 'tracebox',
34 'arch':
35 'mac-amd64',
36 'file_name':
37 'tracebox',
38 'file_size':
Primiano Tucci45646b62021-11-04 11:18:03 +000039 1282808,
Primiano Tuccid3e40bd2021-08-03 10:52:39 +010040 'url':
Primiano Tucci45646b62021-11-04 11:18:03 +000041 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v21.0/mac-amd64/tracebox',
Primiano Tuccid3e40bd2021-08-03 10:52:39 +010042 'sha256':
Primiano Tucci45646b62021-11-04 11:18:03 +000043 '65ece42a5032f8c170c6b6ce97bc554ea4b30675f182fd019744c6ec34b0f7e3',
Primiano Tuccid3e40bd2021-08-03 10:52:39 +010044 'platform':
45 'darwin',
46 'machine': ['x86_64']
47}, {
48 'tool':
49 'tracebox',
50 'arch':
51 'linux-amd64',
52 'file_name':
53 'tracebox',
54 'file_size':
Primiano Tucci45646b62021-11-04 11:18:03 +000055 1701992,
Primiano Tuccid3e40bd2021-08-03 10:52:39 +010056 'url':
Primiano Tucci45646b62021-11-04 11:18:03 +000057 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v21.0/linux-amd64/tracebox',
Primiano Tuccid3e40bd2021-08-03 10:52:39 +010058 'sha256':
Primiano Tucci45646b62021-11-04 11:18:03 +000059 'f9253b69ccd9cc04f8975efcba41d32b6aef0db3fc5c14d49d3fbb89b789eee1',
Primiano Tuccid3e40bd2021-08-03 10:52:39 +010060 'platform':
61 'linux',
62 'machine': ['x86_64']
63}, {
64 'tool':
65 'tracebox',
66 'arch':
67 'linux-amd64',
68 'file_name':
69 'tracebox',
70 'file_size':
Primiano Tucci45646b62021-11-04 11:18:03 +000071 1701992,
Primiano Tuccid3e40bd2021-08-03 10:52:39 +010072 'url':
Primiano Tucci45646b62021-11-04 11:18:03 +000073 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v21.0/linux-amd64/tracebox',
Primiano Tuccid3e40bd2021-08-03 10:52:39 +010074 'sha256':
Primiano Tucci45646b62021-11-04 11:18:03 +000075 'f9253b69ccd9cc04f8975efcba41d32b6aef0db3fc5c14d49d3fbb89b789eee1',
Primiano Tuccid3e40bd2021-08-03 10:52:39 +010076 'platform':
77 'linux',
78 'machine': ['x86_64']
79}, {
80 'tool':
81 'tracebox',
82 'arch':
83 'linux-arm',
84 'file_name':
85 'tracebox',
86 'file_size':
Primiano Tucci45646b62021-11-04 11:18:03 +000087 969560,
Primiano Tuccid3e40bd2021-08-03 10:52:39 +010088 'url':
Primiano Tucci45646b62021-11-04 11:18:03 +000089 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v21.0/linux-arm/tracebox',
Primiano Tuccid3e40bd2021-08-03 10:52:39 +010090 'sha256':
Primiano Tucci45646b62021-11-04 11:18:03 +000091 '085cfbd48e2ca648d2e76b7f99d43295dd6429d2d47899845e3dd81cdaf493e2',
Primiano Tuccid3e40bd2021-08-03 10:52:39 +010092 'platform':
93 'linux',
94 'machine': ['armv6l', 'armv7l', 'armv8l']
95}, {
96 'tool':
97 'tracebox',
98 'arch':
99 'linux-arm64',
100 'file_name':
101 'tracebox',
102 'file_size':
Primiano Tucci45646b62021-11-04 11:18:03 +0000103 1569000,
Primiano Tuccid3e40bd2021-08-03 10:52:39 +0100104 'url':
Primiano Tucci45646b62021-11-04 11:18:03 +0000105 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v21.0/linux-arm64/tracebox',
Primiano Tuccid3e40bd2021-08-03 10:52:39 +0100106 'sha256':
Primiano Tucci45646b62021-11-04 11:18:03 +0000107 '5dc68c8f68d20dd38f5acf749d9a13a8f347afab648b2b5a6807e374e4970b5d',
Primiano Tuccid3e40bd2021-08-03 10:52:39 +0100108 'platform':
109 'linux',
110 'machine': ['aarch64']
111}]
112
113
114# DO NOT EDIT. If you wish to make edits to this code, you need to change only
115# //tools/get_perfetto_prebuilt.py and run /tools/roll-prebuilts to regenerate
116# all the others scripts this is embedded into.
117def get_perfetto_prebuilt(tool_name, soft_fail=False, arch=None):
118 """ Downloads the prebuilt, if necessary, and returns its path on disk. """
119
120 # The first time this is invoked, it downloads the |url| and caches it into
121 # ~/.perfetto/prebuilts/$tool_name. On subsequent invocations it just runs the
122 # cached version.
123 def download_or_get_cached(file_name, url, sha256):
124 import os, hashlib, subprocess
125 dir = os.path.join(
126 os.path.expanduser('~'), '.local', 'share', 'perfetto', 'prebuilts')
127 os.makedirs(dir, exist_ok=True)
128 bin_path = os.path.join(dir, file_name)
129 sha256_path = os.path.join(dir, file_name + '.sha256')
130 needs_download = True
131
132 # Avoid recomputing the SHA-256 on each invocation. The SHA-256 of the last
133 # download is cached into file_name.sha256, just check if that matches.
134 if os.path.exists(bin_path) and os.path.exists(sha256_path):
135 with open(sha256_path, 'rb') as f:
136 digest = f.read().decode()
137 if digest == sha256:
138 needs_download = False
139
140 if needs_download:
141 # Either the filed doesn't exist or the SHA256 doesn't match.
142 tmp_path = bin_path + '.tmp'
143 print('Downloading ' + url)
144 subprocess.check_call(['curl', '-f', '-L', '-#', '-o', tmp_path, url])
145 with open(tmp_path, 'rb') as fd:
146 actual_sha256 = hashlib.sha256(fd.read()).hexdigest()
147 if actual_sha256 != sha256:
148 raise Exception('Checksum mismatch for %s (actual: %s, expected: %s)' %
149 (url, actual_sha256, sha256))
150 os.chmod(tmp_path, 0o755)
151 os.rename(tmp_path, bin_path)
152 with open(sha256_path, 'w') as f:
153 f.write(sha256)
154 return bin_path
155 # --- end of download_or_get_cached() ---
156
157 # --- get_perfetto_prebuilt() function starts here. ---
158 import os, platform, sys
159 plat = sys.platform.lower()
160 machine = platform.machine().lower()
161 manifest_entry = None
162 for entry in PERFETTO_PREBUILT_MANIFEST:
163 # If the caller overrides the arch, just match that (for Android prebuilts).
164 if arch and entry.get('arch') == arch:
165 manifest_entry = entry
166 break
167 # Otherwise guess the local machine arch.
168 if entry.get('tool') == tool_name and entry.get(
169 'platform') == plat and machine in entry.get('machine', []):
170 manifest_entry = entry
171 break
172 if manifest_entry is None:
173 if soft_fail:
174 return None
175 raise Exception(
176 ('No prebuilts available for %s-%s\n' % (plat, machine)) +
177 'See https://perfetto.dev/docs/contributing/build-instructions')
178
179 return download_or_get_cached(
180 file_name=manifest_entry['file_name'],
181 url=manifest_entry['url'],
182 sha256=manifest_entry['sha256'])
183
184
185# END_SECTION_GENERATED_BY(roll-prebuilts)
186
187if __name__ == '__main__':
188 import sys, os
189 bin_path = get_perfetto_prebuilt(TOOL_NAME)
190 os.execv(bin_path, [bin_path] + sys.argv[1:])
191
192#EOF