Skip to content

Commit 9c1aa83

Browse files
authored
chore: Add PolySharp dev dependency and cleanup #if-#endif preprocessor (#2881)
* chore: add polysharp dev dependency * chore: cleanup code by polyfill that generated by polysharp * chore-use-dynamicdependency-polyfill
1 parent c89b4cc commit 9c1aa83

File tree

10 files changed

+25
-43
lines changed

10 files changed

+25
-43
lines changed

src/BenchmarkDotNet/BenchmarkDotNet.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,14 @@
5858
<ItemGroup>
5959
<Compile Include="..\BenchmarkDotNet.Disassembler.x64\DataContracts.cs" Link="Disassemblers\DataContracts.cs" />
6060
</ItemGroup>
61+
<!-- Settings for PolySharp -->
62+
<PropertyGroup>
63+
<PolySharpIncludeRuntimeSupportedAttributes>true</PolySharpIncludeRuntimeSupportedAttributes>
64+
</PropertyGroup>
65+
<ItemGroup>
66+
<PackageReference Include="PolySharp" Version="1.15.0">
67+
<PrivateAssets>all</PrivateAssets>
68+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
69+
</PackageReference>
70+
</ItemGroup>
6171
</Project>

src/BenchmarkDotNet/Detectors/Cpu/Windows/MosCpuDetector.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Management;
5+
using System.Runtime.Versioning;
56
using BenchmarkDotNet.Extensions;
67
using BenchmarkDotNet.Portability;
78
using Perfolizer.Horology;
@@ -11,16 +12,12 @@ namespace BenchmarkDotNet.Detectors.Cpu.Windows;
1112

