Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/projects/csharp/pinvoke/src/app/Program.cs
Original file line number Diff line number Diff line change
@@ -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);
}
13 changes: 13 additions & 0 deletions tests/projects/csharp/pinvoke/src/native/mathlib.c
Original file line number Diff line number Diff line change
@@ -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;
}
13 changes: 13 additions & 0 deletions tests/projects/csharp/pinvoke/test.lua
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions tests/projects/csharp/pinvoke/xmake.lua
Original file line number Diff line number Diff line change
@@ -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")
13 changes: 13 additions & 0 deletions xmake/rules/csharp/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions xmake/rules/csharp/generator/properties.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Loading