Skip to content

Commit

Permalink
libgit2: support insane macOS arm64 varargs
Browse files Browse the repository at this point in the history
.NET does not yet support variadic functions in PInvoke on macOS for
arm64. dotnet/runtime#48796

Do some nonsense until it does.
  • Loading branch information
Edward Thomson committed Mar 2, 2023
1 parent b89e1d8 commit 23056d1
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions Dogged.Native/libgit2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ public static partial class libgit2
{
private const string libgit2_dll = NativeLibrary.Filename;

private static readonly bool isMacArm;

private static NativeInitializer nativeInitializer;

/// <summary>
/// Load and initialize the native library.
/// </summary>
static libgit2()
{
isMacArm = (RuntimeInformation.ProcessArchitecture == Architecture.Arm64 && RuntimeInformation.IsOSPlatform(OSPlatform.OSX));
nativeInitializer = new NativeInitializer();
}

Expand Down Expand Up @@ -416,11 +419,21 @@ public static extern unsafe int git_filter_list_load_ext(
/// <param name="option">The option value to get or set</param>
/// <param name="...">The options to set</param>
/// <returns>0 on success or an error code</returns>
[DllImport(libgit2_dll, CallingConvention = CallingConvention.Cdecl)]
public static extern unsafe int git_libgit2_opts(git_libgit2_opt_t option, git_config_level_t level, git_buf buf);

[DllImport(libgit2_dll, CallingConvention = CallingConvention.Cdecl)]
public static extern unsafe int git_libgit2_opts(git_libgit2_opt_t option, git_config_level_t level, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = Utf8Marshaler.ToNative, MarshalTypeRef = typeof(Utf8Marshaler))] string path);
public static int git_libgit2_opts(git_libgit2_opt_t option, git_config_level_t level, git_buf buf) { return isMacArm ? git_libgit2_opts_mac_arm(option, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, level, buf) : git_libgit2_opts_std(option, level, buf); }
public static int git_libgit2_opts(git_libgit2_opt_t option, git_config_level_t level, string path) { return isMacArm ? git_libgit2_opts_mac_arm(option, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, level, path) : git_libgit2_opts_std(option, level, path); }

// Standard varargs conventions.
[DllImport(libgit2_dll, CallingConvention = CallingConvention.Cdecl, EntryPoint = "git_libgit2_opts")]
public static extern unsafe int git_libgit2_opts_std(git_libgit2_opt_t option, git_config_level_t level, git_buf buf);
[DllImport(libgit2_dll, CallingConvention = CallingConvention.Cdecl, EntryPoint = "git_libgit2_opts")]
public static extern unsafe int git_libgit2_opts_std(git_libgit2_opt_t option, git_config_level_t level, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = Utf8Marshaler.ToNative, MarshalTypeRef = typeof(Utf8Marshaler))] string path);

// macOS on arm64 uses different calling conventions for PInvoke
// because everything is terrible.
[DllImport(libgit2_dll, CallingConvention = CallingConvention.Cdecl, EntryPoint = "git_libgit2_opts")]
public static extern unsafe int git_libgit2_opts_mac_arm(git_libgit2_opt_t option, IntPtr dummy1, IntPtr dummy2, IntPtr dummy3, IntPtr dummy4, IntPtr dummy5, IntPtr dummy6, IntPtr dummy7, git_config_level_t level, git_buf buf);
[DllImport(libgit2_dll, CallingConvention = CallingConvention.Cdecl, EntryPoint = "git_libgit2_opts")]
public static extern unsafe int git_libgit2_opts_mac_arm(git_libgit2_opt_t option, IntPtr dummy1, IntPtr dummy2, IntPtr dummy3, IntPtr dummy4, IntPtr dummy5, IntPtr dummy6, IntPtr dummy7, git_config_level_t level, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = Utf8Marshaler.ToNative, MarshalTypeRef = typeof(Utf8Marshaler))] string path);

/// <summary>
/// Shutdown the global state.
Expand Down

0 comments on commit 23056d1

Please sign in to comment.