Skip to content

Commit

Permalink
Zig bindings improvements
Browse files Browse the repository at this point in the history
* 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  <matheus-catarino@hotmail.com>
  • Loading branch information
kassane committed Oct 26, 2024
1 parent 08d3bf4 commit b2c839c
Show file tree
Hide file tree
Showing 3 changed files with 408 additions and 14 deletions.
57 changes: 47 additions & 10 deletions bindings/zig/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,30 @@ pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const use_llvm = b.option(bool, "use-llvm", "Use LLVM backend (default: true)") orelse true;
const use_clang = b.option(bool, "use-clang", "Use libclang in translate-c (default: true)") orelse true;

// Generate the Zig bindings for OpenDAL C bindings
const opendal_binding = b.addTranslateC(.{
.optimize = optimize,
.target = target,
.link_libc = true,
.root_source_file = b.path("../c/include/opendal.h"),
.use_clang = use_clang, // TODO: set 'false' use fno-llvm/fno-clang (may be zig v1.0)
});

// ZigCoro - (stackful) Coroutine for Zig (library)
const zigcoro = b.dependency("zigcoro", .{}).module("libcoro");

// This function creates a module and adds it to the package's module set, making
// it available to other packages which depend on this one.
const opendal_module = b.addModule("opendal", .{
.root_source_file = b.path("src/opendal.zig"),
.target = target,
.optimize = optimize,
});
opendal_module.addIncludePath(b.path("../c/include"));
opendal_module.addImport("opendal_c_header", opendal_binding.addModule("opendal_c_header"));
opendal_module.addImport("libcoro", zigcoro);

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

// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const unit_tests = b.addTest(.{
const lib_test = b.addTest(.{
.root_source_file = b.path("src/opendal.zig"),
.target = target,
.optimize = optimize,
.use_llvm = use_llvm,
.test_runner = b.dependency("test_runner", .{}).path("test_runner.zig"),
});
if (optimize == .Debug) {
lib_test.addLibraryPath(b.path("../c/target/debug"));
} else {
lib_test.addLibraryPath(b.path("../c/target/release"));
}
lib_test.linkSystemLibrary("opendal_c");
lib_test.linkLibCpp();
lib_test.root_module.addImport("opendal_c_header", opendal_binding.addModule("opendal_c_header"));
lib_test.root_module.addImport("libcoro", zigcoro);

const bdd_test = b.addTest(.{
.name = "bdd_test",
.root_source_file = b.path("test/bdd.zig"),
.target = target,
.optimize = optimize,
.use_llvm = use_llvm,
.test_runner = b.dependency("test_runner", .{}).path("test_runner.zig"),
});

unit_tests.addIncludePath(b.path("../c/include"));
if (optimize == .Debug) {
unit_tests.addLibraryPath(b.path("../c/target/debug"));
bdd_test.addLibraryPath(b.path("../c/target/debug"));
} else {
unit_tests.addLibraryPath(b.path("../c/target/release"));
bdd_test.addLibraryPath(b.path("../c/target/release"));
}
unit_tests.linkSystemLibrary("opendal_c");
unit_tests.linkLibCpp();
unit_tests.root_module.addImport("opendal", opendal_module);
bdd_test.linkSystemLibrary("opendal_c");
bdd_test.linkLibCpp();
bdd_test.root_module.addImport("opendal", opendal_module);

// Creates a step for running unit tests.
const run_unit_tests = b.addRunArtifact(unit_tests);
const run_lib_test = b.addRunArtifact(lib_test);
const run_bdd_test = b.addRunArtifact(bdd_test);
const test_step = b.step("test", "Run OpenDAL Zig bindings tests");
test_step.dependOn(&libopendal_c.step);
test_step.dependOn(&run_unit_tests.step);
test_step.dependOn(&run_lib_test.step);
test_step.dependOn(&run_bdd_test.step);
}
10 changes: 10 additions & 0 deletions bindings/zig/build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
.{
.name = "opendal",
.version = "0.0.1",
.dependencies = .{
.zigcoro = .{
.url = "git+https://github.com/rsepassi/zigcoro#ca58a912c3c0957d6aab0405f4c78f68e1dababb",
.hash = "12204959321c5e16a70944b8cdf423f0fa2bdf7db1f72bacc89b8af85acc4c054d9c",
},
.test_runner = .{
.url = "git+https://gist.github.com/karlseguin/c6bea5b35e4e8d26af6f81c22cb5d76b/#f303e77231e14b405e4e800072e7deacac4a4881",
.hash = "12208ab39744c8c0909d53b8c9889aff0690930d20e98e4f650f9690310e9a860b14",
},
},
.paths = .{
"build.zig",
"build.zig.zon",
Expand Down
Loading

0 comments on commit b2c839c

Please sign in to comment.