Skip to content
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
10 changes: 10 additions & 0 deletions src/BenchmarkDotNet/BenchmarkDotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,14 @@
<ItemGroup>
<Compile Include="..\BenchmarkDotNet.Disassembler.x64\DataContracts.cs" Link="Disassemblers\DataContracts.cs" />
</ItemGroup>
<!-- Settings for PolySharp -->
<PropertyGroup>
<PolySharpIncludeRuntimeSupportedAttributes>true</PolySharpIncludeRuntimeSupportedAttributes>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="PolySharp" Version="1.15.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
9 changes: 3 additions & 6 deletions src/BenchmarkDotNet/Detectors/Cpu/Windows/MosCpuDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Runtime.Versioning;
using BenchmarkDotNet.Extensions;
using BenchmarkDotNet.Portability;
using Perfolizer.Horology;
Expand All @@ -11,16 +12,12 @@ namespace BenchmarkDotNet.Detectors.Cpu.Windows;

internal class MosCpuDetector : ICpuDetector
{
#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
[SupportedOSPlatform("windows")]
public bool IsApplicable() => OsDetector.IsWindows() &&
RuntimeInformation.IsFullFramework &&
!RuntimeInformation.IsMono;

#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
[SupportedOSPlatform("windows")]
public CpuInfo? Detect()
{
if (!IsApplicable()) return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ internal class PowershellWmiCpuDetector : ICpuDetector

public bool IsApplicable() => OsDetector.IsWindows();

#if NET6_0_OR_GREATER
[SupportedOSPlatform("windows")]
#endif
public CpuInfo? Detect()
{
if (!IsApplicable()) return null;
Expand Down
26 changes: 8 additions & 18 deletions src/BenchmarkDotNet/Detectors/OsDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Perfolizer.Models;
using static System.Runtime.InteropServices.RuntimeInformation;
using RuntimeEnvironment = Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment;
using System.Runtime.Versioning;

namespace BenchmarkDotNet.Detectors;

Expand Down Expand Up @@ -113,29 +114,24 @@ private static bool IsUnderWsl()
return null;
}

#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatformGuard("windows")]
#endif
[SupportedOSPlatformGuard("windows")]
internal static bool IsWindows() =>
#if NET6_0_OR_GREATER
OperatingSystem.IsWindows(); // prefer linker-friendly OperatingSystem APIs
#else
IsOSPlatform(OSPlatform.Windows);
#endif

#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatformGuard("linux")]
#endif

[SupportedOSPlatformGuard("linux")]
internal static bool IsLinux() =>
#if NET6_0_OR_GREATER
OperatingSystem.IsLinux();
#else
IsOSPlatform(OSPlatform.Linux);
#endif

#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatformGuard("macos")]
#endif
[SupportedOSPlatformGuard("macos")]
// ReSharper disable once InconsistentNaming
internal static bool IsMacOS() =>
#if NET6_0_OR_GREATER
Expand All @@ -144,19 +140,15 @@ internal static bool IsMacOS() =>
IsOSPlatform(OSPlatform.OSX);
#endif

#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatformGuard("android")]
#endif
[SupportedOSPlatformGuard("android")]
internal static bool IsAndroid() =>
#if NET6_0_OR_GREATER
OperatingSystem.IsAndroid();
#else
Type.GetType("Java.Lang.Object, Mono.Android") != null;
#endif

#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatformGuard("ios")]
#endif
[SupportedOSPlatformGuard("ios")]
// ReSharper disable once InconsistentNaming
internal static bool IsIOS() =>
#if NET6_0_OR_GREATER
Expand All @@ -165,9 +157,7 @@ internal static bool IsIOS() =>
Type.GetType("Foundation.NSObject, Xamarin.iOS") != null;
#endif

#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatformGuard("tvos")]
#endif
[SupportedOSPlatformGuard("tvos")]
// ReSharper disable once InconsistentNaming
internal static bool IsTvOS() =>
#if NET6_0_OR_GREATER
Expand Down
5 changes: 0 additions & 5 deletions src/BenchmarkDotNet/Helpers/FrameworkVersionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ internal static string MapToReleaseVersion(string servicingVersion)
return "4.8.1"; // most probably the last major release of Full .NET Framework
}


#if NET6_0_OR_GREATER
[SupportedOSPlatform("windows")]
#endif
private static int? GetReleaseNumberFromWindowsRegistry()
{
using var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
Expand All @@ -96,9 +93,7 @@ internal static string MapToReleaseVersion(string servicingVersion)
return Convert.ToInt32(ndpKey.GetValue("Release"));
}

#if NET6_0_OR_GREATER
[SupportedOSPlatform("windows")]
#endif
internal static string? GetLatestNetDeveloperPackVersion()
{
if (GetReleaseNumberFromWindowsRegistry() is not int releaseNumber)
Expand Down
2 changes: 0 additions & 2 deletions src/BenchmarkDotNet/Helpers/PowerShellLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ internal class PowerShellLocator
$"{Environment.SystemDirectory}{Path.DirectorySeparatorChar}WindowsPowerShell{Path.DirectorySeparatorChar}" +
$"v1.0{Path.DirectorySeparatorChar}powershell.exe";

#if NET6_0_OR_GREATER
[SupportedOSPlatform("windows")]
#endif
internal static string? LocateOnWindows()
{
if (OsDetector.IsWindows() == false)
Expand Down
3 changes: 2 additions & 1 deletion src/BenchmarkDotNet/Portability/RuntimeInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Management;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;
using BenchmarkDotNet.Detectors;
using BenchmarkDotNet.Environments;
Expand Down Expand Up @@ -42,8 +43,8 @@ internal static class RuntimeInformation
FrameworkDescription.StartsWith(".NET Framework", StringComparison.OrdinalIgnoreCase);
#endif

[SupportedOSPlatformGuard("browser")]
#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatformGuard("browser")]
public static readonly bool IsWasm = OperatingSystem.IsBrowser();
#else
public static readonly bool IsWasm = IsOSPlatform(OSPlatform.Create("BROWSER"));
Expand Down
5 changes: 2 additions & 3 deletions src/BenchmarkDotNet/Running/ConsoleTitler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Runtime.Versioning;
using BenchmarkDotNet.Detectors;
using BenchmarkDotNet.Helpers;

Expand Down Expand Up @@ -60,9 +61,7 @@ public ConsoleTitler(string initialTitle)
}
}

#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatformGuard("windows")]
#endif
[SupportedOSPlatformGuard("windows")]
private static bool PlatformSupportsTitleRead() => OsDetector.IsWindows();

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ namespace BenchmarkDotNet.Toolchains.InProcess.NoEmit
/// </summary>
internal class InProcessNoEmitRunner
{
#if NET6_0_OR_GREATER
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(Runnable))]
#endif
public static int Run(IHost host, BenchmarkCase benchmarkCase)
{
// the first thing to do is to let diagnosers hook in before anything happens
Expand Down
4 changes: 0 additions & 4 deletions tests/BenchmarkDotNet.IntegrationTests/WakeLockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ public class IgnoreWakeLock
[Benchmark] public void Sleep() { }
}

#if !NET462
[SupportedOSPlatform("windows")]
#endif
[TheoryEnvSpecific(EnvRequirement.WindowsOnly, EnvRequirement.NeedsPrivilegedProcess)]
[InlineData(typeof(Default), "SYSTEM")]
[InlineData(typeof(None), "")]
Expand Down Expand Up @@ -124,9 +122,7 @@ public class Default : Base { }
public class Base
{
[Benchmark]
#if !NET462
[SupportedOSPlatform("windows")]
#endif
public void SignalBenchmarkRunningAndWaitForGetPowerRequests()
{
using EventWaitHandle
Expand Down