// Modified by MiniMax for MCode Sandbox Runtime. import { spawnSync } from 'node:child_process' import { mkdirSync } from 'node:fs' import { dirname, join } from 'node:path' import { fileURLToPath } from 'node:url' const nodeArchToDir: Record = { x64: 'x64', arm64: 'arm64' } /** * Common preamble for `vendor//build.ts` scripts: platform * guard, arch resolution, derive `SRC` (sibling source dir under * `vendor/`) and `OUT` (per-arch output dir next to the build script), * and create `OUT`. Exits the process on a guard failure. */ export function setup(opts: { /** `import.meta.url` of the calling build script. */ importMetaUrl: string /** Only run on this platform; bail with a clear error otherwise. */ requirePlatform: NodeJS.Platform /** Sibling directory under `vendor/` holding the source to build. */ srcDirName: string /** Permit an explicit SRT_BUILD_ARCH cross-build on another host platform. */ allowCrossPlatform?: boolean }): { SRC: string; OUT: string; arch: string } { if ( process.platform !== opts.requirePlatform && !(opts.allowCrossPlatform && process.env.SRT_BUILD_ARCH) ) { console.error( `${opts.srcDirName} build: ${opts.requirePlatform} only ` + `(running on ${process.platform})`, ) process.exit(1) } const arch = process.env.SRT_BUILD_ARCH ?? nodeArchToDir[process.arch] if (!arch || !Object.values(nodeArchToDir).includes(arch)) { console.error(`${opts.srcDirName} build: unsupported arch ${process.arch}`) process.exit(1) } const here = dirname(fileURLToPath(opts.importMetaUrl)) const SRC = join(here, '..', opts.srcDirName) const OUT = join(here, arch) mkdirSync(OUT, { recursive: true }) return { SRC, OUT, arch } } /** * Spawn `argv` synchronously with inherited stdio. Exits the process * on spawn failure (`r.error`) or non-zero exit / signal termination. */ export function run(argv: string[]): void { const [cmd, ...args] = argv const r = spawnSync(cmd, args, { stdio: 'inherit' }) if (r.error) { console.error(`${argv.join(' ')} failed to spawn: ${r.error.message}`) process.exit(1) } if (r.status !== 0) { console.error(`${argv.join(' ')} exited ${r.status ?? r.signal}`) process.exit(1) } }