Skip to content

Commit

Permalink
add Windows check to our CsProjClassicNetToolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsitnik committed Jun 8, 2017
1 parent a0148db commit 5513873
Showing 1 changed file with 31 additions and 18 deletions.
Expand Up @@ -4,9 +4,9 @@
using BenchmarkDotNet.Extensions;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Portability;
using BenchmarkDotNet.Running;
using JetBrains.Annotations;
using Microsoft.Win32;

namespace BenchmarkDotNet.Toolchains.CsProj
{
Expand All @@ -21,6 +21,7 @@ public class CsProjClassicNetToolchain : Toolchain
[PublicAPI] public static readonly IToolchain Net461 = new CsProjClassicNetToolchain("net461");
[PublicAPI] public static readonly IToolchain Net462 = new CsProjClassicNetToolchain("net462");
[PublicAPI] public static readonly IToolchain Net47 = new CsProjClassicNetToolchain("net47");
private static readonly IToolchain Default = Net46; // the lowest version we support

[PublicAPI]
public static readonly Lazy<IToolchain> Current = new Lazy<IToolchain>(GetCurrentVersion);
Expand All @@ -41,6 +42,12 @@ public override bool IsSupported(Benchmark benchmark, ILogger logger, IResolver
return false;
}

if (!RuntimeInformation.IsWindows())
{
logger.WriteLineError($"Classic .NET toolchain is supported only for Windows, benchmark '{benchmark.DisplayInfo}' will not be executed");
return false;
}

if (!HostEnvironmentInfo.GetCurrent().IsDotNetCliInstalled())
{
logger.WriteLineError($"BenchmarkDotNet requires dotnet cli toolchain to be installed, benchmark '{benchmark.DisplayInfo}' will not be executed");
Expand All @@ -58,26 +65,32 @@ public override bool IsSupported(Benchmark benchmark, ILogger logger, IResolver

private static IToolchain GetCurrentVersion()
{
try
if (!RuntimeInformation.IsWindows())
return Net46; // we return .NET 4.6 which during validaiton will tell the user about lack of support

return GetCurrentVersionBasedOnWindowsRegistry();
}

// this logic is put to a separate method to avoid any assembly loading issues on non Windows systems
private static IToolchain GetCurrentVersionBasedOnWindowsRegistry()
{
using (var ndpKey = Microsoft.Win32.RegistryKey
.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, Microsoft.Win32.RegistryView.Registry32)
.OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full\\"))
{
using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32)
.OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full\\"))
{
int releaseKey = Convert.ToInt32(ndpKey.GetValue("Release"));
// magic! https://msdn.microsoft.com/en-us/library/hh925568(v=vs.110).aspx
if (releaseKey >= 460798)
return Net47;
if (releaseKey >= 394802)
return Net462;
if (releaseKey >= 394254)
return Net461;
if (ndpKey == null)
return Default;

int releaseKey = Convert.ToInt32(ndpKey.GetValue("Release"));
// magic numbers come from https://msdn.microsoft.com/en-us/library/hh925568(v=vs.110).aspx
if (releaseKey >= 460798)
return Net47;
if (releaseKey >= 394802)
return Net462;
if (releaseKey >= 394254)
return Net461;
}
}
catch (Exception e)
{
throw new Exception("You need to run on Windows with .NET 4.6+ installed", e);

return Default;
}
}
}
Expand Down

0 comments on commit 5513873

Please sign in to comment.