Skip to content
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

Use Environment.OSVersion on Windows and macOS. #11636

Merged
1 commit merged into from
May 14, 2020
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
104 changes: 2 additions & 102 deletions src/Cli/Microsoft.DotNet.Cli.Utils/RuntimeEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,38 +53,18 @@ private static string GetOSVersion()
switch (GetOSPlatform())
{
case Platform.Windows:
return NativeMethods.Windows.RtlGetVersion() ?? string.Empty;
return Environment.OSVersion.Version.ToString(3);
case Platform.Linux:
return GetDistroVersionId() ?? string.Empty;
case Platform.Darwin:
return GetDarwinVersion() ?? string.Empty;
return Environment.OSVersion.Version.ToString(2);
case Platform.FreeBSD:
return GetFreeBSDVersion() ?? string.Empty;
default:
return string.Empty;
}
}

private static string GetDarwinVersion()
{
Version version;
var kernelRelease = NativeMethods.Darwin.GetKernelRelease();
if (!Version.TryParse(kernelRelease, out version) || version.Major < 5)
{
// 10.0 covers all versions prior to Darwin 5
// Similarly, if the version is not a valid version number, but we have still detected that it is Darwin, we just assume
// it is OS X 10.0
return "10.0";
}
else
{
// Mac OS X 10.1 mapped to Darwin 5.x, and the mapping continues that way
// So just subtract 4 from the Darwin version.
// https://en.wikipedia.org/wiki/Darwin_%28operating_system%29
return $"10.{version.Major - 4}";
}
}

private static string GetFreeBSDVersion()
{
// This is same as sysctl kern.version
Expand Down Expand Up @@ -206,85 +186,5 @@ private static Platform DetermineOSPlatform()

return Platform.Unknown;
}

private static class NativeMethods
{
public static class Darwin
{
private const int CTL_KERN = 1;
private const int KERN_OSRELEASE = 2;

public unsafe static string GetKernelRelease()
{
const uint BUFFER_LENGTH = 32;

var name = stackalloc int[2];
name[0] = CTL_KERN;
name[1] = KERN_OSRELEASE;

var buf = stackalloc byte[(int)BUFFER_LENGTH];
var len = stackalloc uint[1];
*len = BUFFER_LENGTH;

try
{
// If the buffer isn't big enough, it seems sysctl still returns 0 and just sets len to the
// necessary buffer size. This appears to be contrary to the man page, but it's easy to detect
// by simply checking len against the buffer length.
if (sysctl(name, 2, buf, len, IntPtr.Zero, 0) == 0 && *len < BUFFER_LENGTH)
{
return Marshal.PtrToStringAnsi((IntPtr)buf, (int)*len);
}
}
catch (Exception ex)
{
throw new PlatformNotSupportedException("Error reading Darwin Kernel Version", ex);
}
throw new PlatformNotSupportedException("Unknown error reading Darwin Kernel Version");
}

[DllImport("libc")]
private unsafe static extern int sysctl(
int* name,
uint namelen,
byte* oldp,
uint* oldlenp,
IntPtr newp,
uint newlen);
}

public static class Windows
{
[StructLayout(LayoutKind.Sequential)]
internal struct RTL_OSVERSIONINFOEX
{
internal uint dwOSVersionInfoSize;
internal uint dwMajorVersion;
internal uint dwMinorVersion;
internal uint dwBuildNumber;
internal uint dwPlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
internal string szCSDVersion;
}

// This call avoids the shimming Windows does to report old versions
[DllImport("ntdll")]
private static extern int RtlGetVersion(out RTL_OSVERSIONINFOEX lpVersionInformation);

internal static string RtlGetVersion()
{
RTL_OSVERSIONINFOEX osvi = new RTL_OSVERSIONINFOEX();
osvi.dwOSVersionInfoSize = (uint)Marshal.SizeOf(osvi);
if (RtlGetVersion(out osvi) == 0)
{
return $"{osvi.dwMajorVersion}.{osvi.dwMinorVersion}.{osvi.dwBuildNumber}";
}
else
{
return null;
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ public void VerifyWindows()
Version osVersion = Version.Parse(RuntimeEnvironment.OperatingSystemVersion);
Version expectedOSVersion = Environment.OSVersion.Version;

// 3 parts of the version should be supplied for Windows
Assert.Equal(expectedOSVersion.Major, osVersion.Major);
Assert.Equal(expectedOSVersion.Minor, osVersion.Minor);
Assert.Equal(expectedOSVersion.Build, osVersion.Build);
Assert.Equal(-1, osVersion.Revision);
}

[MacOsOnlyFact]
Expand All @@ -36,9 +38,13 @@ public void VerifyMacOs()
Assert.Equal("Mac OS X", RuntimeEnvironment.OperatingSystem);

Version osVersion = Version.Parse(RuntimeEnvironment.OperatingSystemVersion);
Version expectedOSVersion = Environment.OSVersion.Version;

Assert.Equal(Environment.OSVersion.Version.Major, osVersion.Major);
Assert.Equal(Environment.OSVersion.Version.Minor, osVersion.Minor);
// 2 parts of the version should be supplied for macOS
Assert.Equal(expectedOSVersion.Major, osVersion.Major);
Assert.Equal(expectedOSVersion.Minor, osVersion.Minor);
Assert.Equal(-1, osVersion.Build);
Assert.Equal(-1, osVersion.Revision);
}

[LinuxOnlyFact]
Expand Down
21 changes: 9 additions & 12 deletions src/Tests/Microsoft.NET.TestFramework/EnvironmentInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,16 @@ public static string GetCompatibleRid(string targetFramework = null)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
Version osVersion;
if (Version.TryParse(DotNet.Cli.Utils.RuntimeEnvironment.OperatingSystemVersion, out osVersion))
Version osVersion = Environment.OSVersion.Version;
if (osVersion > new Version(10, 11))
{
if (osVersion > new Version(10, 11))
{
// netcoreapp1.0 only supports osx.10.10 and osx.10.11
rid = "osx.10.11-x64";
}
else if (osVersion > new Version(10, 12))
{
// netcoreapp1.1 only supports up to osx.10.12
rid = "osx.10.12-x64";
}
// netcoreapp1.0 only supports osx.10.10 and osx.10.11
rid = "osx.10.11-x64";
}
else if (osVersion > new Version(10, 12))
{
// netcoreapp1.1 only supports up to osx.10.12
rid = "osx.10.12-x64";
}
}
}
Expand Down