Skip to content

Commit ab87189

Browse files
authored
Merge pull request #1918 from libgit2/tfm-change
Change TFM to netcoreapp3.1
2 parents 66dae0f + 9a9a297 commit ab87189

File tree

6 files changed

+28
-78
lines changed

6 files changed

+28
-78
lines changed

Diff for: LibGit2Sharp.Tests/GlobalSettingsFixture.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Reflection;
34
using System.Text.RegularExpressions;
45
using LibGit2Sharp.Core;
56
using LibGit2Sharp.Tests.TestHelpers;
@@ -64,12 +65,13 @@ public void LoadFromSpecifiedPath(string architecture)
6465
var testDir = Path.GetDirectoryName(typeof(GlobalSettingsFixture).Assembly.Location);
6566
var testAppExe = Path.Combine(testDir, $"NativeLibraryLoadTestApp.{architecture}.exe");
6667
var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
67-
var platformDir = Path.Combine(tempDir, "plat");
68+
var platformDir = Path.Combine(tempDir, "plat", architecture);
69+
var libraryPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "lib", "win32", architecture);
6870

6971
try
7072
{
71-
Directory.CreateDirectory(Path.Combine(platformDir, architecture));
72-
File.Copy(Path.Combine(GlobalSettings.NativeLibraryPath, architecture, nativeDllFileName), Path.Combine(platformDir, architecture, nativeDllFileName));
73+
Directory.CreateDirectory(platformDir);
74+
File.Copy(Path.Combine(libraryPath, nativeDllFileName), Path.Combine(platformDir, nativeDllFileName));
7375

7476
var (output, exitCode) = ProcessHelper.RunProcess(testAppExe, arguments: $@"{NativeDllName.Name} ""{platformDir}""", workingDirectory: tempDir);
7577

Diff for: LibGit2Sharp/Core/NativeMethods.cs

+17-62
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
// Restrict the set of directories where the native library is loaded from to safe directories.
1010
[assembly: DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.ApplicationDirectory | DllImportSearchPath.SafeDirectories)]
1111

