Skip to content

Commit

Permalink
[Build] Native: prefix folder with platform (for future unification o…
Browse files Browse the repository at this point in the history
…f .NET 5 without RID)
  • Loading branch information
xen2 committed Mar 11, 2021
1 parent a1b195e commit 2b4bb6a
Show file tree
Hide file tree
Showing 96 changed files with 238 additions and 262 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions sources/core/Stride.Core/Diagnostics/VTuneProfiler.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright (c) Stride contributors (https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.

using System.Collections.Generic;
using Stride.Core.Annotations;
#if STRIDE_PLATFORM_DESKTOP
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Stride.Core.Annotations;

namespace Stride.Core.Diagnostics
{
Expand Down Expand Up @@ -45,7 +45,7 @@ public static void Pause()
}
}

public static readonly bool IsAvailable = NativeLibrary.LoadLibrary(VTune2015DllName) != IntPtr.Zero;
public static readonly bool IsAvailable = NativeLibrary.Load(VTune2015DllName) != IntPtr.Zero;

public static Event CreateEvent([NotNull] string eventName)
{
Expand Down
2 changes: 1 addition & 1 deletion sources/core/Stride.Core/Native/NativeInvoke.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static class NativeInvoke

static NativeInvoke()
{
NativeLibrary.PreloadLibrary("libcore", typeof(NativeInvoke));
NativeLibraryHelper.PreloadLibrary("libcore", typeof(NativeInvoke));
}

/// <summary>
Expand Down
177 changes: 0 additions & 177 deletions sources/core/Stride.Core/Native/NativeLibrary.cs

This file was deleted.

139 changes: 139 additions & 0 deletions sources/core/Stride.Core/Native/NativeLibraryHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// Copyright (c) Stride contributors (https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;

namespace Stride.Core
{
public static class NativeLibraryHelper
{
private static readonly Dictionary<string, IntPtr> LoadedLibraries = new Dictionary<string, IntPtr>();

/// <summary>
/// Try to preload the library.
/// This is useful when we want to have AnyCPU .NET and CPU-specific native code.
/// Only available on Windows for now.
/// </summary>
/// <param name="libraryName">Name of the library.</param>
/// <param name="owner">Type whose assembly location is related to the native library (we can't use GetCallingAssembly as it might be wrong due to optimizations).</param>
/// <exception cref="System.InvalidOperationException">Library could not be loaded.</exception>
public static void PreloadLibrary(string libraryName, Type owner)
{
#if STRIDE_PLATFORM_DESKTOP
lock (LoadedLibraries)
{
// If already loaded, just exit as we want to load it just once
if (LoadedLibraries.ContainsKey(libraryName))
{
return;
}

string cpu;
string platform;

switch (RuntimeInformation.ProcessArchitecture)
{
case Architecture.X86:
cpu = "x86";
break;
case Architecture.X64:
cpu = "x64";
break;
case Architecture.Arm:
cpu = "ARM";
break;
default:
throw new PlatformNotSupportedException();
}

switch (Platform.Type)
{
case PlatformType.Windows:
platform = "win";
break;
case PlatformType.Linux:
platform = "linux";
break;
case PlatformType.macOS:
platform = "osx";
break;
default:
throw new PlatformNotSupportedException();
}

// We are trying to load the dll from a shadow path if it is already registered, otherwise we use it directly from the folder
{
foreach (var libraryPath in new[]
{
Path.Combine(Path.GetDirectoryName(owner.GetTypeInfo().Assembly.Location), $"{platform}-{cpu}"),
Path.Combine(Environment.CurrentDirectory, $"{platform}-{cpu}"),
// Also try without platform for Windows-only packages (backward compat for editor packages)
Path.Combine(Path.GetDirectoryName(owner.GetTypeInfo().Assembly.Location), $"{cpu}"),
Path.Combine(Environment.CurrentDirectory, $"{cpu}"),
})
{
var libraryFilename = Path.Combine(libraryPath, libraryName);
if (NativeLibrary.TryLoad(libraryFilename, out var result))
{
LoadedLibraries.Add(libraryName.ToLowerInvariant(), result);
return;
}
}
}

// Attempt to load it from PATH
foreach (var p in Environment.GetEnvironmentVariable("PATH").Split(Path.PathSeparator))
{
var libraryFilename = Path.Combine(p, libraryName);
if (NativeLibrary.TryLoad(libraryFilename, out var result))
{
LoadedLibraries.Add(libraryName.ToLowerInvariant(), result);
return;
}
}

throw new InvalidOperationException($"Could not load native library {libraryName} using CPU architecture {cpu}.");
}
#endif
}

/// <summary>
/// UnLoad a specific native dynamic library loaded previously by <see cref="LoadLibrary" />.
/// </summary>
/// <param name="libraryName">Name of the library to unload.</param>
public static void UnLoad(string libraryName)
{
#if STRIDE_PLATFORM_DESKTOP
lock (LoadedLibraries)
{
IntPtr libHandle;
if (LoadedLibraries.TryGetValue(libraryName, out libHandle))
{
NativeLibrary.Free(libHandle);
LoadedLibraries.Remove(libraryName);
}
}
#endif
}

/// <summary>
/// UnLoad all native dynamic library loaded previously by <see cref="LoadLibrary"/>.
/// </summary>
public static void UnLoadAll()
{
#if STRIDE_PLATFORM_DESKTOP
lock (LoadedLibraries)
{
foreach (var libraryItem in LoadedLibraries)
{
NativeLibrary.Free(libraryItem.Value);
}
LoadedLibraries.Clear();
}
#endif
}
}
}
2 changes: 1 addition & 1 deletion sources/core/Stride.Core/Native/lz4/NativeLZ4Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public abstract class NativeLz4Base
{
static NativeLz4Base()
{
NativeLibrary.PreloadLibrary(NativeInvoke.Library, typeof(NativeLz4Base));
NativeLibraryHelper.PreloadLibrary(NativeInvoke.Library, typeof(NativeLz4Base));
}

[DllImport(NativeInvoke.Library, CallingConvention = CallingConvention.Cdecl)]
Expand Down
2 changes: 1 addition & 1 deletion sources/engine/Stride.Assets.Models/AssimpAssetImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class AssimpAssetImporter : ModelAssetImporter
{
static AssimpAssetImporter()
{
NativeLibrary.PreloadLibrary("assimp-vc140-mt.dll", typeof(AssimpAssetImporter));
NativeLibraryHelper.PreloadLibrary("assimp-vc140-mt.dll", typeof(AssimpAssetImporter));
}

// Supported file extensions for this importer
Expand Down
2 changes: 1 addition & 1 deletion sources/engine/Stride.Assets.Models/FbxAssetImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class FbxAssetImporter : ModelAssetImporter
{
static FbxAssetImporter()
{
NativeLibrary.PreloadLibrary("libfbxsdk.dll", typeof(FbxAssetImporter));
NativeLibraryHelper.PreloadLibrary("libfbxsdk.dll", typeof(FbxAssetImporter));
}

// Supported file extensions for this importer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal class ColliderShapeAssetCompiler : AssetCompilerBase
{
static ColliderShapeAssetCompiler()
{
NativeLibrary.PreloadLibrary("VHACD.dll", typeof(ColliderShapeAssetCompiler));
NativeLibraryHelper.PreloadLibrary("VHACD.dll", typeof(ColliderShapeAssetCompiler));
}

public override IEnumerable<BuildDependencyInfo> GetInputTypes(AssetItem assetItem)
Expand Down
2 changes: 1 addition & 1 deletion sources/engine/Stride.Audio/NativeInvoke.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal static class NativeInvoke

internal static void PreLoad()
{
NativeLibrary.PreloadLibrary("libstrideaudio", typeof(NativeInvoke));
NativeLibraryHelper.PreloadLibrary("libstrideaudio", typeof(NativeInvoke));
}

static NativeInvoke()
Expand Down
Loading

0 comments on commit 2b4bb6a

Please sign in to comment.