Skip to content

Commit

Permalink
Link against libc for desktop linux
Browse files Browse the repository at this point in the history
  • Loading branch information
hitsounds committed Mar 26, 2023
1 parent 95ee42e commit 0c62305
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/vk/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ public static NativeLibrary Load(string libraryName)
{
return new WindowsNativeLibrary(libraryName);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
else if ((RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
&& RuntimeInformation.OSDescription.Contains("Unix"))
|| RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
#if NET5_0
|| OperatingSystem.IsAndroid()
Expand All @@ -113,6 +114,10 @@ public static NativeLibrary Load(string libraryName)
{
return new UnixNativeLibrary(libraryName);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return new LinuxNativeLibrary(libraryName);
}
else
{
throw new PlatformNotSupportedException("Cannot load native libraries on this platform: " + RuntimeInformation.OSDescription);
Expand Down Expand Up @@ -151,7 +156,7 @@ public UnixNativeLibrary(string libraryName) : base(libraryName)
protected override IntPtr LoadLibrary(string libraryName)
{
Libdl.dlerror();
IntPtr handle = Libdl.dlopen(libraryName, Libdl.RTLD_NOW);
IntPtr handle = Libc.dlopen(libraryName, Libdl.RTLD_NOW);
if (handle == IntPtr.Zero && !Path.IsPathRooted(libraryName))
{
string baseDir = AppContext.BaseDirectory;
Expand All @@ -175,5 +180,39 @@ protected override IntPtr LoadFunction(string functionName)
return Libdl.dlsym(NativeHandle, functionName);
}
}

private class LinuxNativeLibrary : NativeLibrary
{
public LinuxNativeLibrary(string libraryName) : base(libraryName)
{
}

protected override IntPtr LoadLibrary(string libraryName)
{
Libc.dlerror();
IntPtr handle = Libc.dlopen(libraryName, Libc.RTLD_NOW);
if (handle == IntPtr.Zero && !Path.IsPathRooted(libraryName))
{
string baseDir = AppContext.BaseDirectory;
if (!string.IsNullOrWhiteSpace(baseDir))
{
string localPath = Path.Combine(baseDir, libraryName);
handle = Libc.dlopen(localPath, Libc.RTLD_NOW);
}
}

return handle;
}

protected override void FreeLibrary(IntPtr libraryHandle)
{
Libc.dlclose(libraryHandle);
}

protected override IntPtr LoadFunction(string functionName)
{
return Libc.dlsym(NativeHandle, functionName);
}
}
}
}
22 changes: 22 additions & 0 deletions src/vk/Libc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Runtime.InteropServices;

namespace Vulkan
{
internal static class Libc
{
[DllImport("libc")]
public static extern IntPtr dlopen(string fileName, int flags);

[DllImport("libc")]
public static extern IntPtr dlsym(IntPtr handle, string name);

[DllImport("libc")]
public static extern int dlclose(IntPtr handle);

[DllImport("libc")]
public static extern string dlerror();

public const int RTLD_NOW = 0x002;
}
}

0 comments on commit 0c62305

Please sign in to comment.