12-
#pragma warning disable IDE1006 // Naming Styles
13-
14-
// ReSharper disable InconsistentNaming
1512
namespace LibGit2Sharp.Core
1613
{
1714
internal static class NativeMethods
@@ -22,15 +19,13 @@ internal static class NativeMethods
2219
// An object tied to the lifecycle of the NativeMethods static class.
2320
// This will handle initialization and shutdown of the underlying
2421
// native library.
25-
#pragma warning disable 0414
2622
private static NativeShutdownObject shutdownObject;
27-
#pragma warning restore 0414
2823

2924
static NativeMethods()
3025
{
3126
if (Platform.IsRunningOnNetFramework() || Platform.IsRunningOnNetCore())
3227
{
33-
// Use .NET Core 3.0+ NativeLibrary when available.
28+
// Use NativeLibrary when available.
3429
if (!TryUseNativeLibrary())
3530
{
3631
// NativeLibrary is not available, fall back.
@@ -40,6 +35,7 @@ static NativeMethods()
4035
// If this call succeeds further DllImports will find the library loaded and not attempt to load it again.
4136
// If it fails the next DllImport will load the library from safe directories.
4237
string nativeLibraryPath = GetGlobalSettingsNativeLibraryPath();
38+
4339
if (nativeLibraryPath != null)
4440
{
4541
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
@@ -61,89 +57,46 @@ static NativeMethods()
6157
private static string GetGlobalSettingsNativeLibraryPath()
6258
{
6359
string nativeLibraryDir = GlobalSettings.GetAndLockNativeLibraryPath();
60+
6461
if (nativeLibraryDir == null)
6562
{
6663
return null;
6764
}
68-
return Path.Combine(nativeLibraryDir, libgit2 + Platform.GetNativeLibraryExtension());
69-
}
70-
71-
private delegate bool TryLoadLibraryByNameDelegate(string libraryName, Assembly assembly, DllImportSearchPath? searchPath, out IntPtr handle);
72-
private delegate bool TryLoadLibraryByPathDelegate(string libraryPath, out IntPtr handle);
73-
74-
static TryLoadLibraryByNameDelegate _tryLoadLibraryByName;
75-
static TryLoadLibraryByPathDelegate _tryLoadLibraryByPath;
76-
77-
static bool TryLoadLibrary(string libraryName, Assembly assembly, DllImportSearchPath? searchPath, out IntPtr handle)
78-
{
79-
if (_tryLoadLibraryByName == null)
80-
{
81-
throw new NotSupportedException();
82-
}
83-
return _tryLoadLibraryByName(libraryName, assembly, searchPath, out handle);
84-
}
8565

86-
static bool TryLoadLibrary(string libraryPath, out IntPtr handle)
87-
{
88-
if (_tryLoadLibraryByPath == null)
89-
{
90-
throw new NotSupportedException();
91-
}
92-
return _tryLoadLibraryByPath(libraryPath, out handle);
66+
return Path.Combine(nativeLibraryDir, libgit2 + Platform.GetNativeLibraryExtension());
9367
}
9468

69+
#if NETSTANDARD
70+
private static bool TryUseNativeLibrary() => false;
71+
#else
9572
private static bool TryUseNativeLibrary()
9673
{
97-
// NativeLibrary is available in .NET Core 3.0+.
98-
// We use reflection to use NativeLibrary so this library can target 'netstandard2.0'.
99-
100-
Type dllImportResolverType = Type.GetType("System.Runtime.InteropServices.DllImportResolver, System.Runtime.InteropServices", throwOnError: false);
101-
Type nativeLibraryType = Type.GetType("System.Runtime.InteropServices.NativeLibrary, System.Runtime.InteropServices", throwOnError: false);
102-
var tryLoadLibraryByName = (TryLoadLibraryByNameDelegate)nativeLibraryType?.GetMethod("TryLoad",
103-
new Type[] { typeof(string), typeof(Assembly), typeof(DllImportSearchPath?), typeof(IntPtr).MakeByRefType() })?.CreateDelegate(typeof(TryLoadLibraryByNameDelegate));
104-
var tryLoadLibraryByPath = (TryLoadLibraryByPathDelegate)nativeLibraryType?.GetMethod("TryLoad",
105-
new Type[] { typeof(string), typeof(IntPtr).MakeByRefType() })?.CreateDelegate(typeof(TryLoadLibraryByPathDelegate));
106-
MethodInfo setDllImportResolver = nativeLibraryType?.GetMethod("SetDllImportResolver", new Type[] { typeof(Assembly), dllImportResolverType });
107-
108-
if (dllImportResolverType == null ||
109-
nativeLibraryType == null ||
110-
tryLoadLibraryByName == null ||
111-
tryLoadLibraryByPath == null ||
112-
setDllImportResolver == null)
113-
{
114-
return false;
115-
}
116-
117-
_tryLoadLibraryByPath = tryLoadLibraryByPath;
118-
_tryLoadLibraryByName = tryLoadLibraryByName;
119-
120-
// NativeMethods.SetDllImportResolver(typeof(NativeMethods).Assembly, ResolveDll);
121-
object resolveDelegate = typeof(NativeMethods).GetMethod(nameof(ResolveDll), BindingFlags.NonPublic | BindingFlags.Static).CreateDelegate(dllImportResolverType);
122-
setDllImportResolver.Invoke(null, new object[] { typeof(NativeMethods).Assembly, resolveDelegate });
74+
NativeLibrary.SetDllImportResolver(typeof(NativeMethods).Assembly, ResolveDll);
12375

12476
return true;
12577
}
12678

12779
private static IntPtr ResolveDll(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
12880
{
12981
IntPtr handle = IntPtr.Zero;
82+
13083
if (libraryName == libgit2)
13184
{
13285
// Use GlobalSettings.NativeLibraryPath when set.
13386
string nativeLibraryPath = GetGlobalSettingsNativeLibraryPath();
134-
if (nativeLibraryPath != null &&
135-
TryLoadLibrary(nativeLibraryPath, out handle))
87+
88+
if (nativeLibraryPath != null && NativeLibrary.TryLoad(nativeLibraryPath, out handle))
13689
{
13790
return handle;
13891
}
13992

14093
// Use Default DllImport resolution.
141-
if (TryLoadLibrary(libraryName, assembly, searchPath, out handle))
94+
if (NativeLibrary.TryLoad(libraryName, assembly, searchPath, out handle))
14295
{
14396
return handle;
14497
}
14598

146-
// We cary a number of .so files for Linux which are linked against various
99+
// We carry a number of .so files for Linux which are linked against various
147100
// libc/OpenSSL libraries. Try them out.
148101
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
149102
{
@@ -158,16 +111,19 @@ private static IntPtr ResolveDll(string libraryName, Assembly assembly, DllImpor
158111
foreach (var runtimeFolder in Directory.GetDirectories(runtimesDirectory, $"*-{processorArchitecture}"))
159112
{
160113
string libPath = Path.Combine(runtimeFolder, "native", $"lib{libraryName}.so");
161-
if (TryLoadLibrary(libPath, out handle))
114+
115+
if (NativeLibrary.TryLoad(libPath, out handle))
162116
{
163117
return handle;
164118
}
165119
}
166120
}
167121
}
168122
}
123+
169124
return handle;
170125
}
126+
#endif
171127

172128
public const int RTLD_NOW = 0x002;
173129

@@ -2110,4 +2066,3 @@ internal static extern unsafe int git_worktree_prune(
21102066
git_worktree_prune_options options);
21112067
}
21122068
}
2113-
// ReSharper restore InconsistentNaming

Diff for: LibGit2Sharp/Core/Platform.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ internal enum OperatingSystemType
1212

1313
internal static class Platform
1414
{
15-
public static string ProcessorArchitecture => IntPtr.Size == 8 ? "x64" : "x86";
15+
public static string ProcessorArchitecture => RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant();
1616

1717
public static OperatingSystemType OperatingSystem
1818
{

Diff for: LibGit2Sharp/GlobalSettings.cs

+2-9
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static GlobalSettings()
3232
if (netFX)
3333
{
3434
// For .NET Framework apps the dependencies are deployed to lib/win32/{architecture} directory
35-
nativeLibraryDefaultPath = Path.Combine(GetExecutingAssemblyDirectory(), "lib", "win32");
35+
nativeLibraryDefaultPath = Path.Combine(GetExecutingAssemblyDirectory(), "lib", "win32", Platform.ProcessorArchitecture);
3636
}
3737
else
3838
{
@@ -159,8 +159,6 @@ public static LogConfiguration LogConfiguration
159159
/// <summary>
160160
/// Sets a path for loading native binaries on .NET Framework or .NET Core.
161161
/// When specified, native library will first be searched under the given path.
162-
/// On .NET Framework a subdirectory corresponding to the architecture ("x86" or "x64") is appended,
163-
/// otherwise the native library is expected to be found in the directory as specified.
164162
///
165163
/// If the library is not found it will be searched in standard search paths:
166164
/// <see cref="DllImportSearchPath.AssemblyDirectory"/>,
@@ -170,10 +168,6 @@ public static LogConfiguration LogConfiguration
170168
/// This must be set before any other calls to the library,
171169
/// and is not available on other platforms than .NET Framework and .NET Core.
172170
/// </para>
173-
/// <para>
174-
/// If not specified on .NET Framework it defaults to lib/win32 subdirectory
175-
/// of the directory where this assembly is loaded from.
176-
/// </para>
177171
/// </summary>
178172
public static string NativeLibraryPath
179173
{
@@ -213,8 +207,7 @@ public static string NativeLibraryPath
213207
internal static string GetAndLockNativeLibraryPath()
214208
{
215209
nativeLibraryPathLocked = true;
216-
string result = nativeLibraryPath ?? nativeLibraryDefaultPath;
217-
return Platform.IsRunningOnNetFramework() ? Path.Combine(result, Platform.ProcessorArchitecture) : result;
210+
return nativeLibraryPath ?? nativeLibraryDefaultPath;
218211
}
219212

220213
/// <summary>

Diff for: LibGit2Sharp/LibGit2Sharp.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0;netcoreapp2.1</TargetFrameworks>
4+
<TargetFrameworks>netstandard2.0;netcoreapp3.1</TargetFrameworks>
55
<GenerateDocumentationFile>true</GenerateDocumentationFile>
66
<Description>LibGit2Sharp brings all the might and speed of libgit2, a native Git implementation, to the managed world of .NET</Description>
77
<Company>LibGit2Sharp contributors</Company>

Diff for: NativeLibraryLoadTestApp/TestApp.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class TestApp
1111
private static extern IntPtr GetModuleHandle(string path);
1212

1313
[DllImport("kernel32")]
14-
private static extern int GetModuleFileName(IntPtr handle, [Out]StringBuilder path, int size);
14+
private static extern int GetModuleFileName(IntPtr handle, [Out] StringBuilder path, int size);
1515

1616
static int Main(string[] args)
1717
{
@@ -23,7 +23,7 @@ static int Main(string[] args)
2323

2424
var moduleName = args[0];
2525
var loadFromDirectory = args[1];
26-
var expectedPath = Path.Combine(loadFromDirectory, (IntPtr.Size == 4) ? "x86" : "x64", moduleName + ".dll");
26+
var expectedPath = Path.Combine(loadFromDirectory, moduleName + ".dll");
2727

2828
GlobalSettings.NativeLibraryPath = loadFromDirectory;
2929
var isValid = Repository.IsValid(Path.GetTempPath());

0 commit comments

Comments
 (0)