Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 1 | // Copyright (C) 2021 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | 'use strict'; |
| 16 | |
Hector Dearman | 969ecb9 | 2022-06-06 12:18:48 +0100 | [diff] [blame] | 17 | |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 18 | // This script takes care of: |
| 19 | // - The build process for the whole UI and the chrome extension. |
| 20 | // - The HTTP dev-server with live-reload capabilities. |
| 21 | // The reason why this is a hand-rolled script rather than a conventional build |
| 22 | // system is keeping incremental build fast and maintaining the set of |
| 23 | // dependencies contained. |
| 24 | // The only way to keep incremental build fast (i.e. O(seconds) for the |
| 25 | // edit-one-line -> reload html cycles) is to run both the TypeScript compiler |
| 26 | // and the rollup bundler in --watch mode. Any other attempt, leads to O(10s) |
| 27 | // incremental-build times. |
| 28 | // This script allows mixing build tools that support --watch mode (tsc and |
Primiano Tucci | 9b56703 | 2021-02-12 14:18:12 +0100 | [diff] [blame] | 29 | // rollup) and auto-triggering-on-file-change rules via node-watch. |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 30 | // When invoked without any argument (e.g., for production builds), this script |
| 31 | // just runs all the build tasks serially. It doesn't to do any mtime-based |
| 32 | // check, it always re-runs all the tasks. |
Primiano Tucci | 9b56703 | 2021-02-12 14:18:12 +0100 | [diff] [blame] | 33 | // When invoked with --watch, it mounts a pipeline of tasks based on node-watch |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 34 | // and runs them together with tsc --watch and rollup --watch. |
| 35 | // The output directory structure is carefully crafted so that any change to UI |
| 36 | // sources causes cascading triggers of the next steps. |
| 37 | // The overall build graph looks as follows: |
| 38 | // +----------------+ +-----------------------------+ |
| 39 | // | protos/*.proto |----->| pbjs out/tsc/gen/protos.js |--+ |
| 40 | // +----------------+ +-----------------------------+ | |
| 41 | // +-----------------------------+ | |
| 42 | // | pbts out/tsc/gen/protos.d.ts|<-+ |
| 43 | // +-----------------------------+ |
| 44 | // | |
| 45 | // V +-------------------------+ |
| 46 | // +---------+ +-----+ | out/tsc/frontend/*.js | |
| 47 | // | ui/*.ts |------------->| tsc |-> +-------------------------+ +--------+ |
| 48 | // +---------+ +-----+ | out/tsc/controller/*.js |-->| rollup | |
| 49 | // ^ +-------------------------+ +--------+ |
| 50 | // +------------+ | out/tsc/engine/*.js | | |
| 51 | // +-----------+ |*.wasm.js | +-------------------------+ | |
| 52 | // |ninja *.cc |->|*.wasm.d.ts | | |
| 53 | // +-----------+ |*.wasm |-----------------+ | |
| 54 | // +------------+ | | |
| 55 | // V V |
| 56 | // +-----------+ +------+ +------------------------------------------------+ |
| 57 | // | ui/*.scss |->| scss |--->| Final out/dist/ dir | |
| 58 | // +-----------+ +------+ +------------------------------------------------+ |
| 59 | // +----------------------+ | +----------+ +---------+ +--------------------+| |
| 60 | // | src/assets/*.png | | | assets/ | |*.wasm.js| | frontend_bundle.js || |
| 61 | // +----------------------+ | | *.css | |*.wasm | +--------------------+| |
Hector Dearman | 995d191 | 2021-07-13 16:51:38 +0100 | [diff] [blame] | 62 | // | buildtools/typefaces |-->| | *.png | +---------+ | engine_bundle.js || |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 63 | // +----------------------+ | | *.woff2 | +--------------------+| |
Hector Dearman | 995d191 | 2021-07-13 16:51:38 +0100 | [diff] [blame] | 64 | // | buildtools/legacy_tv | | | tv.html | |traceconv_bundle.js || |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 65 | // +----------------------+ | +----------+ +--------------------+| |
| 66 | // +------------------------------------------------+ |
| 67 | |
| 68 | const argparse = require('argparse'); |
Hector Dearman | 969ecb9 | 2022-06-06 12:18:48 +0100 | [diff] [blame] | 69 | const childProcess = require('child_process'); |
Tuchila Octavian | 885f689 | 2021-04-06 10:44:13 +0100 | [diff] [blame] | 70 | const crypto = require('crypto'); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 71 | const fs = require('fs'); |
| 72 | const http = require('http'); |
| 73 | const path = require('path'); |
Primiano Tucci | 9b56703 | 2021-02-12 14:18:12 +0100 | [diff] [blame] | 74 | const fswatch = require('node-watch'); // Like fs.watch(), but works on Linux. |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 75 | const pjoin = path.join; |
| 76 | |
| 77 | const ROOT_DIR = path.dirname(__dirname); // The repo root. |
| 78 | const VERSION_SCRIPT = pjoin(ROOT_DIR, 'tools/write_version_header.py'); |
Hector Dearman | 07ed69a | 2021-08-23 11:12:25 +0100 | [diff] [blame] | 79 | const GEN_IMPORTS_SCRIPT = pjoin(ROOT_DIR, 'tools/gen_ui_imports'); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 80 | |
| 81 | const cfg = { |
| 82 | watch: false, |
| 83 | verbose: false, |
| 84 | debug: false, |
Primiano Tucci | d60b8d8 | 2023-11-15 22:52:10 +0000 | [diff] [blame] | 85 | bigtrace: false, |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 86 | startHttpServer: false, |
Daniele Di Proietto | ff0f6e5 | 2021-11-11 17:53:21 +0000 | [diff] [blame] | 87 | httpServerListenHost: '127.0.0.1', |
| 88 | httpServerListenPort: 10000, |
Hector Dearman | a9545e5 | 2022-05-17 12:23:25 +0100 | [diff] [blame] | 89 | wasmModules: ['trace_processor', 'traceconv'], |
Alexander Timin | b5d09d0 | 2022-07-26 21:10:02 +0000 | [diff] [blame] | 90 | crossOriginIsolation: false, |
Primiano Tucci | 1bbaa53 | 2022-06-08 19:22:45 +0100 | [diff] [blame] | 91 | testFilter: '', |
Ankit Gupta | c290453 | 2022-10-07 18:57:39 +0530 | [diff] [blame] | 92 | noOverrideGnArgs: false, |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 93 | |
| 94 | // The fields below will be changed by main() after cmdline parsing. |
| 95 | // Directory structure: |
Primiano Tucci | 7eead2e | 2021-02-16 11:46:04 +0100 | [diff] [blame] | 96 | // out/xxx/ -> outDir : Root build dir, for both ninja/wasm and UI. |
| 97 | // ui/ -> outUiDir : UI dir. All outputs from this script. |
| 98 | // tsc/ -> outTscDir : Transpiled .ts -> .js. |
| 99 | // gen/ -> outGenDir : Auto-generated .ts/.js (e.g. protos). |
| 100 | // dist/ -> outDistRootDir : Only index.html and service_worker.js |
| 101 | // v1.2/ -> outDistDir : JS bundles and assets |
| 102 | // chrome_extension/ : Chrome extension. |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 103 | outDir: pjoin(ROOT_DIR, 'out/ui'), |
Primiano Tucci | 7eead2e | 2021-02-16 11:46:04 +0100 | [diff] [blame] | 104 | version: '', // v1.2.3, derived from the CHANGELOG + git. |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 105 | outUiDir: '', |
Andrew Shulaev | bb4e6b2 | 2022-07-04 10:18:04 +0100 | [diff] [blame] | 106 | outUiTestArtifactsDir: '', |
Primiano Tucci | 7eead2e | 2021-02-16 11:46:04 +0100 | [diff] [blame] | 107 | outDistRootDir: '', |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 108 | outTscDir: '', |
| 109 | outGenDir: '', |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 110 | outDistDir: '', |
| 111 | outExtDir: '', |
Primiano Tucci | d60b8d8 | 2023-11-15 22:52:10 +0000 | [diff] [blame] | 112 | outBigtraceDistDir: '', |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | const RULES = [ |
Primiano Tucci | 7eead2e | 2021-02-16 11:46:04 +0100 | [diff] [blame] | 116 | {r: /ui\/src\/assets\/index.html/, f: copyIndexHtml}, |
Lalit Maganti | b1d5a98 | 2023-11-17 17:54:12 +0000 | [diff] [blame] | 117 | {r: /ui\/src\/assets\/bigtrace.html/, f: copyBigtraceHtml}, |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 118 | {r: /ui\/src\/assets\/((.*)[.]png)/, f: copyAssets}, |
| 119 | {r: /buildtools\/typefaces\/(.+[.]woff2)/, f: copyAssets}, |
| 120 | {r: /buildtools\/catapult_trace_viewer\/(.+(js|html))/, f: copyAssets}, |
| 121 | {r: /ui\/src\/assets\/.+[.]scss/, f: compileScss}, |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 122 | {r: /ui\/src\/chrome_extension\/.*/, f: copyExtensionAssets}, |
Andrew Shulaev | bb4e6b2 | 2022-07-04 10:18:04 +0100 | [diff] [blame] | 123 | { |
| 124 | r: /ui\/src\/test\/diff_viewer\/(.+[.](?:html|js))/, |
| 125 | f: copyUiTestArtifactsAssets, |
| 126 | }, |
Primiano Tucci | 7eead2e | 2021-02-16 11:46:04 +0100 | [diff] [blame] | 127 | {r: /.*\/dist\/.+\/(?!manifest\.json).*/, f: genServiceWorkerManifestJson}, |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 128 | {r: /.*\/dist\/.*/, f: notifyLiveServer}, |
| 129 | ]; |
| 130 | |
Hector Dearman | 969ecb9 | 2022-06-06 12:18:48 +0100 | [diff] [blame] | 131 | const tasks = []; |
| 132 | let tasksTot = 0; |
| 133 | let tasksRan = 0; |
| 134 | const httpWatches = []; |
| 135 | const tStart = Date.now(); |
| 136 | const subprocesses = []; |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 137 | |
Primiano Tucci | a60ef18 | 2021-06-11 15:37:12 +0100 | [diff] [blame] | 138 | async function main() { |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 139 | const parser = new argparse.ArgumentParser(); |
Hector Dearman | 04750c1 | 2023-03-27 15:14:40 +0100 | [diff] [blame] | 140 | parser.add_argument('--out', {help: 'Output directory'}); |
| 141 | parser.add_argument('--watch', '-w', {action: 'store_true'}); |
| 142 | parser.add_argument('--serve', '-s', {action: 'store_true'}); |
| 143 | parser.add_argument('--serve-host', {help: '--serve bind host'}); |
| 144 | parser.add_argument('--serve-port', {help: '--serve bind port', type: 'int'}); |
| 145 | parser.add_argument('--verbose', '-v', {action: 'store_true'}); |
| 146 | parser.add_argument('--no-build', '-n', {action: 'store_true'}); |
| 147 | parser.add_argument('--no-wasm', '-W', {action: 'store_true'}); |
| 148 | parser.add_argument('--run-unittests', '-t', {action: 'store_true'}); |
| 149 | parser.add_argument('--run-integrationtests', '-T', {action: 'store_true'}); |
| 150 | parser.add_argument('--debug', '-d', {action: 'store_true'}); |
Primiano Tucci | d60b8d8 | 2023-11-15 22:52:10 +0000 | [diff] [blame] | 151 | parser.add_argument('--bigtrace', {action: 'store_true'}); |
Hector Dearman | 04750c1 | 2023-03-27 15:14:40 +0100 | [diff] [blame] | 152 | parser.add_argument('--interactive', '-i', {action: 'store_true'}); |
| 153 | parser.add_argument('--rebaseline', '-r', {action: 'store_true'}); |
| 154 | parser.add_argument('--no-depscheck', {action: 'store_true'}); |
| 155 | parser.add_argument('--cross-origin-isolation', {action: 'store_true'}); |
| 156 | parser.add_argument('--test-filter', '-f', { |
| 157 | help: 'filter Jest tests by regex, e.g. \'chrome_render\'', |
| 158 | }); |
| 159 | parser.add_argument('--no-override-gn-args', {action: 'store_true'}); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 160 | |
Hector Dearman | 04750c1 | 2023-03-27 15:14:40 +0100 | [diff] [blame] | 161 | const args = parser.parse_args(); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 162 | const clean = !args.no_build; |
| 163 | cfg.outDir = path.resolve(ensureDir(args.out || cfg.outDir)); |
| 164 | cfg.outUiDir = ensureDir(pjoin(cfg.outDir, 'ui'), clean); |
Andrew Shulaev | bb4e6b2 | 2022-07-04 10:18:04 +0100 | [diff] [blame] | 165 | cfg.outUiTestArtifactsDir = ensureDir(pjoin(cfg.outDir, 'ui-test-artifacts')); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 166 | cfg.outExtDir = ensureDir(pjoin(cfg.outUiDir, 'chrome_extension')); |
| 167 | cfg.outDistRootDir = ensureDir(pjoin(cfg.outUiDir, 'dist')); |
Primiano Tucci | 7eead2e | 2021-02-16 11:46:04 +0100 | [diff] [blame] | 168 | const proc = exec('python3', [VERSION_SCRIPT, '--stdout'], {stdout: 'pipe'}); |
| 169 | cfg.version = proc.stdout.toString().trim(); |
| 170 | cfg.outDistDir = ensureDir(pjoin(cfg.outDistRootDir, cfg.version)); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 171 | cfg.outTscDir = ensureDir(pjoin(cfg.outUiDir, 'tsc')); |
| 172 | cfg.outGenDir = ensureDir(pjoin(cfg.outUiDir, 'tsc/gen')); |
Primiano Tucci | 1bbaa53 | 2022-06-08 19:22:45 +0100 | [diff] [blame] | 173 | cfg.testFilter = args.test_filter || ''; |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 174 | cfg.watch = !!args.watch; |
| 175 | cfg.verbose = !!args.verbose; |
| 176 | cfg.debug = !!args.debug; |
Primiano Tucci | d60b8d8 | 2023-11-15 22:52:10 +0000 | [diff] [blame] | 177 | cfg.bigtrace = !!args.bigtrace; |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 178 | cfg.startHttpServer = args.serve; |
Ankit Gupta | c290453 | 2022-10-07 18:57:39 +0530 | [diff] [blame] | 179 | cfg.noOverrideGnArgs = !!args.no_override_gn_args; |
Primiano Tucci | d60b8d8 | 2023-11-15 22:52:10 +0000 | [diff] [blame] | 180 | if (args.bigtrace) { |
| 181 | cfg.outBigtraceDistDir = ensureDir(pjoin(cfg.outDistDir, 'bigtrace')); |
| 182 | } |
Daniele Di Proietto | ff0f6e5 | 2021-11-11 17:53:21 +0000 | [diff] [blame] | 183 | if (args.serve_host) { |
Hector Dearman | 969ecb9 | 2022-06-06 12:18:48 +0100 | [diff] [blame] | 184 | cfg.httpServerListenHost = args.serve_host; |
Daniele Di Proietto | ff0f6e5 | 2021-11-11 17:53:21 +0000 | [diff] [blame] | 185 | } |
| 186 | if (args.serve_port) { |
Hector Dearman | 969ecb9 | 2022-06-06 12:18:48 +0100 | [diff] [blame] | 187 | cfg.httpServerListenPort = args.serve_port; |
Daniele Di Proietto | ff0f6e5 | 2021-11-11 17:53:21 +0000 | [diff] [blame] | 188 | } |
Primiano Tucci | a60ef18 | 2021-06-11 15:37:12 +0100 | [diff] [blame] | 189 | if (args.interactive) { |
| 190 | process.env.PERFETTO_UI_TESTS_INTERACTIVE = '1'; |
| 191 | } |
Primiano Tucci | 8bcf608 | 2021-06-11 17:15:22 +0100 | [diff] [blame] | 192 | if (args.rebaseline) { |
| 193 | process.env.PERFETTO_UI_TESTS_REBASELINE = '1'; |
| 194 | } |
Alexander Timin | b5d09d0 | 2022-07-26 21:10:02 +0000 | [diff] [blame] | 195 | if (args.cross_origin_isolation) { |
| 196 | cfg.crossOriginIsolation = true; |
| 197 | } |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 198 | |
| 199 | process.on('SIGINT', () => { |
| 200 | console.log('\nSIGINT received. Killing all child processes and exiting'); |
| 201 | for (const proc of subprocesses) { |
| 202 | if (proc) proc.kill('SIGINT'); |
| 203 | } |
| 204 | process.exit(130); // 130 -> Same behavior of bash when killed by SIGINT. |
| 205 | }); |
| 206 | |
Lalit Maganti | 43bc33d | 2022-03-07 15:27:07 +0000 | [diff] [blame] | 207 | if (!args.no_depscheck) { |
| 208 | // Check that deps are current before starting. |
| 209 | const installBuildDeps = pjoin(ROOT_DIR, 'tools/install-build-deps'); |
| 210 | const checkDepsPath = pjoin(cfg.outDir, '.check_deps'); |
Hector Dearman | c11ab53 | 2022-03-15 18:01:27 +0000 | [diff] [blame] | 211 | let args = [installBuildDeps, `--check-only=${checkDepsPath}`, '--ui']; |
| 212 | |
Hector Dearman | 969ecb9 | 2022-06-06 12:18:48 +0100 | [diff] [blame] | 213 | if (process.platform === 'darwin') { |
| 214 | const result = childProcess.spawnSync('arch', ['-arm64', 'true']); |
Hector Dearman | c11ab53 | 2022-03-15 18:01:27 +0000 | [diff] [blame] | 215 | const isArm64Capable = result.status === 0; |
| 216 | if (isArm64Capable) { |
| 217 | const archArgs = [ |
Hector Dearman | 969ecb9 | 2022-06-06 12:18:48 +0100 | [diff] [blame] | 218 | 'arch', |
| 219 | '-arch', |
| 220 | 'arm64', |
Hector Dearman | c11ab53 | 2022-03-15 18:01:27 +0000 | [diff] [blame] | 221 | ]; |
| 222 | args = archArgs.concat(args); |
| 223 | } |
| 224 | } |
| 225 | const cmd = args.shift(); |
| 226 | exec(cmd, args); |
Lalit Maganti | 43bc33d | 2022-03-07 15:27:07 +0000 | [diff] [blame] | 227 | } |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 228 | |
| 229 | console.log('Entering', cfg.outDir); |
| 230 | process.chdir(cfg.outDir); |
| 231 | |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 232 | // Enqueue empty task. This is needed only for --no-build --serve. The HTTP |
| 233 | // server is started when the task queue reaches quiescence, but it takes at |
| 234 | // least one task for that. |
| 235 | addTask(() => {}); |
| 236 | |
| 237 | if (!args.no_build) { |
Andrew Shulaev | 6cf1462 | 2023-02-02 15:20:36 +0000 | [diff] [blame] | 238 | updateSymlinks(); // Links //ui/out -> //out/xxx/ui/ |
| 239 | |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 240 | buildWasm(args.no_wasm); |
| 241 | scanDir('ui/src/assets'); |
| 242 | scanDir('ui/src/chrome_extension'); |
Andrew Shulaev | bb4e6b2 | 2022-07-04 10:18:04 +0100 | [diff] [blame] | 243 | scanDir('ui/src/test/diff_viewer'); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 244 | scanDir('buildtools/typefaces'); |
| 245 | scanDir('buildtools/catapult_trace_viewer'); |
Hector Dearman | 07ed69a | 2021-08-23 11:12:25 +0100 | [diff] [blame] | 246 | generateImports('ui/src/tracks', 'all_tracks.ts'); |
Steve Golton | 24a0d91 | 2023-07-01 19:34:02 +0100 | [diff] [blame] | 247 | generateImports('ui/src/plugins', 'all_plugins.ts'); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 248 | compileProtos(); |
| 249 | genVersion(); |
| 250 | transpileTsProject('ui'); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 251 | transpileTsProject('ui/src/service_worker'); |
Primiano Tucci | d60b8d8 | 2023-11-15 22:52:10 +0000 | [diff] [blame] | 252 | if (cfg.bigtrace) { |
| 253 | transpileTsProject('ui/src/bigtrace'); |
| 254 | } |
Hector Dearman | 5f9da79 | 2023-03-27 20:12:49 +0100 | [diff] [blame] | 255 | |
| 256 | if (cfg.watch) { |
| 257 | transpileTsProject('ui', {watch: cfg.watch}); |
| 258 | transpileTsProject('ui/src/service_worker', {watch: cfg.watch}); |
Lalit Maganti | b1d5a98 | 2023-11-17 17:54:12 +0000 | [diff] [blame] | 259 | if (cfg.bigtrace) { |
| 260 | transpileTsProject('ui/src/bigtrace', {watch: cfg.watch}); |
| 261 | } |
Hector Dearman | 5f9da79 | 2023-03-27 20:12:49 +0100 | [diff] [blame] | 262 | } |
| 263 | |
Primiano Tucci | 7eead2e | 2021-02-16 11:46:04 +0100 | [diff] [blame] | 264 | bundleJs('rollup.config.js'); |
| 265 | genServiceWorkerManifestJson(); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 266 | |
| 267 | // Watches the /dist. When changed: |
| 268 | // - Notifies the HTTP live reload clients. |
| 269 | // - Regenerates the ServiceWorker file map. |
| 270 | scanDir(cfg.outDistRootDir); |
| 271 | } |
| 272 | |
Primiano Tucci | a60ef18 | 2021-06-11 15:37:12 +0100 | [diff] [blame] | 273 | // We should enter the loop only in watch mode, where tsc and rollup are |
| 274 | // asynchronous because they run in watch mode. |
Andrew Shulaev | 6cf1462 | 2023-02-02 15:20:36 +0000 | [diff] [blame] | 275 | if (args.no_build && !isDistComplete()) { |
| 276 | console.log('No build was requested, but artifacts are not available.'); |
| 277 | console.log('In case of execution error, re-run without --no-build.'); |
| 278 | } |
| 279 | if (!args.no_build) { |
| 280 | const tStart = Date.now(); |
| 281 | while (!isDistComplete()) { |
| 282 | const secs = Math.ceil((Date.now() - tStart) / 1000); |
| 283 | process.stdout.write( |
| 284 | `\t\tWaiting for first build to complete... ${secs} s\r`); |
| 285 | await new Promise((r) => setTimeout(r, 500)); |
| 286 | } |
Primiano Tucci | a60ef18 | 2021-06-11 15:37:12 +0100 | [diff] [blame] | 287 | } |
| 288 | if (cfg.watch) console.log('\nFirst build completed!'); |
| 289 | |
| 290 | if (cfg.startHttpServer) { |
| 291 | startServer(); |
| 292 | } |
| 293 | if (args.run_unittests) { |
| 294 | runTests('jest.unittest.config.js'); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 295 | } |
Primiano Tucci | 8bcf608 | 2021-06-11 17:15:22 +0100 | [diff] [blame] | 296 | if (args.run_integrationtests) { |
| 297 | runTests('jest.integrationtest.config.js'); |
| 298 | } |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | // ----------- |
| 302 | // Build rules |
| 303 | // ----------- |
| 304 | |
Primiano Tucci | a60ef18 | 2021-06-11 15:37:12 +0100 | [diff] [blame] | 305 | function runTests(cfgFile) { |
| 306 | const args = [ |
| 307 | '--rootDir', |
| 308 | cfg.outTscDir, |
| 309 | '--verbose', |
| 310 | '--runInBand', |
| 311 | '--detectOpenHandles', |
| 312 | '--forceExit', |
| 313 | '--projects', |
Hector Dearman | 969ecb9 | 2022-06-06 12:18:48 +0100 | [diff] [blame] | 314 | pjoin(ROOT_DIR, 'ui/config', cfgFile), |
Primiano Tucci | a60ef18 | 2021-06-11 15:37:12 +0100 | [diff] [blame] | 315 | ]; |
Primiano Tucci | 1bbaa53 | 2022-06-08 19:22:45 +0100 | [diff] [blame] | 316 | if (cfg.testFilter.length > 0) { |
| 317 | args.push('-t', cfg.testFilter); |
| 318 | } |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 319 | if (cfg.watch) { |
| 320 | args.push('--watchAll'); |
Hector Dearman | 48d3408 | 2023-06-21 15:43:08 +0100 | [diff] [blame] | 321 | addTask(execModule, ['jest', args, {async: true}]); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 322 | } else { |
Hector Dearman | 48d3408 | 2023-06-21 15:43:08 +0100 | [diff] [blame] | 323 | addTask(execModule, ['jest', args]); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
Lalit Maganti | b1d5a98 | 2023-11-17 17:54:12 +0000 | [diff] [blame] | 327 | function cpHtml(src, filename) { |
| 328 | let html = fs.readFileSync(src).toString(); |
| 329 | // First copy the html as-is into the dist/v1.2.3/ directory. This is |
| 330 | // only used for archival purporses, so one can open |
| 331 | // ui.perfetto.dev/v1.2.3/ to skip the auto-update and channel logic. |
| 332 | fs.writeFileSync(pjoin(cfg.outDistDir, filename), html); |
Primiano Tucci | 7eead2e | 2021-02-16 11:46:04 +0100 | [diff] [blame] | 333 | |
Lalit Maganti | b1d5a98 | 2023-11-17 17:54:12 +0000 | [diff] [blame] | 334 | // Then copy it into the dist/ root by patching the version code. |
| 335 | // TODO(primiano): in next CLs, this script should take a |
| 336 | // --release_map=xxx.json argument, to populate this with multiple channels. |
| 337 | const versionMap = JSON.stringify({'stable': cfg.version}); |
| 338 | const bodyRegex = /data-perfetto_version='[^']*'/; |
| 339 | html = html.replace(bodyRegex, `data-perfetto_version='${versionMap}'`); |
| 340 | fs.writeFileSync(pjoin(cfg.outDistRootDir, filename), html); |
| 341 | } |
| 342 | |
| 343 | function copyIndexHtml(src) { |
| 344 | addTask(cpHtml, [src, 'index.html']); |
| 345 | } |
| 346 | |
| 347 | function copyBigtraceHtml(src) { |
| 348 | if (cfg.bigtrace) { |
| 349 | addTask(cpHtml, [src, 'bigtrace.html']); |
| 350 | } |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | function copyAssets(src, dst) { |
| 354 | addTask(cp, [src, pjoin(cfg.outDistDir, 'assets', dst)]); |
Lalit Maganti | ac807c9 | 2023-12-04 12:19:43 +0000 | [diff] [blame] | 355 | if (cfg.bigtrace) { |
| 356 | addTask(cp, [src, pjoin(cfg.outBigtraceDistDir, 'assets', dst)]); |
| 357 | } |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 358 | } |
| 359 | |
Andrew Shulaev | bb4e6b2 | 2022-07-04 10:18:04 +0100 | [diff] [blame] | 360 | function copyUiTestArtifactsAssets(src, dst) { |
| 361 | addTask(cp, [src, pjoin(cfg.outUiTestArtifactsDir, dst)]); |
| 362 | } |
| 363 | |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 364 | function compileScss() { |
| 365 | const src = pjoin(ROOT_DIR, 'ui/src/assets/perfetto.scss'); |
| 366 | const dst = pjoin(cfg.outDistDir, 'perfetto.css'); |
| 367 | // In watch mode, don't exit(1) if scss fails. It can easily happen by |
Tuchila Octavian | 885f689 | 2021-04-06 10:44:13 +0100 | [diff] [blame] | 368 | // having a typo in the css. It will still print an error. |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 369 | const noErrCheck = !!cfg.watch; |
Hector Dearman | 48d3408 | 2023-06-21 15:43:08 +0100 | [diff] [blame] | 370 | const args = [src, dst]; |
| 371 | if (!cfg.verbose) { |
| 372 | args.unshift('--quiet'); |
| 373 | } |
| 374 | addTask(execModule, ['sass', args, {noErrCheck}]); |
Lalit Maganti | b1d5a98 | 2023-11-17 17:54:12 +0000 | [diff] [blame] | 375 | if (cfg.bigtrace) { |
| 376 | addTask(cp, [dst, pjoin(cfg.outBigtraceDistDir, 'perfetto.css')]); |
| 377 | } |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | function compileProtos() { |
| 381 | const dstJs = pjoin(cfg.outGenDir, 'protos.js'); |
| 382 | const dstTs = pjoin(cfg.outGenDir, 'protos.d.ts'); |
Hector Dearman | 5f9da79 | 2023-03-27 20:12:49 +0100 | [diff] [blame] | 383 | // We've ended up pulling in all the protos (via trace.proto, |
| 384 | // trace_packet.proto) below which means |dstJs| ends up being |
| 385 | // 23k lines/12mb. We should probably not do that. |
| 386 | // TODO(hjd): Figure out how to use lazy with pbjs/pbts. |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 387 | const inputs = [ |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 388 | 'protos/perfetto/common/trace_stats.proto', |
| 389 | 'protos/perfetto/common/tracing_service_capabilities.proto', |
| 390 | 'protos/perfetto/config/perfetto_config.proto', |
| 391 | 'protos/perfetto/ipc/consumer_port.proto', |
| 392 | 'protos/perfetto/ipc/wire_protocol.proto', |
| 393 | 'protos/perfetto/metrics/metrics.proto', |
Alexander Timin | b510291 | 2022-10-12 22:18:35 +0100 | [diff] [blame] | 394 | 'protos/perfetto/trace/perfetto/perfetto_metatrace.proto', |
| 395 | 'protos/perfetto/trace/trace.proto', |
| 396 | 'protos/perfetto/trace/trace_packet.proto', |
| 397 | 'protos/perfetto/trace_processor/trace_processor.proto', |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 398 | ]; |
Hector Dearman | 4a9b692 | 2022-10-19 18:07:11 +0100 | [diff] [blame] | 399 | // Can't put --no-comments here - The comments are load bearing for |
| 400 | // the pbts invocation which follows. |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 401 | const pbjsArgs = [ |
Hector Dearman | 4a9b692 | 2022-10-19 18:07:11 +0100 | [diff] [blame] | 402 | '--no-beautify', |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 403 | '--force-number', |
Hector Dearman | 5f9da79 | 2023-03-27 20:12:49 +0100 | [diff] [blame] | 404 | '--no-delimited', |
| 405 | '--no-verify', |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 406 | '-t', |
| 407 | 'static-module', |
| 408 | '-w', |
| 409 | 'commonjs', |
| 410 | '-p', |
| 411 | ROOT_DIR, |
| 412 | '-o', |
Hector Dearman | 969ecb9 | 2022-06-06 12:18:48 +0100 | [diff] [blame] | 413 | dstJs, |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 414 | ].concat(inputs); |
Hector Dearman | 48d3408 | 2023-06-21 15:43:08 +0100 | [diff] [blame] | 415 | addTask(execModule, ['pbjs', pbjsArgs]); |
Hector Dearman | 5f9da79 | 2023-03-27 20:12:49 +0100 | [diff] [blame] | 416 | |
| 417 | // Note: If you are looking into slowness of pbts it is not pbts |
| 418 | // itself that is slow. It invokes jsdoc to parse the comments out of |
| 419 | // the |dstJs| with https://github.com/hegemonic/catharsis which is |
| 420 | // pinning a CPU core the whole time. |
Hector Dearman | 4a9b692 | 2022-10-19 18:07:11 +0100 | [diff] [blame] | 421 | const pbtsArgs = ['--no-comments', '-p', ROOT_DIR, '-o', dstTs, dstJs]; |
Hector Dearman | 48d3408 | 2023-06-21 15:43:08 +0100 | [diff] [blame] | 422 | addTask(execModule, ['pbts', pbtsArgs]); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 423 | } |
| 424 | |
Hector Dearman | 07ed69a | 2021-08-23 11:12:25 +0100 | [diff] [blame] | 425 | function generateImports(dir, name) { |
| 426 | // We have to use the symlink (ui/src/gen) rather than cfg.outGenDir |
| 427 | // below since we want to generate the correct relative imports. For example: |
| 428 | // ui/src/frontend/foo.ts |
| 429 | // import '../gen/all_plugins.ts'; |
| 430 | // ui/src/gen/all_plugins.ts (aka ui/out/tsc/gen/all_plugins.ts) |
| 431 | // import '../frontend/some_plugin.ts'; |
| 432 | const dstTs = pjoin(ROOT_DIR, 'ui/src/gen', name); |
| 433 | const inputDir = pjoin(ROOT_DIR, dir); |
| 434 | const args = [GEN_IMPORTS_SCRIPT, inputDir, '--out', dstTs]; |
| 435 | addTask(exec, ['python3', args]); |
| 436 | } |
| 437 | |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 438 | // Generates a .ts source that defines the VERSION and SCM_REVISION constants. |
| 439 | function genVersion() { |
| 440 | const cmd = 'python3'; |
| 441 | const args = |
Tuchila Octavian | 885f689 | 2021-04-06 10:44:13 +0100 | [diff] [blame] | 442 | [VERSION_SCRIPT, '--ts_out', pjoin(cfg.outGenDir, 'perfetto_version.ts')]; |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 443 | addTask(exec, [cmd, args]); |
| 444 | } |
| 445 | |
Primiano Tucci | 7eead2e | 2021-02-16 11:46:04 +0100 | [diff] [blame] | 446 | function updateSymlinks() { |
Primiano Tucci | a60ef18 | 2021-06-11 15:37:12 +0100 | [diff] [blame] | 447 | // /ui/out -> /out/ui. |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 448 | mklink(cfg.outUiDir, pjoin(ROOT_DIR, 'ui/out')); |
Primiano Tucci | 7eead2e | 2021-02-16 11:46:04 +0100 | [diff] [blame] | 449 | |
Hector Dearman | 1811ec4 | 2022-05-26 14:45:18 +0100 | [diff] [blame] | 450 | // /ui/src/gen -> /out/ui/ui/tsc/gen) |
Hector Dearman | 969ecb9 | 2022-06-06 12:18:48 +0100 | [diff] [blame] | 451 | mklink(cfg.outGenDir, pjoin(ROOT_DIR, 'ui/src/gen')); |
Hector Dearman | 1811ec4 | 2022-05-26 14:45:18 +0100 | [diff] [blame] | 452 | |
Primiano Tucci | a60ef18 | 2021-06-11 15:37:12 +0100 | [diff] [blame] | 453 | // /out/ui/test/data -> /test/data (For UI tests). |
| 454 | mklink( |
| 455 | pjoin(ROOT_DIR, 'test/data'), |
| 456 | pjoin(ensureDir(pjoin(cfg.outDir, 'test')), 'data')); |
| 457 | |
Primiano Tucci | 7eead2e | 2021-02-16 11:46:04 +0100 | [diff] [blame] | 458 | // Creates a out/dist_version -> out/dist/v1.2.3 symlink, so rollup config |
| 459 | // can point to that without having to know the current version number. |
| 460 | mklink( |
| 461 | path.relative(cfg.outUiDir, cfg.outDistDir), |
| 462 | pjoin(cfg.outUiDir, 'dist_version')); |
| 463 | |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 464 | mklink( |
Hector Dearman | 969ecb9 | 2022-06-06 12:18:48 +0100 | [diff] [blame] | 465 | pjoin(ROOT_DIR, 'ui/node_modules'), pjoin(cfg.outTscDir, 'node_modules')); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 466 | } |
| 467 | |
Hector Dearman | a9545e5 | 2022-05-17 12:23:25 +0100 | [diff] [blame] | 468 | // Invokes ninja for building the {trace_processor, traceconv} Wasm modules. |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 469 | // It copies the .wasm directly into the out/dist/ dir, and the .js/.ts into |
| 470 | // out/tsc/, so the typescript compiler and the bundler can pick them up. |
| 471 | function buildWasm(skipWasmBuild) { |
| 472 | if (!skipWasmBuild) { |
Ankit Gupta | c290453 | 2022-10-07 18:57:39 +0530 | [diff] [blame] | 473 | if (!cfg.noOverrideGnArgs) { |
| 474 | const gnArgs = ['gen', `--args=is_debug=${cfg.debug}`, cfg.outDir]; |
| 475 | addTask(exec, [pjoin(ROOT_DIR, 'tools/gn'), gnArgs]); |
| 476 | } |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 477 | |
Tuchila Octavian | 885f689 | 2021-04-06 10:44:13 +0100 | [diff] [blame] | 478 | const ninjaArgs = ['-C', cfg.outDir]; |
Hector Dearman | 969ecb9 | 2022-06-06 12:18:48 +0100 | [diff] [blame] | 479 | ninjaArgs.push(...cfg.wasmModules.map((x) => `${x}_wasm`)); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 480 | addTask(exec, [pjoin(ROOT_DIR, 'tools/ninja'), ninjaArgs]); |
| 481 | } |
| 482 | |
| 483 | const wasmOutDir = pjoin(cfg.outDir, 'wasm'); |
| 484 | for (const wasmMod of cfg.wasmModules) { |
| 485 | // The .wasm file goes directly into the dist dir (also .map in debug) |
| 486 | for (const ext of ['.wasm'].concat(cfg.debug ? ['.wasm.map'] : [])) { |
| 487 | const src = `${wasmOutDir}/${wasmMod}${ext}`; |
| 488 | addTask(cp, [src, pjoin(cfg.outDistDir, wasmMod + ext)]); |
| 489 | } |
| 490 | // The .js / .ts go into intermediates, they will be bundled by rollup. |
| 491 | for (const ext of ['.js', '.d.ts']) { |
| 492 | const fname = `${wasmMod}${ext}`; |
| 493 | addTask(cp, [pjoin(wasmOutDir, fname), pjoin(cfg.outGenDir, fname)]); |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | // This transpiles all the sources (frontend, controller, engine, extension) in |
| 499 | // one go. The only project that has a dedicated invocation is service_worker. |
Hector Dearman | 5f9da79 | 2023-03-27 20:12:49 +0100 | [diff] [blame] | 500 | function transpileTsProject(project, options) { |
Primiano Tucci | 7eead2e | 2021-02-16 11:46:04 +0100 | [diff] [blame] | 501 | const args = ['--project', pjoin(ROOT_DIR, project)]; |
Hector Dearman | 5f9da79 | 2023-03-27 20:12:49 +0100 | [diff] [blame] | 502 | |
Hector Dearman | 7b47a24 | 2023-03-28 13:31:41 +0100 | [diff] [blame] | 503 | if (options !== undefined && options.watch) { |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 504 | args.push('--watch', '--preserveWatchOutput'); |
Hector Dearman | 48d3408 | 2023-06-21 15:43:08 +0100 | [diff] [blame] | 505 | addTask(execModule, ['tsc', args, {async: true}]); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 506 | } else { |
Hector Dearman | 48d3408 | 2023-06-21 15:43:08 +0100 | [diff] [blame] | 507 | addTask(execModule, ['tsc', args]); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
| 511 | // Creates the three {frontend, controller, engine}_bundle.js in one invocation. |
| 512 | function bundleJs(cfgName) { |
Tuchila Octavian | 885f689 | 2021-04-06 10:44:13 +0100 | [diff] [blame] | 513 | const rcfg = pjoin(ROOT_DIR, 'ui/config', cfgName); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 514 | const args = ['-c', rcfg, '--no-indent']; |
Primiano Tucci | d60b8d8 | 2023-11-15 22:52:10 +0000 | [diff] [blame] | 515 | if (cfg.bigtrace) { |
| 516 | args.push('--environment', 'ENABLE_BIGTRACE:true'); |
| 517 | } |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 518 | args.push(...(cfg.verbose ? [] : ['--silent'])); |
| 519 | if (cfg.watch) { |
Hector Dearman | 5f9da79 | 2023-03-27 20:12:49 +0100 | [diff] [blame] | 520 | // --waitForBundleInput is sadly quite busted so it is required ts |
| 521 | // has build at least once before invoking this. |
| 522 | args.push('--watch', '--no-watch.clearScreen'); |
Hector Dearman | 48d3408 | 2023-06-21 15:43:08 +0100 | [diff] [blame] | 523 | addTask(execModule, ['rollup', args, {async: true}]); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 524 | } else { |
Hector Dearman | 48d3408 | 2023-06-21 15:43:08 +0100 | [diff] [blame] | 525 | addTask(execModule, ['rollup', args]); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
Primiano Tucci | 7eead2e | 2021-02-16 11:46:04 +0100 | [diff] [blame] | 529 | function genServiceWorkerManifestJson() { |
Hector Dearman | 969ecb9 | 2022-06-06 12:18:48 +0100 | [diff] [blame] | 530 | function makeManifest() { |
Primiano Tucci | 7eead2e | 2021-02-16 11:46:04 +0100 | [diff] [blame] | 531 | const manifest = {resources: {}}; |
| 532 | // When building the subresource manifest skip source maps, the manifest |
| 533 | // itself and the copy of the index.html which is copied under /v1.2.3/. |
| 534 | // The root /index.html will be fetched by service_worker.js separately. |
| 535 | const skipRegex = /(\.map|manifest\.json|index.html)$/; |
Hector Dearman | 969ecb9 | 2022-06-06 12:18:48 +0100 | [diff] [blame] | 536 | walk(cfg.outDistDir, (absPath) => { |
Primiano Tucci | 7eead2e | 2021-02-16 11:46:04 +0100 | [diff] [blame] | 537 | const contents = fs.readFileSync(absPath); |
| 538 | const relPath = path.relative(cfg.outDistDir, absPath); |
| 539 | const b64 = crypto.createHash('sha256').update(contents).digest('base64'); |
| 540 | manifest.resources[relPath] = 'sha256-' + b64; |
| 541 | }, skipRegex); |
| 542 | const manifestJson = JSON.stringify(manifest, null, 2); |
| 543 | fs.writeFileSync(pjoin(cfg.outDistDir, 'manifest.json'), manifestJson); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 544 | } |
Hector Dearman | 969ecb9 | 2022-06-06 12:18:48 +0100 | [diff] [blame] | 545 | addTask(makeManifest, []); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | function startServer() { |
Daniele Di Proietto | ff0f6e5 | 2021-11-11 17:53:21 +0000 | [diff] [blame] | 549 | console.log( |
| 550 | 'Starting HTTP server on', |
| 551 | `http://${cfg.httpServerListenHost}:${cfg.httpServerListenPort}`); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 552 | http.createServer(function(req, res) { |
| 553 | console.debug(req.method, req.url); |
| 554 | let uri = req.url.split('?', 1)[0]; |
| 555 | if (uri.endsWith('/')) { |
| 556 | uri += 'index.html'; |
| 557 | } |
| 558 | |
| 559 | if (uri === '/live_reload') { |
| 560 | // Implements the Server-Side-Events protocol. |
| 561 | const head = { |
| 562 | 'Content-Type': 'text/event-stream', |
| 563 | 'Connection': 'keep-alive', |
Hector Dearman | 969ecb9 | 2022-06-06 12:18:48 +0100 | [diff] [blame] | 564 | 'Cache-Control': 'no-cache', |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 565 | }; |
| 566 | res.writeHead(200, head); |
| 567 | const arrayIdx = httpWatches.length; |
| 568 | // We never remove from the array, the delete leaves an undefined item |
| 569 | // around. It makes keeping track of the index easier at the cost of a |
| 570 | // small leak. |
| 571 | httpWatches.push(res); |
| 572 | req.on('close', () => delete httpWatches[arrayIdx]); |
| 573 | return; |
| 574 | } |
| 575 | |
Tuchila Octavian | 1d423b2 | 2021-07-01 09:39:54 +0100 | [diff] [blame] | 576 | let absPath = path.normalize(path.join(cfg.outDistRootDir, uri)); |
| 577 | // We want to be able to use the data in '/test/' for e2e tests. |
| 578 | // However, we don't want do create a symlink into the 'dist/' dir, |
| 579 | // because 'dist/' gets shipped on the production server. |
| 580 | if (uri.startsWith('/test/')) { |
| 581 | absPath = pjoin(ROOT_DIR, uri); |
| 582 | } |
| 583 | |
Primiano Tucci | d24cfc5 | 2022-02-24 21:24:49 +0000 | [diff] [blame] | 584 | // Don't serve contents outside of the project root (b/221101533). |
| 585 | if (path.relative(ROOT_DIR, absPath).startsWith('..')) { |
| 586 | res.writeHead(403); |
| 587 | res.end('403 Forbidden - Request path outside of the repo root'); |
| 588 | return; |
| 589 | } |
| 590 | |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 591 | fs.readFile(absPath, function(err, data) { |
| 592 | if (err) { |
| 593 | res.writeHead(404); |
| 594 | res.end(JSON.stringify(err)); |
| 595 | return; |
| 596 | } |
| 597 | |
| 598 | const mimeMap = { |
| 599 | 'html': 'text/html', |
| 600 | 'css': 'text/css', |
| 601 | 'js': 'application/javascript', |
| 602 | 'wasm': 'application/wasm', |
| 603 | }; |
| 604 | const ext = uri.split('.').pop(); |
| 605 | const cType = mimeMap[ext] || 'octect/stream'; |
| 606 | const head = { |
| 607 | 'Content-Type': cType, |
| 608 | 'Content-Length': data.length, |
| 609 | 'Last-Modified': fs.statSync(absPath).mtime.toUTCString(), |
| 610 | 'Cache-Control': 'no-cache', |
| 611 | }; |
Alexander Timin | b5d09d0 | 2022-07-26 21:10:02 +0000 | [diff] [blame] | 612 | if (cfg.crossOriginIsolation) { |
| 613 | head['Cross-Origin-Opener-Policy'] = 'same-origin'; |
| 614 | head['Cross-Origin-Embedder-Policy'] = 'require-corp'; |
| 615 | } |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 616 | res.writeHead(200, head); |
Primiano Tucci | 7eead2e | 2021-02-16 11:46:04 +0100 | [diff] [blame] | 617 | res.write(data); |
| 618 | res.end(); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 619 | }); |
| 620 | }) |
Daniele Di Proietto | ff0f6e5 | 2021-11-11 17:53:21 +0000 | [diff] [blame] | 621 | .listen(cfg.httpServerListenPort, cfg.httpServerListenHost); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 622 | } |
| 623 | |
Primiano Tucci | a60ef18 | 2021-06-11 15:37:12 +0100 | [diff] [blame] | 624 | function isDistComplete() { |
| 625 | const requiredArtifacts = [ |
Primiano Tucci | a60ef18 | 2021-06-11 15:37:12 +0100 | [diff] [blame] | 626 | 'frontend_bundle.js', |
| 627 | 'engine_bundle.js', |
Hector Dearman | 995d191 | 2021-07-13 16:51:38 +0100 | [diff] [blame] | 628 | 'traceconv_bundle.js', |
Primiano Tucci | a60ef18 | 2021-06-11 15:37:12 +0100 | [diff] [blame] | 629 | 'trace_processor.wasm', |
| 630 | 'perfetto.css', |
| 631 | ]; |
| 632 | const relPaths = new Set(); |
Hector Dearman | 969ecb9 | 2022-06-06 12:18:48 +0100 | [diff] [blame] | 633 | walk(cfg.outDistDir, (absPath) => { |
Primiano Tucci | a60ef18 | 2021-06-11 15:37:12 +0100 | [diff] [blame] | 634 | relPaths.add(path.relative(cfg.outDistDir, absPath)); |
| 635 | }); |
| 636 | for (const fName of requiredArtifacts) { |
| 637 | if (!relPaths.has(fName)) return false; |
| 638 | } |
| 639 | return true; |
| 640 | } |
| 641 | |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 642 | // Called whenever a change in the out/dist directory is detected. It sends a |
| 643 | // Server-Side-Event to the live_reload.ts script. |
| 644 | function notifyLiveServer(changedFile) { |
| 645 | for (const cli of httpWatches) { |
| 646 | if (cli === undefined) continue; |
| 647 | cli.write( |
| 648 | 'data: ' + path.relative(cfg.outDistRootDir, changedFile) + '\n\n'); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | function copyExtensionAssets() { |
| 653 | addTask(cp, [ |
| 654 | pjoin(ROOT_DIR, 'ui/src/assets/logo-128.png'), |
Hector Dearman | 969ecb9 | 2022-06-06 12:18:48 +0100 | [diff] [blame] | 655 | pjoin(cfg.outExtDir, 'logo-128.png'), |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 656 | ]); |
| 657 | addTask(cp, [ |
| 658 | pjoin(ROOT_DIR, 'ui/src/chrome_extension/manifest.json'), |
Hector Dearman | 969ecb9 | 2022-06-06 12:18:48 +0100 | [diff] [blame] | 659 | pjoin(cfg.outExtDir, 'manifest.json'), |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 660 | ]); |
| 661 | } |
| 662 | |
| 663 | // ----------------------- |
| 664 | // Task chaining functions |
| 665 | // ----------------------- |
| 666 | |
| 667 | function addTask(func, args) { |
| 668 | const task = new Task(func, args); |
| 669 | for (const t of tasks) { |
| 670 | if (t.identity === task.identity) { |
| 671 | return; |
| 672 | } |
| 673 | } |
| 674 | tasks.push(task); |
| 675 | setTimeout(runTasks, 0); |
| 676 | } |
| 677 | |
| 678 | function runTasks() { |
| 679 | const snapTasks = tasks.splice(0); // snap = std::move(tasks). |
| 680 | tasksTot += snapTasks.length; |
| 681 | for (const task of snapTasks) { |
| 682 | const DIM = '\u001b[2m'; |
| 683 | const BRT = '\u001b[37m'; |
| 684 | const RST = '\u001b[0m'; |
| 685 | const ms = (new Date(Date.now() - tStart)).toISOString().slice(17, -1); |
| 686 | const ts = `[${DIM}${ms}${RST}]`; |
| 687 | const descr = task.description.substr(0, 80); |
| 688 | console.log(`${ts} ${BRT}${++tasksRan}/${tasksTot}${RST}\t${descr}`); |
Hector Dearman | 969ecb9 | 2022-06-06 12:18:48 +0100 | [diff] [blame] | 689 | task.func.apply(/* this=*/ undefined, task.args); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 690 | } |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | // Executes all the RULES that match the given |absPath|. |
| 694 | function scanFile(absPath) { |
| 695 | console.assert(fs.existsSync(absPath)); |
| 696 | console.assert(path.isAbsolute(absPath)); |
| 697 | const normPath = path.relative(ROOT_DIR, absPath); |
| 698 | for (const rule of RULES) { |
| 699 | const match = rule.r.exec(normPath); |
| 700 | if (!match || match[0] !== normPath) continue; |
| 701 | const captureGroup = match.length > 1 ? match[1] : undefined; |
| 702 | rule.f(absPath, captureGroup); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | // Walks the passed |dir| recursively and, for each file, invokes the matching |
Primiano Tucci | 9b56703 | 2021-02-12 14:18:12 +0100 | [diff] [blame] | 707 | // RULES. If --watch is used, it also installs a fswatch() and re-triggers the |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 708 | // matching RULES on each file change. |
| 709 | function scanDir(dir, regex) { |
Hector Dearman | 969ecb9 | 2022-06-06 12:18:48 +0100 | [diff] [blame] | 710 | const filterFn = regex ? (absPath) => regex.test(absPath) : () => true; |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 711 | const absDir = path.isAbsolute(dir) ? dir : pjoin(ROOT_DIR, dir); |
| 712 | // Add a fs watch if in watch mode. |
| 713 | if (cfg.watch) { |
Primiano Tucci | 9a92cf7 | 2021-02-13 17:42:34 +0100 | [diff] [blame] | 714 | fswatch(absDir, {recursive: true}, (_eventType, filePath) => { |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 715 | if (!filterFn(filePath)) return; |
| 716 | if (cfg.verbose) { |
| 717 | console.log('File change detected', _eventType, filePath); |
| 718 | } |
| 719 | if (fs.existsSync(filePath)) { |
| 720 | scanFile(filePath, filterFn); |
| 721 | } |
| 722 | }); |
| 723 | } |
Hector Dearman | 969ecb9 | 2022-06-06 12:18:48 +0100 | [diff] [blame] | 724 | walk(absDir, (f) => { |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 725 | if (filterFn(f)) scanFile(f); |
| 726 | }); |
| 727 | } |
| 728 | |
| 729 | function exec(cmd, args, opts) { |
| 730 | opts = opts || {}; |
| 731 | opts.stdout = opts.stdout || 'inherit'; |
| 732 | if (cfg.verbose) console.log(`${cmd} ${args.join(' ')}\n`); |
| 733 | const spwOpts = {cwd: cfg.outDir, stdio: ['ignore', opts.stdout, 'inherit']}; |
| 734 | const checkExitCode = (code, signal) => { |
| 735 | if (signal === 'SIGINT' || signal === 'SIGTERM') return; |
| 736 | if (code !== 0 && !opts.noErrCheck) { |
| 737 | console.error(`${cmd} ${args.join(' ')} failed with code ${code}`); |
| 738 | process.exit(1); |
| 739 | } |
| 740 | }; |
| 741 | if (opts.async) { |
Hector Dearman | 969ecb9 | 2022-06-06 12:18:48 +0100 | [diff] [blame] | 742 | const proc = childProcess.spawn(cmd, args, spwOpts); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 743 | const procIndex = subprocesses.length; |
| 744 | subprocesses.push(proc); |
| 745 | return new Promise((resolve, _reject) => { |
| 746 | proc.on('exit', (code, signal) => { |
| 747 | delete subprocesses[procIndex]; |
| 748 | checkExitCode(code, signal); |
| 749 | resolve(); |
| 750 | }); |
| 751 | }); |
| 752 | } else { |
Hector Dearman | 969ecb9 | 2022-06-06 12:18:48 +0100 | [diff] [blame] | 753 | const spawnRes = childProcess.spawnSync(cmd, args, spwOpts); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 754 | checkExitCode(spawnRes.status, spawnRes.signal); |
| 755 | return spawnRes; |
| 756 | } |
| 757 | } |
| 758 | |
Hector Dearman | 48d3408 | 2023-06-21 15:43:08 +0100 | [diff] [blame] | 759 | function execModule(module, args, opts) { |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 760 | const modPath = pjoin(ROOT_DIR, 'ui/node_modules/.bin', module); |
Hector Dearman | 48d3408 | 2023-06-21 15:43:08 +0100 | [diff] [blame] | 761 | return exec(modPath, args || [], opts); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | // ------------------------------------------ |
| 765 | // File system & subprocess utility functions |
| 766 | // ------------------------------------------ |
| 767 | |
| 768 | class Task { |
| 769 | constructor(func, args) { |
| 770 | this.func = func; |
| 771 | this.args = args || []; |
| 772 | // |identity| is used to dedupe identical tasks in the queue. |
| 773 | this.identity = JSON.stringify([this.func.name, this.args]); |
| 774 | } |
| 775 | |
| 776 | get description() { |
| 777 | const ret = this.func.name.startsWith('exec') ? [] : [this.func.name]; |
Hector Dearman | 969ecb9 | 2022-06-06 12:18:48 +0100 | [diff] [blame] | 778 | const flattenedArgs = [].concat(...this.args); |
Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 779 | for (const arg of flattenedArgs) { |
| 780 | const argStr = `${arg}`; |
| 781 | if (argStr.startsWith('/')) { |
| 782 | ret.push(path.relative(cfg.outDir, arg)); |
| 783 | } else { |
| 784 | ret.push(argStr); |
| 785 | } |
| 786 | } |
| 787 | return ret.join(' '); |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | function walk(dir, callback, skipRegex) { |
| 792 | for (const child of fs.readdirSync(dir)) { |
| 793 | const childPath = pjoin(dir, child); |
| 794 | const stat = fs.lstatSync(childPath); |
| 795 | if (skipRegex !== undefined && skipRegex.test(child)) continue; |
| 796 | if (stat.isDirectory()) { |
| 797 | walk(childPath, callback, skipRegex); |
| 798 | } else if (!stat.isSymbolicLink()) { |
| 799 | callback(childPath); |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | function ensureDir(dirPath, clean) { |
| 805 | const exists = fs.existsSync(dirPath); |
| 806 | if (exists && clean) { |
| 807 | console.log('rm', dirPath); |
| 808 | fs.rmSync(dirPath, {recursive: true}); |
| 809 | } |
| 810 | if (!exists || clean) fs.mkdirSync(dirPath, {recursive: true}); |
| 811 | return dirPath; |
| 812 | } |
| 813 | |
| 814 | function cp(src, dst) { |
| 815 | ensureDir(path.dirname(dst)); |
| 816 | if (cfg.verbose) { |
| 817 | console.log( |
| 818 | 'cp', path.relative(ROOT_DIR, src), '->', path.relative(ROOT_DIR, dst)); |
| 819 | } |
| 820 | fs.copyFileSync(src, dst); |
| 821 | } |
| 822 | |
| 823 | function mklink(src, dst) { |
| 824 | // If the symlink already points to the right place don't touch it. This is |
| 825 | // to avoid changing the mtime of the ui/ dir when unnecessary. |
| 826 | if (fs.existsSync(dst)) { |
| 827 | if (fs.lstatSync(dst).isSymbolicLink() && fs.readlinkSync(dst) === src) { |
| 828 | return; |
| 829 | } else { |
| 830 | fs.unlinkSync(dst); |
| 831 | } |
| 832 | } |
| 833 | fs.symlinkSync(src, dst); |
| 834 | } |
| 835 | |
| 836 | main(); |