Skip to content

Support Windows ARM64 #13

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

Merged
16 commits merged into from
Jun 3, 2021
Merged
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
35 changes: 0 additions & 35 deletions .travis.yml

This file was deleted.

10 changes: 5 additions & 5 deletions LibGit2Sharp.Tests/CommitFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1058,8 +1058,8 @@ public void CanPrettifyAMessage()
}

private readonly string signedCommit =
"tree 6b79e22d69bf46e289df0345a14ca059dfc9bdf6\n" +
"parent 34734e478d6cf50c27c9d69026d93974d052c454\n" +
"tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\n" +
"parent 8496071c1b46c854b31185ea97743be6a8774479\n" +
"author Ben Burkert <ben@benburkert.com> 1358451456 -0800\n" +
"committer Ben Burkert <ben@benburkert.com> 1358451456 -0800\n" +
"gpgsig -----BEGIN PGP SIGNATURE-----\n" +
Expand Down Expand Up @@ -1102,8 +1102,8 @@ public void CanPrettifyAMessage()
"-----END PGP SIGNATURE-----";

private readonly string signedData =
"tree 6b79e22d69bf46e289df0345a14ca059dfc9bdf6\n" +
"parent 34734e478d6cf50c27c9d69026d93974d052c454\n" +
"tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\n" +
"parent 8496071c1b46c854b31185ea97743be6a8774479\n" +
"author Ben Burkert <ben@benburkert.com> 1358451456 -0800\n" +
"committer Ben Burkert <ben@benburkert.com> 1358451456 -0800\n" +
"\n" +
Expand Down Expand Up @@ -1155,7 +1155,7 @@ public void CanCreateACommitString()
[Fact]
public void CanCreateASignedCommit()
{
string repoPath = InitNewRepository();
string repoPath = SandboxStandardTestRepo();
using (var repo = new Repository(repoPath))
{
var odb = repo.ObjectDatabase;
Expand Down
10 changes: 5 additions & 5 deletions LibGit2Sharp.Tests/GlobalSettingsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@ public void CanGetMinimumCompiledInFeatures()
public void CanRetrieveValidVersionString()
{
// Version string format is:
// Major.Minor.Patch[-previewTag]+g{LibGit2Sharp_abbrev_hash}.libgit2-{libgit2_abbrev_hash} (x86|x64 - features)
// Major.Minor.Patch[-previewTag]+{LibGit2Sharp_abbrev_hash}.libgit2-{libgit2_abbrev_hash} (x86|x64|arm64 - features)
// Example output:
// "0.25.0-preview.52+g871d13a67f.libgit2-15e1193 (x86 - Threads, Https)"
// "0.25.0-preview.52+871d13a67f.libgit2-15e1193 (x86 - Threads, Https)"

string versionInfo = GlobalSettings.Version.ToString();

// The GlobalSettings.Version returned string should contain :
// version: '0.25.0[-previewTag]' LibGit2Sharp version number.
// git2SharpHash: '871d13a67f' LibGit2Sharp hash.
// arch: 'x86' or 'x64' libgit2 target.
// arch: 'x86', 'x64' or 'arm64' libgit2 target.
// git2Features: 'Threads, Ssh' libgit2 features compiled with.
string regex = @"^(?<version>\d+\.\d+\.\d+(-[\w\-\.]+)?\+(g(?<git2SharpHash>[a-f0-9]{10})\.)?libgit2-[a-f0-9]{7}) \((?<arch>\w+) - (?<git2Features>(?:\w*(?:, )*\w+)*)\)$";
string regex = @"^(?<version>\d+\.\d+\.\d+(-[\w\-\.]+)?\+((?<git2SharpHash>[a-f0-9]{10})\.)?libgit2-[a-f0-9]{7}) \((?<arch>\w+) - (?<git2Features>(?:\w*(?:, )*\w+)*)\)$";

Assert.NotNull(versionInfo);

Match regexResult = Regex.Match(versionInfo, regex);

Assert.True(regexResult.Success, "The following version string format is enforced:" +
"Major.Minor.Patch[-previewTag]+g{LibGit2Sharp_abbrev_hash}.libgit2-{libgit2_abbrev_hash} (x86|x64 - features). " +
"Major.Minor.Patch[-previewTag]+{LibGit2Sharp_abbrev_hash}.libgit2-{libgit2_abbrev_hash} (x86|x64|arm64 - features). " +
"But found \"" + versionInfo + "\" instead.");
}

Expand Down
31 changes: 14 additions & 17 deletions LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net46;netcoreapp2.0</TargetFrameworks>
<TargetFrameworks>net472;net5.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\LibGit2Sharp\LibGit2Sharp.csproj" />
<ProjectReference Include="..\NativeLibraryLoadTestApp\x86\NativeLibraryLoadTestApp.x86.csproj" Condition="'$(TargetFramework)' == 'net46'" ReferenceOutputAssembly="false" OutputItemType="TestAppExe" />
<ProjectReference Include="..\NativeLibraryLoadTestApp\x64\NativeLibraryLoadTestApp.x64.csproj" Condition="'$(TargetFramework)' == 'net46'" ReferenceOutputAssembly="false" OutputItemType="TestAppExe" />
<ProjectReference Include="..\NativeLibraryLoadTestApp\x86\NativeLibraryLoadTestApp.x86.csproj" Condition="'$(TargetFramework)' != 'net5.0'" ReferenceOutputAssembly="false" OutputItemType="TestAppExe" />
<ProjectReference Include="..\NativeLibraryLoadTestApp\x64\NativeLibraryLoadTestApp.x64.csproj" Condition="'$(TargetFramework)' != 'net5.0'" ReferenceOutputAssembly="false" OutputItemType="TestAppExe" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="Moq" Version="4.9.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.console" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="xunit.skippablefact" Version="1.3.6" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
<PackageReference Include="Moq" Version="4.10.1" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.console" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="xunit.skippablefact" Version="1.3.12" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\LibGit2Sharp\Core\Platform.cs" Link="TestHelpers\Platform.cs" />
<Compile Remove="desktop\**" Condition=" '$(TargetFramework)' != 'net46' " />
</ItemGroup>

<ItemGroup>
<Content Include="Resources\**\*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Compile Remove="desktop\**" Condition="'$(TargetFramework)' == 'net5.0'" />
<Content Include="Resources\**\*.*" CopyToOutputDirectory="PreserveNewest" />
<None Update="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<Target Name="CopyTestAppExes" AfterTargets="ResolveProjectReferences">
Expand All @@ -43,4 +40,4 @@

<Import Project="..\Targets\GenerateNativeDllName.targets" />

</Project>
</Project>
4 changes: 4 additions & 0 deletions LibGit2Sharp.Tests/xunit.runner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://xunit.github.io/schema/current/xunit.runner.schema.json",
"shadowCopy": false
}
139 changes: 126 additions & 13 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -29,32 +30,144 @@ static NativeMethods()
{
if (Platform.IsRunningOnNetFramework() || Platform.IsRunningOnNetCore())
{
string nativeLibraryDir = GlobalSettings.GetAndLockNativeLibraryPath();
if (nativeLibraryDir != null)
// Use .NET Core 3.0+ NativeLibrary when available.
if (!TryUseNativeLibrary())
{
string nativeLibraryPath = Path.Combine(nativeLibraryDir, libgit2 + Platform.GetNativeLibraryExtension());
// NativeLibrary is not available, fall back.

// Use GlobalSettings.NativeLibraryPath when set.
// Try to load the .dll from the path explicitly.
// If this call succeeds further DllImports will find the library loaded and not attempt to load it again.
// If it fails the next DllImport will load the library from safe directories.
#if NETFRAMEWORK
if (Platform.OperatingSystem == OperatingSystemType.Windows)
#else
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
#endif
string nativeLibraryPath = GetGlobalSettingsNativeLibraryPath();
if (nativeLibraryPath != null)
{
LoadWindowsLibrary(nativeLibraryPath);
}
else
{
LoadUnixLibrary(nativeLibraryPath, RTLD_NOW);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
LoadWindowsLibrary(nativeLibraryPath);
}
else
{
LoadUnixLibrary(nativeLibraryPath, RTLD_NOW);
}
}
}
}

InitializeNativeLibrary();
}

