forked from tigerbeetle/tigerbeetle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
1042 lines (908 loc) · 36.8 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
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const std = @import("std");
const assert = std.debug.assert;
const builtin = @import("builtin");
const CrossTarget = std.zig.CrossTarget;
const Mode = std.builtin.Mode;
const config = @import("./src/config.zig");
// TigerBeetle binary requires certain CPU feature and supports a closed set of CPUs. Here, we
// specify exactly which features the binary needs. Client shared libraries might be more lax with
// CPU features required.
const supported_targets: []const CrossTarget = supported_targets: {
@setEvalBranchQuota(100_000);
var result: []const CrossTarget = &.{};
const triples = .{
"aarch64-linux",
"aarch64-macos",
"x86_64-linux",
"x86_64-macos",
"x86_64-windows",
};
const cpus = .{
"baseline+aes+neon",
"baseline+aes+neon",
"x86_64_v3+aes",
"x86_64_v3+aes",
"x86_64_v3+aes",
};
for (triples, cpus) |triple, cpu| {
result = result ++ .{CrossTarget.parse(.{
.arch_os_abi = triple,
.cpu_features = cpu,
}) catch unreachable};
}
break :supported_targets result;
};
pub fn build(b: *std.Build) !void {
// A compile error stack trace of 10 is arbitrary in size but helps with debugging.
b.reference_trace = 10;
var target = b.standardTargetOptions(.{});
// Patch the target to use the right CPU. This is a somewhat hacky way to do this, but the core
// idea here is to keep this file as the source of truth for what we need from the CPU.
for (supported_targets) |supported_target| {
if (target.result.cpu.arch == supported_target.cpu_arch) {
// CPU model detection from: https://github.com/ziglang/zig/blob/0.13.0/lib/std/zig/system.zig#L320
target.result.cpu.model = switch (supported_target.cpu_model) {
.native => @panic("pre-defined supported target assumed runtime-detected cpu model"),
.baseline, .determined_by_cpu_arch => std.Target.Cpu.baseline(supported_target.cpu_arch.?).model,
.explicit => |model| model,
};
target.result.cpu.features.addFeatureSet(supported_target.cpu_features_add);
target.result.cpu.features.removeFeatureSet(supported_target.cpu_features_sub);
break;
}
} else @panic("error: unsupported target");
const mode = b.standardOptimizeOption(.{ .preferred_optimize_mode = .ReleaseSafe });
const emit_llvm_ir = b.option(bool, "emit-llvm-ir", "Emit LLVM IR (.ll file)") orelse false;
const enforce_no_changes = blk: {
if (b.option(
bool,
"enforce-no-changes",
"Enforce that generated files are not changed",
)) |v| break :blk v;
if (b.graph.env_map.get("CI")) |ci_env| {
if (std.mem.eql(u8, ci_env, "true"))
break :blk true;
std.debug.panic("expected environment variable CI to be 'true' but got '{s}'", .{ci_env});
}
break :blk false;
};
const options = b.addOptions();
// The "tigerbeetle version" command includes the build-time commit hash.
const git_commit = b.option(
[]const u8,
"git-commit",
"The git commit revision of the source code.",
) orelse std.mem.trimRight(u8, b.run(&.{ "git", "rev-parse", "--verify", "HEAD" }), "\n");
assert(git_commit.len == 40);
options.addOption(?[40]u8, "git_commit", git_commit[0..40].*);
options.addOption(
?[]const u8,
"release",
b.option([]const u8, "config-release", "Release triple."),
);
options.addOption(
?[]const u8,
"release_client_min",
b.option([]const u8, "config-release-client-min", "Minimum client release triple."),
);
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_options_module = options.createModule();
const vsr_module = b.addModule("vsr", .{
.root_source_file = b.path("src/vsr.zig"),
});
vsr_module.addImport("vsr_options", vsr_options_module);
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.result.os.tag == .windows and target.result.abi == .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",
};
const tracy = b.addSystemCommand(&.{
"git",
"clone",
"--branch=v0.9.1",
"https://github.com/wolfpld/tracy.git",
}).addOutputDirectoryArg("tracy");
vsr_module.addCSourceFile(.{
.file = tracy.path(b, "./public/TracyClient.cpp"),
.flags = tracy_c_flags,
});
vsr_module.addIncludePath(tracy.path(b, "./public/tracy"));
vsr_module.link_libc = true;
vsr_module.link_libcpp = true;
if (target.result.os.tag == .windows) {
vsr_module.linkSystemLibrary("dbghelp", .{});
vsr_module.linkSystemLibrary("ws2_32", .{});
}
},
}
{
// Run a tigerbeetle build without running codegen and waiting for llvm
// see <https://github.com/ziglang/zig/commit/5c0181841081170a118d8e50af2a09f5006f59e1>
// how it's supposed to work.
// In short, codegen only runs if zig build sees a dependency on the binary output of
// the step. So we duplicate the build definition so that it doesn't get polluted by
// b.installArtifact.
// TODO(zig): https://github.com/ziglang/zig/issues/18877
const tigerbeetle = b.addExecutable(.{
.name = "tigerbeetle",
.root_source_file = b.path("src/tigerbeetle/main.zig"),
.target = target,
.optimize = mode,
});
tigerbeetle.root_module.addImport("vsr", vsr_module);
tigerbeetle.root_module.addImport("vsr_options", vsr_options_module);
const check = b.step("check", "Check if Tigerbeetle compiles");
check.dependOn(&tigerbeetle.step);
}
const tigerbeetle = b.addExecutable(.{
.name = "tigerbeetle",
.root_source_file = b.path("src/tigerbeetle/main.zig"),
.target = target,
.optimize = mode,
});
if (mode == .ReleaseSafe) {
tigerbeetle.root_module.strip = tracer_backend == .none;
}
if (emit_llvm_ir) {
_ = tigerbeetle.getEmittedLlvmIr();
}
tigerbeetle.root_module.addImport("vsr", vsr_module);
tigerbeetle.root_module.addImport("vsr_options", vsr_options_module);
b.installArtifact(tigerbeetle);
// Ensure that we get stack traces even in release builds.
tigerbeetle.root_module.omit_frame_pointer = false;
{
const run_cmd = b.addRunArtifact(tigerbeetle);
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.getEmittedBin(),
b.pathJoin(&.{ "../../", tigerbeetle.out_filename }),
);
move_cmd.step.dependOn(&tigerbeetle.step);
const install_step = b.getInstallStep();
install_step.dependOn(&move_cmd.step);
}
{
const aof = b.addExecutable(.{
.name = "aof",
.root_source_file = b.path("src/aof.zig"),
.target = target,
.optimize = mode,
});
aof.root_module.addOptions("vsr_options", options);
const run_cmd = b.addRunArtifact(aof);
if (b.args) |args| run_cmd.addArgs(args);
const run_step = b.step("aof", "Run TigerBeetle AOF Utility");
run_step.dependOn(&run_cmd.step);
}
const tb_client_header = blk: {
const tb_client_header = b.addExecutable(.{
.name = "tb_client_header",
.root_source_file = b.path("src/clients/c/tb_client_header.zig"),
.target = target,
});
tb_client_header.root_module.addImport("vsr", vsr_module);
tb_client_header.root_module.addOptions("vsr_options", options);
const run = b.addRunArtifact(tb_client_header);
const out_file = run.addOutputFileArg("tb_client.h");
const install = InstallFile.create(
b,
out_file,
b.pathFromRoot("src/clients/c/tb_client.h"),
.{ .enforce_already_installed = enforce_no_changes },
);
break :blk install.getDest();
};
{
const test_filter: ?[]const u8 =
if (b.args != null and b.args.?.len == 1) b.args.?[0] else null;
const unit_tests = b.addTest(.{
.root_source_file = b.path("src/unit_tests.zig"),
.target = target,
.optimize = mode,
.filter = test_filter,
});
unit_tests.root_module.addImport("vsr_options", vsr_options_module);
// for src/clients/c/tb_client_header_test.zig to use cImport on tb_client.h
unit_tests.linkLibC();
unit_tests.addIncludePath(tb_client_header.dirname());
const unit_tests_exe_step = b.step("test:build", "Build the unit tests");
const install_unit_tests_exe = b.addInstallArtifact(unit_tests, .{});
unit_tests_exe_step.dependOn(&install_unit_tests_exe.step);
const run_unit_tests = b.addRunArtifact(unit_tests);
run_unit_tests.setEnvironmentVariable("ZIG_EXE", b.graph.zig_exe);
const unit_tests_step = b.step("test:unit", "Run the unit tests");
unit_tests_step.dependOn(&run_unit_tests.step);
const integration_tests = b.addTest(.{
.root_source_file = b.path("src/integration_tests.zig"),
.target = target,
.optimize = mode,
});
const run_integration_tests = b.addRunArtifact(integration_tests);
// Ensure integration test have tigerbeetle binary.
run_integration_tests.step.dependOn(b.getInstallStep());
const integration_tests_step = b.step("test:integration", "Run the integration tests");
integration_tests_step.dependOn(&run_integration_tests.step);
const run_fmt = b.addFmt(.{ .paths = &.{"."}, .check = true });
const fmt_test_step = b.step("test:fmt", "Check formatting");
fmt_test_step.dependOn(&run_fmt.step);
const test_step = b.step("test", "Run the unit tests");
test_step.dependOn(&run_unit_tests.step);
if (test_filter == null) {
test_step.dependOn(&run_integration_tests.step);
test_step.dependOn(&run_fmt.step);
}
}
// Clients build:
{
var install_step = b.addInstallArtifact(tigerbeetle, .{});
go_client(
b,
mode,
&.{&install_step.step},
tb_client_header,
target,
options,
enforce_no_changes,
);
java_client(
b,
mode,
&.{&install_step.step},
target,
vsr_module,
options,
);
dotnet_client(
b,
mode,
&.{&install_step.step},
target,
options,
);
node_client(
b,
mode,
&.{&install_step.step},
target,
options,
);
c_client(
b,
mode,
&.{&install_step.step},
tb_client_header,
options,
enforce_no_changes,
);
c_client_sample(
b,
mode,
target,
&.{&install_step.step},
options,
);
}
{
const jni_tests_step = b.step("test:jni", "Run the JNI tests");
// We need libjvm.so both at build time and at a runtime, so use `FailStep` when that is not
// available.
if (b.graph.env_map.get("JAVA_HOME")) |java_home| {
const libjvm_path = b.pathJoin(&.{
java_home,
if (builtin.os.tag == .windows) "/lib" else "/lib/server",
});
const tests = b.addTest(.{
.root_source_file = b.path("src/clients/java/src/jni_tests.zig"),
.target = target,
// TODO(zig): The function `JNI_CreateJavaVM` tries to detect
// the stack size and causes a SEGV that is handled by Zig's panic handler.
// https://bugzilla.redhat.com/show_bug.cgi?id=1572811#c7
//
// The workaround is run the tests in "ReleaseFast" mode.
.optimize = if (builtin.os.tag == .windows) .ReleaseFast else mode,
});
tests.linkLibC();
tests.linkSystemLibrary("jvm");
tests.addLibraryPath(.{ .cwd_relative = libjvm_path });
if (builtin.os.tag == .linux) {
// On Linux, detects the abi by calling `ldd` to check if
// the libjvm.so is linked against libc or musl.
// It's reasonable to assume that ldd will be present.
var exit_code: u8 = undefined;
const stderr_behavior = .Ignore;
const ldd_result = try b.runAllowFail(
&.{ "ldd", b.pathJoin(&.{ libjvm_path, "libjvm.so" }) },
&exit_code,
stderr_behavior,
);
tests.root_module.resolved_target.?.result.abi = if (std.mem.indexOf(u8, ldd_result, "musl") != null)
.musl
else if (std.mem.indexOf(u8, ldd_result, "libc") != null)
.gnu
else {
std.log.err("{s}", .{ldd_result});
return error.JavaAbiUnrecognized;
};
}
switch (builtin.os.tag) {
.windows => set_windows_dll(b.allocator, java_home),
.macos => try b.graph.env_map.put("DYLD_LIBRARY_PATH", libjvm_path),
.linux => try b.graph.env_map.put("LD_LIBRARY_PATH", libjvm_path),
else => unreachable,
}
const tests_run = b.addRunArtifact(tests);
jni_tests_step.dependOn(&tests_run.step);
} else {
const fail_step = FailStep.add(
b,
"can't build jni tests tests, JAVA_HOME is not set",
);
jni_tests_step.dependOn(&fail_step.step);
}
}
{
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 };
simulator_options.addOption(
SimulatorLog,
"log",
b.option(
SimulatorLog,
"simulator-log",
"Log only state transitions (short) or everything (full).",
) orelse .short,
);
const simulator = b.addExecutable(.{
.name = "simulator",
.root_source_file = b.path("src/simulator.zig"),
.target = target,
.optimize = simulator_mode,
});
// Ensure that we get stack traces even in release builds.
simulator.root_module.omit_frame_pointer = false;
simulator.root_module.addOptions("vsr_options", options);
simulator.root_module.addOptions("vsr_simulator_options", simulator_options);
const run_cmd = b.addRunArtifact(simulator);
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);
}
{ // Fuzzers: zig build fuzz -- --events-max=100 lsm_tree 123
const fuzz_exe = b.addExecutable(.{
.name = "fuzz",
.root_source_file = b.path("src/fuzz_tests.zig"),
.target = target,
.optimize = mode,
});
fuzz_exe.root_module.omit_frame_pointer = false;
fuzz_exe.root_module.addOptions("vsr_options", options);
const fuzz_run = b.addRunArtifact(fuzz_exe);
if (b.args) |args| fuzz_run.addArgs(args);
const fuzz_step = b.step("fuzz", "Run the specified fuzzer");
fuzz_step.dependOn(&fuzz_run.step);
const fuzz_install_step = b.addInstallArtifact(fuzz_exe, .{});
const fuzz_build_step = b.step("build_fuzz", "Build fuzzers");
fuzz_build_step.dependOn(&fuzz_install_step.step);
}
{ // Free-form automation: `zig build scripts -- ci --language=java`
const scripts_exe = b.addExecutable(.{
.name = "scripts",
.root_source_file = b.path("src/scripts.zig"),
.target = target,
.optimize = mode,
});
const scripts_run = b.addRunArtifact(scripts_exe);
scripts_run.setEnvironmentVariable("ZIG_EXE", b.graph.zig_exe);
if (b.args) |args| scripts_run.addArgs(args);
const scripts_step = b.step("scripts", "Run automation scripts");
scripts_step.dependOn(&scripts_run.step);
}
}
// Zig cross-targets plus Dotnet RID (Runtime Identifier):
const platforms = .{
.{ "x86_64-linux-gnu.2.27", "linux-x64" },
.{ "x86_64-linux-musl", "linux-musl-x64" },
.{ "x86_64-macos", "osx-x64" },
.{ "aarch64-linux-gnu.2.27", "linux-arm64" },
.{ "aarch64-linux-musl", "linux-musl-arm64" },
.{ "aarch64-macos", "osx-arm64" },
.{ "x86_64-windows", "win-x64" },
};
fn strip_glibc_version(triple: []const u8) []const u8 {
if (std.mem.endsWith(u8, triple, "gnu.2.27")) {
return triple[0 .. triple.len - ".2.27".len];
}
assert(std.mem.indexOf(u8, triple, "gnu") == null);
return triple;
}
fn go_client(
b: *std.Build,
mode: Mode,
dependencies: []const *std.Build.Step,
tb_client_header: std.Build.LazyPath,
target: std.Build.ResolvedTarget,
options: *std.Build.Step.Options,
enforce_no_changes: bool,
) 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 = InstallFile.create(
b,
tb_client_header,
b.pathFromRoot("src/clients/go/pkg/native/tb_client.h"),
.{ .enforce_already_installed = enforce_no_changes },
);
build_step.dependOn(&install_header.step);
const bindings = b.addExecutable(.{
.name = "go_bindings",
.root_source_file = b.path("src/go_bindings.zig"),
.target = target,
});
bindings.root_module.addOptions("vsr_options", options);
const bindings_step = b.addRunArtifact(bindings);
inline for (platforms) |platform| {
// We don't need the linux-gnu builds.
if (comptime std.mem.indexOf(u8, platform[0], "linux-gnu") != null) continue;
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];
const cross_target = CrossTarget.parse(.{ .arch_os_abi = name, .cpu_features = "baseline" }) catch unreachable;
const resolved_target = b.resolveTargetQuery(cross_target);
const lib = b.addStaticLibrary(.{
.name = "tb_client",
.root_source_file = b.path("src/tb_client_exports.zig"),
.target = resolved_target,
.optimize = mode,
});
lib.linkLibC();
lib.pie = true;
lib.bundle_compiler_rt = true;
lib.root_module.stack_protector = false;
lib.root_module.addOptions("vsr_options", options);
lib.step.dependOn(&bindings_step.step);
// NB: New way to do lib.setOutputDir(). The ../ is important to escape zig-cache/.
const lib_install = b.addInstallArtifact(lib, .{});
lib_install.dest_dir = .{ .custom = "../src/clients/go/pkg/native/" ++ name };
build_step.dependOn(&lib_install.step);
}
}
fn java_client(
b: *std.Build,
mode: Mode,
dependencies: []const *std.Build.Step,
target: std.Build.ResolvedTarget,
vsr_module: *std.Build.Module,
options: *std.Build.Step.Options,
) void {
const build_step = b.step("java_client", "Build Java client shared library");
for (dependencies) |dependency| {
build_step.dependOn(dependency);
}
const bindings = b.addExecutable(.{
.name = "java_bindings",
.root_source_file = b.path("src/java_bindings.zig"),
.target = target,
});
bindings.root_module.addOptions("vsr_options", options);
const bindings_step = b.addRunArtifact(bindings);
inline for (platforms) |platform| {
const cross_target = CrossTarget.parse(.{ .arch_os_abi = platform[0], .cpu_features = "baseline" }) catch unreachable;
const resolved_target = b.resolveTargetQuery(cross_target);
const lib = b.addSharedLibrary(.{
.name = "tb_jniclient",
.root_source_file = b.path("src/clients/java/src/client.zig"),
.target = resolved_target,
.optimize = mode,
});
lib.linkLibC();
if (resolved_target.result.os.tag == .windows) {
lib.linkSystemLibrary("ws2_32");
lib.linkSystemLibrary("advapi32");
}
lib.root_module.addImport("vsr", vsr_module);
lib.root_module.addOptions("vsr_options", options);
lib.step.dependOn(&bindings_step.step);
// NB: New way to do lib.setOutputDir(). The ../ is important to escape zig-cache/.
const lib_install = b.addInstallArtifact(lib, .{});
lib_install.dest_dir = .{
.custom = "../src/clients/java/src/main/resources/lib/" ++
comptime strip_glibc_version(platform[0]),
};
build_step.dependOn(&lib_install.step);
}
}
fn dotnet_client(
b: *std.Build,
mode: Mode,
dependencies: []const *std.Build.Step,
target: std.Build.ResolvedTarget,
options: *std.Build.Step.Options,
) void {
const build_step = b.step("dotnet_client", "Build dotnet client shared library");
for (dependencies) |dependency| {
build_step.dependOn(dependency);
}
const bindings = b.addExecutable(.{
.name = "dotnet_bindings",
.root_source_file = b.path("src/dotnet_bindings.zig"),
.target = target,
});
bindings.root_module.addOptions("vsr_options", options);
const bindings_step = b.addRunArtifact(bindings);
inline for (platforms) |platform| {
const cross_target = CrossTarget.parse(.{ .arch_os_abi = platform[0], .cpu_features = "baseline" }) catch unreachable;
const resolved_target = b.resolveTargetQuery(cross_target);
const lib = b.addSharedLibrary(.{
.name = "tb_client",
.root_source_file = b.path("src/tb_client_exports.zig"),
.target = resolved_target,
.optimize = mode,
});
lib.linkLibC();
if (resolved_target.result.os.tag == .windows) {
lib.linkSystemLibrary("ws2_32");
lib.linkSystemLibrary("advapi32");
}
lib.root_module.addOptions("vsr_options", options);
lib.step.dependOn(&bindings_step.step);
// NB: New way to do lib.setOutputDir(). The ../ is important to escape zig-cache/
const lib_install = b.addInstallArtifact(lib, .{});
lib_install.dest_dir = .{
.custom = "../src/clients/dotnet/TigerBeetle/runtimes/" ++ platform[1] ++ "/native",
};
build_step.dependOn(&lib_install.step);
}
}
fn node_client(
b: *std.Build,
mode: Mode,
dependencies: []const *std.Build.Step,
target: std.Build.ResolvedTarget,
options: *std.Build.Step.Options,
) void {
const build_step = b.step("node_client", "Build Node client shared library");
for (dependencies) |dependency| {
build_step.dependOn(dependency);
}
const bindings = b.addExecutable(.{
.name = "node_bindings",
.root_source_file = b.path("src/node_bindings.zig"),
.target = target,
});
bindings.root_module.addOptions("vsr_options", options);
const bindings_step = b.addRunArtifact(bindings);
// Run `npm install` to get access to node headers.
var npm_install = b.addSystemCommand(&.{ "npm", "install" });
npm_install.cwd = b.path("./src/clients/node");
// For windows, compile a set of all symbols that could be exported by node and write it to a
// `.def` file for `zig dlltool` to generate a `.lib` file from.
var write_def_file = b.addSystemCommand(&.{
"node", "--eval",
\\const headers = require('node-api-headers')
\\
\\const allSymbols = new Set()
\\for (const ver of Object.values(headers.symbols)) {
\\ for (const sym of ver.node_api_symbols) {
\\ allSymbols.add(sym)
\\ }
\\ for (const sym of ver.js_native_api_symbols) {
\\ allSymbols.add(sym)
\\ }
\\}
\\
\\fs.writeFileSync('./node.def', 'EXPORTS\n ' + Array.from(allSymbols).join('\n '))
});
write_def_file.cwd = b.path("./src/clients/node");
write_def_file.step.dependOn(&npm_install.step);
var run_dll_tool = b.addSystemCommand(&.{
b.graph.zig_exe, "dlltool",
"-m", "i386:x86-64",
"-D", "node.exe",
"-d", "node.def",
"-l", "node.lib",
});
run_dll_tool.cwd = b.path("./src/clients/node");
run_dll_tool.step.dependOn(&write_def_file.step);
inline for (platforms) |platform| {
const cross_target = CrossTarget.parse(.{ .arch_os_abi = platform[0], .cpu_features = "baseline" }) catch unreachable;
const resolved_target = b.resolveTargetQuery(cross_target);
const lib = b.addSharedLibrary(.{
.name = "tb_nodeclient",
.root_source_file = b.path("src/node.zig"),
.target = resolved_target,
.optimize = mode,
});
lib.linkLibC();
lib.step.dependOn(&npm_install.step);
lib.addSystemIncludePath(b.path("src/clients/node/node_modules/node-api-headers/include"));
lib.linker_allow_shlib_undefined = true;
if (resolved_target.result.os.tag == .windows) {
lib.linkSystemLibrary("ws2_32");
lib.linkSystemLibrary("advapi32");
lib.step.dependOn(&run_dll_tool.step);
lib.addLibraryPath(b.path("src/clients/node"));
lib.linkSystemLibrary("node");
}
lib.root_module.addOptions("vsr_options", options);
lib.step.dependOn(&bindings_step.step);
// NB: New way to do lib.setOutputDir(). The ../ is important to escape zig-cache/
const lib_install = b.addInstallFile(
lib.getEmittedBin(),
"../src/clients/node/dist/bin/" ++
comptime strip_glibc_version(platform[0]) ++
"/client.node",
);
build_step.dependOn(&lib_install.step);
}
}
fn c_client(
b: *std.Build,
mode: Mode,
dependencies: []const *std.Build.Step,
tb_client_header: std.Build.LazyPath,
options: *std.Build.Step.Options,
enforce_no_changes: bool,
) 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 = InstallFile.create(
b,
tb_client_header,
b.pathFromRoot("src/clients/c/lib/include/tb_client.h"),
.{ .enforce_already_installed = enforce_no_changes },
);
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 resolved_target = b.resolveTargetQuery(cross_target);
const shared_lib = b.addSharedLibrary(.{
.name = "tb_client",
.root_source_file = b.path("src/tb_client_exports.zig"),
.target = resolved_target,
.optimize = mode,
});
const static_lib = b.addStaticLibrary(.{
.name = "tb_client",
.root_source_file = b.path("src/tb_client_exports.zig"),
.target = resolved_target,
.optimize = mode,
});
static_lib.bundle_compiler_rt = true;
static_lib.pie = true;
for ([_]*std.Build.Step.Compile{ shared_lib, static_lib }) |lib| {
lib.linkLibC();
if (resolved_target.result.os.tag == .windows) {
lib.linkSystemLibrary("ws2_32");
lib.linkSystemLibrary("advapi32");
}
lib.root_module.addOptions("vsr_options", options);
// NB: New way to do lib.setOutputDir(). The ../ is important to escape zig-cache/
const lib_install = b.addInstallArtifact(lib, .{});
lib_install.dest_dir = .{
.custom = "../src/clients/c/lib/" ++ comptime strip_glibc_version(platform[0]),
};
build_step.dependOn(&lib_install.step);
}
}
}
fn c_client_sample(
b: *std.Build,
mode: Mode,
target: std.Build.ResolvedTarget,
dependencies: []const *std.Build.Step,
options: *std.Build.Step.Options,
) 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(.{
.name = "tb_client",
.root_source_file = b.path("src/tb_client_exports.zig"),
.target = target,
.optimize = mode,
});
static_lib.linkLibC();
static_lib.pie = true;
static_lib.bundle_compiler_rt = true;
static_lib.root_module.addOptions("vsr_options", options);
c_sample_build.dependOn(&static_lib.step);
const sample = b.addExecutable(.{
.name = "c_sample",
.target = target,
.optimize = mode,
});
sample.addCSourceFile(.{
.file = b.path("src/clients/c/samples/main.c"),
});
sample.linkLibrary(static_lib);
sample.linkLibC();
if (target.result.os.tag == .windows) {
static_lib.linkSystemLibrary("ws2_32");
static_lib.linkSystemLibrary("advapi32");
// TODO: Illegal instruction on Windows:
sample.root_module.sanitize_c = false;
}
const install_step = b.addInstallArtifact(sample, .{});
c_sample_build.dependOn(&install_step.step);
}
/// Steps which unconditionally fails with a message.
///
/// This is useful for cases where at configuration time you can determine that a certain step
/// can't succeeded (e.g., a system library is not preset on the host system), but you only want
/// to fail the step once the user tries to run it. That is, you don't want to fail the whole build,
/// as other steps might run fine.
const FailStep = struct {
step: std.Build.Step,
message: []const u8,
fn add(b: *std.Build, message: []const u8) *FailStep {
const result = b.allocator.create(FailStep) catch unreachable;
result.* = .{
.step = std.Build.Step.init(.{
.id = .custom,
.name = "failure",
.owner = b,
.makeFn = FailStep.make,
}),
.message = message,
};
return result;
}
fn make(step: *std.Build.Step, _: std.Progress.Node) anyerror!void {
const self: *FailStep = @fieldParentPtr("step", step);
std.log.err("{s}", .{self.message});
return error.FailStep;
}
};
/// Set the JVM DLL directory on Windows.
fn set_windows_dll(allocator: std.mem.Allocator, java_home: []const u8) void {
comptime std.debug.assert(builtin.os.tag == .windows);
const set_dll_directory = struct {
pub extern "kernel32" fn SetDllDirectoryA(path: [*:0]const u8) callconv(.C) std.os.windows.BOOL;
}.SetDllDirectoryA;
const java_bin_path = std.fs.path.joinZ(
allocator,
&.{ java_home, "\\bin" },
) catch unreachable;
_ = set_dll_directory(java_bin_path);
const java_bin_server_path = std.fs.path.joinZ(
allocator,
&.{ java_home, "\\bin\\server" },
) catch unreachable;
_ = set_dll_directory(java_bin_server_path);
}
const InstallFile = struct {
step: std.Build.Step,
source: std.Build.LazyPath,
dest_path: []const u8,
generated: std.Build.GeneratedFile,
enforce_already_installed: bool,
pub fn create(
owner: *std.Build,
source: std.Build.LazyPath,
dest_path: []const u8,
options: struct {
enforce_already_installed: bool = false,
},
) *InstallFile {
assert(dest_path.len != 0);
const install = owner.allocator.create(InstallFile) catch @panic("OOM");
install.* = .{
.step = std.Build.Step.init(.{
.id = .custom,
.name = owner.fmt(
"install {s} to {s}",
.{ source.getDisplayName(), dest_path },
),
.owner = owner,
.makeFn = make,
}),
.source = source.dupe(owner),
.dest_path = owner.dupePath(dest_path),