forked from theseyan/lmdbx-zig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
126 lines (104 loc) · 4.81 KB
/
build.zig
File metadata and controls
126 lines (104 loc) · 4.81 KB
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
const std = @import("std");
const builtin = @import("builtin");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const IS_DEV = if (optimize == .Debug) true else false;
// Whether the target has avx512bw support
const has_avx512 = comptime builtin.cpu.features.isEnabled(@intFromEnum(std.Target.x86.Feature.avx512bw));
// For x86 target, we need .evex512 features explicitly because of a Zig compiler bug.
// Relevant issue: https://github.com/ziglang/zig/issues/20414
// If target doesn't support avx512bw, disable SIMD optimizations completely.
// aarch64 always has NEON.
const target_query = b.resolveTargetQuery(std.Target.Query{
.cpu_arch = target.result.cpu.arch,
.os_tag = target.result.os.tag,
.abi = target.result.abi,
.cpu_features_add = switch (target.result.cpu.arch) {
.x86_64 => blk: {
if (!has_avx512) std.debug.print("Building without SIMD optimizations as target doesn't support avx512bw. This is a Zig compiler bug to be fixed in 0.14.\n", .{});
break :blk std.Target.x86.featureSet(&[_]std.Target.x86.Feature{if (has_avx512) .evex512 else break :blk std.Target.Cpu.Feature.Set.empty});
},
.aarch64 => target.result.cpu.features,
else => std.Target.Cpu.Feature.Set.empty, // Unknown CPU
},
});
const mdbx = b.addModule("lmdbx", .{ .root_source_file = b.path("src/lib.zig") });
// Add CPU features polyfill until https://github.com/ziglang/zig/pull/20081 gets merged
const cpuf_dep = b.dependency("cpu_features", .{});
mdbx.addIncludePath(cpuf_dep.path("cpu_model"));
if (target.result.cpu.arch == .x86_64 or target.result.cpu.arch == .aarch64) {
mdbx.addCSourceFile(.{ .file = switch (target.result.cpu.arch) {
.x86_64 => cpuf_dep.path("cpu_model/x86.c"),
.aarch64 => cpuf_dep.path("cpu_model/aarch64.c"),
else => unreachable,
}, .flags = &.{} });
}
// libMDBX
const mdbx_dep = b.dependency("mdbx", .{});
mdbx.addIncludePath(mdbx_dep.path("."));
// Add headers needed to compile
mdbx.addIncludePath(b.path("src/headers"));
// mdbx.c is amalgated source code
mdbx.addCSourceFile(.{
.file = mdbx_dep.path("mdbx.c"),
.flags = &[_][]const u8{
"-std=gnu11",
"-O2",
"-g",
"-ffunction-sections",
"-fvisibility=hidden",
"-pthread",
"-Wno-error=attributes",
"-fno-semantic-interposition",
"-Wno-unused-command-line-argument",
"-Wno-tautological-compare",
"-Wno-date-time",
"-ULIBMDBX_EXPORTS",
// FIX: On x86_64 Linux, unaligned access IS supported by hardware.
// Force MDBX_UNALIGNED_OK=8 to use direct aligned stores and bypass
// the broken __unaligned fallback path entirely.
"-DMDBX_UNALIGNED_OK=8",
if (IS_DEV) "-DMDBX_DEBUG=2" else "-DMDBX_DEBUG=0",
if (IS_DEV) "-DMDBX_BUILD_FLAGS=\"UNDEBUG\"" else "-DMDBX_BUILD_FLAGS=\"DNDEBUG=1\"",
if (target.result.cpu.arch == .x86_64 and !has_avx512) "-DMDBX_HAVE_BUILTIN_CPU_SUPPORTS=0" else "",
if (target.result.os.tag == .windows) "-includeerrno.h" else "",
if (target.result.os.tag == .windows) "-DMDBX_WITHOUT_MSVC_CRT=1" else "",
switch (target.result.os.tag) {
.windows => "-lm -lntdll -lwinmm -luser32 -lkernel32 -ladvapi32 -lole32",
.macos, .openbsd => "-lm",
else => "-lm -lrt",
},
},
});
mdbx.pic = true; // Enforce PIC
mdbx.sanitize_c = .off; // Address sanitization breaks libMDBX
// Tests
const tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("test/main.zig"),
.target = target_query,
}),
});
tests.linkLibC();
tests.root_module.addImport("lmdbx", mdbx);
const test_runner = b.addRunArtifact(tests);
b.step("test", "Run libMDBX tests").dependOn(&test_runner.step);
// Benchmarks
const bench = b.addExecutable(.{
.name = "lmdbx-benchmark",
.root_module = b.createModule(.{
.root_source_file = b.path("benchmarks/main.zig"),
.target = target_query,
.optimize = if (IS_DEV) .Debug else .ReleaseFast, // moved here
}),
});
bench.linkLibC();
bench.root_module.addImport("lmdbx", mdbx);
b.installArtifact(bench);
// Linker flags for libMDBX
bench.link_gc_sections = true;
bench.link_z_relro = true;
const bench_runner = b.addRunArtifact(bench);
b.step("bench", "Run libMDBX benchmarks").dependOn(&bench_runner.step);
}