Updates
This commit is contained in:
@@ -91,6 +91,18 @@ async function buildOne(host, outRoot) {
|
||||
)
|
||||
}
|
||||
|
||||
// Ensure native helpers are compiled + base64-embedded into the JS bundle
|
||||
console.log('[bare-standalone] building/embedding native helpers…')
|
||||
const { spawnSync } = require('child_process')
|
||||
const nh = spawnSync(process.execPath, [path.join(root, 'scripts', 'build-native-helpers.cjs')], {
|
||||
cwd: root,
|
||||
stdio: 'inherit',
|
||||
env: process.env,
|
||||
})
|
||||
if (nh.status !== 0) {
|
||||
console.warn('[bare-standalone] native helper build failed (JS fallback still works)')
|
||||
}
|
||||
|
||||
const name = 'peardata-server'
|
||||
const outDir = path.join(outRoot, `${name}-${host}`)
|
||||
fs.rmSync(outDir, { recursive: true, force: true })
|
||||
@@ -160,6 +172,20 @@ async function buildOne(host, outRoot) {
|
||||
fs.copyFileSync(binary, flat)
|
||||
binary = flat
|
||||
}
|
||||
// Also ship raw helper next to binary for ops that prefer an external file
|
||||
const pre = path.join(root, 'native', 'prebuilds', `peardata-ebpf-${host}`)
|
||||
const preAlt = path.join(root, 'native', 'prebuilds', 'peardata-ebpf')
|
||||
const helperSrc = fs.existsSync(pre) ? pre : fs.existsSync(preAlt) ? preAlt : null
|
||||
if (helperSrc) {
|
||||
const helperDest = path.join(outDir, 'peardata-ebpf')
|
||||
fs.copyFileSync(helperSrc, helperDest)
|
||||
try {
|
||||
fs.chmodSync(helperDest, 0o755)
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
console.log(`[bare-standalone] shipped helper ${helperDest}`)
|
||||
}
|
||||
console.log(`[bare-standalone] wrote ${binary}`)
|
||||
} else {
|
||||
console.warn(`[bare-standalone] WARN: expected binary not found under ${outDir}`)
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
#!/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()
|
||||
Reference in New Issue
Block a user