Files
peardata/scripts/build-native-helpers.cjs
T
Raven Scott a639b3c953
CI / test (push) Successful in 1m15s
Release rolling / release (push) Successful in 7m6s
Updates
2026-07-18 19:49:57 -04:00

94 lines
2.6 KiB
JavaScript

#!/usr/bin/env node
/**
* Compile native helpers and embed them as base64 into
* server/native/embedded-helpers.js so bare-pack includes them
* inside peardata-server.
*
* node scripts/build-native-helpers.cjs
*/
'use strict'
const fs = require('fs')
const path = require('path')
const { spawnSync } = require('child_process')
const root = path.resolve(__dirname, '..')
const src = path.join(root, 'native', 'peardata-ebpf', 'main.c')
const outDir = path.join(root, 'native', 'prebuilds')
const embedOut = path.join(root, 'server', 'native', 'embedded-helpers.js')
function ensureDir(d) {
fs.mkdirSync(d, { recursive: true })
}
function compile(hostTag) {
ensureDir(outDir)
const outBin = path.join(outDir, `peardata-ebpf-${hostTag}`)
const cc = process.env.CC || 'cc'
const args = ['-O2', '-Wall', '-o', outBin, src]
console.log(`[native] ${cc} ${args.join(' ')}`)
const res = spawnSync(cc, args, { cwd: root, stdio: 'inherit' })
if (res.status !== 0) {
console.warn('[native] compile failed — embedding placeholder stub')
return null
}
try {
fs.chmodSync(outBin, 0o755)
} catch {
// ignore
}
return outBin
}
function hostTag() {
const p = process.platform
const a = process.arch
return `${p}-${a}`
}
function main() {
const tag = hostTag()
let bin = null
if (process.platform === 'linux') {
bin = compile(tag)
} else {
console.warn(`[native] skip compile on ${process.platform} (Linux helper only)`)
}
/** @type {Record<string, string>} */
const helpers = {}
if (bin && fs.existsSync(bin)) {
helpers[`peardata-ebpf-${tag}`] = fs.readFileSync(bin).toString('base64')
// also copy generic name for current host
helpers['peardata-ebpf'] = helpers[`peardata-ebpf-${tag}`]
}
// Keep any previously embedded linux builds if present
try {
const prev = fs.readFileSync(embedOut, 'utf8')
const m = prev.match(/export const EMBEDDED_HELPERS = (\{[\s\S]*?\})\n/)
if (m) {
const old = Function(`return (${m[1]})`)()
for (const [k, v] of Object.entries(old || {})) {
if (!helpers[k] && typeof v === 'string' && v.length > 32) helpers[k] = v
}
}
} catch {
// first run
}
ensureDir(path.dirname(embedOut))
const body = `/* AUTO-GENERATED by scripts/build-native-helpers.cjs — do not edit */
export const EMBEDDED_HELPERS = ${JSON.stringify(helpers, null, 2)}
export const EMBEDDED_HELPER_NAMES = ${JSON.stringify(Object.keys(helpers))}
`
fs.writeFileSync(embedOut, body)
console.log(
`[native] wrote ${embedOut} (${Object.keys(helpers).length} helper(s), ` +
`${Math.round(JSON.stringify(helpers).length / 1024)} KiB json)`
)
}
main()