Skip to content

Commit

Permalink
replace @cImport to translate-c build-system step
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Aug 31, 2024
1 parent 7d4b42e commit 89997b6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
35 changes: 33 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,50 @@ pub fn build(b: *std.Build) void {

const wamrPath = b.dependency("WAMR", .{}).path("core/iwasm/include");

// TODO: https://github.com/ziglang/zig/issues/20630
const wasmC_bindgen = b.addTranslateC(.{
.link_libc = true,
.optimize = optimize,
.target = target,
.root_source_file = .{
// get absolute path of wamr/wasm_c_api.h
.cwd_relative = b.pathJoin(&.{
wamrPath.getPath(b),
"wasm_c_api.h",
}),
},
.use_clang = true, // TODO: set 'false' use fno-llvm/fno-clang
});

const wasmExport_bindgen = b.addTranslateC(.{
.link_libc = true,
.optimize = optimize,
.target = target,
.root_source_file = .{
// get absolute path of wamr/wasm_export.h
.cwd_relative = b.pathJoin(&.{
wamrPath.getPath(b),
"wasm_export.h",
}),
},
.use_clang = true, // TODO: set 'false' use fno-llvm/fno-clang
});

const wamr_module = b.addModule("wamr", .{
.root_source_file = b.path("src/bindings.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
wamr_module.addIncludePath(wamrPath);
wamr_module.addImport("wasm_export", wasmExport_bindgen.addModule("wasm_export"));
wamr_module.addImport("wasm_c_api", wasmC_bindgen.addModule("wasm_c_api"));
for (llvm_libs) |name| {
wamr_module.linkSystemLibrary(name, .{});
}
wamr_module.link_libc = true;

buildTest(b, wamr_module);
}

fn buildTest(b: *std.Build, module: *std.Build.Module) void {
const lib_unit_tests = b.addTest(.{
.name = "wamr-test",
Expand Down
9 changes: 5 additions & 4 deletions src/bindings.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub const c = @cImport({
@cInclude("wasm_export.h");
@cInclude("wasm_c_api.h");
});
// 'c' namespace module
pub const c = struct {
pub const wasm_export = @import("wasm_export");
pub const wasm_c = @import("wasm_c_api");
};
3 changes: 2 additions & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const wamr = @import("wamr");

test {
_ = wamr.c;
_ = wamr.c.wasm_export;
_ = wamr.c.wasm_c;
}

0 comments on commit 89997b6

Please sign in to comment.