-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
196 lines (169 loc) · 6.27 KB
/
build.zig
File metadata and controls
196 lines (169 loc) · 6.27 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
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const arch = target.result.cpu.arch;
// --- Options ---
const enable_json = b.option(bool, "json", "Enable JSON conversion support (requires yyjson)") orelse true;
const enable_error_messages = b.option(bool, "error-messages", "Enable lite3 error messages for debugging") orelse false;
const enable_lto = b.option(bool, "lto", "Enable link-time optimization for the C library (currently unsupported)") orelse false;
if (enable_lto) {
std.log.err("`-Dlto=true` is currently unsupported in lite3-zig; remove the flag.", .{});
std.process.exit(1);
}
// --- Paths ---
const lite3_include_path = b.path("vendor/lite3/include");
const lite3_lib_path = b.path("vendor/lite3/lib");
const shim_include_path = b.path("src");
// --- Compile the lite3 C library ---
// The C library is always compiled with ReleaseFast because:
// 1. It is a vendored dependency whose correctness is tested upstream.
// 2. lite3 uses intentional out-of-bounds prefetch hints (__builtin_prefetch)
// that trigger false positives under Zig's Debug-mode bounds checks.
const lite3_mod = b.createModule(.{
.target = target,
.optimize = .ReleaseFast,
.link_libc = true,
});
const build_options = b.addOptions();
build_options.addOption(bool, "json_enabled", enable_json);
const c_flags: []const []const u8 = &.{
"-std=gnu11",
"-Wall",
"-Wextra",
"-Wpedantic",
"-Wno-gnu-statement-expression",
"-Wno-gnu-zero-variadic-macro-arguments",
};
// Core source files
const core_sources: []const []const u8 = &.{
"vendor/lite3/src/lite3.c",
"vendor/lite3/src/ctx_api.c",
"vendor/lite3/src/debug.c",
};
for (core_sources) |src| {
lite3_mod.addCSourceFile(.{
.file = b.path(src),
.flags = c_flags,
});
}
// JSON-related source files
if (enable_json) {
const json_sources: []const []const u8 = &.{
"vendor/lite3/src/json_enc.c",
"vendor/lite3/src/json_dec.c",
"vendor/lite3/lib/yyjson/yyjson.c",
"vendor/lite3/lib/nibble_base64/base64.c",
};
for (json_sources) |src| {
lite3_mod.addCSourceFile(.{
.file = b.path(src),
.flags = c_flags,
});
}
lite3_mod.addIncludePath(lite3_lib_path);
} else {
// lite3 headers always declare JSON entry points. Provide explicit stubs
// so `-Djson=false` links cleanly and JSON APIs return EINVAL.
lite3_mod.addCSourceFile(.{
.file = b.path("src/lite3_json_disabled.c"),
.flags = c_flags,
});
}
// Add the C shim file (with relaxed warnings for lite3 header quirks)
const shim_flags: []const []const u8 = &.{
"-std=gnu11",
"-Wall",
"-Wextra",
"-Wno-pedantic",
"-Wno-gnu-statement-expression",
"-Wno-gnu-zero-variadic-macro-arguments",
};
lite3_mod.addCSourceFile(.{
.file = b.path("src/lite3_shim.c"),
.flags = shim_flags,
});
lite3_mod.addIncludePath(lite3_include_path);
lite3_mod.addIncludePath(shim_include_path);
if (enable_error_messages) {
lite3_mod.addCMacro("LITE3_ERROR_MESSAGES", "");
}
// Upstream warns that prefetching can crash on non-x86 targets.
// Keep it enabled on x86/x86_64 and disable elsewhere.
if (arch != .x86 and arch != .x86_64) {
lite3_mod.addCMacro("LITE3_DISABLE_PREFETCHING", "1");
}
const lite3_lib = b.addLibrary(.{
.linkage = .static,
.name = "lite3",
.root_module = lite3_mod,
});
b.installArtifact(lite3_lib);
// --- Public Zig module ---
const zig_mod = b.addModule("lite3", .{
.root_source_file = b.path("src/lite3.zig"),
.target = target,
.optimize = optimize,
});
zig_mod.addOptions("lite3_build_options", build_options);
zig_mod.addIncludePath(shim_include_path);
zig_mod.linkLibrary(lite3_lib);
// --- Tests ---
const test_mod = b.createModule(.{
.root_source_file = b.path("src/tests.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "lite3", .module = zig_mod },
},
});
test_mod.addIncludePath(shim_include_path);
test_mod.linkLibrary(lite3_lib);
const tests = b.addTest(.{
.root_module = test_mod,
});
const run_tests = b.addRunArtifact(tests);
const test_step = b.step("test", "Run lite3-zig tests");
test_step.dependOn(&run_tests.step);
// --- Benchmarks ---
const bench_mod = b.createModule(.{
.root_source_file = b.path("src/bench.zig"),
.target = target,
.optimize = .ReleaseFast,
.imports = &.{
.{ .name = "lite3", .module = zig_mod },
},
});
bench_mod.addIncludePath(shim_include_path);
bench_mod.linkLibrary(lite3_lib);
const bench_exe = b.addExecutable(.{
.name = "lite3-bench",
.root_module = bench_mod,
});
const run_bench = b.addRunArtifact(bench_exe);
const bench_step = b.step("bench", "Run lite3-zig benchmarks");
bench_step.dependOn(&run_bench.step);
// --- Examples ---
const example_files = [_]struct { name: []const u8, path: []const u8 }{
.{ .name = "basic", .path = "examples/basic.zig" },
.{ .name = "json_roundtrip", .path = "examples/json_roundtrip.zig" },
};
const examples_step = b.step("examples", "Build example programs");
for (example_files) |ex| {
const ex_mod = b.createModule(.{
.root_source_file = b.path(ex.path),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "lite3", .module = zig_mod },
},
});
ex_mod.addIncludePath(shim_include_path);
ex_mod.linkLibrary(lite3_lib);
const ex_exe = b.addExecutable(.{
.name = ex.name,
.root_module = ex_mod,
});
examples_step.dependOn(&b.addInstallArtifact(ex_exe, .{}).step);
}
}