-
Notifications
You must be signed in to change notification settings - Fork 12
/
build.zig
388 lines (336 loc) · 8.85 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
//! build.zig - zig build for lcdb
//! Copyright (c) 2022, Christopher Jeffrey (MIT License).
//! https://github.com/chjj/lcdb
const std = @import("std");
const ArrayList = std.ArrayList;
const fs = std.fs;
//
// Build
//
pub fn build(b: *std.build.Builder) void {
const pkg_version = "0.0.0";
const abi_version = b.version(0, 0, 0);
const inst_step = b.getInstallStep();
const test_step = b.step("test", "Run tests");
const bench_step = b.step("bench", "Run benchmarks");
//
// Options
//
b.setPreferredReleaseMode(.ReleaseFast);
const target = b.standardTargetOptions(.{});
const mode = b.standardReleaseOptions();
const prefix = b.option([]const u8, "prefix",
"System install prefix (/usr/local)");
const enable_bench = b.option(bool, "bench",
"Build benchmarks (true)") orelse true;
const enable_pic = b.option(bool, "pic", "Force PIC (false)");
const enable_portable = b.option(bool, "portable",
"Be as portable as possible (false)") orelse false;
const enable_pthread = b.option(bool, "pthread",
"Use pthread (true)") orelse true;
const enable_shared = b.option(bool, "shared",
"Build shared library (true)") orelse true;
const enable_tests = b.option(bool, "tests",
"Enable tests (true)") orelse true;
const strip = (b.is_release and !target.isWindows());
//
// Variables
//
var flags = ArrayList([]const u8).init(b.allocator);
var defines = ArrayList([]const u8).init(b.allocator);
var libs = ArrayList([]const u8).init(b.allocator);
defer flags.deinit();
defer defines.deinit();
defer libs.deinit();
//
// Global Flags
//
if (target.isGnuLibC()) {
flags.append("-std=c89") catch unreachable;
defines.append("_GNU_SOURCE") catch unreachable;
}
flags.append("-fvisibility=hidden") catch unreachable;
if (target.isDarwin()) {
flags.append("-mmacosx-version-min=10.7") catch unreachable;
}
if (mode == .ReleaseFast) {
flags.append("-O3") catch unreachable;
}
//
// System Libraries
//
if (target.isWindows()) {
libs.append("kernel32") catch unreachable;
} else {
if (enable_pthread and target.getOsTag() != .wasi) {
flags.append("-pthread") catch unreachable;
libs.append("pthread") catch unreachable;
defines.append("LDB_PTHREAD") catch unreachable;
}
}
//
// Sources
//
const sources = [_][]const u8{
"src/util/arena.c",
"src/util/array.c",
"src/util/atomic.c",
"src/util/bloom.c",
"src/util/buffer.c",
"src/util/cache.c",
"src/util/comparator.c",
"src/util/crc32c.c",
"src/util/env.c",
"src/util/hash.c",
"src/util/internal.c",
"src/util/logger.c",
"src/util/options.c",
"src/util/port.c",
"src/util/random.c",
"src/util/rbt.c",
"src/util/slice.c",
"src/util/snappy.c",
"src/util/status.c",
"src/util/strutil.c",
"src/util/thread_pool.c",
"src/util/vector.c",
"src/table/block.c",
"src/table/block_builder.c",
"src/table/filter_block.c",
"src/table/format.c",
"src/table/iterator.c",
"src/table/merger.c",
"src/table/table.c",
"src/table/table_builder.c",
"src/table/two_level_iterator.c",
"src/builder.c",
"src/c.c",
"src/db_impl.c",
"src/db_iter.c",
"src/dbformat.c",
"src/dumpfile.c",
"src/filename.c",
"src/log_reader.c",
"src/log_writer.c",
"src/memtable.c",
"src/repair.c",
"src/skiplist.c",
"src/table_cache.c",
"src/version_edit.c",
"src/version_set.c",
"src/write_batch.c"
};
//
// Flags
//
const warn_flags = [_][]const u8{
"-pedantic",
"-Wall",
"-Wextra",
"-Wcast-align",
"-Wconditional-uninitialized",
"-Wmissing-prototypes",
"-Wno-implicit-fallthrough",
"-Wno-long-long",
"-Wno-overlength-strings",
"-Wshadow",
"-Wstrict-prototypes",
"-Wundef"
};
for (warn_flags) |flag| {
flags.append(flag) catch unreachable;
}
//
// Defines
//
if (mode == .Debug) {
defines.append("LDB_DEBUG") catch unreachable;
}
if (!enable_portable) {
defines.append("LDB_HAVE_FDATASYNC") catch unreachable;
defines.append("LDB_HAVE_PREAD") catch unreachable;
}
//
// Targets
//
const libname = if (target.isWindows()) "liblcdb" else "lcdb";
const lcdb = b.addStaticLibrary(libname, null);
lcdb.setTarget(target);
lcdb.setBuildMode(mode);
lcdb.install();
lcdb.linkLibC();
lcdb.addIncludeDir("./include");
lcdb.addCSourceFiles(&sources, flags.items);
lcdb.force_pic = enable_pic;
lcdb.strip = strip;
for (defines.items) |def| {
lcdb.defineCMacroRaw(def);
}
if (enable_shared and target.getOsTag() != .wasi) {
const shared = b.addSharedLibrary("lcdb", null, abi_version);
shared.setTarget(target);
shared.setBuildMode(mode);
shared.install();
shared.linkLibC();
shared.addIncludeDir("./include");
shared.addCSourceFiles(&sources, flags.items);
shared.strip = strip;
for (defines.items) |def| {
shared.defineCMacroRaw(def);
}
shared.defineCMacroRaw("LDB_EXPORT");
for (libs.items) |lib| {
shared.linkSystemLibrary(lib);
}
}
const dbutil = b.addExecutable("lcdbutil", null);
dbutil.setTarget(target);
dbutil.setBuildMode(mode);
dbutil.install();
dbutil.linkLibC();
dbutil.linkLibrary(lcdb);
dbutil.addIncludeDir("./include");
dbutil.addCSourceFile("src/dbutil.c", flags.items);
dbutil.strip = strip;
for (defines.items) |def| {
dbutil.defineCMacroRaw(def);
}
for (libs.items) |lib| {
dbutil.linkSystemLibrary(lib);
}
//
// Benchmarks
//
const bench = b.addExecutable("db_bench", null);
bench.setTarget(target);
bench.setBuildMode(mode);
bench.linkLibC();
bench.linkLibrary(lcdb);
bench.addIncludeDir("./include");
bench.addIncludeDir("./src");
bench.addCSourceFiles(&.{
"bench/db_bench.c",
"bench/histogram.c",
"src/util/testutil.c"
}, flags.items);
for (defines.items) |def| {
bench.defineCMacroRaw(def);
}
for (libs.items) |lib| {
bench.linkSystemLibrary(lib);
}
if (enable_bench) {
inst_step.dependOn(&bench.step);
}
bench_step.dependOn(&bench.run().step);
//
// Tests
//
const testutil = b.addStaticLibrary("test", null);
testutil.setTarget(target);
testutil.setBuildMode(mode);
testutil.linkLibC();
testutil.addIncludeDir("./include");
testutil.addCSourceFile("src/util/testutil.c", flags.items);
for (defines.items) |def| {
testutil.defineCMacroRaw(def);
}
const tests = [_][]const u8{
"arena",
"autocompact",
"bloom",
"c",
"cache",
"coding",
"corruption",
"crc32c",
"db",
"dbformat",
"env",
"filename",
"filter_block",
"hash",
"issue178",
"issue200",
"issue320",
"log",
"rbt",
"recovery",
"simple",
"skiplist",
"snappy",
"status",
"strutil",
"table",
"util",
"version_edit",
"version_set",
"write_batch"
};
for (tests) |name| {
const t = b.addExecutable(b.fmt("t-{s}", .{ name }), null);
t.setTarget(target);
t.setBuildMode(mode);
t.linkLibC();
t.linkLibrary(testutil);
t.linkLibrary(lcdb);
t.addIncludeDir("./include");
t.addIncludeDir("./src");
t.addCSourceFile(b.fmt("test/t-{s}.c", .{ name }), flags.items);
for (defines.items) |def| {
t.defineCMacroRaw(def);
}
for (libs.items) |lib| {
t.linkSystemLibrary(lib);
}
if (enable_tests) {
inst_step.dependOn(&t.step);
}
test_step.dependOn(&t.run().step);
}
//
// Package Config
//
if (!target.isWindows() and target.getOsTag() != .wasi) {
const pkg_prefix = prefix orelse "/usr/local";
const pkg_libs = if (enable_pthread) "-lpthread" else "";
const pkg_conf = b.fmt(
\\prefix={s}
\\exec_prefix=${{prefix}}
\\libdir=${{exec_prefix}}/{s}
\\includedir=${{prefix}}/{s}
\\
\\Name: lcdb
\\Version: {s}
\\Description: Database for C.
\\URL: https://github.com/chjj/lcdb
\\
\\Cflags: -I${{includedir}}
\\Libs: -L${{libdir}} -llcdb
\\Libs.private: {s}
\\
,
.{
pkg_prefix,
"lib",
"include",
pkg_version,
pkg_libs
}
);
fs.cwd().writeFile(b.pathFromRoot("lcdb.pc"), pkg_conf) catch {};
}
//
// Install
//
b.installFile("include/lcdb.h", "include/lcdb.h");
b.installFile("include/lcdb_c.h", "include/lcdb_c.h");
if (!target.isWindows() and target.getOsTag() != .wasi) {
b.installFile("LICENSE", "share/licenses/lcdb/LICENSE");
b.installFile("README.md", "share/doc/lcdb/README.md");
b.installFile("lcdb.pc", "lib/pkgconfig/lcdb.pc");
} else {
b.installFile("LICENSE", "LICENSE");
b.installFile("README.md", "README.md");
}
}