Skip to content

Commit

Permalink
chore: Linux load update
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Sep 11, 2024
1 parent d82dbb5 commit a4bb5c9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ jobs:

validation_5_2_win:
name: Validate 5.2 Samples (msbuild)
needs: build_tool
needs: build_tool
runs-on: windows-latest
steps:
- name: Checkout
Expand Down
71 changes: 31 additions & 40 deletions src/Resizetizer/src/SkiaSharpTools.Initializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Uno.Resizetizer;
Expand Down Expand Up @@ -38,9 +39,9 @@ public static void Initialize()
{
NetcoreInitializeWindows();
}
else
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
NetCoreInitializeNix();
NetCoreInitializeMac();
}
}
else
Expand Down Expand Up @@ -119,54 +120,24 @@ private static void SetupResolver()
}
}

private static void NetCoreInitializeNix()
private static void NetCoreInitializeMac()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
foreach (var runtimePath in GetRuntimesFolder())
{
var currentPath = Environment.GetEnvironmentVariable("LD_LIBRARY_PATH");
var newPath = string.IsNullOrEmpty(currentPath)
? runtimePath
: currentPath + Path.PathSeparator + runtimePath;
Environment.SetEnvironmentVariable("LD_LIBRARY_PATH", newPath);
}
}

foreach (var runtimePath in GetRuntimesFolder())
{
var extension = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "dylib" : "so";

var libHarfBuzzSharp = Path.Combine(runtimePath, "libHarfBuzzSharp." + extension);
var libSkiaSharp = Path.Combine(runtimePath, "libSkiaSharp." + extension);

IntPtr localDlOpen(string fileName, int flags)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return Linux.dlopen(fileName, false);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return dlopen_macos(fileName, flags);
}
else
{
throw new NotSupportedException("This operating system is not supported");
}
}
var libHarfBuzzSharp = Path.Combine(runtimePath, "libHarfBuzzSharp.dylib");
var libSkiaSharp = Path.Combine(runtimePath, "libSkiaSharp..dylib");

if (File.Exists(libHarfBuzzSharp)
&& File.Exists(libSkiaSharp))
{
var r1 = localDlOpen(libHarfBuzzSharp, 0);
var r1 = dlopen(libHarfBuzzSharp, 0);

if (r1 == IntPtr.Zero)
{
throw new InvalidOperationException($"Failed to load {libHarfBuzzSharp}");
}

var r2 = localDlOpen(libSkiaSharp, 0);
var r2 = dlopen(libSkiaSharp, 0);

if (r2 == IntPtr.Zero)
{
Expand Down Expand Up @@ -225,7 +196,8 @@ static IntPtr ImportResolver(string libraryName, Assembly assembly, DllImportSea
if (libraryName.Equals("libHarfBuzzSharp", StringComparison.OrdinalIgnoreCase)
|| libraryName.Equals("HarfBuzzSharp", StringComparison.OrdinalIgnoreCase))
{
if (!_tryLoad("libHarfBuzzSharp.dll", typeof(SkiaSharpTools).Assembly, searchFlags, out libHandle))
if (!LinuxTryLoad("libHarfBuzzSharp.dll")
&& !_tryLoad("libHarfBuzzSharp.dll", typeof(SkiaSharpTools).Assembly, searchFlags, out libHandle))
{
throw new InvalidOperationException($"Failed to load libHarfBuzzSharp");
}
Expand All @@ -234,12 +206,31 @@ static IntPtr ImportResolver(string libraryName, Assembly assembly, DllImportSea
if (libraryName.Equals("libSkiaSharp", StringComparison.OrdinalIgnoreCase)
|| libraryName.Equals("SkiaSharp", StringComparison.OrdinalIgnoreCase))
{
if (!_tryLoad("libSkiaSharp.dll", typeof(SkiaSharpTools).Assembly, searchFlags, out libHandle))
if (!LinuxTryLoad("libSkiaSharp.dll")
!_tryLoad("libSkiaSharp.dll", typeof(SkiaSharpTools).Assembly, searchFlags, out libHandle))
{
throw new InvalidOperationException($"Failed to load libSkiaSharp");
}
}

bool LinuxTryLoad(string path, out IntPtr handle)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
foreach (var runtimePath in GetRuntimesFolder())
{
handle = Linux.dlopen(path);

if (handle != IntPtr.Zero)
{
return true;
}
}
}

return false;
}

return libHandle;
}

Expand All @@ -250,7 +241,7 @@ static IntPtr ImportResolver(string libraryName, Assembly assembly, DllImportSea
public static extern IntPtr LoadLibrary(string dllToLoad);

[DllImport("libSystem.dylib", EntryPoint = "dlopen")]
public static extern IntPtr dlopen_macos(string fileName, int flags);
public static extern IntPtr dlopen(string fileName, int flags);

// Imported from https://github.com/mono/SkiaSharp/blob/482e6ee2913a08a7cad76520ccf5fbce97c7c23b/binding/Binding.Shared/LibraryLoader.cs
private static class Linux
Expand Down

0 comments on commit a4bb5c9

Please sign in to comment.