|
| 1 | +/** |
| 2 | + * build.mjs — compila el runner host de tests con clang, cross-platform. |
| 3 | + * |
| 4 | + * Por qué Node y no Make: Windows no tiene make nativo y queremos que el |
| 5 | + * suite corra idéntico en mac/win sin instalar MSYS2. clang sí está |
| 6 | + * disponible cross-platform (LLVM oficial). |
| 7 | + * |
| 8 | + * Uso: |
| 9 | + * node tests/engine_host/build.mjs # compila si fuente cambió |
| 10 | + * node tests/engine_host/build.mjs --force # recompila siempre |
| 11 | + * node tests/engine_host/build.mjs --check # solo verifica clang disponible |
| 12 | + * |
| 13 | + * Output: tests/engine_host/runner (mac/linux) o runner.exe (win). |
| 14 | + * |
| 15 | + * Salidas: |
| 16 | + * exit 0 → compilado, listo para ejecutar |
| 17 | + * exit 2 → clang no disponible (skipear los tests engine_host) |
| 18 | + * exit 1 → error de compilación (test FAIL) |
| 19 | + */ |
| 20 | +import { execFileSync, spawnSync } from 'node:child_process' |
| 21 | +import { existsSync, statSync, readdirSync } from 'node:fs' |
| 22 | +import { dirname, join } from 'node:path' |
| 23 | +import { fileURLToPath } from 'node:url' |
| 24 | + |
| 25 | +const __dirname = dirname(fileURLToPath(import.meta.url)) |
| 26 | +const HERE = __dirname |
| 27 | + |
| 28 | +// ── Detectar clang (mac, win, linux) ────────────────────────────────────── |
| 29 | +function findClang() { |
| 30 | + // Probar simplemente "clang" en PATH (más portable) |
| 31 | + const r = spawnSync('clang', ['--version'], { encoding: 'utf8' }) |
| 32 | + if (r.status === 0) return 'clang' |
| 33 | + return null |
| 34 | +} |
| 35 | + |
| 36 | +const clang = findClang() |
| 37 | +if (process.argv.includes('--check')) { |
| 38 | + if (clang) { console.log('clang OK:', clang); process.exit(0) } |
| 39 | + else { console.error('clang no disponible en PATH'); process.exit(2) } |
| 40 | +} |
| 41 | + |
| 42 | +if (!clang) { |
| 43 | + console.error('build.mjs: clang no disponible. Salta tests engine_host.') |
| 44 | + console.error(' macOS: instala Xcode Command Line Tools (xcode-select --install)') |
| 45 | + console.error(' Windows: instala LLVM (https://releases.llvm.org/) o MSYS2') |
| 46 | + console.error(' Linux: apt/yum install clang') |
| 47 | + process.exit(2) |
| 48 | +} |
| 49 | + |
| 50 | +// ── Recolectar fuentes ──────────────────────────────────────────────────── |
| 51 | +const sources = [] |
| 52 | +for (const f of readdirSync(join(HERE, 'lib'))) { |
| 53 | + if (f.endsWith('.c')) sources.push(join(HERE, 'lib', f)) |
| 54 | +} |
| 55 | +sources.push(join(HERE, 'runner.c')) |
| 56 | + |
| 57 | +// ── Output binary (con extensión adecuada al OS) ────────────────────────── |
| 58 | +const exe = process.platform === 'win32' ? 'runner.exe' : 'runner' |
| 59 | +const exePath = join(HERE, exe) |
| 60 | + |
| 61 | +// ── Decisión rebuild ────────────────────────────────────────────────────── |
| 62 | +const force = process.argv.includes('--force') |
| 63 | +let needsRebuild = force || !existsSync(exePath) |
| 64 | +if (!needsRebuild) { |
| 65 | + const exeMtime = statSync(exePath).mtimeMs |
| 66 | + for (const s of sources) { |
| 67 | + if (statSync(s).mtimeMs > exeMtime) { needsRebuild = true; break } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +if (!needsRebuild) { |
| 72 | + // Ya estamos al día. |
| 73 | + process.exit(0) |
| 74 | +} |
| 75 | + |
| 76 | +// ── Compilar y enlazar de un golpe ──────────────────────────────────────── |
| 77 | +// Flags conservadores: c89 (alineado con el motor), warnings altos pero sin |
| 78 | +// -Werror (algún warning legítimo en el motor portado no debería bloquear). |
| 79 | +const flags = [ |
| 80 | + '-std=c89', '-O0', '-g', |
| 81 | + '-Wall', '-Wextra', '-Wno-unused-parameter', |
| 82 | + '-DAGEMKI_HOST_TEST', |
| 83 | + '-I', join(HERE, 'include'), |
| 84 | +] |
| 85 | + |
| 86 | +try { |
| 87 | + execFileSync(clang, [...flags, ...sources, '-o', exePath], { stdio: 'inherit' }) |
| 88 | +} catch (err) { |
| 89 | + console.error('build.mjs: clang falló') |
| 90 | + process.exit(1) |
| 91 | +} |
| 92 | + |
| 93 | +console.log('build.mjs: ok →', exePath) |
| 94 | +process.exit(0) |
0 commit comments