summaryrefslogtreecommitdiff
path: root/build.zig
diff options
context:
space:
mode:
Diffstat (limited to 'build.zig')
-rw-r--r--build.zig121
1 files changed, 61 insertions, 60 deletions
diff --git a/build.zig b/build.zig
index 8b8ae3e..24d3be6 100644
--- a/build.zig
+++ b/build.zig
@@ -1,9 +1,12 @@
1const std = @import("std"); 1const std = @import("std");
2const builtin = @import("builtin"); 2const builtin = @import("builtin");
3const Step = std.Build.Step;
4const ResolvedTarget = std.Build.ResolvedTarget;
5const Query = std.Target.Query;
3 6
4var sqlite3: ?*std.Build.Step.Compile = null; 7var sqlite3: ?*Step.Compile = null;
5 8
6fn linkSqlite(b: *std.Build.Step.Compile) void { 9fn linkSqlite(b: *Step.Compile) void {
7 if (sqlite3) |lib| { 10 if (sqlite3) |lib| {
8 b.linkLibrary(lib); 11 b.linkLibrary(lib);
9 } else { 12 } else {
@@ -12,22 +15,19 @@ fn linkSqlite(b: *std.Build.Step.Compile) void {
12 } 15 }
13} 16}
14 17
15fn getTarget(original_target: std.zig.CrossTarget, bundled: bool) std.zig.CrossTarget { 18fn getTarget(original_target: ResolvedTarget, bundled: bool) ResolvedTarget {
16 if (bundled) { 19 if (bundled) {
17 var tmp = original_target; 20 var tmp = original_target;
18 21
19 if (tmp.isGnuLibC()) { 22 if (tmp.result.isGnuLibC()) {
20 const min_glibc_version = std.SemanticVersion{ 23 const min_glibc_version = std.SemanticVersion{
21 .major = 2, 24 .major = 2,
22 .minor = 28, 25 .minor = 28,
23 .patch = 0, 26 .patch = 0,
24 }; 27 };
25 if (tmp.glibc_version) |ver| { 28 const ver = tmp.result.os.version_range.linux.glibc;
26 if (ver.order(min_glibc_version) == .lt) { 29 if (ver.order(min_glibc_version) == .lt) {
27 std.debug.panic("sqlite requires glibc version >= 2.28", .{}); 30 std.debug.panic("sqlite requires glibc version >= 2.28", .{});
28 }
29 } else {
30 tmp.setGnuLibCVersion(2, 28, 0);
31 } 31 }
32 } 32 }
33 33
@@ -38,7 +38,7 @@ fn getTarget(original_target: std.zig.CrossTarget, bundled: bool) std.zig.CrossT
38} 38}
39 39
40const TestTarget = struct { 40const TestTarget = struct {
41 target: std.zig.CrossTarget = @as(std.zig.CrossTarget, .{}), 41 query: Query,
42 single_threaded: bool = false, 42 single_threaded: bool = false,
43 bundled: bool, 43 bundled: bool,
44}; 44};
@@ -48,18 +48,18 @@ const ci_targets = switch (builtin.target.cpu.arch) {
48 .linux => [_]TestTarget{ 48 .linux => [_]TestTarget{
49 // Targets linux but other CPU archs. 49 // Targets linux but other CPU archs.
50 TestTarget{ 50 TestTarget{
51 .target = .{}, 51 .query = .{},
52 .bundled = false, 52 .bundled = false,
53 }, 53 },
54 TestTarget{ 54 TestTarget{
55 .target = .{ 55 .query = .{
56 .cpu_arch = .x86_64, 56 .cpu_arch = .x86_64,
57 .abi = .musl, 57 .abi = .musl,
58 }, 58 },
59 .bundled = true, 59 .bundled = true,
60 }, 60 },
61 TestTarget{ 61 TestTarget{
62 .target = .{ 62 .query = .{
63 .cpu_arch = .x86, 63 .cpu_arch = .x86,
64 .abi = .musl, 64 .abi = .musl,
65 }, 65 },
@@ -68,14 +68,14 @@ const ci_targets = switch (builtin.target.cpu.arch) {
68 }, 68 },
69 .windows => [_]TestTarget{ 69 .windows => [_]TestTarget{
70 TestTarget{ 70 TestTarget{
71 .target = .{ 71 .query = .{
72 .cpu_arch = .x86_64, 72 .cpu_arch = .x86_64,
73 .abi = .gnu, 73 .abi = .gnu,
74 }, 74 },
75 .bundled = true, 75 .bundled = true,
76 }, 76 },
77 TestTarget{ 77 TestTarget{
78 .target = .{ 78 .query = .{
79 .cpu_arch = .x86, 79 .cpu_arch = .x86,
80 .abi = .gnu, 80 .abi = .gnu,
81 }, 81 },
@@ -84,14 +84,14 @@ const ci_targets = switch (builtin.target.cpu.arch) {
84 }, 84 },
85 .macos => [_]TestTarget{ 85 .macos => [_]TestTarget{
86 TestTarget{ 86 TestTarget{
87 .target = .{ 87 .query = .{
88 .cpu_arch = .x86_64, 88 .cpu_arch = .x86_64,
89 }, 89 },
90 .bundled = true, 90 .bundled = true,
91 }, 91 },
92 // TODO(vincent): this fails for some reason 92 // TODO(vincent): this fails for some reason
93 // TestTarget{ 93 // TestTarget{
94 // .target = .{ 94 // .query =.{
95 // .cpu_arch = .aarch64, 95 // .cpu_arch = .aarch64,
96 // }, 96 // },
97 // .bundled = true, 97 // .bundled = true,
@@ -99,14 +99,14 @@ const ci_targets = switch (builtin.target.cpu.arch) {
99 }, 99 },
100 else => [_]TestTarget{ 100 else => [_]TestTarget{
101 TestTarget{ 101 TestTarget{
102 .target = .{}, 102 .query = .{},
103 .bundled = false, 103 .bundled = false,
104 }, 104 },
105 }, 105 },
106 }, 106 },
107 else => [_]TestTarget{ 107 else => [_]TestTarget{
108 TestTarget{ 108 TestTarget{
109 .target = .{}, 109 .query = .{},
110 .bundled = false, 110 .bundled = false,
111 }, 111 },
112 }, 112 },
@@ -117,39 +117,39 @@ const all_test_targets = switch (builtin.target.cpu.arch) {
117 .linux => [_]TestTarget{ 117 .linux => [_]TestTarget{
118 // Targets linux but other CPU archs. 118 // Targets linux but other CPU archs.
119 TestTarget{ 119 TestTarget{
120 .target = .{}, 120 .query = .{},
121 .bundled = false, 121 .bundled = false,
122 }, 122 },
123 TestTarget{ 123 TestTarget{
124 .target = .{ 124 .query = .{
125 .cpu_arch = .x86_64, 125 .cpu_arch = .x86_64,
126 .abi = .musl, 126 .abi = .musl,
127 }, 127 },
128 .bundled = true, 128 .bundled = true,
129 }, 129 },
130 TestTarget{ 130 TestTarget{
131 .target = .{ 131 .query = .{
132 .cpu_arch = .x86, 132 .cpu_arch = .x86,
133 .abi = .musl, 133 .abi = .musl,
134 }, 134 },
135 .bundled = true, 135 .bundled = true,
136 }, 136 },
137 TestTarget{ 137 TestTarget{
138 .target = .{ 138 .query = .{
139 .cpu_arch = .aarch64, 139 .cpu_arch = .aarch64,
140 .abi = .musl, 140 .abi = .musl,
141 }, 141 },
142 .bundled = true, 142 .bundled = true,
143 }, 143 },
144 TestTarget{ 144 TestTarget{
145 .target = .{ 145 .query = .{
146 .cpu_arch = .riscv64, 146 .cpu_arch = .riscv64,
147 .abi = .musl, 147 .abi = .musl,
148 }, 148 },
149 .bundled = true, 149 .bundled = true,
150 }, 150 },
151 TestTarget{ 151 TestTarget{
152 .target = .{ 152 .query = .{
153 .cpu_arch = .mips, 153 .cpu_arch = .mips,
154 .abi = .musl, 154 .abi = .musl,
155 }, 155 },
@@ -157,7 +157,7 @@ const all_test_targets = switch (builtin.target.cpu.arch) {
157 }, 157 },
158 // TODO(vincent): failing for some time for unknown reasons 158 // TODO(vincent): failing for some time for unknown reasons
159 // TestTarget{ 159 // TestTarget{
160 // .target = .{ 160 // .query =.{
161 // .cpu_arch = .arm, 161 // .cpu_arch = .arm,
162 // .abi = .musleabihf, 162 // .abi = .musleabihf,
163 // }, 163 // },
@@ -165,14 +165,14 @@ const all_test_targets = switch (builtin.target.cpu.arch) {
165 // }, 165 // },
166 // Targets windows 166 // Targets windows
167 TestTarget{ 167 TestTarget{
168 .target = .{ 168 .query = .{
169 .cpu_arch = .x86_64, 169 .cpu_arch = .x86_64,
170 .os_tag = .windows, 170 .os_tag = .windows,
171 }, 171 },
172 .bundled = true, 172 .bundled = true,
173 }, 173 },
174 TestTarget{ 174 TestTarget{
175 .target = .{ 175 .query = .{
176 .cpu_arch = .x86, 176 .cpu_arch = .x86,
177 .os_tag = .windows, 177 .os_tag = .windows,
178 }, 178 },
@@ -180,14 +180,14 @@ const all_test_targets = switch (builtin.target.cpu.arch) {
180 }, 180 },
181 // Targets macOS 181 // Targets macOS
182 TestTarget{ 182 TestTarget{
183 .target = .{ 183 .query = .{
184 .cpu_arch = .x86_64, 184 .cpu_arch = .x86_64,
185 .os_tag = .macos, 185 .os_tag = .macos,
186 }, 186 },
187 .bundled = true, 187 .bundled = true,
188 }, 188 },
189 TestTarget{ 189 TestTarget{
190 .target = .{ 190 .query = .{
191 .cpu_arch = .aarch64, 191 .cpu_arch = .aarch64,
192 .os_tag = .macos, 192 .os_tag = .macos,
193 }, 193 },
@@ -196,14 +196,14 @@ const all_test_targets = switch (builtin.target.cpu.arch) {
196 }, 196 },
197 .windows => [_]TestTarget{ 197 .windows => [_]TestTarget{
198 TestTarget{ 198 TestTarget{
199 .target = .{ 199 .query = .{
200 .cpu_arch = .x86_64, 200 .cpu_arch = .x86_64,
201 .abi = .gnu, 201 .abi = .gnu,
202 }, 202 },
203 .bundled = true, 203 .bundled = true,
204 }, 204 },
205 TestTarget{ 205 TestTarget{
206 .target = .{ 206 .query = .{
207 .cpu_arch = .x86, 207 .cpu_arch = .x86,
208 .abi = .gnu, 208 .abi = .gnu,
209 }, 209 },
@@ -212,11 +212,11 @@ const all_test_targets = switch (builtin.target.cpu.arch) {
212 }, 212 },
213 .freebsd => [_]TestTarget{ 213 .freebsd => [_]TestTarget{
214 TestTarget{ 214 TestTarget{
215 .target = .{}, 215 .query = .{},
216 .bundled = false, 216 .bundled = false,
217 }, 217 },
218 TestTarget{ 218 TestTarget{
219 .target = .{ 219 .query = .{
220 .cpu_arch = .x86_64, 220 .cpu_arch = .x86_64,
221 }, 221 },
222 .bundled = true, 222 .bundled = true,
@@ -224,7 +224,7 @@ const all_test_targets = switch (builtin.target.cpu.arch) {
224 }, 224 },
225 .macos => [_]TestTarget{ 225 .macos => [_]TestTarget{
226 TestTarget{ 226 TestTarget{
227 .target = .{ 227 .query = .{
228 .cpu_arch = .x86_64, 228 .cpu_arch = .x86_64,
229 }, 229 },
230 .bundled = true, 230 .bundled = true,
@@ -232,23 +232,23 @@ const all_test_targets = switch (builtin.target.cpu.arch) {
232 }, 232 },
233 else => [_]TestTarget{ 233 else => [_]TestTarget{
234 TestTarget{ 234 TestTarget{
235 .target = .{}, 235 .query = .{},
236 .bundled = false, 236 .bundled = false,
237 }, 237 },
238 }, 238 },
239 }, 239 },
240 else => [_]TestTarget{ 240 else => [_]TestTarget{
241 TestTarget{ 241 TestTarget{
242 .target = .{}, 242 .query = .{},
243 .bundled = false, 243 .bundled = false,
244 }, 244 },
245 }, 245 },
246}; 246};
247 247
248fn computeTestTargets(target: std.zig.CrossTarget, ci: ?bool) ?[]const TestTarget { 248fn computeTestTargets(isNative: bool, ci: ?bool) ?[]const TestTarget {
249 if (ci != null and ci.?) return &ci_targets; 249 if (ci != null and ci.?) return &ci_targets;
250 250
251 if (target.isNative()) { 251 if (isNative) {
252 // If the target is native we assume the user didn't change it with -Dtarget and run all test targets. 252 // If the target is native we assume the user didn't change it with -Dtarget and run all test targets.
253 return &all_test_targets; 253 return &all_test_targets;
254 } 254 }
@@ -263,10 +263,11 @@ pub fn build(b: *std.Build) !void {
263 const use_bundled = b.option(bool, "use_bundled", "Use the bundled sqlite3 source instead of linking the system library (default false)"); 263 const use_bundled = b.option(bool, "use_bundled", "Use the bundled sqlite3 source instead of linking the system library (default false)");
264 const ci = b.option(bool, "ci", "Build and test in the CI on GitHub"); 264 const ci = b.option(bool, "ci", "Build and test in the CI on GitHub");
265 265
266 const target = b.standardTargetOptions(.{}); 266 const query = b.standardTargetOptionsQueryOnly(.{});
267 const target = b.resolveTargetQuery(query);
267 const optimize = b.standardOptimizeOption(.{}); 268 const optimize = b.standardOptimizeOption(.{});
268 269
269 _ = b.addModule("sqlite", .{ .source_file = .{ .path = "sqlite.zig" } }); 270 _ = b.addModule("sqlite", .{ .root_source_file = .{ .path = "sqlite.zig" } });
270 271
271 const sqlite_lib = b.addStaticLibrary(.{ 272 const sqlite_lib = b.addStaticLibrary(.{
272 .name = "sqlite", 273 .name = "sqlite",
@@ -302,8 +303,8 @@ pub fn build(b: *std.Build) !void {
302 const preprocess_files_tool_run = b.addRunArtifact(preprocess_files_tool); 303 const preprocess_files_tool_run = b.addRunArtifact(preprocess_files_tool);
303 preprocess_files_run.dependOn(&preprocess_files_tool_run.step); 304 preprocess_files_run.dependOn(&preprocess_files_tool_run.step);
304 305
305 const test_targets = computeTestTargets(target, ci) orelse &[_]TestTarget{.{ 306 const test_targets = computeTestTargets(query.isNative(), ci) orelse &[_]TestTarget{.{
306 .target = target, 307 .query = query,
307 .bundled = use_bundled orelse false, 308 .bundled = use_bundled orelse false,
308 }}; 309 }};
309 const test_step = b.step("test", "Run library tests"); 310 const test_step = b.step("test", "Run library tests");
@@ -315,10 +316,10 @@ pub fn build(b: *std.Build) !void {
315 316
316 for (test_targets) |test_target| { 317 for (test_targets) |test_target| {
317 const bundled = use_bundled orelse test_target.bundled; 318 const bundled = use_bundled orelse test_target.bundled;
318 const cross_target = getTarget(test_target.target, bundled); 319 const cross_target = getTarget(b.resolveTargetQuery(test_target.query), bundled);
319 const single_threaded_txt = if (test_target.single_threaded) "single" else "multi"; 320 const single_threaded_txt = if (test_target.single_threaded) "single" else "multi";
320 const test_name = b.fmt("{s}-{s}-{s}", .{ 321 const test_name = b.fmt("{s}-{s}-{s}", .{
321 try cross_target.zigTriple(b.allocator), 322 try cross_target.result.zigTriple(b.allocator),
322 @tagName(optimize), 323 @tagName(optimize),
323 single_threaded_txt, 324 single_threaded_txt,
324 }); 325 });
@@ -359,7 +360,7 @@ pub fn build(b: *std.Build) !void {
359 linkSqlite(lib); 360 linkSqlite(lib);
360 361
361 const tests_options = b.addOptions(); 362 const tests_options = b.addOptions();
362 tests.addOptions("build_options", tests_options); 363 tests.root_module.addImport("build_options", tests_options.createModule());
363 364
364 tests_options.addOption(bool, "in_memory", in_memory); 365 tests_options.addOption(bool, "in_memory", in_memory);
365 tests_options.addOption(?[]const u8, "dbfile", dbfile); 366 tests_options.addOption(?[]const u8, "dbfile", dbfile);
@@ -392,9 +393,9 @@ pub fn build(b: *std.Build) !void {
392 fuzz_lib.linkLibrary(lib); 393 fuzz_lib.linkLibrary(lib);
393 fuzz_lib.want_lto = true; 394 fuzz_lib.want_lto = true;
394 fuzz_lib.bundle_compiler_rt = true; 395 fuzz_lib.bundle_compiler_rt = true;
395 fuzz_lib.addAnonymousModule("sqlite", .{ 396 fuzz_lib.root_module.addImport("sqlite", b.createModule(.{
396 .source_file = .{ .path = "sqlite.zig" }, 397 .root_source_file = .{ .path = "sqlite.zig" },
397 }); 398 }));
398 399
399 // Setup the output name 400 // Setup the output name
400 const fuzz_executable_name = "fuzz"; 401 const fuzz_executable_name = "fuzz";
@@ -423,9 +424,9 @@ pub fn build(b: *std.Build) !void {
423 }); 424 });
424 fuzz_debug_exe.addIncludePath(.{ .path = "c" }); 425 fuzz_debug_exe.addIncludePath(.{ .path = "c" });
425 fuzz_debug_exe.linkLibrary(lib); 426 fuzz_debug_exe.linkLibrary(lib);
426 fuzz_debug_exe.addAnonymousModule("sqlite", .{ 427 fuzz_debug_exe.root_module.addImport("sqlite", b.createModule(.{
427 .source_file = .{ .path = "sqlite.zig" }, 428 .root_source_file = .{ .path = "sqlite.zig" },
428 }); 429 }));
429 430
430 // Only install fuzz-debug when the fuzz step is run 431 // Only install fuzz-debug when the fuzz step is run
431 const install_fuzz_debug_exe = b.addInstallArtifact(fuzz_debug_exe, .{}); 432 const install_fuzz_debug_exe = b.addInstallArtifact(fuzz_debug_exe, .{});
@@ -446,11 +447,11 @@ pub fn build(b: *std.Build) !void {
446 .target = getTarget(target, true), 447 .target = getTarget(target, true),
447 .optimize = optimize, 448 .optimize = optimize,
448 }); 449 });
449 zigcrypto_loadable_ext.force_pic = true; 450 zigcrypto_loadable_ext.pie = true;
450 zigcrypto_loadable_ext.addIncludePath(.{ .path = "c" }); 451 zigcrypto_loadable_ext.addIncludePath(.{ .path = "c" });
451 zigcrypto_loadable_ext.addAnonymousModule("sqlite", .{ 452 zigcrypto_loadable_ext.root_module.addImport("sqlite", b.createModule(.{
452 .source_file = .{ .path = "sqlite.zig" }, 453 .root_source_file = .{ .path = "sqlite.zig" },
453 }); 454 }));
454 zigcrypto_loadable_ext.linkLibrary(lib); 455 zigcrypto_loadable_ext.linkLibrary(lib);
455 456
456 const install_zigcrypto_loadable_ext = b.addInstallArtifact(zigcrypto_loadable_ext, .{}); 457 const install_zigcrypto_loadable_ext = b.addInstallArtifact(zigcrypto_loadable_ext, .{});
@@ -462,9 +463,9 @@ pub fn build(b: *std.Build) !void {
462 .optimize = optimize, 463 .optimize = optimize,
463 }); 464 });
464 zigcrypto_test.addIncludePath(.{ .path = "c" }); 465 zigcrypto_test.addIncludePath(.{ .path = "c" });
465 zigcrypto_test.addAnonymousModule("sqlite", .{ 466 zigcrypto_test.root_module.addImport("sqlite", b.createModule(.{
466 .source_file = .{ .path = "sqlite.zig" }, 467 .root_source_file = .{ .path = "sqlite.zig" },
467 }); 468 }));
468 zigcrypto_test.linkLibrary(lib); 469 zigcrypto_test.linkLibrary(lib);
469 470
470 const install_zigcrypto_test = b.addInstallArtifact(zigcrypto_test, .{}); 471 const install_zigcrypto_test = b.addInstallArtifact(zigcrypto_test, .{});