summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2026-03-20 11:15:57 +0800
committerGitHub <[email protected]>2026-03-20 11:15:57 +0800
commit5982d5110c74890122372c0ab6040ef0d0c9d9b5 (patch)
tree55b3a613ffd57c6d752b723cc2a1aed7259a9814
parentcdd83334681855ed171d5df058a1e18011e173d6 (diff)
parent144688ea6acdae672a731532861886c777057e1b (diff)
Merge pull request #7410 from xmake-io/csharp
csharp call c++ library
-rw-r--r--.github/workflows/macos.yml1
-rw-r--r--tests/projects/csharp/pinvoke/src/app/Program.cs16
-rw-r--r--tests/projects/csharp/pinvoke/src/native/mathlib.c13
-rw-r--r--tests/projects/csharp/pinvoke/test.lua13
-rw-r--r--tests/projects/csharp/pinvoke/xmake.lua10
-rw-r--r--xmake/rules/csharp/config.lua13
-rw-r--r--xmake/rules/csharp/generator/properties.lua2
7 files changed, 68 insertions, 0 deletions
diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml
index 57feb362f..0d4b9a96c 100644
--- a/.github/workflows/macos.yml
+++ b/.github/workflows/macos.yml
@@ -25,6 +25,7 @@ jobs:
# WyriHaximus/github-action-get-previous-tag@master need it
fetch-depth: 0
submodules: true
+
- name: Prepare local xmake
run: cp -rf . ../xmake-source
- uses: xmake-io/github-action-setup-xmake@v1
diff --git a/tests/projects/csharp/pinvoke/src/app/Program.cs b/tests/projects/csharp/pinvoke/src/app/Program.cs
new file mode 100644
index 000000000..b5c6c9625
--- /dev/null
+++ b/tests/projects/csharp/pinvoke/src/app/Program.cs
@@ -0,0 +1,16 @@
+using System.Runtime.InteropServices;
+
+int sum = NativeMethods.add(3, 4);
+int product = NativeMethods.multiply(5, 6);
+Console.WriteLine($"add(3,4)={sum}");
+Console.WriteLine($"multiply(5,6)={product}");
+
+internal static class NativeMethods {
+ private const string NativeLib = "mathlib";
+
+ [DllImport(NativeLib)]
+ internal static extern int add(int a, int b);
+
+ [DllImport(NativeLib)]
+ internal static extern int multiply(int a, int b);
+}
diff --git a/tests/projects/csharp/pinvoke/src/native/mathlib.c b/tests/projects/csharp/pinvoke/src/native/mathlib.c
new file mode 100644
index 000000000..3d2840073
--- /dev/null
+++ b/tests/projects/csharp/pinvoke/src/native/mathlib.c
@@ -0,0 +1,13 @@
+#ifdef _WIN32
+#define EXPORT __declspec(dllexport)
+#else
+#define EXPORT __attribute__((visibility("default")))
+#endif
+
+EXPORT int add(int a, int b) {
+ return a + b;
+}
+
+EXPORT int multiply(int a, int b) {
+ return a * b;
+}
diff --git a/tests/projects/csharp/pinvoke/test.lua b/tests/projects/csharp/pinvoke/test.lua
new file mode 100644
index 000000000..d909316b7
--- /dev/null
+++ b/tests/projects/csharp/pinvoke/test.lua
@@ -0,0 +1,13 @@
+import("detect.sdks.find_dotnet")
+
+function test_build(t)
+ if is_subhost("msys") then
+ return t:skip("csharp not supported on msys")
+ end
+ local dotnet = find_dotnet()
+ if dotnet and dotnet.sdkver then
+ t:build()
+ else
+ return t:skip("dotnet sdk not found")
+ end
+end
diff --git a/tests/projects/csharp/pinvoke/xmake.lua b/tests/projects/csharp/pinvoke/xmake.lua
new file mode 100644
index 000000000..db0dc7ebd
--- /dev/null
+++ b/tests/projects/csharp/pinvoke/xmake.lua
@@ -0,0 +1,10 @@
+add_rules("mode.debug", "mode.release")
+
+target("mathlib")
+ set_kind("shared")
+ add_files("src/native/*.c")
+
+target("app")
+ set_kind("binary")
+ add_deps("mathlib")
+ add_files("src/app/*.cs")
diff --git a/xmake/rules/csharp/config.lua b/xmake/rules/csharp/config.lua
index 3f6b71844..e9dbc71e0 100644
--- a/xmake/rules/csharp/config.lua
+++ b/xmake/rules/csharp/config.lua
@@ -44,4 +44,17 @@ function main(target)
end, {dependfile = dependfile, files = sourcefiles, values = depcsproj})
target:data_set("csharp.csproj", csprojfile)
+
+ -- add native shared library search paths for P/Invoke
+ for _, dep in ipairs(target:orderdeps()) do
+ if dep:is_shared() and not dep:rule("csharp.build") then
+ if is_host("windows") then
+ target:add("runenvs", "PATH", dep:targetdir())
+ elseif is_host("macosx") then
+ target:add("runenvs", "DYLD_LIBRARY_PATH", dep:targetdir())
+ else
+ target:add("runenvs", "LD_LIBRARY_PATH", dep:targetdir())
+ end
+ end
+ end
end
diff --git a/xmake/rules/csharp/generator/properties.lua b/xmake/rules/csharp/generator/properties.lua
index 15702eae4..a931b1db3 100644
--- a/xmake/rules/csharp/generator/properties.lua
+++ b/xmake/rules/csharp/generator/properties.lua
@@ -67,6 +67,8 @@ function _resolve_assembly_name(context)
end
+-- resolve runtime identifier from user config or auto-detect from target plat/arch
+-- e.g. "osx-x64", "osx-arm64", "linux-x64", "win-x64"
-- register a single-value csharp.* property entry
function _register_property(register, suffix, xml, default, extra)
local entry = table.join({