/
build.zig
963 lines (827 loc) · 33 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
const std = @import("std");
const builtin = @import("builtin");
const CrossTarget = std.zig.CrossTarget;
const Mode = std.builtin.Mode;
const config = @import("./src/config.zig");
pub fn build(b: *std.build.Builder) void {
const target = b.standardTargetOptions(.{});
const mode = b.standardReleaseOptions();
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
const options = b.addOptions();
// The "tigerbeetle version" command includes the build-time commit hash.
if (git_commit(allocator)) |commit| {
options.addOption(?[]const u8, "git_commit", commit[0..]);
} else {
options.addOption(?[]const u8, "git_commit", null);
}
options.addOption(
?[]const u8,
"git_tag",
if (git_tag(allocator)) |tag| tag.constSlice() else null,
);
options.addOption(
config.ConfigBase,
"config_base",
b.option(config.ConfigBase, "config", "Base configuration.") orelse .default,
);
options.addOption(
std.log.Level,
"config_log_level",
b.option(std.log.Level, "config-log-level", "Log level.") orelse .info,
);
const tracer_backend = b.option(
config.TracerBackend,
"tracer-backend",
"Which backend to use for tracing.",
) orelse .none;
options.addOption(config.TracerBackend, "tracer_backend", tracer_backend);
const aof_record_enable = b.option(bool, "config-aof-record", "Enable AOF Recording.") orelse false;
const aof_recovery_enable = b.option(bool, "config-aof-recovery", "Enable AOF Recovery mode.") orelse false;
options.addOption(bool, "config_aof_record", aof_record_enable);
options.addOption(bool, "config_aof_recovery", aof_recovery_enable);
const hash_log_mode = b.option(
config.HashLogMode,
"hash-log-mode",
"Log hashes (used for debugging non-deterministic executions).",
) orelse .none;
options.addOption(config.HashLogMode, "hash_log_mode", hash_log_mode);
const vsr_package = std.build.Pkg{
.name = "vsr",
.path = .{ .path = "src/vsr.zig" },
.dependencies = &.{options.getPackage("vsr_options")},
};
const tigerbeetle = b.addExecutable("tigerbeetle", "src/tigerbeetle/main.zig");
tigerbeetle.setTarget(target);
tigerbeetle.setBuildMode(mode);
tigerbeetle.addPackage(vsr_package);
tigerbeetle.install();
// Ensure that we get stack traces even in release builds.
tigerbeetle.omit_frame_pointer = false;
tigerbeetle.addOptions("vsr_options", options);
link_tracer_backend(tigerbeetle, tracer_backend, target);
{
const run_cmd = tigerbeetle.run();
if (b.args) |args| run_cmd.addArgs(args);
const run_step = b.step("run", "Run TigerBeetle");
run_step.dependOn(&run_cmd.step);
}
{
// "zig build install" moves the server executable to the root folder:
const move_cmd = b.addInstallBinFile(
tigerbeetle.getOutputSource(),
b.pathJoin(&.{ "../../", tigerbeetle.out_filename }),
);
move_cmd.step.dependOn(&tigerbeetle.step);
var install_step = b.getInstallStep();
install_step.dependOn(&move_cmd.step);
}
{
const benchmark = b.addExecutable("benchmark", "src/benchmark.zig");
benchmark.setTarget(target);
benchmark.setBuildMode(mode);
benchmark.addPackage(vsr_package);
benchmark.install();
benchmark.addOptions("vsr_options", options);
link_tracer_backend(benchmark, tracer_backend, target);
const run_cmd = benchmark.run();
if (b.args) |args| run_cmd.addArgs(args);
const run_step = b.step("benchmark", "Run TigerBeetle benchmark");
run_step.dependOn(&run_cmd.step);
}
{
const aof = b.addExecutable("aof", "src/aof.zig");
aof.setTarget(target);
aof.setBuildMode(mode);
aof.install();
aof.addOptions("vsr_options", options);
link_tracer_backend(aof, tracer_backend, target);
const run_cmd = aof.run();
if (b.args) |args| run_cmd.addArgs(args);
const run_step = b.step("aof", "Run TigerBeetle AOF Utility");
run_step.dependOn(&run_cmd.step);
}
// Linting targets
// We currently have: lint_zig_fmt, lint_tigerstyle, lint_shellcheck, lint_validate_docs.
// The meta-target lint runs them all
{
// lint_zig_fmt
// TODO: Better way of running zig fmt?
const lint_zig_fmt = b.addSystemCommand(&.{ b.zig_exe, "fmt", "--check", "src/" });
const lint_zig_fmt_step = b.step("lint_zig_fmt", "Run zig fmt on src/");
lint_zig_fmt_step.dependOn(&lint_zig_fmt.step);
// lint_tigerstyle
const lint_tigerstyle = b.addExecutable("lint_tigerstyle", "scripts/lint_tigerstyle.zig");
lint_tigerstyle.setTarget(target);
lint_tigerstyle.setBuildMode(mode);
const run_cmd = lint_tigerstyle.run();
if (b.args) |args| {
run_cmd.addArgs(args);
} else {
run_cmd.addArg("src");
}
const lint_tigerstyle_step = b.step("lint_tigerstyle", "Run the linter on src/");
lint_tigerstyle_step.dependOn(&run_cmd.step);
// lint_shellcheck
const lint_shellcheck = b.addSystemCommand(&.{ "sh", "-c", "command -v shellcheck >/dev/null" ++
" || (echo -e '\\033[0;31mPlease install shellcheck - https://www.shellcheck.net/\\033[0m' && exit 1)" ++
" && shellcheck $(find ./src ./scripts -type f -name '*.sh')" });
const lint_shellcheck_step = b.step("lint_shellcheck", "Run shellcheck on **.sh");
lint_shellcheck_step.dependOn(&lint_shellcheck.step);
// lint_validate_docs
const lint_validate_docs = b.addSystemCommand(&.{"scripts/validate_docs.sh"});
const lint_validate_docs_step = b.step("lint_validate_docs", "Validate docs");
lint_validate_docs_step.dependOn(&lint_validate_docs.step);
// TODO: Iterate above? Make it impossible to neglect to add somehow?
// lint
const lint_step = b.step("lint", "Run all defined linters");
lint_step.dependOn(lint_tigerstyle_step);
lint_step.dependOn(lint_zig_fmt_step);
lint_step.dependOn(lint_shellcheck_step);
lint_step.dependOn(lint_validate_docs_step);
}
// Executable which generates src/clients/c/tb_client.h
const tb_client_header_generate = blk: {
const tb_client_header = b.addExecutable("tb_client_header", "src/clients/c/tb_client_header.zig");
tb_client_header.addOptions("vsr_options", options);
tb_client_header.setMainPkgPath("src");
tb_client_header.setTarget(target);
break :blk tb_client_header.run();
};
{
const test_filter = b.option(
[]const u8,
"test-filter",
"Skip tests that do not match filter",
);
const unit_tests = b.addTest("src/unit_tests.zig");
unit_tests.setTarget(target);
unit_tests.setBuildMode(mode);
unit_tests.addOptions("vsr_options", options);
unit_tests.step.dependOn(&tb_client_header_generate.step);
unit_tests.setFilter(test_filter);
link_tracer_backend(unit_tests, tracer_backend, target);
// for src/clients/c/tb_client_header_test.zig to use cImport on tb_client.h
unit_tests.linkLibC();
unit_tests.addIncludeDir("src/clients/c/");
const unit_tests_step = b.step("test:unit", "Run the unit tests");
unit_tests_step.dependOn(&unit_tests.step);
const test_step = b.step("test", "Run the unit tests");
test_step.dependOn(&unit_tests.step);
if (test_filter == null) {
// Test that our demos compile, but don't run them.
inline for (.{
"demo_01_create_accounts",
"demo_02_lookup_accounts",
"demo_03_create_transfers",
"demo_04_create_pending_transfers",
"demo_05_post_pending_transfers",
"demo_06_void_pending_transfers",
"demo_07_lookup_transfers",
}) |demo| {
const demo_exe = b.addExecutable(demo, "src/demos/" ++ demo ++ ".zig");
demo_exe.addPackage(vsr_package);
demo_exe.setTarget(target);
test_step.dependOn(&demo_exe.step);
}
}
const unit_tests_exe = b.addTestExe("tests", "src/unit_tests.zig");
unit_tests_exe.setTarget(target);
unit_tests_exe.setBuildMode(mode);
unit_tests_exe.addOptions("vsr_options", options);
unit_tests_exe.step.dependOn(&tb_client_header_generate.step);
unit_tests_exe.setFilter(test_filter);
link_tracer_backend(unit_tests_exe, tracer_backend, target);
// for src/clients/c/tb_client_header_test.zig to use cImport on tb_client.h
unit_tests_exe.linkLibC();
unit_tests_exe.addIncludeDir("src/clients/c/");
const unit_tests_exe_step = b.step("test:build", "Build the unit tests");
const install_unit_tests_exe = b.addInstallArtifact(unit_tests_exe);
unit_tests_exe_step.dependOn(&install_unit_tests_exe.step);
}
// Clients build:
{
var install_step = b.addInstallArtifact(tigerbeetle);
go_client(
b,
mode,
&.{ &install_step.step, &tb_client_header_generate.step },
target,
options,
tracer_backend,
);
java_client(
b,
mode,
&.{&install_step.step},
target,
options,
tracer_backend,
);
dotnet_client(
b,
mode,
&.{&install_step.step},
target,
options,
tracer_backend,
);
node_client(
b,
mode,
&.{&install_step.step},
target,
options,
tracer_backend,
);
c_client(
b,
mode,
&.{ &install_step.step, &tb_client_header_generate.step },
options,
tracer_backend,
);
c_client_sample(
b,
mode,
target,
&.{ &install_step.step, &tb_client_header_generate.step },
options,
tracer_backend,
);
run_with_tb(
allocator,
b,
mode,
target,
);
client_integration(
allocator,
b,
mode,
target,
);
client_docs(
allocator,
b,
mode,
target,
);
}
{
const simulator_options = b.addOptions();
// When running without a SEED, default to release.
const simulator_mode = if (b.args == null) .ReleaseSafe else mode;
const StateMachine = enum { testing, accounting };
simulator_options.addOption(
StateMachine,
"state_machine",
b.option(
StateMachine,
"simulator-state-machine",
"State machine.",
) orelse .accounting,
);
const SimulatorLog = enum { full, short };
const default_simulator_log = if (simulator_mode == .ReleaseSafe) SimulatorLog.short else .full;
simulator_options.addOption(
SimulatorLog,
"log",
b.option(
SimulatorLog,
"simulator-log",
"Log only state transitions (short) or everything (full).",
) orelse default_simulator_log,
);
const simulator = b.addExecutable("simulator", "src/simulator.zig");
simulator.setTarget(target);
simulator.setBuildMode(simulator_mode);
// Ensure that we get stack traces even in release builds.
simulator.omit_frame_pointer = false;
simulator.addOptions("vsr_options", options);
simulator.addOptions("vsr_simulator_options", simulator_options);
link_tracer_backend(simulator, tracer_backend, target);
const run_cmd = simulator.run();
if (b.args) |args| {
run_cmd.addArgs(args);
}
const install_step = b.addInstallArtifact(simulator);
const build_step = b.step("simulator", "Build the Simulator");
build_step.dependOn(&install_step.step);
const run_step = b.step("simulator_run", "Run the Simulator");
run_step.dependOn(&run_cmd.step);
}
{
const vopr = b.addExecutable("vopr", "src/vopr.zig");
vopr.setTarget(target);
// Ensure that we get stack traces even in release builds.
vopr.omit_frame_pointer = false;
vopr.addOptions("vsr_options", options);
link_tracer_backend(vopr, tracer_backend, target);
const run_cmd = vopr.run();
if (b.args) |args| {
run_cmd.addArgs(args);
vopr.setBuildMode(mode);
} else {
vopr.setBuildMode(.ReleaseSafe);
}
const step = b.step("vopr", "Run the VOPR");
step.dependOn(&run_cmd.step);
}
inline for (.{
.{
.name = "fuzz_ewah",
.file = "src/ewah_fuzz.zig",
.description = "Fuzz EWAH codec. Args: [--seed <seed>]",
},
.{
.name = "fuzz_lsm_forest",
.file = "src/lsm/forest_fuzz.zig",
.description = "Fuzz the LSM forest. Args: [--seed <seed>] [--events-max <count>]",
},
.{
.name = "fuzz_lsm_manifest_log",
.file = "src/lsm/manifest_log_fuzz.zig",
.description = "Fuzz the ManifestLog. Args: [--seed <seed>] [--events-max <count>]",
},
.{
.name = "fuzz_lsm_manifest_level",
.file = "src/lsm/manifest_level_fuzz.zig",
.description = "Fuzz the ManifestLevel. Args: [--seed <seed>] [--events-max <count>]",
},
.{
.name = "fuzz_lsm_tree",
.file = "src/lsm/tree_fuzz.zig",
.description = "Fuzz the LSM tree. Args: [--seed <seed>] [--events-max <count>]",
},
.{
.name = "fuzz_lsm_segmented_array",
.file = "src/lsm/segmented_array_fuzz.zig",
.description = "Fuzz the LSM segmented array. Args: [--seed <seed>]",
},
.{
.name = "fuzz_vsr_journal_format",
.file = "src/vsr/journal_format_fuzz.zig",
.description = "Fuzz the WAL format. Args: [--seed <seed>]",
},
.{
.name = "fuzz_vsr_superblock",
.file = "src/vsr/superblock_fuzz.zig",
.description = "Fuzz the SuperBlock. Args: [--seed <seed>] [--events-max <count>]",
},
.{
.name = "fuzz_vsr_superblock_free_set",
.file = "src/vsr/superblock_free_set_fuzz.zig",
.description = "Fuzz the SuperBlock FreeSet. Args: [--seed <seed>]",
},
.{
.name = "fuzz_vsr_superblock_quorums",
.file = "src/vsr/superblock_quorums_fuzz.zig",
.description = "Fuzz the SuperBlock Quorums. Args: [--seed <seed>]",
},
}) |fuzzer| {
const exe = b.addExecutable(fuzzer.name, fuzzer.file);
exe.setMainPkgPath("src");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.omit_frame_pointer = false;
exe.addOptions("vsr_options", options);
link_tracer_backend(exe, tracer_backend, target);
const install_step = b.addInstallArtifact(exe);
const build_step = b.step("build_" ++ fuzzer.name, fuzzer.description);
build_step.dependOn(&install_step.step);
const run_cmd = exe.run();
if (b.args) |args| run_cmd.addArgs(args);
const run_step = b.step(fuzzer.name, fuzzer.description);
run_step.dependOn(&run_cmd.step);
}
inline for (.{
.{
.name = "benchmark_ewah",
.file = "src/ewah_benchmark.zig",
.description = "EWAH codec",
},
.{
.name = "benchmark_eytzinger",
.file = "src/lsm/eytzinger_benchmark.zig",
.description = "array search",
},
.{
.name = "benchmark_segmented_array",
.file = "src/lsm/segmented_array_benchmark.zig",
.description = "SegmentedArray search",
},
}) |benchmark| {
const exe = b.addExecutable(benchmark.name, benchmark.file);
exe.setTarget(target);
exe.setBuildMode(.ReleaseSafe);
exe.setMainPkgPath("src");
exe.addOptions("vsr_options", options);
link_tracer_backend(exe, tracer_backend, target);
const build_step = b.step("build_" ++ benchmark.name, "Build " ++ benchmark.description ++ " benchmark");
build_step.dependOn(&exe.step);
const run_cmd = exe.run();
const step = b.step(benchmark.name, "Benchmark " ++ benchmark.description);
step.dependOn(&run_cmd.step);
}
}
fn git_commit(allocator: std.mem.Allocator) ?[40]u8 {
const exec_result = std.ChildProcess.exec(.{
.allocator = allocator,
.argv = &.{ "git", "rev-parse", "--verify", "HEAD" },
}) catch return null;
defer allocator.free(exec_result.stdout);
defer allocator.free(exec_result.stderr);
// +1 for trailing newline.
if (exec_result.stdout.len != 40 + 1) return null;
if (exec_result.stderr.len != 0) return null;
var output: [40]u8 = undefined;
std.mem.copy(u8, &output, exec_result.stdout[0..40]);
return output;
}
fn git_tag(allocator: std.mem.Allocator) ?std.BoundedArray(u8, 100) {
const exec_result = std.ChildProcess.exec(.{
.allocator = allocator,
.argv = &.{ "git", "describe", "--tags" },
}) catch return null;
defer allocator.free(exec_result.stdout);
defer allocator.free(exec_result.stderr);
if (exec_result.stderr.len != 0) return null;
var tag = std.BoundedArray(u8, 100){ .buffer = undefined };
tag.appendSlice(exec_result.stdout) catch {
std.debug.print("`git describe --tags` output too long.\n", .{});
return null;
};
return tag;
}
fn link_tracer_backend(
exe: *std.build.LibExeObjStep,
tracer_backend: config.TracerBackend,
target: std.zig.CrossTarget,
) void {
switch (tracer_backend) {
.none => {},
.tracy => {
// Code here is based on
// https://github.com/ziglang/zig/blob/a660df4900520c505a0865707552dcc777f4b791/build.zig#L382
// On mingw, we need to opt into windows 7+ to get some features required by tracy.
const tracy_c_flags: []const []const u8 = if (target.isWindows() and target.getAbi() == .gnu)
&[_][]const u8{
"-DTRACY_ENABLE=1",
"-DTRACY_FIBERS=1",
"-fno-sanitize=undefined",
"-D_WIN32_WINNT=0x601",
}
else
&[_][]const u8{
"-DTRACY_ENABLE=1",
"-DTRACY_FIBERS=1",
"-fno-sanitize=undefined",
};
exe.addIncludeDir("./tools/tracy/public/tracy");
exe.addCSourceFile("./tools/tracy/public/TracyClient.cpp", tracy_c_flags);
exe.linkLibC();
exe.linkSystemLibraryName("c++");
if (target.isWindows()) {
exe.linkSystemLibrary("dbghelp");
exe.linkSystemLibrary("ws2_32");
}
},
}
}
// Zig cross-targets plus Dotnet RID (Runtime Identifier):
const platforms = .{
.{ "x86_64-linux-gnu", "linux-x64" },
.{ "x86_64-linux-musl", "linux-musl-x64" },
.{ "x86_64-macos", "osx-x64" },
.{ "aarch64-linux-gnu", "linux-arm64" },
.{ "aarch64-linux-musl", "linux-musl-arm64" },
.{ "aarch64-macos", "osx-arm64" },
.{ "x86_64-windows", "win-x64" },
};
fn go_client(
b: *std.build.Builder,
mode: Mode,
dependencies: []const *std.build.Step,
target: CrossTarget,
options: *std.build.OptionsStep,
tracer_backend: config.TracerBackend,
) void {
const build_step = b.step("go_client", "Build Go client shared library");
for (dependencies) |dependency| {
build_step.dependOn(dependency);
}
// Updates the generated header file:
const install_header = b.addInstallFile(
.{ .path = "src/clients/c/tb_client.h" },
"../src/clients/go/pkg/native/tb_client.h",
);
const bindings = b.addExecutable("go_bindings", "src/clients/go/go_bindings.zig");
bindings.addOptions("vsr_options", options);
bindings.setTarget(target);
bindings.setMainPkgPath("src");
const bindings_step = bindings.run();
inline for (platforms) |platform| {
const name = if (comptime std.mem.eql(u8, platform[0], "x86_64-linux-musl"))
"x86_64-linux"
else if (comptime std.mem.eql(u8, platform[0], "aarch64-linux-musl"))
"aarch64-linux"
else
platform[0];
// We don't need the linux-gnu builds.
if (comptime !std.mem.endsWith(u8, platform[0], "linux-gnu")) {
const cross_target = CrossTarget.parse(.{ .arch_os_abi = name, .cpu_features = "baseline" }) catch unreachable;
const lib = b.addStaticLibrary("tb_client", "src/clients/c/tb_client.zig");
lib.setMainPkgPath("src");
lib.setTarget(cross_target);
lib.setBuildMode(mode);
lib.linkLibC();
lib.pie = true;
lib.bundle_compiler_rt = true;
lib.setOutputDir("src/clients/go/pkg/native/" ++ name);
lib.addOptions("vsr_options", options);
link_tracer_backend(lib, tracer_backend, cross_target);
lib.step.dependOn(&install_header.step);
lib.step.dependOn(&bindings_step.step);
build_step.dependOn(&lib.step);
}
}
}
fn java_client(
b: *std.build.Builder,
mode: Mode,
dependencies: []const *std.build.Step,
target: CrossTarget,
options: *std.build.OptionsStep,
tracer_backend: config.TracerBackend,
) void {
const build_step = b.step("java_client", "Build Java client shared library");
for (dependencies) |dependency| {
build_step.dependOn(dependency);
}
const bindings = b.addExecutable("java_bindings", "src/clients/java/java_bindings.zig");
bindings.addOptions("vsr_options", options);
bindings.setTarget(target);
bindings.setMainPkgPath("src");
const bindings_step = bindings.run();
inline for (platforms) |platform| {
const cross_target = CrossTarget.parse(.{ .arch_os_abi = platform[0], .cpu_features = "baseline" }) catch unreachable;
const lib = b.addSharedLibrary("tb_jniclient", "src/clients/java/src/client.zig", .unversioned);
lib.setMainPkgPath("src");
lib.addPackagePath("jui", "src/clients/java/lib/jui/src/jui.zig");
lib.setOutputDir("src/clients/java/src/main/resources/lib/" ++ platform[0]);
lib.setTarget(cross_target);
lib.setBuildMode(mode);
lib.linkLibC();
if (cross_target.os_tag.? == .windows) {
lib.linkSystemLibrary("ws2_32");
lib.linkSystemLibrary("advapi32");
}
lib.addOptions("vsr_options", options);
link_tracer_backend(lib, tracer_backend, cross_target);
lib.step.dependOn(&bindings_step.step);
build_step.dependOn(&lib.step);
}
}
fn dotnet_client(
b: *std.build.Builder,
mode: Mode,
dependencies: []const *std.build.Step,
target: CrossTarget,
options: *std.build.OptionsStep,
tracer_backend: config.TracerBackend,
) void {
const build_step = b.step("dotnet_client", "Build dotnet client shared library");
for (dependencies) |dependency| {
build_step.dependOn(dependency);
}
const bindings = b.addExecutable("dotnet_bindings", "src/clients/dotnet/dotnet_bindings.zig");
bindings.addOptions("vsr_options", options);
bindings.setTarget(target);
bindings.setMainPkgPath("src");
const bindings_step = bindings.run();
inline for (platforms) |platform| {
const cross_target = CrossTarget.parse(.{ .arch_os_abi = platform[0], .cpu_features = "baseline" }) catch unreachable;
const lib = b.addSharedLibrary("tb_client", "src/clients/c/tb_client.zig", .unversioned);
lib.setMainPkgPath("src");
lib.setOutputDir("src/clients/dotnet/TigerBeetle/runtimes/" ++ platform[1] ++ "/native");
lib.setTarget(cross_target);
lib.setBuildMode(mode);
lib.linkLibC();
if (cross_target.os_tag.? == .windows) {
lib.linkSystemLibrary("ws2_32");
lib.linkSystemLibrary("advapi32");
}
lib.addOptions("vsr_options", options);
link_tracer_backend(lib, tracer_backend, cross_target);
lib.step.dependOn(&bindings_step.step);
build_step.dependOn(&lib.step);
}
}
fn node_client(
b: *std.build.Builder,
mode: Mode,
dependencies: []const *std.build.Step,
target: CrossTarget,
options: *std.build.OptionsStep,
tracer_backend: config.TracerBackend,
) void {
const build_step = b.step("node_client", "Build Node client shared library");
for (dependencies) |dependency| {
build_step.dependOn(dependency);
}
const bindings = b.addExecutable("node_bindings", "src/clients/node/node_bindings.zig");
bindings.addOptions("vsr_options", options);
bindings.setTarget(target);
bindings.setMainPkgPath("src");
const bindings_step = bindings.run();
inline for (platforms) |platform| {
const cross_target = CrossTarget.parse(.{ .arch_os_abi = platform[0], .cpu_features = "baseline" }) catch unreachable;
if (cross_target.os_tag.? == .windows) {
// No Windows support just yet. We need to be on a version with https://github.com/ziglang/zig/commit/b97a68c529b5db15705f4d542d8ead616d27c880
} else {
const lib = b.addSharedLibrary("tb_nodeclient", "src/clients/node/src/node.zig", .unversioned);
lib.setMainPkgPath("src");
lib.setOutputDir("src/clients/node/dist/bin/" ++ platform[0]);
// This is provided by the node-api-headers package; make sure to run `npm install` under `src/clients/node`
// if you're running zig build node_client manually.
lib.addSystemIncludeDir("src/clients/node/node_modules/node-api-headers/include");
lib.setTarget(cross_target);
lib.setBuildMode(mode);
lib.linkLibC();
lib.linker_allow_shlib_undefined = true;
if (cross_target.os_tag.? == .windows) {
lib.linkSystemLibrary("ws2_32");
lib.linkSystemLibrary("advapi32");
}
lib.addOptions("vsr_options", options);
link_tracer_backend(lib, tracer_backend, cross_target);
lib.step.dependOn(&bindings_step.step);
build_step.dependOn(&lib.step);
}
}
}
fn c_client(
b: *std.build.Builder,
mode: Mode,
dependencies: []const *std.build.Step,
options: *std.build.OptionsStep,
tracer_backend: config.TracerBackend,
) void {
const build_step = b.step("c_client", "Build C client library");
for (dependencies) |dependency| {
build_step.dependOn(dependency);
}
// Updates the generated header file:
const install_header = b.addInstallFile(
.{ .path = "src/clients/c/tb_client.h" },
"../src/clients/c/lib/include/tb_client.h",
);
build_step.dependOn(&install_header.step);
inline for (platforms) |platform| {
const cross_target = CrossTarget.parse(.{ .arch_os_abi = platform[0], .cpu_features = "baseline" }) catch unreachable;
const shared_lib = b.addSharedLibrary("tb_client", "src/clients/c/tb_client.zig", .unversioned);
const static_lib = b.addStaticLibrary("tb_client", "src/clients/c/tb_client.zig");
static_lib.bundle_compiler_rt = true;
static_lib.pie = true;
for ([_]*std.build.LibExeObjStep{ shared_lib, static_lib }) |lib| {
lib.setMainPkgPath("src");
lib.setOutputDir("src/clients/c/lib/" ++ platform[0]);
lib.setTarget(cross_target);
lib.setBuildMode(mode);
lib.linkLibC();
if (cross_target.os_tag.? == .windows) {
lib.linkSystemLibrary("ws2_32");
lib.linkSystemLibrary("advapi32");
}
lib.addOptions("vsr_options", options);
link_tracer_backend(lib, tracer_backend, cross_target);
build_step.dependOn(&lib.step);
}
}
}
fn c_client_sample(
b: *std.build.Builder,
mode: Mode,
target: CrossTarget,
dependencies: []const *std.build.Step,
options: *std.build.OptionsStep,
tracer_backend: config.TracerBackend,
) void {
const c_sample_build = b.step("c_sample", "Build the C client sample");
for (dependencies) |dependency| {
c_sample_build.dependOn(dependency);
}
const static_lib = b.addStaticLibrary("tb_client", "src/clients/c/tb_client.zig");
static_lib.setMainPkgPath("src");
static_lib.linkage = .static;
static_lib.linkLibC();
static_lib.setBuildMode(mode);
static_lib.setTarget(target);
static_lib.pie = true;
static_lib.bundle_compiler_rt = true;
static_lib.addOptions("vsr_options", options);
link_tracer_backend(static_lib, tracer_backend, target);
c_sample_build.dependOn(&static_lib.step);
const sample = b.addExecutable("c_sample", "src/clients/c/samples/main.c");
sample.setBuildMode(mode);
sample.setTarget(target);
sample.linkLibrary(static_lib);
sample.linkLibC();
if (target.isWindows()) {
static_lib.linkSystemLibrary("ws2_32");
static_lib.linkSystemLibrary("advapi32");
// TODO: Illegal instruction on Windows:
sample.disable_sanitize_c = true;
}
const install_step = b.addInstallArtifact(sample);
c_sample_build.dependOn(&install_step.step);
}
// Allows a build step to run the command it builds after it builds it if the user passes --.
// e.g.: ./scripts/build.sh docs_generate --
// Whereas `./scripts/build.sh docs_generate` would not run the command.
fn maybe_execute(
b: *std.build.Builder,
allocator: std.mem.Allocator,
step: *std.build.Step,
binary_name: []const u8,
) void {
var to_run = std.ArrayList([]const u8).init(allocator);
const sep = if (builtin.os.tag == .windows) "\\" else "/";
const ext = if (builtin.os.tag == .windows) ".exe" else "";
to_run.append(
std.fmt.allocPrint(
allocator,
".{s}zig-out{s}bin{s}{s}{s}",
.{
sep,
sep,
sep,
binary_name,
ext,
},
) catch unreachable,
) catch unreachable;
var args = std.process.args();
var build_and_run = false;
while (args.next(allocator)) |arg_or_err| {
const arg = arg_or_err catch unreachable;
if (std.mem.eql(u8, arg, "--")) {
build_and_run = true;
}
if (!build_and_run) continue;
to_run.append(arg) catch unreachable;
}
if (build_and_run) {
const run = b.addSystemCommand(to_run.items);
step.dependOn(&run.step);
}
}
// See src/clients/README.md for documentation.
fn run_with_tb(
allocator: std.mem.Allocator,
b: *std.build.Builder,
mode: Mode,
target: CrossTarget,
) void {
const run_with_tb_build = b.step("run_with_tb", "Build the run_with_tb helper");
const binary = b.addExecutable("run_with_tb", "src/clients/run_with_tb.zig");
binary.setBuildMode(mode);
binary.setTarget(target);
run_with_tb_build.dependOn(&binary.step);
const install_step = b.addInstallArtifact(binary);
run_with_tb_build.dependOn(&install_step.step);
maybe_execute(b, allocator, run_with_tb_build, "run_with_tb");
}
// See src/clients/README.md for documentation.
fn client_integration(
allocator: std.mem.Allocator,
b: *std.build.Builder,
mode: Mode,
target: CrossTarget,
) void {
const client_integration_build = b.step("client_integration", "Run sample integration tests for a client library");
const binary = b.addExecutable("client_integration", "src/clients/integration.zig");
binary.setBuildMode(mode);
binary.setTarget(target);
client_integration_build.dependOn(&binary.step);
const install_step = b.addInstallArtifact(binary);
client_integration_build.dependOn(&install_step.step);
maybe_execute(b, allocator, client_integration_build, "client_integration");
}
// See src/clients/README.md for documentation.
fn client_docs(
allocator: std.mem.Allocator,
b: *std.build.Builder,
mode: Mode,
target: CrossTarget,
) void {
const client_docs_build = b.step("client_docs", "Run sample integration tests for a client library");
const binary = b.addExecutable("client_docs", "src/clients/docs_generate.zig");
binary.setBuildMode(mode);
binary.setTarget(target);
client_docs_build.dependOn(&binary.step);
const install_step = b.addInstallArtifact(binary);
client_docs_build.dependOn(&install_step.step);
maybe_execute(b, allocator, client_docs_build, "client_docs");
}