-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
277 lines (236 loc) · 10.9 KB
/
build.zig
File metadata and controls
277 lines (236 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
const std = @import("std");
const build_zon = @import("build.zig.zon");
pub fn build(b: *std.Build) void {
// Add standard options for target and optimization mode.
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{
.preferred_optimize_mode = .ReleaseFast,
});
// Version from build.zig.zon (single source of truth)
const zon_version = build_zon.version;
const extension_version_default = b.fmt("v{s}", .{zon_version});
// Build options for DuckDB Extension configuration
const extension_name = b.option([]const u8, "extension-name", "Extension name (default: vizier)") orelse "vizier";
const extension_api_version = b.option([]const u8, "api-version", "DuckDB Extension API version (default: v1.2.0)") orelse "v1.2.0";
const extension_version = b.option([]const u8, "extension-version", "Extension version") orelse extension_version_default;
const platform = b.option([]const u8, "platform", "Target platform (e.g., linux_amd64, linux_arm64)") orelse detectPlatform(target);
// Resolve the extension-template-c dependency from build.zig.zon
const ext_template_dep = b.dependency("extension-template-c", .{});
const ext_template_capi_path = ext_template_dep.path("duckdb_capi");
const duckdb_module = b.addModule("duckdb", .{
.root_source_file = b.path("src/duckdb.zig"),
});
// Build options passed to library code
const build_options = b.addOptions();
build_options.addOption([]const u8, "version", zon_version);
const duckdb_path = b.option([]const u8, "duckdb-path", "Path to the DuckDB CLI executable") orelse
(b.findProgram(&.{"duckdb"}, &.{}) catch "");
build_options.addOption([]const u8, "duckdb_path", duckdb_path);
const root_module = b.createModule(.{
.root_source_file = b.path("src/lib.zig"),
.target = target,
.optimize = optimize,
});
root_module.addImport("duckdb", duckdb_module);
root_module.addImport("build_options", build_options.createModule());
const lib = b.addLibrary(.{
.name = extension_name,
.root_module = root_module,
.linkage = .dynamic,
});
const extension_filename = b.fmt("{s}.duckdb_extension", .{extension_name});
lib.install_name = extension_filename;
// Add the C source file that handles DuckDB API integration
lib.root_module.addCSourceFile(.{
.file = b.path("src/extension.c"),
.flags = &.{"-std=c11"},
});
// Add include path for DuckDB headers (from build.zig.zon dependency)
lib.root_module.addIncludePath(ext_template_capi_path);
// Link libc (required for C code)
lib.root_module.linkSystemLibrary("c", .{});
// Add C macro for extension name
lib.root_module.addCMacro("DUCKDB_EXTENSION_NAME", extension_name);
lib.root_module.addCMacro("DUCKDB_BUILD_LOADABLE_EXTENSION", "1");
// Allow undefined symbols - they will be provided by DuckDB at runtime
lib.linker_allow_shlib_undefined = true;
// Install the library artifact
const lib_install = b.addInstallArtifact(lib, .{});
b.getInstallStep().dependOn(&lib_install.step);
// Test configuration - use a separate test file that doesn't require DuckDB runtime
const test_module = b.createModule(.{
.root_source_file = b.path("src/lib_test.zig"),
.target = target,
.optimize = optimize,
});
test_module.addImport("duckdb", duckdb_module);
const lib_unit_tests = b.addTest(.{
.root_module = test_module,
});
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
// ---- test-unit: inline unit tests and regression tests ----
const test_unit_step = b.step("test-unit", "Run unit and regression tests");
test_unit_step.dependOn(&run_lib_unit_tests.step);
// ---- test-property: property-based tests using Minish ----
const minish_dep = b.dependency("minish", .{});
const minish_module = minish_dep.module("minish");
const extract_module = b.addModule("vizier/extract", .{
.root_source_file = b.path("src/vizier/extract.zig"),
});
const capture_module = b.addModule("vizier/capture", .{
.root_source_file = b.path("src/vizier/capture.zig"),
});
const inspect_module = b.addModule("vizier/inspect", .{
.root_source_file = b.path("src/vizier/inspect.zig"),
});
const prop_test_module = b.createModule(.{
.root_source_file = b.path("tests/property_tests.zig"),
.target = target,
.optimize = optimize,
});
prop_test_module.addImport("duckdb", duckdb_module);
prop_test_module.addImport("minish", minish_module);
prop_test_module.addImport("vizier/extract", extract_module);
prop_test_module.addImport("vizier/capture", capture_module);
prop_test_module.addImport("vizier/inspect", inspect_module);
const prop_tests = b.addTest(.{
.root_module = prop_test_module,
});
const run_prop_tests = b.addRunArtifact(prop_tests);
const test_property_step = b.step("test-property", "Run property-based tests");
test_property_step.dependOn(&run_prop_tests.step);
// Clean step - removes build artifacts and cache
const clean_step = b.step("clean", "Remove build artifacts and cache");
const clean_cmd = b.addSystemCommand(&[_][]const u8{
"rm",
"-rf",
"zig-out",
".zig-cache",
});
clean_step.dependOn(&clean_cmd.step);
// Detect the library file extension based on target OS
const lib_filename = getLibFilename(b, target, extension_name);
// Windows DLLs go to bin/, other platforms go to lib/
const os_tag = target.result.os.tag;
const lib_path = if (os_tag == .windows)
b.getInstallPath(.bin, lib_filename)
else
b.getInstallPath(.lib, lib_filename);
// Add metadata step - adds DuckDB extension metadata for proper loading (name, version, platform, API version)
// Note: we will be using DuckDB extension API version (v1.2.0) so the extension will be compatible with DuckDB versions >= 1.2.0
const add_metadata_step = b.step("add-metadata", "Add DuckDB extension metadata");
const metadata_cmd = b.addSystemCommand(&[_][]const u8{
"python3",
"scripts/append_extension_metadata.py",
"-l",
lib_path,
"-n",
extension_name,
"-o",
b.getInstallPath(.lib, extension_filename),
"-dv",
extension_api_version,
"-ev",
extension_version,
"-p",
platform,
});
metadata_cmd.step.dependOn(b.getInstallStep());
add_metadata_step.dependOn(&metadata_cmd.step);
// ---- test-integration: load extension in DuckDB and validate outputs ----
const integration_test_module = b.createModule(.{
.root_source_file = b.path("tests/integration_tests.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
integration_test_module.addImport("build_options", build_options.createModule());
const integration_tests = b.addTest(.{
.root_module = integration_test_module,
});
const run_integration_tests = b.addRunArtifact(integration_tests);
run_integration_tests.step.dependOn(&metadata_cmd.step);
const test_integration_step = b.step("test-integration", "Run integration tests (requires DuckDB)");
test_integration_step.dependOn(&run_integration_tests.step);
// ---- test: run all tests (unit + property + integration) ----
const test_step = b.step("test", "Run all tests (unit, property, integration)");
test_step.dependOn(test_unit_step);
test_step.dependOn(test_property_step);
test_step.dependOn(test_integration_step);
// Interactive DuckDB session with extension loaded
const duckdb_step = b.step("duckdb", "Start interactive DuckDB session with extension loaded");
const load_cmd = b.fmt("LOAD 'zig-out/lib/{s}'", .{extension_filename});
const run_duckdb = b.addSystemCommand(&[_][]const u8{
"duckdb",
"-unsigned",
"-cmd",
load_cmd,
"-cmd",
"SELECT 'Extension loaded successfully!' as status",
});
run_duckdb.step.dependOn(&metadata_cmd.step);
duckdb_step.dependOn(&run_duckdb.step);
// Generate DuckDB Zig bindings from C API
const gen_bindings_step = b.step("duckdb-translate", "Generate Zig bindings from DuckDB C API");
const capi_dir = ext_template_capi_path.getPath(b);
const ext_header = ext_template_dep.path("duckdb_capi/duckdb_extension.h").getPath(b);
// Pipe through sed to strip machine-specific source-location comments
// (lines like `// /abs/path/foo.h:123:4`) that translate-c emits, so
// src/duckdb.zig does not churn per user on regeneration.
const translate_shell = b.fmt("zig translate-c -I {s} {s} | sed '/^\\/\\/ \\/.*\\.h:[0-9][0-9]*:[0-9][0-9]*$/d' > src/duckdb.zig", .{ capi_dir, ext_header });
const translate_cmd = b.addSystemCommand(&[_][]const u8{
"sh",
"-c",
translate_shell,
});
gen_bindings_step.dependOn(&translate_cmd.step);
// Build all: build + add metadata
const build_all_step = b.step("build-all", "Build extension and add metadata");
build_all_step.dependOn(b.getInstallStep());
build_all_step.dependOn(add_metadata_step);
// The documentation step remains unchanged.
const docs_step = b.step("docs", "Generate API documentation");
const doc_install_path = "docs/api";
const gen_docs_cmd = b.addSystemCommand(&[_][]const u8{
b.graph.zig_exe,
"build-lib",
"src/lib.zig",
"-femit-docs=" ++ doc_install_path,
});
const mkdir_cmd = b.addSystemCommand(&[_][]const u8{
"mkdir", "-p", doc_install_path,
});
gen_docs_cmd.step.dependOn(&mkdir_cmd.step);
docs_step.dependOn(&gen_docs_cmd.step);
}
fn detectPlatform(target: std.Build.ResolvedTarget) []const u8 {
const os_tag = target.result.os.tag;
const cpu_arch = target.result.cpu.arch;
if (cpu_arch == .x86_64) {
if (os_tag == .linux) return "linux_amd64";
if (os_tag == .macos) return "osx_amd64";
if (os_tag == .windows) return "windows_amd64";
} else if (cpu_arch == .aarch64) {
if (os_tag == .linux) return "linux_arm64";
if (os_tag == .macos) return "osx_arm64";
}
return "unknown";
}
fn getLibExtension(target: std.Build.ResolvedTarget) []const u8 {
const os_tag = target.result.os.tag;
return switch (os_tag) {
.windows => ".dll",
.macos => ".dylib",
else => ".so",
};
}
fn getLibFilename(b: *std.Build, target: std.Build.ResolvedTarget, extension_name: []const u8) []const u8 {
const lib_extension = getLibExtension(target);
const os_tag = target.result.os.tag;
// Note: Windows DLLs don't use "lib" prefix, but other platforms usually do
if (os_tag == .windows) {
return b.fmt("{s}{s}", .{ extension_name, lib_extension });
} else {
return b.fmt("lib{s}{s}", .{ extension_name, lib_extension });
}
}