Skip to content

Commit

Permalink
refactor (bindings/zig): Improvements (#5247)
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane authored Nov 6, 2024
1 parent f547276 commit a594365
Show file tree
Hide file tree
Showing 3 changed files with 430 additions and 18 deletions.
69 changes: 55 additions & 14 deletions bindings/zig/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,38 @@ 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,
.link_libcpp = true,
});
opendal_module.addImport("opendal_c_header", opendal_binding.addModule("opendal_c_header"));
opendal_module.addImport("libcoro", zigcoro);
opendal_module.addLibraryPath(switch (optimize) {
.Debug => b.path("../c/target/debug"),
else => b.path("../c/target/release"),
});
opendal_module.addIncludePath(b.path("../c/include"));
opendal_module.linkSystemLibrary("opendal_c", .{});

// =============== OpenDAL C bindings ===============

// 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 @@ -39,27 +63,44 @@ pub fn build(b: *std.Build) void {
libopendal_c.step.dependOn(config_libopendal_c);
build_libopendal_c.dependOn(&libopendal_c.step);

// =============== OpenDAL C bindings ===============

// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const unit_tests = b.addTest(.{
.root_source_file = b.path("test/bdd.zig"),

// Test library
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"),
});
lib_test.addLibraryPath(switch (optimize) {
.Debug => b.path("../c/target/debug"),
else => b.path("../c/target/release"),
});
lib_test.linkLibCpp();
lib_test.linkSystemLibrary("opendal_c");
lib_test.root_module.addImport("opendal_c_header", opendal_binding.addModule("opendal_c_header"));
lib_test.root_module.addImport("libcoro", zigcoro);

unit_tests.addIncludePath(b.path("../c/include"));
if (optimize == .Debug) {
unit_tests.addLibraryPath(b.path("../c/target/debug"));
} else {
unit_tests.addLibraryPath(b.path("../c/target/release"));
}
unit_tests.linkSystemLibrary("opendal_c");
unit_tests.linkLibCpp();
unit_tests.root_module.addImport("opendal", opendal_module);
// BDD sample test
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"),
});
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 a594365

Please sign in to comment.