Skip to content

Commit ac57778

Browse files
debpalashclaude
andcommitted
build: vendor SQLite amalgamation + resolve macOS SDK paths (true static binary)
Two changes that make the binary genuinely self-contained and fix Intel-Mac cross-compilation: - Vendor the SQLite amalgamation (lib/sqlite3.c/.h + sqlite3ext.h) and compile it in, instead of linkSystemLibrary("sqlite3"). The binary no longer has a dynamic libsqlite3 dependency (verified via otool) — closing the gap in the "single static binary, zero deps" claim — and cross-compiles without a target-arch system-library lookup. - build.zig: resolve the macOS SDK path via xcrun and add its framework/lib search paths, so cross-compiling x86_64 on an arm host can find Accelerate / Metal / Foundation (native builds auto-detect these; cross builds don't). Verified: native arm64 build + 14/14 integration tests pass; x86_64 cross-build produces a working, fully self-contained binary (runs under Rosetta). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 0698d53 commit ac57778

4 files changed

Lines changed: 284484 additions & 2 deletions

File tree

build.zig

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,13 @@ pub fn build(b: *std.Build) void {
4848
/// rather than via `linkSystemLibrary`, so we never accidentally pick up a
4949
/// Homebrew dylib and we end up with a genuinely static binary.
5050
fn configureModule(b: *std.Build, mod: *std.Build.Module, is_macos: bool) void {
51-
// SQLite3 (system library) + sqlite-vec (compiled from source).
52-
mod.linkSystemLibrary("sqlite3", .{});
51+
// SQLite is vendored (amalgamation) and compiled in, not linked from the
52+
// system — so the binary is genuinely self-contained (no libsqlite3 dynamic
53+
// dep) and cross-compiles cleanly (no system-library lookup for the target).
54+
mod.addCSourceFile(.{
55+
.file = b.path("lib/sqlite3.c"),
56+
.flags = &[_][]const u8{ "-O2", "-DSQLITE_THREADSAFE=1", "-DSQLITE_DQS=0", "-DSQLITE_DEFAULT_MEMSTATUS=0", "-DSQLITE_OMIT_DEPRECATED" },
57+
});
5358
mod.addCSourceFile(.{
5459
.file = b.path("lib/sqlite-vec.c"),
5560
.flags = &[_][]const u8{ "-O3", "-fomit-frame-pointer", "-DSQLITE_CORE" },
@@ -77,6 +82,14 @@ fn configureModule(b: *std.Build, mod: *std.Build.Module, is_macos: bool) void {
7782
mod.linkSystemLibrary("c++", .{});
7883

7984
if (is_macos) {
85+
// Make the macOS SDK frameworks/libs discoverable. Native builds detect
86+
// these automatically, but cross-compiling (x86_64 on an arm host) does
87+
// not, so we add the SDK paths explicitly.
88+
if (sdkPath(b)) |sdk| {
89+
mod.addFrameworkPath(.{ .cwd_relative = b.fmt("{s}/System/Library/Frameworks", .{sdk}) });
90+
mod.addLibraryPath(.{ .cwd_relative = b.fmt("{s}/usr/lib", .{sdk}) });
91+
}
92+
8093
// macOS: Metal + Accelerate BLAS backends and the frameworks they need.
8194
mod.addObjectFile(b.path("lib/llama.cpp/build/ggml/src/ggml-blas/libggml-blas.a"));
8295
mod.addObjectFile(b.path("lib/llama.cpp/build/ggml/src/ggml-metal/libggml-metal.a"));
@@ -87,3 +100,10 @@ fn configureModule(b: *std.Build, mod: *std.Build.Module, is_macos: bool) void {
87100
mod.linkFramework("Foundation", .{});
88101
}
89102
}
103+
104+
/// Resolve the active macOS SDK path via `xcrun`, or null if unavailable.
105+
fn sdkPath(b: *std.Build) ?[]const u8 {
106+
const out = b.run(&.{ "xcrun", "--show-sdk-path" });
107+
const trimmed = std.mem.trim(u8, out, " \t\r\n");
108+
return if (trimmed.len == 0) null else trimmed;
109+
}

0 commit comments

Comments
 (0)