1#!/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) 30# Revision: v25.0 31PERFETTO_PREBUILT_MANIFEST = [{ 32 'tool': 33 'tracebox', 34 'arch': 35 'mac-amd64', 36 'file_name': 37 'tracebox', 38 'file_size': 39 1382584, 40 'url': 41 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v25.0/mac-amd64/tracebox', 42 'sha256': 43 '2b5eecebd92810d6f88598904fc52db4930f5f234bcb749c839eb25ebaf42a8c', 44 'platform': 45 'darwin', 46 'machine': ['x86_64'] 47}, { 48 'tool': 49 'tracebox', 50 'arch': 51 'mac-arm64', 52 'file_name': 53 'tracebox', 54 'file_size': 55 1275896, 56 'url': 57 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v25.0/mac-arm64/tracebox', 58 'sha256': 59 '51049ce25a123b8990658bddbb52f5984e80c8c4e2eb06780f22534218cdbd1d', 60 'platform': 61 'darwin', 62 'machine': ['arm64'] 63}, { 64 'tool': 65 'tracebox', 66 'arch': 67 'linux-amd64', 68 'file_name': 69 'tracebox', 70 'file_size': 71 1786784, 72 'url': 73 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v25.0/linux-amd64/tracebox', 74 'sha256': 75 '0df104d808715733cf717d5f2808e48e57bf20a851b49e1e579c516a34f4f9f9', 76 'platform': 77 'linux', 78 'machine': ['x86_64'] 79}, { 80 'tool': 81 'tracebox', 82 'arch': 83 'linux-amd64', 84 'file_name': 85 'tracebox', 86 'file_size': 87 1786784, 88 'url': 89 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v25.0/linux-amd64/tracebox', 90 'sha256': 91 '0df104d808715733cf717d5f2808e48e57bf20a851b49e1e579c516a34f4f9f9', 92 'platform': 93 'linux', 94 'machine': ['x86_64'] 95}, { 96 'tool': 97 'tracebox', 98 'arch': 99 'linux-arm', 100 'file_name': 101 'tracebox', 102 'file_size': 103 1032796, 104 'url': 105 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v25.0/linux-arm/tracebox', 106 'sha256': 107 '4db159c84073f95e5f704137bff4c97aa32a714a9c93ba77f99eeedf9f3fc714', 108 'platform': 109 'linux', 110 'machine': ['armv6l', 'armv7l', 'armv8l'] 111}, { 112 'tool': 113 'tracebox', 114 'arch': 115 'linux-arm64', 116 'file_name': 117 'tracebox', 118 'file_size': 119 1663696, 120 'url': 121 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v25.0/linux-arm64/tracebox', 122 'sha256': 123 '75b7d877808a927962d15f8d5659ccce082e07797305cc739809aef98853bfce', 124 'platform': 125 'linux', 126 'machine': ['aarch64'] 127}] 128 129 130# DO NOT EDIT. If you wish to make edits to this code, you need to change only 131# //tools/get_perfetto_prebuilt.py and run /tools/roll-prebuilts to regenerate 132# all the others scripts this is embedded into. 133def get_perfetto_prebuilt(tool_name, soft_fail=False, arch=None): 134 """ Downloads the prebuilt, if necessary, and returns its path on disk. """ 135 136 # The first time this is invoked, it downloads the |url| and caches it into 137 # ~/.perfetto/prebuilts/$tool_name. On subsequent invocations it just runs the 138 # cached version. 139 def download_or_get_cached(file_name, url, sha256): 140 import os, hashlib, subprocess 141 dir = os.path.join( 142 os.path.expanduser('~'), '.local', 'share', 'perfetto', 'prebuilts') 143 os.makedirs(dir, exist_ok=True) 144 bin_path = os.path.join(dir, file_name) 145 sha256_path = os.path.join(dir, file_name + '.sha256') 146 needs_download = True 147 148 # Avoid recomputing the SHA-256 on each invocation. The SHA-256 of the last 149 # download is cached into file_name.sha256, just check if that matches. 150 if os.path.exists(bin_path) and os.path.exists(sha256_path): 151 with open(sha256_path, 'rb') as f: 152 digest = f.read().decode() 153 if digest == sha256: 154 needs_download = False 155 156 if needs_download: 157 # Either the filed doesn't exist or the SHA256 doesn't match. 158 tmp_path = bin_path + '.tmp' 159 print('Downloading ' + url) 160 subprocess.check_call(['curl', '-f', '-L', '-#', '-o', tmp_path, url]) 161 with open(tmp_path, 'rb') as fd: 162 actual_sha256 = hashlib.sha256(fd.read()).hexdigest() 163 if actual_sha256 != sha256: 164 raise Exception('Checksum mismatch for %s (actual: %s, expected: %s)' % 165 (url, actual_sha256, sha256)) 166 os.chmod(tmp_path, 0o755) 167 os.rename(tmp_path, bin_path) 168 with open(sha256_path, 'w') as f: 169 f.write(sha256) 170 return bin_path 171 # --- end of download_or_get_cached() --- 172 173 # --- get_perfetto_prebuilt() function starts here. --- 174 import os, platform, sys 175 plat = sys.platform.lower() 176 machine = platform.machine().lower() 177 manifest_entry = None 178 for entry in PERFETTO_PREBUILT_MANIFEST: 179 # If the caller overrides the arch, just match that (for Android prebuilts). 180 if arch and entry.get('arch') == arch: 181 manifest_entry = entry 182 break 183 # Otherwise guess the local machine arch. 184 if entry.get('tool') == tool_name and entry.get( 185 'platform') == plat and machine in entry.get('machine', []): 186 manifest_entry = entry 187 break 188 if manifest_entry is None: 189 if soft_fail: 190 return None 191 raise Exception( 192 ('No prebuilts available for %s-%s\n' % (plat, machine)) + 193 'See https://perfetto.dev/docs/contributing/build-instructions') 194 195 return download_or_get_cached( 196 file_name=manifest_entry['file_name'], 197 url=manifest_entry['url'], 198 sha256=manifest_entry['sha256']) 199 200 201# END_SECTION_GENERATED_BY(roll-prebuilts) 202 203if __name__ == '__main__': 204 import sys, os 205 bin_path = get_perfetto_prebuilt(TOOL_NAME) 206 os.execv(bin_path, [bin_path] + sys.argv[1:]) 207 208#EOF 209