summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorruki <[email protected]>2026-03-28 00:48:45 +0800
committerruki <[email protected]>2026-03-28 00:48:45 +0800
commitbec4169441e5cd07fc14e886a1759b1f879ca06c (patch)
treeda914fffe17f12079e69dbb7e28a94e67c7ed56e /tests
parente7f5e0fc203ebb4adb26365a7e1e4fe9e82e1be7 (diff)
add c interop support for zigzig
Diffstat (limited to 'tests')
-rw-r--r--tests/projects/zig/c_interop/src/main.zig11
-rw-r--r--tests/projects/zig/c_interop/src/native/mathlib.c9
-rw-r--r--tests/projects/zig/c_interop/src/native/mathlib.h7
-rw-r--r--tests/projects/zig/c_interop/test.lua10
-rw-r--r--tests/projects/zig/c_interop/xmake.lua12
-rw-r--r--tests/projects/zig/c_interop_with_package/src/main.zig10
-rw-r--r--tests/projects/zig/c_interop_with_package/test.lua10
-rw-r--r--tests/projects/zig/c_interop_with_package/xmake.lua7
8 files changed, 76 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")