~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/stream

jetstream v2 in zig · mirror of https://tangled.org/zat.dev/stream

0
0
198 lines · 7.8 KB Raw History
const std = @import("std");
const manifest = @import("build.zig.zon");

// vendored C dependencies, pinned by url+hash in build.zig.zon — no system
// libraries, no host-specific search paths; cross-compiles with -Dtarget.
fn linkVendoredC(mod: *std.Build.Module, b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) void {
    const zstd_dep = b.dependency("zstd", .{});
    const xxhash_dep = b.dependency("xxhash", .{});

    const clib_mod = b.createModule(.{
        .target = target,
        .optimize = optimize,
        .link_libc = true,
    });
    const zstd_flags: []const []const u8 = &.{
        // upstream libzstd's own defaults, minus assembly (portable across
        // -Dtarget) and multithreading (we compress on our own threads)
        "-DZSTD_DISABLE_ASM",
        "-DZSTD_MULTITHREAD=0",
        // zstd namespaces its bundled xxhash the same way its Makefile does,
        // so it can't collide with the standalone xxhash below
        "-DXXH_NAMESPACE=ZSTD_",
    };
    clib_mod.addCSourceFiles(.{
        .root = zstd_dep.path("lib"),
        .files = &.{
            "common/debug.c",
            "common/entropy_common.c",
            "common/error_private.c",
            "common/fse_decompress.c",
            "common/pool.c",
            "common/threading.c",
            "common/xxhash.c",
            "common/zstd_common.c",
            "compress/fse_compress.c",
            "compress/hist.c",
            "compress/huf_compress.c",
            "compress/zstd_compress_literals.c",
            "compress/zstd_compress_sequences.c",
            "compress/zstd_compress_superblock.c",
            "compress/zstd_compress.c",
            "compress/zstd_double_fast.c",
            "compress/zstd_fast.c",
            "compress/zstd_lazy.c",
            "compress/zstd_ldm.c",
            "compress/zstd_opt.c",
            "compress/zstd_preSplit.c",
            "compress/zstdmt_compress.c",
            "decompress/huf_decompress.c",
            "decompress/zstd_ddict.c",
            "decompress/zstd_decompress_block.c",
            "decompress/zstd_decompress.c",
        },
        .flags = zstd_flags,
    });
    clib_mod.addCSourceFiles(.{
        .root = xxhash_dep.path("."),
        .files = &.{"xxhash.c"},
        .flags = &.{},
    });

    const clib = b.addLibrary(.{ .name = "stream-vendored-c", .root_module = clib_mod });
    mod.linkLibrary(clib);
}

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const zat = b.dependency("zat", .{
        .target = target,
        .optimize = optimize,
    });
    const websocket = b.dependency("websocket", .{
        .target = target,
        .optimize = optimize,
    });
    const rocksdb = b.dependency("rocksdb", .{
        .target = target,
        .optimize = optimize,
    });
    const otel = b.dependency("otel", .{
        .target = target,
        .optimize = optimize,
    });

    const imports: []const std.Build.Module.Import = &.{
        .{ .name = "zat", .module = zat.module("zat") },
        .{ .name = "websocket", .module = websocket.module("websocket") },
        .{ .name = "rocksdb", .module = rocksdb.module("bindings") },
        // Timestamp-rule bulk loading uses RocksDB's external-SST C API,
        // which the typed bindings do not currently wrap.
        .{ .name = "rocksdb_raw", .module = rocksdb.module("rocksdb") },
        .{ .name = "otel-api", .module = otel.module("otel-api") },
        .{ .name = "otel-sdk", .module = otel.module("otel-sdk") },
        .{ .name = "otel-exporters", .module = otel.module("otel-exporters") },
    };

    // build provenance for the stream_build_info canary metric
    const build_options = b.addOptions();
    build_options.addOption([]const u8, "version", b.option([]const u8, "version", "Release version reported by the version command") orelse manifest.version);
    build_options.addOption([]const u8, "build_date", b.option([]const u8, "build-date", "RFC3339 build date reported by the version command") orelse "unknown");
    build_options.addOption([]const u8, "git_sha", git_sha: {
        // The configure result is cached and nothing it can track changes
        // when HEAD moves; without this the binary reports a stale commit.
        b.graph.poisonCache();
        var code: u8 = 0;
        const result = b.runAllowFail(&.{ "git", "rev-parse", "--short", "HEAD" }, &code, .ignore);
        if (result) |output| {
            break :git_sha std.mem.trimEnd(u8, output, "\n \t");
        } else |_| {
            break :git_sha "unknown";
        }
    });
    build_options.addOption([]const u8, "optimize", @tagName(optimize));

    const exe_mod = b.createModule(.{
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
        .imports = imports,
    });
    exe_mod.addImport("build_options", build_options.createModule());
    exe_mod.link_libc = true;
    exe_mod.link_libcpp = true;
    linkVendoredC(exe_mod, b, target, optimize);
    const exe = b.addExecutable(.{
        .name = "stream",
        .root_module = exe_mod,
    });
    b.installArtifact(exe);

    const tls_probe_mod = b.createModule(.{
        .root_source_file = b.path("src/tls_probe.zig"),
        .target = target,
        .optimize = optimize,
        .imports = &.{.{ .name = "websocket", .module = websocket.module("websocket") }},
        .link_libc = true,
    });
    tls_probe_mod.addImport("build_options", build_options.createModule());
    const tls_probe = b.addExecutable(.{ .name = "stream-tls-probe", .root_module = tls_probe_mod });
    b.installArtifact(tls_probe);
    b.step("tls-probe", "Verify backfill TLS rejects compromised chains").dependOn(&b.addRunArtifact(tls_probe).step);

    const run_exe = b.addRunArtifact(exe);
    run_exe.addPassthruArgs();
    const run_step = b.step("run", "run stream");
    run_step.dependOn(&run_exe.step);

    const sample_mod = b.createModule(.{
        .root_source_file = b.path("src/write_sample.zig"),
        .target = target,
        .optimize = optimize,
        .imports = imports,
    });
    sample_mod.link_libc = true;
    sample_mod.link_libcpp = true;
    linkVendoredC(sample_mod, b, target, optimize);
    const write_sample = b.addExecutable(.{ .name = "write-sample-segment", .root_module = sample_mod });
    const run_sample = b.addRunArtifact(write_sample);
    run_sample.addPassthruArgs();
    b.step("write-sample", "write a sample active segment (writer dev tool)").dependOn(&run_sample.step);

    const bench_mod = b.createModule(.{
        .root_source_file = b.path("src/bench.zig"),
        .target = target,
        .optimize = optimize,
        .imports = imports,
    });
    bench_mod.link_libc = true;
    bench_mod.link_libcpp = true;
    linkVendoredC(bench_mod, b, target, optimize);
    const bench = b.addExecutable(.{ .name = "bench", .root_module = bench_mod });
    const run_bench = b.addRunArtifact(bench);
    run_bench.addPassthruArgs();
    b.step("bench", "hot-path timings per subsystem (comparative, not a gate)").dependOn(&run_bench.step);

    const test_step = b.step("test", "run unit tests");
    const test_mod = b.createModule(.{
        .root_source_file = b.path("src/tests.zig"),
        .target = target,
        .optimize = optimize,
        .imports = imports,
    });
    test_mod.addImport("build_options", build_options.createModule());
    test_mod.link_libc = true;
    test_mod.link_libcpp = true;
    linkVendoredC(test_mod, b, target, optimize);
    const test_filters: []const []const u8 = if (b.option([]const u8, "test-filter", "Only compile tests whose name contains this text")) |filter|
        b.allocator.dupe([]const u8, &.{filter}) catch @panic("out of memory")
    else
        &.{};
    const t = b.addTest(.{
        .root_module = test_mod,
        .filters = test_filters,
    });
    test_step.dependOn(&b.addRunArtifact(t).step);
}