1213
internal class MosCpuDetector : ICpuDetector
1314
{
14-
#if NET6_0_OR_GREATER
15-
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
16-
#endif
15+
[SupportedOSPlatform("windows")]
1716
public bool IsApplicable() => OsDetector.IsWindows() &&
1817
RuntimeInformation.IsFullFramework &&
1918
!RuntimeInformation.IsMono;
2019

21-
#if NET6_0_OR_GREATER
22-
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
23-
#endif
20+
[SupportedOSPlatform("windows")]
2421
public CpuInfo? Detect()
2522
{
2623
if (!IsApplicable()) return null;

src/BenchmarkDotNet/Detectors/Cpu/Windows/PowershellWmiCpuDetector.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ internal class PowershellWmiCpuDetector : ICpuDetector
2121

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

24-
#if NET6_0_OR_GREATER
2524
[SupportedOSPlatform("windows")]
26-
#endif
2725
public CpuInfo? Detect()
2826
{
2927
if (!IsApplicable()) return null;

src/BenchmarkDotNet/Detectors/OsDetector.cs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Perfolizer.Models;
99
using static System.Runtime.InteropServices.RuntimeInformation;
1010
using RuntimeEnvironment = Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment;
11+
using System.Runtime.Versioning;
1112

1213
namespace BenchmarkDotNet.Detectors;
1314

@@ -113,29 +114,24 @@ private static bool IsUnderWsl()
113114
return null;
114115
}
115116

116-
#if NET6_0_OR_GREATER
117-
[System.Runtime.Versioning.SupportedOSPlatformGuard("windows")]
118-
#endif
117+
[SupportedOSPlatformGuard("windows")]
119118
internal static bool IsWindows() =>
120119
#if NET6_0_OR_GREATER
121120
OperatingSystem.IsWindows(); // prefer linker-friendly OperatingSystem APIs
122121
#else
123122
IsOSPlatform(OSPlatform.Windows);
124123
#endif
125124

126-
#if NET6_0_OR_GREATER
127-
[System.Runtime.Versioning.SupportedOSPlatformGuard("linux")]
128-
#endif
125+
126+
[SupportedOSPlatformGuard("linux")]
129127
internal static bool IsLinux() =>
130128
#if NET6_0_OR_GREATER
131129
OperatingSystem.IsLinux();
132130
#else
133131
IsOSPlatform(OSPlatform.Linux);
134132
#endif
135133

136-
#if NET6_0_OR_GREATER
137-
[System.Runtime.Versioning.SupportedOSPlatformGuard("macos")]
138-
#endif
134+
[SupportedOSPlatformGuard("macos")]
139135
// ReSharper disable once InconsistentNaming
140136
internal static bool IsMacOS() =>
141137
#if NET6_0_OR_GREATER
@@ -144,19 +140,15 @@ internal static bool IsMacOS() =>
144140
IsOSPlatform(OSPlatform.OSX);
145141
#endif
146142

147-
#if NET6_0_OR_GREATER
148-
[System.Runtime.Versioning.SupportedOSPlatformGuard("android")]
149-
#endif
143+
[SupportedOSPlatformGuard("android")]
150144
internal static bool IsAndroid() =>
151145
#if NET6_0_OR_GREATER
152146
OperatingSystem.IsAndroid();
153147
#else
154148
Type.GetType("Java.Lang.Object, Mono.Android") != null;
155149
#endif
156150

157-
#if NET6_0_OR_GREATER
158-
[System.Runtime.Versioning.SupportedOSPlatformGuard("ios")]
159-
#endif
151+
[SupportedOSPlatformGuard("ios")]
160152
// ReSharper disable once InconsistentNaming
161153
internal static bool IsIOS() =>
162154
#if NET6_0_OR_GREATER
@@ -165,9 +157,7 @@ internal static bool IsIOS() =>
165157
Type.GetType("Foundation.NSObject, Xamarin.iOS") != null;
166158
#endif
167159

168-
#if NET6_0_OR_GREATER
169-
[System.Runtime.Versioning.SupportedOSPlatformGuard("tvos")]
170-
#endif
160+
[SupportedOSPlatformGuard("tvos")]
171161
// ReSharper disable once InconsistentNaming
172162
internal static bool IsTvOS() =>
173163
#if NET6_0_OR_GREATER

src/BenchmarkDotNet/Helpers/FrameworkVersionHelper.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,7 @@ internal static string MapToReleaseVersion(string servicingVersion)
8383
return "4.8.1"; // most probably the last major release of Full .NET Framework
8484
}
8585

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

99-
#if NET6_0_OR_GREATER
10096
[SupportedOSPlatform("windows")]
101-
#endif
10297
internal static string? GetLatestNetDeveloperPackVersion()
10398
{
10499
if (GetReleaseNumberFromWindowsRegistry() is not int releaseNumber)

src/BenchmarkDotNet/Helpers/PowerShellLocator.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ internal class PowerShellLocator
1616
$"{Environment.SystemDirectory}{Path.DirectorySeparatorChar}WindowsPowerShell{Path.DirectorySeparatorChar}" +
1717
$"v1.0{Path.DirectorySeparatorChar}powershell.exe";
1818

19-
#if NET6_0_OR_GREATER
2019
[SupportedOSPlatform("windows")]
21-
#endif
2220
internal static string? LocateOnWindows()
2321
{
2422
if (OsDetector.IsWindows() == false)

src/BenchmarkDotNet/Portability/RuntimeInformation.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Management;
77
using System.Reflection;
88
using System.Runtime.InteropServices;
9+
using System.Runtime.Versioning;
910
using System.Text.RegularExpressions;
1011
using BenchmarkDotNet.Detectors;
1112
using BenchmarkDotNet.Environments;
@@ -42,8 +43,8 @@ internal static class RuntimeInformation
4243
FrameworkDescription.StartsWith(".NET Framework", StringComparison.OrdinalIgnoreCase);
4344
#endif
4445

46+
[SupportedOSPlatformGuard("browser")]
4547
#if NET6_0_OR_GREATER
46-
[System.Runtime.Versioning.SupportedOSPlatformGuard("browser")]
4748
public static readonly bool IsWasm = OperatingSystem.IsBrowser();
4849
#else
4950
public static readonly bool IsWasm = IsOSPlatform(OSPlatform.Create("BROWSER"));

src/BenchmarkDotNet/Running/ConsoleTitler.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Runtime.Versioning;
34
using BenchmarkDotNet.Detectors;
45
using BenchmarkDotNet.Helpers;
56

@@ -60,9 +61,7 @@ public ConsoleTitler(string initialTitle)
6061
}
6162
}
6263

63-
#if NET6_0_OR_GREATER
64-
[System.Runtime.Versioning.SupportedOSPlatformGuard("windows")]
65-
#endif
64+
[SupportedOSPlatformGuard("windows")]
6665
private static bool PlatformSupportsTitleRead() => OsDetector.IsWindows();
6766

6867
/// <summary>

src/BenchmarkDotNet/Toolchains/InProcess/NoEmit/InProcessNoEmitRunner.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ namespace BenchmarkDotNet.Toolchains.InProcess.NoEmit
1616
/// </summary>
1717
internal class InProcessNoEmitRunner
1818
{
19-
#if NET6_0_OR_GREATER
2019
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(Runnable))]
21-
#endif
2220
public static int Run(IHost host, BenchmarkCase benchmarkCase)
2321
{
2422
// the first thing to do is to let diagnosers hook in before anything happens

tests/BenchmarkDotNet.IntegrationTests/WakeLockTests.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ public class IgnoreWakeLock
8585
[Benchmark] public void Sleep() { }
8686
}
8787

88-
#if !NET462
8988
[SupportedOSPlatform("windows")]
90-
#endif
9189
[TheoryEnvSpecific(EnvRequirement.WindowsOnly, EnvRequirement.NeedsPrivilegedProcess)]
9290
[InlineData(typeof(Default), "SYSTEM")]
9391
[InlineData(typeof(None), "")]
@@ -124,9 +122,7 @@ public class Default : Base { }
124122
public class Base
125123
{
126124
[Benchmark]
127-
#if !NET462
128125
[SupportedOSPlatform("windows")]
129-
#endif
130126
public void SignalBenchmarkRunningAndWaitForGetPowerRequests()
131127
{
132128
using EventWaitHandle

0 commit comments

Comments
 (0)