-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathbuild.zig
349 lines (295 loc) · 13.1 KB
/
build.zig
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
const std = @import("std");
const Build = std.Build;
pub fn build(b: *Build) void {
defer makeZlsNotInstallAnythingDuringBuildOnSave(b);
// CLI options
const target = b.standardTargetOptions(.{
.default_target = defaultTargetDetectM3() orelse .{},
});
const optimize = b.standardOptimizeOption(.{});
const filters = b.option([]const []const u8, "filter", "List of filters, used for example to filter unit tests by name"); // specified as a series like `-Dfilter="filter1" -Dfilter="filter2"`
const enable_tsan = b.option(bool, "enable-tsan", "Enable TSan for the test suite");
const blockstore_db = b.option(BlockstoreDB, "blockstore", "Blockstore database backend") orelse .rocksdb;
const no_run = b.option(bool, "no-run",
\\Don't run any of the executables implied by the specified steps, only install them.
\\Use in conjunction with 'no-bin' to avoid installation as well.
) orelse false;
const no_bin = b.option(bool, "no-bin",
\\Don't install any of the binaries implied by the specified steps, only run them.
\\Use in conjunction with 'no-run' to avoid running as well.
) orelse false;
// Build options
const build_options = b.addOptions();
build_options.addOption(BlockstoreDB, "blockstore_db", blockstore_db);
// CLI build steps
const install_step = b.getInstallStep();
const sig_step = b.step("sig", "Run the sig executable");
const test_step = b.step("test", "Run library tests");
const fuzz_step = b.step("fuzz", "Gossip fuzz testing");
const benchmark_step = b.step("benchmark", "Benchmark client");
const geyser_reader_step = b.step("geyser_reader", "Read data from geyser");
const svm_step = b.step("svm", "Run the SVM client");
const docs_step = b.step("docs", "Generate and install documentation for the Sig Library");
// Dependencies
const dep_opts = .{ .target = target, .optimize = optimize };
const base58_dep = b.dependency("base58-zig", dep_opts);
const base58_module = base58_dep.module("base58-zig");
const zig_network_dep = b.dependency("zig-network", dep_opts);
const zig_network_module = zig_network_dep.module("network");
const zig_cli_dep = b.dependency("zig-cli", dep_opts);
const zig_cli_module = zig_cli_dep.module("zig-cli");
const httpz_dep = b.dependency("httpz", dep_opts);
const httpz_mod = httpz_dep.module("httpz");
const zstd_dep = b.dependency("zstd", dep_opts);
const zstd_mod = zstd_dep.module("zstd");
const rocksdb_dep = b.dependency("rocksdb", dep_opts);
const rocksdb_mod = rocksdb_dep.module("rocksdb-bindings");
const lsquic_dep = b.dependency("lsquic", dep_opts);
const lsquic_mod = lsquic_dep.module("lsquic");
const ssl_dep = lsquic_dep.builder.dependency("boringssl", dep_opts);
const ssl_mod = ssl_dep.module("ssl");
const xev_dep = b.dependency("xev", dep_opts);
const xev_mod = xev_dep.module("xev");
const pretty_table_dep = b.dependency("prettytable", dep_opts);
const pretty_table_mod = pretty_table_dep.module("prettytable");
// expose Sig as a module
const sig_mod = b.addModule("sig", .{
.root_source_file = b.path("src/sig.zig"),
});
sig_mod.addOptions("build-options", build_options);
sig_mod.addImport("zig-network", zig_network_module);
sig_mod.addImport("base58-zig", base58_module);
sig_mod.addImport("zig-cli", zig_cli_module);
sig_mod.addImport("httpz", httpz_mod);
sig_mod.addImport("zstd", zstd_mod);
switch (blockstore_db) {
.rocksdb => sig_mod.addImport("rocksdb", rocksdb_mod),
.hashmap => {},
}
// main executable
const sig_exe = b.addExecutable(.{
.name = "sig",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.sanitize_thread = enable_tsan,
});
sig_step.dependOn(&sig_exe.step);
install_step.dependOn(&sig_exe.step);
// make sure pyroscope's got enough info to profile
sig_exe.build_id = .fast;
sig_exe.root_module.omit_frame_pointer = false;
sig_exe.root_module.strip = false;
sig_exe.linkLibC();
sig_exe.root_module.addOptions("build-options", build_options);
sig_exe.root_module.addImport("base58-zig", base58_module);
sig_exe.root_module.addImport("httpz", httpz_mod);
sig_exe.root_module.addImport("zig-cli", zig_cli_module);
sig_exe.root_module.addImport("zig-network", zig_network_module);
sig_exe.root_module.addImport("zstd", zstd_mod);
sig_exe.root_module.addImport("lsquic", lsquic_mod);
sig_exe.root_module.addImport("ssl", ssl_mod);
sig_exe.root_module.addImport("xev", xev_mod);
switch (blockstore_db) {
.rocksdb => sig_exe.root_module.addImport("rocksdb", rocksdb_mod),
.hashmap => {},
}
if (!no_bin) {
const sig_install = b.addInstallArtifact(sig_exe, .{});
sig_step.dependOn(&sig_install.step);
install_step.dependOn(&sig_install.step);
}
if (!no_run) {
const sig_run = b.addRunArtifact(sig_exe);
sig_step.dependOn(&sig_run.step);
sig_run.addArgs(b.args orelse &.{});
}
// unit tests
const unit_tests_exe = b.addTest(.{
.root_source_file = b.path("src/tests.zig"),
.target = target,
.optimize = optimize,
.sanitize_thread = enable_tsan,
.filters = filters orelse &.{},
});
test_step.dependOn(&unit_tests_exe.step);
install_step.dependOn(&unit_tests_exe.step);
unit_tests_exe.linkLibC();
unit_tests_exe.root_module.addOptions("build-options", build_options);
unit_tests_exe.root_module.addImport("base58-zig", base58_module);
unit_tests_exe.root_module.addImport("httpz", httpz_mod);
unit_tests_exe.root_module.addImport("zig-network", zig_network_module);
unit_tests_exe.root_module.addImport("zstd", zstd_mod);
switch (blockstore_db) {
.rocksdb => unit_tests_exe.root_module.addImport("rocksdb", rocksdb_mod),
.hashmap => {},
}
if (!no_bin) {
const unit_tests_install = b.addInstallArtifact(unit_tests_exe, .{});
test_step.dependOn(&unit_tests_install.step);
install_step.dependOn(&unit_tests_install.step);
}
if (!no_run) {
const unit_tests_run = b.addRunArtifact(unit_tests_exe);
test_step.dependOn(&unit_tests_run.step);
}
// fuzz test
const fuzz_exe = b.addExecutable(.{
.name = "fuzz",
.root_source_file = b.path("src/fuzz.zig"),
.target = target,
.optimize = optimize,
.sanitize_thread = enable_tsan,
});
fuzz_step.dependOn(&fuzz_exe.step);
install_step.dependOn(&fuzz_exe.step);
fuzz_exe.linkLibC();
fuzz_exe.root_module.addOptions("build-options", build_options);
fuzz_exe.root_module.addImport("base58-zig", base58_module);
fuzz_exe.root_module.addImport("zig-network", zig_network_module);
fuzz_exe.root_module.addImport("httpz", httpz_mod);
fuzz_exe.root_module.addImport("zstd", zstd_mod);
switch (blockstore_db) {
.rocksdb => fuzz_exe.root_module.addImport("rocksdb", rocksdb_mod),
.hashmap => {},
}
if (!no_bin) {
const fuzz_install = b.addInstallArtifact(fuzz_exe, .{});
fuzz_step.dependOn(&fuzz_install.step);
install_step.dependOn(&fuzz_install.step);
}
if (!no_run) {
const fuzz_run = b.addRunArtifact(fuzz_exe);
fuzz_step.dependOn(&fuzz_run.step);
fuzz_run.addArgs(b.args orelse &.{});
}
// benchmarks
const benchmark_exe = b.addExecutable(.{
.name = "benchmark",
.root_source_file = b.path("src/benchmarks.zig"),
.target = target,
.optimize = optimize,
.sanitize_thread = enable_tsan,
});
benchmark_step.dependOn(&benchmark_exe.step);
install_step.dependOn(&benchmark_exe.step);
benchmark_exe.linkLibC();
benchmark_exe.root_module.addOptions("build-options", build_options);
benchmark_exe.root_module.addImport("base58-zig", base58_module);
benchmark_exe.root_module.addImport("zig-network", zig_network_module);
benchmark_exe.root_module.addImport("httpz", httpz_mod);
benchmark_exe.root_module.addImport("zstd", zstd_mod);
benchmark_exe.root_module.addImport("prettytable", pretty_table_mod);
switch (blockstore_db) {
.rocksdb => benchmark_exe.root_module.addImport("rocksdb", rocksdb_mod),
.hashmap => {},
}
if (!no_bin) {
const benchmark_install = b.addInstallArtifact(benchmark_exe, .{});
benchmark_step.dependOn(&benchmark_install.step);
install_step.dependOn(&benchmark_install.step);
}
if (!no_run) {
const benchmark_run = b.addRunArtifact(benchmark_exe);
benchmark_step.dependOn(&benchmark_run.step);
benchmark_run.addArgs(b.args orelse &.{});
}
// geyser reader
const geyser_reader_exe = b.addExecutable(.{
.name = "geyser",
.root_source_file = b.path("src/geyser/main.zig"),
.target = target,
.optimize = optimize,
.sanitize_thread = enable_tsan,
});
geyser_reader_step.dependOn(&geyser_reader_exe.step);
install_step.dependOn(&geyser_reader_exe.step);
geyser_reader_exe.root_module.addImport("sig", sig_mod);
geyser_reader_exe.root_module.addImport("zig-cli", zig_cli_module);
if (!no_bin) {
const geyser_reader_install = b.addInstallArtifact(geyser_reader_exe, .{});
geyser_reader_step.dependOn(&geyser_reader_install.step);
install_step.dependOn(&geyser_reader_install.step);
}
if (!no_run) {
const geyser_reader_run = b.addRunArtifact(geyser_reader_exe);
geyser_reader_step.dependOn(&geyser_reader_run.step);
geyser_reader_run.addArgs(b.args orelse &.{});
}
const svm_exe = b.addExecutable(.{
.name = "svm",
.root_source_file = b.path("src/svm/main.zig"),
.target = target,
.optimize = optimize,
.sanitize_thread = enable_tsan,
});
svm_step.dependOn(&svm_exe.step);
install_step.dependOn(&svm_exe.step);
svm_exe.root_module.addImport("sig", sig_mod);
if (!no_bin) {
const svm_install = b.addInstallArtifact(svm_exe, .{});
svm_step.dependOn(&svm_install.step);
install_step.dependOn(&svm_install.step);
}
if (!no_run) {
const svm_run = b.addRunArtifact(svm_exe);
svm_step.dependOn(&svm_run.step);
svm_run.addArgs(b.args orelse &.{});
}
// docs for the Sig library
const install_sig_docs = b.addInstallDirectory(.{
.source_dir = sig_exe.getEmittedDocs(),
.install_dir = .prefix,
.install_subdir = "docs",
});
docs_step.dependOn(&install_sig_docs.step);
}
const BlockstoreDB = enum {
rocksdb,
hashmap,
};
/// Reference/inspiration: https://kristoff.it/blog/improving-your-zls-experience/
fn makeZlsNotInstallAnythingDuringBuildOnSave(b: *Build) void {
const zls_is_build_runner = b.option(bool, "zls-is-build-runner", "" ++
"Option passed by zls to indicate that it's the one running this build script (configured in the local zls.json). " ++
"This should not be specified on the command line nor as a dependency argument.") orelse false;
if (!zls_is_build_runner) return;
for (b.install_tls.step.dependencies.items) |*install_step_dep| {
const install_artifact = install_step_dep.*.cast(Build.Step.InstallArtifact) orelse continue;
const artifact = install_artifact.artifact;
install_step_dep.* = &artifact.step;
// this will make it so `-fno-emit-bin` is passed, meaning
// that the compiler will only go as far as semantically
// analyzing the code, without sending it to any backend,
// namely the slow-to-compile LLVM.
artifact.generated_bin = null;
}
}
/// TODO: remove after updating to 0.14, where M3/M4 feature detection is fixed.
/// Ref: https://github.com/ziglang/zig/pull/21116
fn defaultTargetDetectM3() ?std.Target.Query {
const builtin = @import("builtin");
if (builtin.os.tag != .macos) return null;
switch (builtin.cpu.arch) {
.aarch64, .aarch64_be => {},
else => return null,
}
var cpu_family: std.c.CPUFAMILY = undefined;
var len: usize = @sizeOf(std.c.CPUFAMILY);
std.posix.sysctlbynameZ("hw.cpufamily", &cpu_family, &len, null, 0) catch unreachable;
// Detects M4 as M3 to get around missing C flag translations when passing the target to dependencies.
// https://github.com/Homebrew/brew/blob/64edbe6b7905c47b113c1af9cb1a2009ed57a5c7/Library/Homebrew/extend/os/mac/hardware/cpu.rb#L106
const model: *const std.Target.Cpu.Model = switch (@intFromEnum(cpu_family)) {
else => return null,
0x2876f5b5 => &std.Target.aarch64.cpu.apple_a17, // ARM_COLL
0xfa33415e => &std.Target.aarch64.cpu.apple_m3, // ARM_IBIZA
0x5f4dea93 => &std.Target.aarch64.cpu.apple_m3, // ARM_LOBOS
0x72015832 => &std.Target.aarch64.cpu.apple_m3, // ARM_PALMA
0x6f5129ac => &std.Target.aarch64.cpu.apple_m3, // ARM_DONAN (M4)
0x17d5b93a => &std.Target.aarch64.cpu.apple_m3, // ARM_BRAVA (M4)
};
return .{
.cpu_arch = builtin.cpu.arch,
.cpu_model = .{ .explicit = model },
};
}