-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.zig
260 lines (224 loc) · 8.04 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
const std = @import("std");
const builtin = @import("builtin");
const mem = std.mem;
const Build = std.Build;
const Step = Build.Step;
pub fn build(b: *Build) void {
if (comptime !checkVersion())
@compileError("Update your zig toolchain to >= 0.13.0");
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const lmdb_upstream = b.dependency(
"lmdb",
.{ .target = target, .optimize = optimize },
);
const lmdb_root = "libraries/liblmdb";
const strip = b.option(bool, "strip", "Strip debug information") orelse false;
const lto = b.option(bool, "lto", "Enable link time optimization") orelse false;
// writing WritingLibFiles isn't implemented on windows
// and zld the only linker supported on macos
const is_macos = builtin.os.tag == .macos;
const is_windows = builtin.os.tag == .windows;
const use_lld = if (is_macos) false else if (is_windows) true else switch (optimize) {
.Debug => false,
else => true,
};
const use_lto = if (is_macos) false else if (use_lld) lto else false;
const liblmdb = b.addStaticLibrary(.{
.name = "lmdb",
.target = target,
.optimize = optimize,
.link_libc = true,
.strip = strip,
.use_llvm = switch (optimize) {
.Debug => if (is_windows) true else false,
else => true,
},
.use_lld = use_lld,
});
liblmdb.want_lto = use_lto;
liblmdb.root_module.sanitize_c = false;
const liblmdb_src = .{
"mdb.c",
"midl.c",
};
const lmdb_includes = .{
"lmdb.h",
"midl.h",
};
const cflags = .{
"-pthread",
"-std=c23",
};
liblmdb.addCSourceFiles(.{
.root = lmdb_upstream.path(lmdb_root),
.files = &liblmdb_src,
.flags = &cflags,
});
liblmdb.addIncludePath(lmdb_upstream.path(lmdb_root));
liblmdb.root_module.addCMacro("_XOPEN_SOURCE", "600");
if (is_macos) {
liblmdb.root_module.addCMacro("_DARWIN_C_SOURCE", "");
}
liblmdb.installHeadersDirectory(
lmdb_upstream.path(lmdb_root),
"",
.{ .include_extensions = &lmdb_includes },
);
b.installArtifact(liblmdb);
const lmdb_tools = [_][]const u8{
"mdb_copy.c",
"mdb_drop.c",
"mdb_dump.c",
"mdb_load.c",
"mdb_stat.c",
"mplay.c",
};
const tools_step = b.step("tools", "Install lmdb tools");
for (lmdb_tools) |tool_file| {
const bin_name = tool_file[0..mem.indexOfScalar(u8, tool_file, '.').?];
const tool = b.addExecutable(.{
.name = bin_name,
.target = target,
.optimize = optimize,
.link_libc = true,
.strip = strip,
.use_llvm = switch (optimize) {
.Debug => false,
else => true,
},
.use_lld = use_lld,
});
tool.root_module.sanitize_c = false;
tool.addCSourceFiles(.{
.root = lmdb_upstream.path(lmdb_root),
.files = &.{tool_file},
.flags = &cflags,
});
tool.addIncludePath(lmdb_upstream.path(lmdb_root));
tool.root_module.addCMacro("_XOPEN_SOURCE", "600");
if (is_macos) {
tool.root_module.addCMacro("_DARWIN_C_SOURCE", "");
}
tool.linkLibrary(liblmdb);
const install_tool = b.addInstallArtifact(tool, .{});
tools_step.dependOn(&install_tool.step);
}
const lmdb_api = b.addTranslateC(.{
.root_source_file = b.path("include/c.h"),
.target = b.graph.host,
.optimize = .Debug,
});
if (@hasDecl(Step.TranslateC, "addIncludeDir")) {
const path = lmdb_upstream.path(lmdb_root);
const absolute_include = path.getPath2(b, null);
lmdb_api.addIncludeDir(absolute_include);
} else {
lmdb_api.addIncludePath(lmdb_upstream.path(lmdb_root));
}
_ = b.addModule("lmdb", .{
.root_source_file = lmdb_api.getOutput(),
.target = target,
.optimize = optimize,
});
const cflags_test = .{
"-pthread",
"-std=c17", //c23 forbids function use without prototype
"-Wno-format",
"-Wno-implicit-function-declaration",
};
const lmdb_test = [_][]const u8{
"mtest.c",
"mtest2.c",
"mtest3.c",
"mtest4.c",
"mtest5.c",
// "mtest6.c", // disabled as it requires building liblmdb with MDB_DEBUG
};
const MakeOptions = if (@hasDecl(Step, "MakeOptions"))
Step.MakeOptions
else
std.Progress.Node;
const test_step = b.step("test", "Run lmdb tests");
const run_create_testdb = struct {
fn makeFn(step: *Step, options: MakeOptions) !void {
_ = options;
const test_run = Step.cast(step, Step.Run).?;
const subpath = "testdb/";
if (@hasDecl(Build.LazyPath, "getPath3")) {
const bin_path = test_run.cwd.?.getPath3(step.owner, step);
bin_path.makePath(subpath) catch unreachable;
} else {
const bin_path = test_run.cwd.?.getPath2(step.owner, step);
const owner = test_run.step.owner;
const full_path = owner.fmt("{s}/{s}", .{ bin_path, subpath });
owner.cache_root.handle.makeDir(full_path) catch |err| switch (err) {
error.PathAlreadyExists => {},
else => unreachable,
};
}
}
fn create_testdb(owner: *Build, test_dirname: Build.LazyPath) *Step {
const run = Step.Run.create(owner, "create testdb at the generated path");
run.step.makeFn = makeFn;
run.setCwd(test_dirname);
return &run.step;
}
}.create_testdb;
const install_test_step = b.step("install-test", "Install lmdb tests");
const install_test_subpath = "test/";
install_test_step.makeFn = struct {
fn makeFn(step: *Step, options: MakeOptions) !void {
_ = options;
const step_build = step.owner;
std.fs.cwd().makeDir(step_build.fmt(
"{s}/{s}/testdb/",
.{ step_build.install_prefix, install_test_subpath },
)) catch |err| switch (err) {
error.PathAlreadyExists => {},
else => unreachable,
};
}
}.makeFn;
for (lmdb_test) |test_file| {
const test_name = test_file[0..mem.indexOfScalar(u8, test_file, '.').?];
const test_exe = b.addExecutable(.{
.name = test_name,
.target = target,
.optimize = .Debug,
.link_libc = true,
.use_lld = use_lld,
});
test_exe.root_module.sanitize_c = false;
test_exe.addCSourceFiles(.{
.root = lmdb_upstream.path(lmdb_root),
.files = &.{test_file},
.flags = &cflags_test,
});
test_exe.addIncludePath(lmdb_upstream.path(lmdb_root));
test_exe.linkLibrary(liblmdb);
const test_dirname = test_exe.getEmittedBin().dirname();
const install_test_exe = b.addInstallArtifact(test_exe, .{ .dest_dir = .{ .override = .{
.custom = install_test_subpath,
} } });
const run = b.addRunArtifact(test_exe);
run.setCwd(test_dirname);
run.expectExitCode(0);
run.enableTestRunnerMode();
const create_testdb = run_create_testdb(run.step.owner, test_dirname);
create_testdb.dependOn(&test_exe.step);
run.step.dependOn(create_testdb);
test_step.dependOn(&run.step);
install_test_step.dependOn(&install_test_exe.step);
}
}
// ensures the currently in-use zig version is at least the minimum required
fn checkVersion() bool {
if (!@hasDecl(builtin, "zig_version")) {
return false;
}
const needed_version = std.SemanticVersion{ .major = 0, .minor = 13, .patch = 0 };
const version = builtin.zig_version;
const order = version.order(needed_version);
return order != .lt;
}