diff options
| author | ruki <[email protected]> | 2026-03-28 00:48:45 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2026-03-28 00:48:45 +0800 |
| commit | bec4169441e5cd07fc14e886a1759b1f879ca06c (patch) | |
| tree | da914fffe17f12079e69dbb7e28a94e67c7ed56e | |
| parent | e7f5e0fc203ebb4adb26365a7e1e4fe9e82e1be7 (diff) | |
add c interop support for zigzig
| -rw-r--r-- | tests/projects/zig/c_interop/src/main.zig | 11 | ||||
| -rw-r--r-- | tests/projects/zig/c_interop/src/native/mathlib.c | 9 | ||||
| -rw-r--r-- | tests/projects/zig/c_interop/src/native/mathlib.h | 7 | ||||
| -rw-r--r-- | tests/projects/zig/c_interop/test.lua | 10 | ||||
| -rw-r--r-- | tests/projects/zig/c_interop/xmake.lua | 12 | ||||
| -rw-r--r-- | tests/projects/zig/c_interop_with_package/src/main.zig | 10 | ||||
| -rw-r--r-- | tests/projects/zig/c_interop_with_package/test.lua | 10 | ||||
| -rw-r--r-- | tests/projects/zig/c_interop_with_package/xmake.lua | 7 | ||||
| -rw-r--r-- | xmake/languages/zig/xmake.lua | 3 | ||||
| -rw-r--r-- | xmake/modules/core/tools/zig.lua | 10 |
10 files changed, 89 insertions, 0 deletions
diff --git a/tests/projects/zig/c_interop/src/main.zig b/tests/projects/zig/c_interop/src/main.zig new file mode 100644 index 000000000..6cd61100d --- /dev/null +++ b/tests/projects/zig/c_interop/src/main.zig @@ -0,0 +1,11 @@ +const std = @import("std"); +const c = @cImport({ + @cInclude("mathlib.h"); +}); + +pub fn main() void { + const sum = c.c_add(3, 4); + const product = c.c_multiply(5, 6); + std.debug.print("c_add(3,4)={d}\n", .{sum}); + std.debug.print("c_multiply(5,6)={d}\n", .{product}); +} diff --git a/tests/projects/zig/c_interop/src/native/mathlib.c b/tests/projects/zig/c_interop/src/native/mathlib.c new file mode 100644 index 000000000..8728d1df3 --- /dev/null +++ b/tests/projects/zig/c_interop/src/native/mathlib.c @@ -0,0 +1,9 @@ +#include "mathlib.h" + +int c_add(int a, int b) { + return a + b; +} + +int c_multiply(int a, int b) { + return a * b; +} diff --git a/tests/projects/zig/c_interop/src/native/mathlib.h b/tests/projects/zig/c_interop/src/native/mathlib.h new file mode 100644 index 000000000..78dcbc931 --- /dev/null +++ b/tests/projects/zig/c_interop/src/native/mathlib.h @@ -0,0 +1,7 @@ +#ifndef MATHLIB_H +#define MATHLIB_H + +int c_add(int a, int b); +int c_multiply(int a, int b); + +#endif diff --git a/tests/projects/zig/c_interop/test.lua b/tests/projects/zig/c_interop/test.lua new file mode 100644 index 000000000..62ed37e4d --- /dev/null +++ b/tests/projects/zig/c_interop/test.lua @@ -0,0 +1,10 @@ +import("lib.detect.find_tool") + +function test_build(t) + local zig = find_tool("zig") + if zig then + t:build() + else + return t:skip("zig not found") + end +end diff --git a/tests/projects/zig/c_interop/xmake.lua b/tests/projects/zig/c_interop/xmake.lua new file mode 100644 index 000000000..3c93834bb --- /dev/null +++ b/tests/projects/zig/c_interop/xmake.lua @@ -0,0 +1,12 @@ +add_rules("mode.debug", "mode.release") + +target("mathlib") + set_kind("static") + add_files("src/native/*.c") + add_includedirs("src/native", {public = true}) + +target("test") + set_kind("binary") + add_deps("mathlib") + add_files("src/*.zig") + add_includedirs("src/native") diff --git a/tests/projects/zig/c_interop_with_package/src/main.zig b/tests/projects/zig/c_interop_with_package/src/main.zig new file mode 100644 index 000000000..21dd9eb9a --- /dev/null +++ b/tests/projects/zig/c_interop_with_package/src/main.zig @@ -0,0 +1,10 @@ +const std = @import("std"); +const c = @cImport({ + @cInclude("zlib.h"); +}); + +pub fn main() void { + const version = c.zlibVersion(); + const ver_str: [*:0]const u8 = @ptrCast(version); + std.debug.print("zlib version: {s}\n", .{ver_str}); +} diff --git a/tests/projects/zig/c_interop_with_package/test.lua b/tests/projects/zig/c_interop_with_package/test.lua new file mode 100644 index 000000000..62ed37e4d --- /dev/null +++ b/tests/projects/zig/c_interop_with_package/test.lua @@ -0,0 +1,10 @@ +import("lib.detect.find_tool") + +function test_build(t) + local zig = find_tool("zig") + if zig then + t:build() + else + return t:skip("zig not found") + end +end diff --git a/tests/projects/zig/c_interop_with_package/xmake.lua b/tests/projects/zig/c_interop_with_package/xmake.lua new file mode 100644 index 000000000..56b95995d --- /dev/null +++ b/tests/projects/zig/c_interop_with_package/xmake.lua @@ -0,0 +1,7 @@ +add_rules("mode.debug", "mode.release") +add_requires("zlib", {system = false}) + +target("test") + set_kind("binary") + add_files("src/*.zig") + add_packages("zlib") diff --git a/xmake/languages/zig/xmake.lua b/xmake/languages/zig/xmake.lua index 32f522d82..ed0a64a50 100644 --- a/xmake/languages/zig/xmake.lua +++ b/xmake/languages/zig/xmake.lua @@ -36,6 +36,9 @@ language("zig") , "target.warnings" , "target.optimize:check" , "target.vectorexts:check" + , "target.includedirs" + , "target.sysincludedirs" + , "target.defines" } , binary = { "config.linkdirs" diff --git a/xmake/modules/core/tools/zig.lua b/xmake/modules/core/tools/zig.lua index b45ad3177..a279d5a5a 100644 --- a/xmake/modules/core/tools/zig.lua +++ b/xmake/modules/core/tools/zig.lua @@ -48,6 +48,16 @@ function nf_define(self, macro) return {"-D" .. macro} end +-- make the includedir flag (for @cImport) +function nf_includedir(self, dir) + return {"-I", dir} +end + +-- make the sysincludedir flag (for @cImport) +function nf_sysincludedir(self, dir) + return nf_includedir(self, dir) +end + -- make the optimize flag function nf_optimize(self, level) local maps = |
