Merge pull request #24768 from jtattermusch/macox_big_sur_detection

Unify OS detection logic between Grpc.Core.PlatformApis and Grpc.Tools
pull/24577/head
Jan Tattermusch 4 years ago committed by GitHub
commit e257ef7585
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 46
      src/csharp/Grpc.Tools/Common.cs

@ -66,27 +66,53 @@ namespace Grpc.Tools
default: Cpu = CpuKind.Unknown; break;
}
#else
// Running under either Mono or full MS framework.
Os = OsKind.Windows;
if (Type.GetType("Mono.Runtime", throwOnError: false) != null)
// Using the same best-effort detection logic as Grpc.Core/PlatformApis.cs
var platform = Environment.OSVersion.Platform;
if (platform == PlatformID.Win32NT || platform == PlatformID.Win32S || platform == PlatformID.Win32Windows)
{
// Congratulations. We are running under Mono.
var plat = Environment.OSVersion.Platform;
if (plat == PlatformID.MacOSX)
Os = OsKind.Windows;
}
else if (platform == PlatformID.Unix && GetUname() == "Darwin")
{
Os = OsKind.MacOsX;
}
else if (plat == PlatformID.Unix || (int)plat == 128)
else
{
// This is how Mono detects OSX internally.
Os = File.Exists("/usr/lib/libc.dylib") ? OsKind.MacOsX : OsKind.Linux;
}
Os = OsKind.Linux;
}
// Hope we are not building on ARM under Xamarin!
Cpu = Environment.Is64BitProcess ? CpuKind.X64 : CpuKind.X86;
#endif
}
[DllImport("libc")]
static extern int uname(IntPtr buf);
// This code is copied from Grpc.Core/PlatformApis.cs
static string GetUname()
{
var buffer = Marshal.AllocHGlobal(8192);
try
{
if (uname(buffer) == 0)
{
return Marshal.PtrToStringAnsi(buffer);
}
return string.Empty;
}
catch
{
return string.Empty;
}
finally
{
if (buffer != IntPtr.Zero)
{
Marshal.FreeHGlobal(buffer);
}
}
}
};
// Exception handling helpers.

Loading…
Cancel
Save