Skip to content

Commit 2d88433

Browse files
committed
Zig bindings improvements
* Operator wrapper added * more unittests * wip: add async (library) support * replace `@cImport/@cInclude` to `translate-c` only * add custom testrunner output Signed-off-by: Matheus C. França <[email protected]>
1 parent 3fe8caa commit 2d88433

File tree

3 files changed

+422
-14
lines changed

3 files changed

+422
-14
lines changed

bindings/zig/build.zig

+47-10
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,30 @@ pub fn build(b: *std.Build) void {
2121
const target = b.standardTargetOptions(.{});
2222
const optimize = b.standardOptimizeOption(.{});
2323

24+
const use_llvm = b.option(bool, "use-llvm", "Use LLVM backend (default: true)") orelse true;
25+
const use_clang = b.option(bool, "use-clang", "Use libclang in translate-c (default: true)") orelse true;
26+
27+
// Generate the Zig bindings for OpenDAL C bindings
28+
const opendal_binding = b.addTranslateC(.{
29+
.optimize = optimize,
30+
.target = target,
31+
.link_libc = true,
32+
.root_source_file = b.path("../c/include/opendal.h"),
33+
.use_clang = use_clang, // TODO: set 'false' use fno-llvm/fno-clang (may be zig v1.0)
34+
});
35+
36+
// ZigCoro - (stackful) Coroutine for Zig (library)
37+
const zigcoro = b.dependency("zigcoro", .{}).module("libcoro");
38+
2439
// This function creates a module and adds it to the package's module set, making
2540
// it available to other packages which depend on this one.
2641
const opendal_module = b.addModule("opendal", .{
2742
.root_source_file = b.path("src/opendal.zig"),
2843
.target = target,
2944
.optimize = optimize,
3045
});
31-
opendal_module.addIncludePath(b.path("../c/include"));
46+
opendal_module.addImport("opendal_c_header", opendal_binding.addModule("opendal_c_header"));
47+
opendal_module.addImport("libcoro", zigcoro);
3248

3349
// Creates a step for building the dependent C bindings
3450
const libopendal_c_cmake = b.addSystemCommand(&[_][]const u8{ "cmake", "-S", "../c", "-B", "../c/build" });
@@ -41,25 +57,46 @@ pub fn build(b: *std.Build) void {
4157

4258
// Creates a step for unit testing. This only builds the test executable
4359
// but does not run it.
44-
const unit_tests = b.addTest(.{
60+
const lib_test = b.addTest(.{
61+
.root_source_file = b.path("src/opendal.zig"),
62+
.target = target,
63+
.optimize = optimize,
64+
.use_llvm = use_llvm,
65+
.test_runner = b.dependency("test_runner", .{}).path("test_runner.zig"),
66+
});
67+
if (optimize == .Debug) {
68+
lib_test.addLibraryPath(b.path("../c/target/debug"));
69+
} else {
70+
lib_test.addLibraryPath(b.path("../c/target/release"));
71+
}
72+
lib_test.linkSystemLibrary("opendal_c");
73+
lib_test.linkLibCpp();
74+
lib_test.root_module.addImport("opendal_c_header", opendal_binding.addModule("opendal_c_header"));
75+
lib_test.root_module.addImport("libcoro", zigcoro);
76+
77+
const bdd_test = b.addTest(.{
78+
.name = "bdd_test",
4579
.root_source_file = b.path("test/bdd.zig"),
4680
.target = target,
4781
.optimize = optimize,
82+
.use_llvm = use_llvm,
83+
.test_runner = b.dependency("test_runner", .{}).path("test_runner.zig"),
4884
});
4985

50-
unit_tests.addIncludePath(b.path("../c/include"));
5186
if (optimize == .Debug) {
52-
unit_tests.addLibraryPath(b.path("../c/target/debug"));
87+
bdd_test.addLibraryPath(b.path("../c/target/debug"));
5388
} else {
54-
unit_tests.addLibraryPath(b.path("../c/target/release"));
89+
bdd_test.addLibraryPath(b.path("../c/target/release"));
5590
}
56-
unit_tests.linkSystemLibrary("opendal_c");
57-
unit_tests.linkLibCpp();
58-
unit_tests.root_module.addImport("opendal", opendal_module);
91+
bdd_test.linkSystemLibrary("opendal_c");
92+
bdd_test.linkLibCpp();
93+
bdd_test.root_module.addImport("opendal", opendal_module);
5994

6095
// Creates a step for running unit tests.
61-
const run_unit_tests = b.addRunArtifact(unit_tests);
96+
const run_lib_test = b.addRunArtifact(lib_test);
97+
const run_bdd_test = b.addRunArtifact(bdd_test);
6298
const test_step = b.step("test", "Run OpenDAL Zig bindings tests");
6399
test_step.dependOn(&libopendal_c.step);
64-
test_step.dependOn(&run_unit_tests.step);
100+
test_step.dependOn(&run_lib_test.step);
101+
test_step.dependOn(&run_bdd_test.step);
65102
}

bindings/zig/build.zig.zon

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
.{
22
.name = "opendal",
33
.version = "0.0.1",
4+
.dependencies = .{
5+
.zigcoro = .{
6+
.url = "git+https://github.com/rsepassi/zigcoro#ca58a912c3c0957d6aab0405f4c78f68e1dababb",
7+
.hash = "12204959321c5e16a70944b8cdf423f0fa2bdf7db1f72bacc89b8af85acc4c054d9c",
8+
},
9+
.test_runner = .{
10+
.url = "git+https://gist.github.com/karlseguin/c6bea5b35e4e8d26af6f81c22cb5d76b/#f303e77231e14b405e4e800072e7deacac4a4881",
11+
.hash = "12208ab39744c8c0909d53b8c9889aff0690930d20e98e4f650f9690310e9a860b14",
12+
},
13+
},
414
.paths = .{
515
"build.zig",
616
"build.zig.zon",

0 commit comments

Comments
 (0)