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
|
const std = @import("std");
const builtin = @import("builtin");
const Step = std.Build.Step;
const ResolvedTarget = std.Build.ResolvedTarget;
const Query = std.Target.Query;
var sqlite3: ?*Step.Compile = null;
fn linkSqlite(b: *Step.Compile) void {
if (sqlite3) |lib| {
b.linkLibrary(lib);
} else {
b.linkLibC();
b.linkSystemLibrary("sqlite3");
}
}
fn getTarget(original_target: ResolvedTarget, bundled: bool) ResolvedTarget {
if (bundled) {
var tmp = original_target;
if (tmp.result.isGnuLibC()) {
const min_glibc_version = std.SemanticVersion{
.major = 2,
.minor = 28,
.patch = 0,
};
const ver = tmp.result.os.version_range.linux.glibc;
if (ver.order(min_glibc_version) == .lt) {
std.debug.panic("sqlite requires glibc version >= 2.28", .{});
}
}
return tmp;
}
return original_target;
}
const TestTarget = struct {
query: Query,
single_threaded: bool = false,
bundled: bool,
};
const ci_targets = switch (builtin.target.cpu.arch) {
.x86_64 => switch (builtin.target.os.tag) {
.linux => [_]TestTarget{
// Targets linux but other CPU archs.
TestTarget{
.query = .{},
.bundled = false,
},
TestTarget{
.query = .{
.cpu_arch = .x86_64,
.abi = .musl,
},
.bundled = true,
},
TestTarget{
.query = .{
.cpu_arch = .x86,
.abi = .musl,
},
.bundled = true,
},
},
.windows => [_]TestTarget{
TestTarget{
.query = .{
.cpu_arch = .x86_64,
.abi = .gnu,
},
.bundled = true,
},
TestTarget{
.query = .{
.cpu_arch = .x86,
.abi = .gnu,
},
.bundled = true,
},
},
.macos => [_]TestTarget{
TestTarget{
.query = .{
.cpu_arch = .x86_64,
},
.bundled = true,
},
// TODO(vincent): this fails for some reason
// TestTarget{
// .query =.{
// .cpu_arch = .aarch64,
// },
// .bundled = true,
// },
},
else => [_]TestTarget{
TestTarget{
.query = .{},
.bundled = false,
},
},
},
else => [_]TestTarget{
TestTarget{
.query = .{},
.bundled = false,
},
},
};
const all_test_targets = switch (builtin.target.cpu.arch) {
.x86_64 => switch (builtin.target.os.tag) {
.linux => [_]TestTarget{
// Targets linux but other CPU archs.
TestTarget{
.query = .{},
.bundled = false,
},
TestTarget{
.query = .{
.cpu_arch = .x86_64,
.abi = .musl,
},
.bundled = true,
},
TestTarget{
.query = .{
.cpu_arch = .x86,
.abi = .musl,
},
.bundled = true,
},
TestTarget{
.query = .{
.cpu_arch = .aarch64,
.abi = .musl,
},
.bundled = true,
},
TestTarget{
.query = .{
.cpu_arch = .riscv64,
.abi = .musl,
},
.bundled = true,
},
TestTarget{
.query = .{
.cpu_arch = .mips,
.abi = .musl,
},
.bundled = true,
},
// TODO(vincent): failing for some time for unknown reasons
// TestTarget{
// .query =.{
// .cpu_arch = .arm,
// .abi = .musleabihf,
// },
// .bundled = true,
// },
// Targets windows
TestTarget{
.query = .{
.cpu_arch = .x86_64,
.os_tag = .windows,
},
.bundled = true,
},
TestTarget{
.query = .{
.cpu_arch = .x86,
.os_tag = .windows,
},
.bundled = true,
},
// Targets macOS
TestTarget{
.query = .{
.cpu_arch = .x86_64,
.os_tag = .macos,
},
.bundled = true,
},
TestTarget{
.query = .{
.cpu_arch = .aarch64,
.os_tag = .macos,
},
.bundled = true,
},
},
.windows => [_]TestTarget{
TestTarget{
.query = .{
.cpu_arch = .x86_64,
.abi = .gnu,
},
.bundled = true,
},
TestTarget{
.query = .{
.cpu_arch = .x86,
.abi = .gnu,
},
.bundled = true,
},
},
.freebsd => [_]TestTarget{
TestTarget{
.query = .{},
.bundled = false,
},
TestTarget{
.query = .{
.cpu_arch = .x86_64,
},
.bundled = true,
},
},
.macos => [_]TestTarget{
TestTarget{
.query = .{
.cpu_arch = .x86_64,
},
.bundled = true,
},
},
else => [_]TestTarget{
TestTarget{
.query = .{},
.bundled = false,
},
},
},
else => [_]TestTarget{
TestTarget{
.query = .{},
.bundled = false,
},
},
};
fn computeTestTargets(isNative: bool, ci: ?bool) ?[]const TestTarget {
if (ci != null and ci.?) return &ci_targets;
if (isNative) {
// If the target is native we assume the user didn't change it with -Dtarget and run all test targets.
return &all_test_targets;
}
// Otherwise we run a single test target.
return null;
}
pub fn build(b: *std.Build) !void {
const in_memory = b.option(bool, "in_memory", "Should the tests run with sqlite in memory (default true)") orelse true;
const dbfile = b.option([]const u8, "dbfile", "Always use this database file instead of a temporary one");
const use_bundled = b.option(bool, "use_bundled", "Use the bundled sqlite3 source instead of linking the system library (default false)");
const ci = b.option(bool, "ci", "Build and test in the CI on GitHub");
const query = b.standardTargetOptionsQueryOnly(.{});
const target = b.resolveTargetQuery(query);
const optimize = b.standardOptimizeOption(.{});
_ = b.addModule("sqlite", .{ .root_source_file = .{ .path = "sqlite.zig" } });
const sqlite_lib = b.addStaticLibrary(.{
.name = "sqlite",
.target = target,
.optimize = optimize,
});
sqlite_lib.addIncludePath(.{ .path = "c/" });
sqlite_lib.addCSourceFile(.{
.file = .{ .path = "c/sqlite3.c" },
.flags = &[_][]const u8{"-std=c99"},
});
sqlite_lib.linkLibC();
sqlite_lib.installHeader("c/sqlite3.h", "sqlite3.h");
b.installArtifact(sqlite_lib);
// Tool to preprocess the sqlite header files.
//
// Due to limitations of translate-c the standard header files can't be used for building loadable extensions
// so we have this tool which creates usable header files.
const preprocess_files_tool = b.addExecutable(.{
.name = "preprocess-files",
.root_source_file = .{ .path = "tools/preprocess_files.zig" },
.target = getTarget(target, true),
.optimize = optimize,
});
// Add a top-level step to run the preprocess-files tool
const preprocess_files_run = b.step("preprocess-files", "Run the preprocess-files tool");
const preprocess_files_tool_run = b.addRunArtifact(preprocess_files_tool);
preprocess_files_run.dependOn(&preprocess_files_tool_run.step);
const test_targets = computeTestTargets(query.isNative(), ci) orelse &[_]TestTarget{.{
.query = query,
.bundled = use_bundled orelse false,
}};
const test_step = b.step("test", "Run library tests");
// By default the tests will only be execute for native test targets, however they will be compiled
// for _all_ targets defined in `test_targets`.
//
// If you want to execute tests for other targets you can pass -fqemu, -fdarling, -fwine, -frosetta.
for (test_targets) |test_target| {
const bundled = use_bundled orelse test_target.bundled;
const cross_target = getTarget(b.resolveTargetQuery(test_target.query), bundled);
const single_threaded_txt = if (test_target.single_threaded) "single" else "multi";
const test_name = b.fmt("{s}-{s}-{s}", .{
try cross_target.result.zigTriple(b.allocator),
@tagName(optimize),
single_threaded_txt,
});
const tests = b.addTest(.{
.name = test_name,
.target = cross_target,
.optimize = optimize,
.root_source_file = .{ .path = "sqlite.zig" },
.single_threaded = test_target.single_threaded,
});
const run_tests = b.addRunArtifact(tests);
if (bundled) {
const lib = b.addStaticLibrary(.{
.name = "sqlite",
.target = cross_target,
.optimize = optimize,
});
lib.addCSourceFile(.{
.file = .{ .path = "c/sqlite3.c" },
.flags = &[_][]const u8{"-std=c99"},
});
lib.linkLibC();
sqlite3 = lib;
}
if (bundled) tests.addIncludePath(.{ .path = "c" });
linkSqlite(tests);
const lib = b.addStaticLibrary(.{
.name = "zig-sqlite",
.root_source_file = .{ .path = "sqlite.zig" },
.target = cross_target,
.optimize = optimize,
});
if (bundled) lib.addIncludePath(.{ .path = "c" });
linkSqlite(lib);
const tests_options = b.addOptions();
tests.root_module.addImport("build_options", tests_options.createModule());
tests_options.addOption(bool, "in_memory", in_memory);
tests_options.addOption(?[]const u8, "dbfile", dbfile);
test_step.dependOn(&run_tests.step);
}
// Fuzzing
const lib = b.addStaticLibrary(.{
.name = "sqlite",
.target = getTarget(target, true),
.optimize = optimize,
});
lib.addCSourceFile(.{
.file = .{ .path = "c/sqlite3.c" },
.flags = &[_][]const u8{"-std=c99"},
});
lib.addIncludePath(.{ .path = "c" });
lib.linkLibC();
// The library
const fuzz_lib = b.addStaticLibrary(.{
.name = "fuzz-lib",
.root_source_file = .{ .path = "fuzz/main.zig" },
.target = getTarget(target, true),
.optimize = optimize,
});
fuzz_lib.addIncludePath(.{ .path = "c" });
fuzz_lib.linkLibrary(lib);
fuzz_lib.want_lto = true;
fuzz_lib.bundle_compiler_rt = true;
fuzz_lib.root_module.addImport("sqlite", b.createModule(.{
.root_source_file = .{ .path = "sqlite.zig" },
}));
// Setup the output name
const fuzz_executable_name = "fuzz";
const fuzz_exe_path = try b.cache_root.join(b.allocator, &.{fuzz_executable_name});
// We want `afl-clang-lto -o path/to/output path/to/library`
const fuzz_compile = b.addSystemCommand(&.{ "afl-clang-lto", "-o", fuzz_exe_path });
fuzz_compile.addArtifactArg(lib);
fuzz_compile.addArtifactArg(fuzz_lib);
// Install the cached output to the install 'bin' path
const fuzz_install = b.addInstallBinFile(.{ .path = fuzz_exe_path }, fuzz_executable_name);
// Add a top-level step that compiles and installs the fuzz executable
const fuzz_compile_run = b.step("fuzz", "Build executable for fuzz testing using afl-clang-lto");
// fuzz_compile_run.dependOn(&fuzz_lib.step);
fuzz_compile_run.dependOn(&fuzz_compile.step);
fuzz_compile_run.dependOn(&fuzz_install.step);
// Compile a companion exe for debugging crashes
const fuzz_debug_exe = b.addExecutable(.{
.name = "fuzz-debug",
.root_source_file = .{ .path = "fuzz/main.zig" },
.target = getTarget(target, true),
.optimize = optimize,
});
fuzz_debug_exe.addIncludePath(.{ .path = "c" });
fuzz_debug_exe.linkLibrary(lib);
fuzz_debug_exe.root_module.addImport("sqlite", b.createModule(.{
.root_source_file = .{ .path = "sqlite.zig" },
}));
// Only install fuzz-debug when the fuzz step is run
const install_fuzz_debug_exe = b.addInstallArtifact(fuzz_debug_exe, .{});
fuzz_compile_run.dependOn(&install_fuzz_debug_exe.step);
//
// Examples
//
// Loadable extension
//
// This builds an example shared library with the extension and a binary that tests it.
const zigcrypto_loadable_ext = b.addSharedLibrary(.{
.name = "zigcrypto",
.root_source_file = .{ .path = "examples/zigcrypto.zig" },
.version = null,
.target = getTarget(target, true),
.optimize = optimize,
});
zigcrypto_loadable_ext.pie = true;
zigcrypto_loadable_ext.addIncludePath(.{ .path = "c" });
zigcrypto_loadable_ext.root_module.addImport("sqlite", b.createModule(.{
.root_source_file = .{ .path = "sqlite.zig" },
}));
zigcrypto_loadable_ext.linkLibrary(lib);
const install_zigcrypto_loadable_ext = b.addInstallArtifact(zigcrypto_loadable_ext, .{});
const zigcrypto_test = b.addExecutable(.{
.name = "zigcrypto-test",
.root_source_file = .{ .path = "examples/zigcrypto_test.zig" },
.target = getTarget(target, true),
.optimize = optimize,
});
zigcrypto_test.addIncludePath(.{ .path = "c" });
zigcrypto_test.root_module.addImport("sqlite", b.createModule(.{
.root_source_file = .{ .path = "sqlite.zig" },
}));
zigcrypto_test.linkLibrary(lib);
const install_zigcrypto_test = b.addInstallArtifact(zigcrypto_test, .{});
const zigcrypto_compile_run = b.step("zigcrypto", "Build the 'zigcrypto' SQLite loadable extension");
zigcrypto_compile_run.dependOn(&install_zigcrypto_loadable_ext.step);
zigcrypto_compile_run.dependOn(&install_zigcrypto_test.step);
}
|