@@ -2,9 +2,9 @@
|
||||
|
||||
import * as fs from "node:fs/promises";
|
||||
import * as path from "node:path";
|
||||
import { $ } from "bun";
|
||||
|
||||
interface BinaryTarget {
|
||||
id: string;
|
||||
platform: string;
|
||||
arch: string;
|
||||
target: string;
|
||||
@@ -17,30 +17,35 @@ const entrypoint = "./packages/coding-agent/src/cli.ts";
|
||||
const isDryRun = process.argv.includes("--dry-run");
|
||||
const targets: BinaryTarget[] = [
|
||||
{
|
||||
id: "darwin-arm64",
|
||||
platform: "darwin",
|
||||
arch: "arm64",
|
||||
target: "bun-darwin-arm64",
|
||||
outfile: "packages/coding-agent/binaries/omp-darwin-arm64",
|
||||
},
|
||||
{
|
||||
id: "darwin-x64",
|
||||
platform: "darwin",
|
||||
arch: "x64",
|
||||
target: "bun-darwin-x64",
|
||||
outfile: "packages/coding-agent/binaries/omp-darwin-x64",
|
||||
},
|
||||
{
|
||||
id: "linux-x64",
|
||||
platform: "linux",
|
||||
arch: "x64",
|
||||
target: "bun-linux-x64-modern",
|
||||
outfile: "packages/coding-agent/binaries/omp-linux-x64",
|
||||
},
|
||||
{
|
||||
id: "linux-arm64",
|
||||
platform: "linux",
|
||||
arch: "arm64",
|
||||
target: "bun-linux-arm64",
|
||||
outfile: "packages/coding-agent/binaries/omp-linux-arm64",
|
||||
},
|
||||
{
|
||||
id: "win32-x64",
|
||||
platform: "win32",
|
||||
arch: "x64",
|
||||
target: "bun-windows-x64-modern",
|
||||
@@ -48,19 +53,53 @@ const targets: BinaryTarget[] = [
|
||||
},
|
||||
];
|
||||
|
||||
function parseRequestedTargets(): Set<string> | null {
|
||||
const flagIndex = process.argv.findIndex(arg => arg === "--targets");
|
||||
const flagValue =
|
||||
flagIndex >= 0
|
||||
? process.argv[flagIndex + 1]
|
||||
: process.argv.find(arg => arg.startsWith("--targets="))?.split("=", 2)[1] ?? Bun.env.RELEASE_TARGETS;
|
||||
|
||||
if (!flagValue) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new Set(
|
||||
flagValue
|
||||
.split(",")
|
||||
.map(value => value.trim())
|
||||
.filter(Boolean),
|
||||
);
|
||||
}
|
||||
|
||||
function shouldAdhocSignDarwinBinary(target: BinaryTarget): boolean {
|
||||
return target.platform === "darwin" && process.platform === "darwin";
|
||||
}
|
||||
|
||||
async function runCommand(command: string[], cwd: string, env: NodeJS.ProcessEnv = Bun.env): Promise<void> {
|
||||
const proc = Bun.spawn(command, {
|
||||
cwd,
|
||||
env,
|
||||
stdout: "inherit",
|
||||
stderr: "inherit",
|
||||
});
|
||||
const exitCode = await proc.exited;
|
||||
if (exitCode !== 0) {
|
||||
throw new Error(`Command failed with exit code ${exitCode}: ${command.join(" ")}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function embedNative(target: BinaryTarget): Promise<void> {
|
||||
if (isDryRun) {
|
||||
console.log(`DRY RUN bun --cwd=packages/natives run embed:native [${target.platform}/${target.arch}]`);
|
||||
return;
|
||||
}
|
||||
|
||||
await $`bun --cwd=packages/natives run embed:native`
|
||||
.cwd(repoRoot)
|
||||
.env({
|
||||
...Bun.env,
|
||||
TARGET_PLATFORM: target.platform,
|
||||
TARGET_ARCH: target.arch,
|
||||
});
|
||||
await runCommand(["bun", "--cwd=packages/natives", "run", "embed:native"], repoRoot, {
|
||||
...Bun.env,
|
||||
TARGET_PLATFORM: target.platform,
|
||||
TARGET_ARCH: target.arch,
|
||||
});
|
||||
}
|
||||
|
||||
async function buildBinary(target: BinaryTarget): Promise<void> {
|
||||
@@ -71,9 +110,19 @@ async function buildBinary(target: BinaryTarget): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
await $`bun build --compile --define PI_COMPILED=true --root . --external mupdf --target=${target.target} ${entrypoint} --outfile ${target.outfile}`.cwd(
|
||||
const buildEnv = shouldAdhocSignDarwinBinary(target)
|
||||
? { ...Bun.env, BUN_NO_CODESIGN_MACHO_BINARY: "1" }
|
||||
: Bun.env;
|
||||
await runCommand(
|
||||
["bun", "build", "--compile", "--define", "PI_COMPILED=true", "--root", ".", "--external", "mupdf", "--target", target.target, entrypoint, "--outfile", target.outfile],
|
||||
repoRoot,
|
||||
buildEnv,
|
||||
);
|
||||
|
||||
// Bun 1.3.12 emits a truncated Mach-O signature on darwin builds.
|
||||
if (shouldAdhocSignDarwinBinary(target)) {
|
||||
await runCommand(["codesign", "--force", "--sign", "-", path.join(repoRoot, target.outfile)], repoRoot);
|
||||
}
|
||||
}
|
||||
|
||||
async function generateBundle(): Promise<void> {
|
||||
@@ -81,7 +130,7 @@ async function generateBundle(): Promise<void> {
|
||||
console.log("DRY RUN bun --cwd=packages/stats scripts/generate-client-bundle.ts --generate");
|
||||
return;
|
||||
}
|
||||
await $`bun --cwd=packages/stats scripts/generate-client-bundle.ts --generate`.cwd(repoRoot);
|
||||
await runCommand(["bun", "--cwd=packages/stats", "scripts/generate-client-bundle.ts", "--generate"], repoRoot);
|
||||
}
|
||||
|
||||
async function resetArtifacts(): Promise<void> {
|
||||
@@ -90,15 +139,33 @@ async function resetArtifacts(): Promise<void> {
|
||||
console.log("DRY RUN bun --cwd=packages/stats scripts/generate-client-bundle.ts --reset");
|
||||
return;
|
||||
}
|
||||
await $`bun --cwd=packages/natives run embed:native --reset`.cwd(repoRoot);
|
||||
await $`bun --cwd=packages/stats scripts/generate-client-bundle.ts --reset`.cwd(repoRoot);
|
||||
await runCommand(["bun", "--cwd=packages/natives", "run", "embed:native", "--reset"], repoRoot);
|
||||
await runCommand(["bun", "--cwd=packages/stats", "scripts/generate-client-bundle.ts", "--reset"], repoRoot);
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const requestedTargets = parseRequestedTargets();
|
||||
const selectedTargets = requestedTargets
|
||||
? targets.filter(target => requestedTargets.has(target.id))
|
||||
: targets;
|
||||
|
||||
if (requestedTargets) {
|
||||
const unknownTargets = [...requestedTargets].filter(
|
||||
requestedTarget => !targets.some(target => target.id === requestedTarget),
|
||||
);
|
||||
if (unknownTargets.length > 0) {
|
||||
throw new Error(`Unknown release target(s): ${unknownTargets.join(", ")}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (selectedTargets.length === 0) {
|
||||
throw new Error("No release targets selected.");
|
||||
}
|
||||
|
||||
await fs.mkdir(binariesDir, { recursive: true });
|
||||
await generateBundle();
|
||||
try {
|
||||
for (const target of targets) {
|
||||
for (const target of selectedTargets) {
|
||||
await buildBinary(target);
|
||||
}
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user