From 89997b63f3329262f0362c21af385d1f291452ca Mon Sep 17 00:00:00 2001 From: Matheus Catarino Date: Sat, 31 Aug 2024 10:46:28 -0300 Subject: [PATCH] replace `@cImport` to `translate-c` build-system step --- build.zig | 35 +++++++++++++++++++++++++++++++++-- src/bindings.zig | 9 +++++---- src/main.zig | 3 ++- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/build.zig b/build.zig index 5a20925..395330c 100644 --- a/build.zig +++ b/build.zig @@ -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", diff --git a/src/bindings.zig b/src/bindings.zig index e08dc42..ec72416 100644 --- a/src/bindings.zig +++ b/src/bindings.zig @@ -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"); +}; diff --git a/src/main.zig b/src/main.zig index 88f20cb..73e3910 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,5 +1,6 @@ const wamr = @import("wamr"); test { - _ = wamr.c; + _ = wamr.c.wasm_export; + _ = wamr.c.wasm_c; }