Skip to content
1        10        20        30        40        50        60        70        80        901const std = @import("std");23/// The binary is built on one machine and run on another, so its code must not4/// assume the builder's CPU. Zig defaults to the native CPU model, which emitted5/// AVX-512 instructions in std (notably the CA-certificate parser) and crashed6/// with SIGILL on hosts that lack them. Baseline is plain x86-64.7const default_target: std.Target.Query = .{ .cpu_model = .baseline };89pub fn build(b: *std.Build) void {10    const target = b.standardTargetOptions(.{ .default_target = default_target });11    const optimize = b.standardOptimizeOption(.{});12    const duckdb_dir = b.option(13        []const u8,14        "duckdb-dir",15        "Directory holding the DuckDB static archives (see scripts/fetch-duckdb.sh)",16    ) orelse "vendor/duckdb/v2.0.0";1718    const exe = b.addExecutable(.{19        .name = "totenschloss",20        .root_source_file = b.path("src/main.zig"),21        .target = target,22        .optimize = optimize,23    });24    linkDuckDb(b, exe, duckdb_dir);25    b.installArtifact(exe);2627    const run_cmd = b.addRunArtifact(exe);28    run_cmd.step.dependOn(b.getInstallStep());29    if (b.args) |args| run_cmd.addArgs(args);30    b.step("run", "Run the server").dependOn(&run_cmd.step);3132    const tests = b.addTest(.{33        .root_source_file = b.path("src/main.zig"),34        .target = target,35        .optimize = optimize,36    });37    linkDuckDb(b, tests, duckdb_dir);38    const run_tests = b.addRunArtifact(tests);39    b.step("test", "Run unit tests").dependOn(&run_tests.step);40}4142/// Links the vendored DuckDB archives into the binary instead of linking a43/// shared libduckdb.so. The archive order matters: libstdc++ first, then the44/// engine, then the extension archives that plug into it.45///46/// libc stays dynamic on purpose: DuckDB loads the ducklake and httpfs47/// extensions with dlopen, which a fully static binary cannot do.48fn linkDuckDb(b: *std.Build, compile: *std.Build.Step.Compile, duckdb_dir: []const u8) void {49    const dir = if (std.fs.path.isAbsolute(duckdb_dir))50        std.Build.LazyPath{ .cwd_relative = duckdb_dir }51    else52        b.path(duckdb_dir);5354    compile.linkLibC();55    compile.addIncludePath(dir);5657    const archives = [_][]const u8{58        "libstdc++.a",59        "libduckdb_static.a",60        "libcore_functions_extension.a",61        "libjson_extension.a",62        "libparquet_extension.a",63        "libicu_extension.a",64        "libduckdb_httplib.a",65        "libduckdb_loadable_extensions.a",66    };67    for (archives) |archive| compile.addObjectFile(dir.path(b, archive));6869    // The C++ runtime needs an unwinder; Zig ships libunwind for the target.70    compile.linkSystemLibrary("unwind");71}