-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.zig
More file actions
343 lines (295 loc) · 10.9 KB
/
build.zig
File metadata and controls
343 lines (295 loc) · 10.9 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
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
const std = @import("std");
const builtin = @import("builtin");
const mem = std.mem;
const Build = std.Build;
const Step = Build.Step;
const Compile = Step.Compile;
const LinkMode = std.builtin.LinkMode;
const OptimizeMode = std.builtin.OptimizeMode;
const lmdb_root = "libraries/liblmdb";
const cflags: []const []const u8 = &.{
"-pthread",
"-std=c23",
};
const sanitize_c: std.zig.SanitizeC = .off;
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 strip = b.option(bool, "strip", "Strip debug information") orelse false;
const linkage = b.option(LinkMode, "linkage", "Link mode for Lmdb") orelse .static;
const lto = b.option(std.zig.LtoMode, "lto", "Enable link time optimization") orelse .none;
const lmdb_upstream = b.dependency(
"lmdb",
.{ .target = target, .optimize = optimize },
);
const build_opt: BuildOpt = .{
.upstream = lmdb_upstream,
.lto = lto,
.strip = strip,
.target = target,
.optimize = optimize,
.linkage = linkage,
};
const lmdb: BuildLmdb = .{
.b = b,
.opt = build_opt,
};
const liblmdb = lmdb.lib();
lmdb.tools(liblmdb);
lmdb.api();
lmdb.tests(liblmdb);
}
const BuildLmdb = struct {
b: *Build,
opt: BuildOpt,
fn lib(bl: BuildLmdb) *Compile {
const opt = bl.opt;
const b = bl.b;
const lmdb_includes: []const []const u8 = &.{
"lmdb.h",
"midl.h",
};
const liblmdb_src: []const []const u8 = &.{
"mdb.c",
"midl.c",
};
const module = b.createModule(.{
.target = opt.target,
.optimize = opt.optimize,
.link_libc = true,
.strip = opt.strip,
.sanitize_c = sanitize_c,
});
module.addCSourceFiles(.{
.root = opt.upstream.path(lmdb_root),
.files = liblmdb_src,
.flags = cflags,
});
module.addIncludePath(opt.upstream.path(lmdb_root));
module.addCMacro("_XOPEN_SOURCE", "800");
if (opt.isMacos()) {
module.addCMacro("_DARWIN_C_SOURCE", "");
}
const liblmdb = b.addLibrary(.{
.name = "lmdb",
.linkage = opt.linkage,
.root_module = module,
.use_llvm = opt.use_llvm(),
.use_lld = opt.use_lld(),
});
liblmdb.lto = opt.use_lto();
liblmdb.installHeadersDirectory(
opt.upstream.path(lmdb_root),
"",
.{ .include_extensions = lmdb_includes },
);
b.installArtifact(liblmdb);
return liblmdb;
}
fn api(bl: BuildLmdb) void {
const b = bl.b;
const opt = bl.opt;
const lmdb_api = b.addTranslateC(.{
.root_source_file = b.path("include/c.h"),
.target = b.graph.host,
.optimize = .Debug,
});
lmdb_api.addIncludePath(opt.upstream.path(lmdb_root));
_ = b.addModule("lmdb", .{
.root_source_file = lmdb_api.getOutput(),
.target = opt.target,
.optimize = opt.optimize,
});
}
fn tools(bl: BuildLmdb, liblmdb: *Compile) void {
const b = bl.b;
const tools_step = b.step("tools", "Install lmdb tools");
const build_tools = struct {
fn build_tools(bl_: BuildLmdb, liblmdb_: *Compile, tools_step_: *Step, lmdb_tools: []const []const u8) void {
const b_ = bl_.b;
const opt_ = bl_.opt;
for (lmdb_tools) |tool_file| {
const bin_name = tool_file[0..mem.findScalar(u8, tool_file, '.').?];
const module = b_.createModule(.{
.target = opt_.target,
.optimize = opt_.optimize,
.link_libc = true,
.strip = opt_.strip,
.sanitize_c = sanitize_c,
});
module.addIncludePath(opt_.upstream.path(lmdb_root));
module.addCMacro("_XOPEN_SOURCE", "800");
if (opt_.isMacos()) {
module.addCMacro("_DARWIN_C_SOURCE", "");
}
module.linkLibrary(liblmdb_);
module.addCSourceFiles(.{
.root = opt_.upstream.path(lmdb_root),
.files = &.{tool_file},
.flags = cflags,
});
const tool = b_.addExecutable(.{
.name = bin_name,
.root_module = module,
.use_llvm = opt_.use_llvm(),
.use_lld = opt_.use_lld(),
});
const install_tool = b_.addInstallArtifact(tool, .{});
tools_step_.dependOn(&install_tool.step);
}
}
}.build_tools;
const core_tools = [_][]const u8{
"mdb_copy.c",
"mdb_drop.c",
"mdb_dump.c",
"mdb_load.c",
"mdb_stat.c",
};
build_tools(bl, liblmdb, tools_step, core_tools[0..]);
// Disable mplay because windows doesn't have the posix system header
// 'sys/wait.h' and it doesn't compile on `musl` libc becaues `stdin`
// and `stdout` are defined as `const` which `mplay.c` tries to modify
const extra_tools = [_][]const u8{"mplay.c"};
const disable_mplay = true;
if (!disable_mplay) build_tools(bl, liblmdb, tools_step, extra_tools[0..]);
}
fn tests(bl: BuildLmdb, liblmdb: *Compile) void {
const b = bl.b;
const opt = bl.opt;
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: Step.MakeOptions) !void {
_ = options;
const step_build = step.owner;
std.Io.Dir.cwd().createDirPath(step_build.graph.io, step_build.fmt(
"{s}/{s}/testdb/",
.{ step_build.install_prefix, install_test_subpath },
)) catch |err| switch (err) {
error.PathAlreadyExists => {},
else => unreachable,
};
}
}.makeFn;
const create_testdb = struct {
fn makeFn(step: *Step, options: Step.MakeOptions) !void {
_ = options;
const test_run = Step.cast(step, Step.Run).?;
const step_build = test_run.step.owner;
const subpath = "testdb/";
const bin_path = test_run.cwd.?.getPath3(step.owner, step);
bin_path.createDirPath(step_build.graph.io, subpath) catch 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 test_step = b.step("test", "Run lmdb tests");
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 cflags_test = .{
"-pthread",
"-std=c17", //c23 forbids function use without prototype
"-Wno-format",
"-Wno-implicit-function-declaration",
};
for (lmdb_test) |test_file| {
const test_name = test_file[0..mem.findScalar(u8, test_file, '.').?];
const module = b.createModule(.{
.target = opt.target,
.optimize = .Debug,
.link_libc = true,
.sanitize_c = sanitize_c,
});
module.addCSourceFiles(.{
.root = opt.upstream.path(lmdb_root),
.files = &.{test_file},
.flags = &cflags_test,
});
module.addIncludePath(opt.upstream.path(lmdb_root));
module.linkLibrary(liblmdb);
const test_exe = b.addExecutable(.{
.name = test_name,
.root_module = module,
.use_lld = opt.use_lld(),
});
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);
const run_create_testdb = create_testdb(run.step.owner, test_dirname);
run_create_testdb.dependOn(&test_exe.step);
run.step.dependOn(run_create_testdb);
test_step.dependOn(&run.step);
install_test_step.dependOn(&install_test_exe.step);
}
}
};
const BuildOpt = struct {
upstream: *Build.Dependency,
target: Build.ResolvedTarget,
optimize: OptimizeMode,
strip: bool,
lto: std.zig.LtoMode,
linkage: LinkMode,
fn isOs(os: std.Target.Os.Tag, target: Build.ResolvedTarget) bool {
return builtin.os.tag == os or target.result.os.tag == os;
}
fn isMacos(opt: BuildOpt) bool {
return isOs(.macos, opt.target);
}
fn isWindows(opt: BuildOpt) bool {
return isOs(.windows, opt.target);
}
fn use_lto(opt: BuildOpt) std.zig.LtoMode {
return if (opt.isMacos())
.none
else if (opt.use_lld())
opt.lto
else
.none;
}
fn use_llvm(opt: BuildOpt) bool {
return switch (opt.optimize) {
.Debug => if (opt.isWindows()) true else false,
else => true,
};
}
// writing WritingLibFiles in zld isn't implemented on windows
// and zld is the only linker supported on macos
fn use_lld(opt: BuildOpt) bool {
return if (opt.isMacos()) false else if (opt.isWindows()) true else switch (opt.optimize) {
.Debug => false,
else => true,
};
}
};
// 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 = 15, .patch = 1 };
const version = builtin.zig_version;
const order = version.order(needed_version);
return order != .lt;
}