Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 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 | |
| 23 | BASH_FALLBACK = """ " |
| 24 | exec python3 - "$@" <<'#'EOF |
| 25 | #""" |
| 26 | |
| 27 | TOOL_NAME = 'tracebox' |
| 28 | |
| 29 | # BEGIN_SECTION_GENERATED_BY(roll-prebuilts) |
Primiano Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 30 | # Revision: v21.0 |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 31 | PERFETTO_PREBUILT_MANIFEST = [{ |
| 32 | 'tool': |
| 33 | 'tracebox', |
| 34 | 'arch': |
| 35 | 'mac-amd64', |
| 36 | 'file_name': |
| 37 | 'tracebox', |
| 38 | 'file_size': |
Primiano Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 39 | 1282808, |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 40 | 'url': |
Primiano Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 41 | 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v21.0/mac-amd64/tracebox', |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 42 | 'sha256': |
Primiano Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 43 | '65ece42a5032f8c170c6b6ce97bc554ea4b30675f182fd019744c6ec34b0f7e3', |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 44 | '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 Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 55 | 1701992, |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 56 | 'url': |
Primiano Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 57 | 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v21.0/linux-amd64/tracebox', |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 58 | 'sha256': |
Primiano Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 59 | 'f9253b69ccd9cc04f8975efcba41d32b6aef0db3fc5c14d49d3fbb89b789eee1', |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 60 | '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 Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 71 | 1701992, |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 72 | 'url': |
Primiano Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 73 | 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v21.0/linux-amd64/tracebox', |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 74 | 'sha256': |
Primiano Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 75 | 'f9253b69ccd9cc04f8975efcba41d32b6aef0db3fc5c14d49d3fbb89b789eee1', |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 76 | '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 Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 87 | 969560, |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 88 | 'url': |
Primiano Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 89 | 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v21.0/linux-arm/tracebox', |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 90 | 'sha256': |
Primiano Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 91 | '085cfbd48e2ca648d2e76b7f99d43295dd6429d2d47899845e3dd81cdaf493e2', |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 92 | '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 Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 103 | 1569000, |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 104 | 'url': |
Primiano Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 105 | 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v21.0/linux-arm64/tracebox', |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 106 | 'sha256': |
Primiano Tucci | 45646b6 | 2021-11-04 11:18:03 +0000 | [diff] [blame] | 107 | '5dc68c8f68d20dd38f5acf749d9a13a8f347afab648b2b5a6807e374e4970b5d', |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 108 | '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. |
| 117 | def 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 | |
| 187 | if __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 |