1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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);
}