Skip to content

Commit

Permalink
ci(bindings/zig): Fix build and test of zig on 0.13 (#5102)
Browse files Browse the repository at this point in the history
* ci(bindings/zig): Fix build and test of zig on 0.13

Signed-off-by: Xuanwo <github@xuanwo.io>

* Add docs

Signed-off-by: Xuanwo <github@xuanwo.io>

---------

Signed-off-by: Xuanwo <github@xuanwo.io>
  • Loading branch information
Xuanwo authored Sep 8, 2024
1 parent dd02e29 commit abec745
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 45 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci_bindings_zig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ on:
branches:
- main
tags:
- '*'
- "*"
pull_request:
branches:
- main
Expand All @@ -47,7 +47,7 @@ jobs:
- uses: actions/checkout@v4
- uses: korandoru/setup-zig@v1
with:
zig-version: 0.11.0
zig-version: 0.13.0

- name: Setup Rust toolchain
uses: ./.github/actions/setup
Expand Down
1 change: 1 addition & 0 deletions bindings/zig/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
zig-cache/
zig-out/
.zig-cache/
6 changes: 1 addition & 5 deletions bindings/zig/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@

To compile OpenDAL from source code, you need:

- [Zig](https://ziglang.org/download) 0.11.0 or higher

> **Note**:
>
> 0.11.0 is not released yet. You can use master instead before the official 0.11.0 released.
- [Zig](https://ziglang.org/download) 0.13.0 or higher

```bash
# build libopendal_c (underneath call make -C ../c)
Expand Down
49 changes: 17 additions & 32 deletions bindings/zig/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,59 +23,44 @@ pub fn build(b: *std.Build) void {

// 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.
_ = b.addModule("opendal", .{
.source_file = .{
.path = "src/opendal.zig",
},
.dependencies = &.{},
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"));

// Creates a step for building the dependent C bindings
const libopendal_c = buildLibOpenDAL(b);
const libopendal_c = b.addSystemCommand(&[_][]const u8{
"make",
"-C",
"../c",
"build",
});
const build_libopendal_c = b.step("libopendal_c", "Build OpenDAL C bindings");
build_libopendal_c.dependOn(&libopendal_c.step);

// 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 = .{
.path = "test/bdd.zig",
},
.root_source_file = b.path("test/bdd.zig"),
.target = target,
.optimize = optimize,
});
unit_tests.addIncludePath(.{ .path = "../c/include" });
unit_tests.addModule("opendal", module(b));

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

// Creates a step for running unit tests.
const run_unit_tests = b.addRunArtifact(unit_tests);
const test_step = b.step("test", "Run OpenDAL Zig bindings tests");
test_step.dependOn(&libopendal_c.step);
test_step.dependOn(&run_unit_tests.step);
}

fn buildLibOpenDAL(b: *std.Build) *std.Build.Step.Run {
const basedir = comptime std.fs.path.dirname(@src().file) orelse null;
const c_bindings_dir = basedir ++ "/../c";
return b.addSystemCommand(&[_][]const u8{
"make",
"-C",
c_bindings_dir,
"build",
});
}

pub fn module(b: *std.Build) *std.Build.Module {
return b.createModule(.{
.source_file = .{
.path = "src/opendal.zig",
},
});
}
9 changes: 9 additions & 0 deletions bindings/zig/build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.{
.name = "opendal",
.version = "0.0.1",
.paths = .{
"build.zig",
"build.zig.zon",
"src",
},
}
12 changes: 6 additions & 6 deletions bindings/zig/test/bdd.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ test "Opendal BDD test" {
self.path = "test";
self.content = "Hello, World!";

var options: [*c]opendal.c.opendal_operator_options = opendal.c.opendal_operator_options_new();
const options: [*c]opendal.c.opendal_operator_options = opendal.c.opendal_operator_options_new();
defer opendal.c.opendal_operator_options_free(options);
opendal.c.opendal_operator_options_set(options, "root", "/myroot");

// Given A new OpenDAL Blocking Operator
var result = opendal.c.opendal_operator_new(self.scheme, options);
const result = opendal.c.opendal_operator_new(self.scheme, options);
testing.expectEqual(result.@"error", null) catch unreachable;
self.p = result.op;

Expand All @@ -67,22 +67,22 @@ test "Opendal BDD test" {
try testing.expectEqual(result, null);

// The blocking file "test" should exist
var e: opendal.c.opendal_result_is_exist = opendal.c.opendal_operator_is_exist(testkit.p, testkit.path);
const e: opendal.c.opendal_result_is_exist = opendal.c.opendal_operator_is_exist(testkit.p, testkit.path);
try testing.expectEqual(e.@"error", null);
try testing.expect(e.is_exist);

// The blocking file "test" entry mode must be file
var s: opendal.c.opendal_result_stat = opendal.c.opendal_operator_stat(testkit.p, testkit.path);
const s: opendal.c.opendal_result_stat = opendal.c.opendal_operator_stat(testkit.p, testkit.path);
try testing.expectEqual(s.@"error", null);
var meta: [*c]opendal.c.opendal_metadata = s.meta;
const meta: [*c]opendal.c.opendal_metadata = s.meta;
try testing.expect(opendal.c.opendal_metadata_is_file(meta));

// The blocking file "test" content length must be 13
try testing.expectEqual(opendal.c.opendal_metadata_content_length(meta), 13);
defer opendal.c.opendal_metadata_free(meta);

// The blocking file "test" must have content "Hello, World!"
var r: opendal.c.opendal_result_read = opendal.c.opendal_operator_read(testkit.p, testkit.path);
const r: opendal.c.opendal_result_read = opendal.c.opendal_operator_read(testkit.p, testkit.path);
defer opendal.c.opendal_bytes_free(r.data);
try testing.expect(r.@"error" == null);
try testing.expectEqual(std.mem.len(testkit.content), r.data.*.len);
Expand Down

0 comments on commit abec745

Please sign in to comment.