private static string GetGlobalSettingsNativeLibraryPath()
{
string nativeLibraryDir = GlobalSettings.GetAndLockNativeLibraryPath();
if (nativeLibraryDir == null)
{
return null;
}
return Path.Combine(nativeLibraryDir, libgit2 + Platform.GetNativeLibraryExtension());
}

private delegate bool TryLoadLibraryByNameDelegate(string libraryName, Assembly assembly, DllImportSearchPath? searchPath, out IntPtr handle);
private delegate bool TryLoadLibraryByPathDelegate(string libraryPath, out IntPtr handle);

static TryLoadLibraryByNameDelegate _tryLoadLibraryByName;
static TryLoadLibraryByPathDelegate _tryLoadLibraryByPath;

static bool TryLoadLibrary(string libraryName, Assembly assembly, DllImportSearchPath? searchPath, out IntPtr handle)
{
if (_tryLoadLibraryByName == null)
{
throw new NotSupportedException();
}
return _tryLoadLibraryByName(libraryName, assembly, searchPath, out handle);
}

static bool TryLoadLibrary(string libraryPath, out IntPtr handle)
{
if (_tryLoadLibraryByPath == null)
{
throw new NotSupportedException();
}
return _tryLoadLibraryByPath(libraryPath, out handle);
}

