Skip to content

Commit

Permalink
Fix including x86 native libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
lithiumtoast committed Nov 25, 2020
1 parent 8b8f331 commit 9b33d8c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 21 deletions.
20 changes: 20 additions & 0 deletions src/dotnet/projects/production/Ankura.FNA/Ankura.FNA.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<PackagePath>runtimes/win-x64/native/FAudio.dll</PackagePath>
</Content>
<Content Include="$(RepositoryRootPath)/lib/FAudio/win-x86/FAudio.dll">
<Link>libs/win-x86/FAudio.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<PackagePath>runtimes/win-x86/native/FAudio.dll</PackagePath>
</Content>
<Content Include="$(RepositoryRootPath)/lib/FAudio/osx-x64/libFAudio.0.dylib">
<Link>libs/osx-x64/libFAudio.0.dylib</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand All @@ -58,6 +63,11 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<PackagePath>runtimes/win-x64/native/FNA3D.dll</PackagePath>
</Content>
<Content Include="$(RepositoryRootPath)/lib/FNA3D/win-x86/FNA3D.dll">
<Link>libs/win-x86/FNA3D.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<PackagePath>runtimes/win-x86/native/FNA3D.dll</PackagePath>
</Content>
<Content Include="$(RepositoryRootPath)/lib/FNA3D/osx-x64/libFNA3D.0.dylib">
<Link>libs/osx-x64/libFNA3D.0.dylib</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down Expand Up @@ -86,6 +96,11 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<PackagePath>runtimes/win-x64/native/SDL2.dll</PackagePath>
</Content>
<Content Include="$(RepositoryRootPath)/lib/SDL2/win-x86/SDL2.dll">
<Link>libs/win-x86/SDL2.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<PackagePath>runtimes/win-x86/native/SDL2.dll</PackagePath>
</Content>
<Content Include="$(RepositoryRootPath)/lib/SDL2/osx-x64/libSDL2-2.0.0.dylib">
<Link>libs/osx-x64/libSDL2-2.0.0.dylib</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand All @@ -104,6 +119,11 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<PackagePath>runtimes/win-x64/native/libtheorafile.dll</PackagePath>
</Content>
<Content Include="$(RepositoryRootPath)/lib/theorafile/win-x86/libtheorafile.dll">
<Link>libs/win-x86/libtheorafile.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<PackagePath>runtimes/win-x86/native/libtheorafile.dll</PackagePath>
</Content>
<Content Include="$(RepositoryRootPath)/lib/theorafile/osx-x64/libtheorafile.dylib">
<Link>libs/osx-x64/libtheorafile.dylib</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
47 changes: 26 additions & 21 deletions src/dotnet/projects/production/Ankura/Ankura/Native/Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ namespace Ankura
{
public static class Native
{
private static IEnumerable<string>? _searchDirectories;
private static IEnumerable<string>? _librarySearchDirectories;
private static RuntimePlatform? _platform;

public static RuntimePlatform RuntimePlatform
{
get
{
_platform ??= GetRuntimePlatform();
return _platform.Value;
}
}

public static void SetDllImportResolver(Assembly assembly)
{
Expand All @@ -25,8 +35,7 @@ public static IntPtr LoadLibrary(string libraryFilePath)
{
EnsureIs64BitArchitecture();

var platform = GetRuntimePlatform();
return platform switch
return RuntimePlatform switch
{
RuntimePlatform.Linux => libdl.dlopen(libraryFilePath, 0x101),
RuntimePlatform.Windows => Kernel32.LoadLibrary(libraryFilePath),
Expand All @@ -37,8 +46,7 @@ public static IntPtr LoadLibrary(string libraryFilePath)

public static bool FreeLibrary(IntPtr libraryHandle)
{
var platform = GetRuntimePlatform();
return platform switch
return RuntimePlatform switch
{
RuntimePlatform.Linux => libdl.dlclose(libraryHandle) == 0,
RuntimePlatform.Windows => Kernel32.FreeLibrary(libraryHandle) != 0,
Expand All @@ -49,8 +57,7 @@ public static bool FreeLibrary(IntPtr libraryHandle)

public static IntPtr GetLibraryFunctionPointer(IntPtr libraryHandle, string functionName)
{
var platform = GetRuntimePlatform();
return platform switch
return RuntimePlatform switch
{
RuntimePlatform.Linux => libdl.dlsym(libraryHandle, functionName),
RuntimePlatform.Windows => Kernel32.GetProcAddress(libraryHandle, functionName),
Expand Down Expand Up @@ -84,7 +91,7 @@ public static T GetLibraryFunction<T>(IntPtr libraryHandle, string functionName)
return Marshal.GetDelegateForFunctionPointer<T>(functionHandle);
}

public static RuntimePlatform GetRuntimePlatform()
private static RuntimePlatform GetRuntimePlatform()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Expand All @@ -105,7 +112,7 @@ public static RuntimePlatform GetRuntimePlatform()
return RuntimePlatform.Unknown;
}

public static string GetLibraryFileExtension(RuntimePlatform platform)
private static string GetLibraryFileExtension(RuntimePlatform platform)
{
return platform switch
{
Expand All @@ -119,31 +126,30 @@ public static string GetLibraryFileExtension(RuntimePlatform platform)
};
}

public static string GetRuntimeIdentifier(RuntimePlatform platform)
private static string GetRuntimeIdentifier()
{
return platform switch
return RuntimePlatform switch
{
RuntimePlatform.Windows => "win-x64",
RuntimePlatform.Windows => Environment.Is64BitProcess ? "win-x64" : "win-x86",
RuntimePlatform.macOS => "osx-x64",
RuntimePlatform.Linux => "linux-x64",
RuntimePlatform.Android => throw new NotImplementedException(),
RuntimePlatform.iOS => throw new NotImplementedException(),
RuntimePlatform.Unknown => throw new NotSupportedException(),
_ => throw new ArgumentOutOfRangeException(nameof(platform), platform, null)
_ => throw new ArgumentOutOfRangeException(nameof(RuntimePlatform), RuntimePlatform, null)
};
}

private static IEnumerable<string> GetSearchDirectories()
{
if (_searchDirectories != null)
if (_librarySearchDirectories != null)
{
return _searchDirectories;
return _librarySearchDirectories;
}

var platform = GetRuntimePlatform();
var runtimeIdentifier = GetRuntimeIdentifier(platform);
var runtimeIdentifier = GetRuntimeIdentifier();

return _searchDirectories = new[]
return _librarySearchDirectories = new[]
{
Environment.CurrentDirectory,
AppDomain.CurrentDomain.BaseDirectory!,
Expand All @@ -154,9 +160,8 @@ private static IEnumerable<string> GetSearchDirectories()

private static string GetLibraryPath(string libraryName)
{
var platform = GetRuntimePlatform();
var libraryPrefix = platform == RuntimePlatform.Windows ? string.Empty : "lib";
var libraryFileExtension = GetLibraryFileExtension(platform);
var libraryPrefix = RuntimePlatform == RuntimePlatform.Windows ? string.Empty : "lib";
var libraryFileExtension = GetLibraryFileExtension(RuntimePlatform);
var libraryFileName = $"{libraryPrefix}{libraryName}";

var directories = GetSearchDirectories();
Expand Down

0 comments on commit 9b33d8c

Please sign in to comment.