singularity-forge/native/scripts/build.js
ace-pm 9d739dfa5d Rename GSD→SF: complete rebrand from fork origin
- All gsdDir/gsdRoot/gsdHome → sfDir/sfRootDir/sfHome
- GSDWorkspace* → SFWorkspace* interfaces
- bootstrapGsdProject → bootstrapProject
- runGSDDoctor → runSFDoctor
- GsdClient → SfClient, gsd-client.ts → sf-client.ts
- .gsd/ → .sf/ in all tests, docs, docker, native, vscode
- Auto-migration: headless detects .gsd/ → renames to .sf/
- Deleted gsd-phase-state.ts backward-compat re-export
- Renamed bin/gsd-from-source → bin/sf-from-source
- Updated mintlify docs, github workflows, docker configs
2026-04-15 18:33:47 +02:00

101 lines
2.9 KiB
JavaScript

#!/usr/bin/env node
/**
* Build script for the SF native Rust addon.
*
* Usage:
* node native/scripts/build.js # release build
* node native/scripts/build.js --dev # debug build
*
* Runs `cargo build` in the engine crate directory and copies the resulting
* shared library to `native/addon/` with a `.node` extension so Node.js
* can load it via `require()`.
*/
import { execSync } from "node:child_process";
import * as fs from "node:fs";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const nativeRoot = path.resolve(__dirname, "..");
const engineDir = path.join(nativeRoot, "crates", "engine");
const addonDir = path.join(nativeRoot, "addon");
const isDev = process.argv.includes("--dev");
const profile = isDev ? "debug" : "release";
const cargoArgs = ["build"];
if (!isDev) cargoArgs.push("--release");
function getCargoEnvironment() {
return {
...process.env,
// Optimize for native CPU when building locally
RUSTFLAGS: process.env.RUSTFLAGS || "-C target-cpu=native",
};
}
function getCargoTargetDirectory() {
if (process.env.CARGO_TARGET_DIR) {
return path.resolve(process.env.CARGO_TARGET_DIR);
}
const metadataRaw = execSync("cargo metadata --format-version 1 --no-deps", {
cwd: engineDir,
stdio: ["ignore", "pipe", "inherit"],
env: getCargoEnvironment(),
}).toString();
const metadata = JSON.parse(metadataRaw);
if (typeof metadata.target_directory !== "string" || metadata.target_directory.length === 0) {
throw new Error("cargo metadata did not return a target_directory");
}
return path.resolve(metadata.target_directory);
}
console.log(`Building forge-engine (${profile})...`);
try {
execSync(`cargo ${cargoArgs.join(" ")}`, {
cwd: engineDir,
stdio: "inherit",
env: getCargoEnvironment(),
});
} catch {
process.exit(1);
}
// Locate the built library using Cargo's actual target directory. Under Nix this
// is often redirected to a shared cache path rather than native/target.
const cargoTargetRoot = getCargoTargetDirectory();
const targetDir = path.join(cargoTargetRoot, profile);
const platformTag = `${process.platform}-${process.arch}`;
const libraryNames = {
darwin: "libforge_engine.dylib",
linux: "libforge_engine.so",
win32: "forge_engine.dll",
};
const libName = libraryNames[process.platform];
if (!libName) {
console.error(`Unsupported platform: ${process.platform}`);
process.exit(1);
}
const sourcePath = path.join(targetDir, libName);
if (!fs.existsSync(sourcePath)) {
console.error(`Built library not found at: ${sourcePath}`);
process.exit(1);
}
fs.mkdirSync(addonDir, { recursive: true });
const destFilename = isDev
? "forge_engine.dev.node"
: `forge_engine.${platformTag}.node`;
const destPath = path.join(addonDir, destFilename);
fs.copyFileSync(sourcePath, destPath);
console.log(`Installed: ${destPath}`);
console.log("Build complete.");