private static bool TryUseNativeLibrary()
{
// NativeLibrary is available in .NET Core 3.0+.
// We use reflection to use NativeLibrary so this library can target 'netstandard2.0'.

Type dllImportResolverType = Type.GetType("System.Runtime.InteropServices.DllImportResolver, System.Runtime.InteropServices", throwOnError: false);
Type nativeLibraryType = Type.GetType("System.Runtime.InteropServices.NativeLibrary, System.Runtime.InteropServices", throwOnError: false);
var tryLoadLibraryByName = (TryLoadLibraryByNameDelegate)nativeLibraryType?.GetMethod("TryLoad",
new Type[] { typeof(string), typeof(Assembly), typeof(DllImportSearchPath?), typeof(IntPtr).MakeByRefType() })?.CreateDelegate(typeof(TryLoadLibraryByNameDelegate));
var tryLoadLibraryByPath = (TryLoadLibraryByPathDelegate)nativeLibraryType?.GetMethod("TryLoad",
new Type[] { typeof(string), typeof(IntPtr).MakeByRefType() })?.CreateDelegate(typeof(TryLoadLibraryByPathDelegate));
MethodInfo setDllImportResolver = nativeLibraryType?.GetMethod("SetDllImportResolver", new Type[] { typeof(Assembly), dllImportResolverType});

if (dllImportResolverType == null ||
nativeLibraryType == null ||
tryLoadLibraryByName == null ||
tryLoadLibraryByPath == null ||
setDllImportResolver == null)
{
return false;
}

_tryLoadLibraryByPath = tryLoadLibraryByPath;
_tryLoadLibraryByName = tryLoadLibraryByName;

// NativeMethods.SetDllImportResolver(typeof(NativeMethods).Assembly, ResolveDll);
object resolveDelegate = typeof(NativeMethods).GetMethod(nameof(ResolveDll), BindingFlags.NonPublic | BindingFlags.Static).CreateDelegate(dllImportResolverType);
setDllImportResolver.Invoke(null, new object[] { typeof(NativeMethods).Assembly, resolveDelegate });

return true;
}

private static IntPtr ResolveDll(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
{
IntPtr handle = IntPtr.Zero;
if (libraryName == libgit2)
{
// Use GlobalSettings.NativeLibraryPath when set.
string nativeLibraryPath = GetGlobalSettingsNativeLibraryPath();
if (nativeLibraryPath != null &&
TryLoadLibrary(nativeLibraryPath, out handle))
{
return handle;
}

// Use Default DllImport resolution.
if (TryLoadLibrary(libraryName, assembly, searchPath, out handle))
{
return handle;
}

// We cary a number of .so files for Linux which are linked against various
// libc/OpenSSL libraries. Try them out.
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
// The libraries are located at 'runtimes/<rid>/native/lib{libraryName}.so'
// The <rid> ends with the processor architecture. e.g. fedora-x64.
string assemblyDirectory = Path.GetDirectoryName(typeof(NativeMethods).Assembly.Location);
string processorArchitecture = RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant();
string runtimesDirectory = Path.Combine(assemblyDirectory, "runtimes");

if (Directory.Exists(runtimesDirectory))
{
foreach (var runtimeFolder in Directory.GetDirectories(runtimesDirectory, $"*-{processorArchitecture}"))
{
string libPath = Path.Combine(runtimeFolder, "native", $"lib{libraryName}.so");
if (TryLoadLibrary(libPath, out handle))
{
return handle;
}
}
}
}
}
return handle;
}

public const int RTLD_NOW = 0x002;

[DllImport("libdl", EntryPoint = "dlopen")]
Expand Down
Loading