Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link to libc instead of libdl on desktop linux #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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;
}
}