From 8c296e556d1cd308ce63dc68f8673c2810ecb6e0 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Fri, 2 Feb 2024 08:04:01 -0800 Subject: [PATCH] Make PlatformConfiguration properties trimmable (#2717) --- .../Binding.Shared/PlatformConfiguration.cs | 64 +++++++++++-------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/binding/Binding.Shared/PlatformConfiguration.cs b/binding/Binding.Shared/PlatformConfiguration.cs index e3c3b8b481..7a1fbdf18f 100644 --- a/binding/Binding.Shared/PlatformConfiguration.cs +++ b/binding/Binding.Shared/PlatformConfiguration.cs @@ -18,42 +18,52 @@ public static class PlatformConfiguration { private const string LibCLibrary = "libc"; - public static bool IsUnix { get; } + public static bool IsUnix => IsMac || IsLinux; - public static bool IsWindows { get; } - - public static bool IsMac { get; } - - public static bool IsLinux { get; } - - public static bool IsArm { get; } - - public static bool Is64Bit { get; } + public static bool IsWindows { +#if WINDOWS_UWP + get => true; +#elif NET6_0_OR_GREATER + get => OperatingSystem.IsWindows (); +#else + get => RuntimeInformation.IsOSPlatform (OSPlatform.Windows); +#endif + } - static PlatformConfiguration () - { + public static bool IsMac { #if WINDOWS_UWP - IsMac = false; - IsLinux = false; - IsUnix = false; - IsWindows = true; - - var arch = Package.Current.Id.Architecture; - const ProcessorArchitecture arm64 = (ProcessorArchitecture)12; - IsArm = arch == ProcessorArchitecture.Arm || arch == arm64; + get => false; +#elif NET6_0_OR_GREATER + get => OperatingSystem.IsMacOS (); #else - IsMac = RuntimeInformation.IsOSPlatform (OSPlatform.OSX); - IsLinux = RuntimeInformation.IsOSPlatform (OSPlatform.Linux); - IsUnix = IsMac || IsLinux; - IsWindows = RuntimeInformation.IsOSPlatform (OSPlatform.Windows); + get => RuntimeInformation.IsOSPlatform (OSPlatform.OSX); +#endif + } - var arch = RuntimeInformation.ProcessArchitecture; - IsArm = arch == Architecture.Arm || arch == Architecture.Arm64; + public static bool IsLinux { +#if WINDOWS_UWP + get => false; +#elif NET6_0_OR_GREATER + get => OperatingSystem.IsLinux (); +#else + get => RuntimeInformation.IsOSPlatform (OSPlatform.Linux); #endif + } - Is64Bit = IntPtr.Size == 8; + public static bool IsArm { +#if WINDOWS_UWP + get { + var arch = Package.Current.Id.Architecture; + const ProcessorArchitecture arm64 = (ProcessorArchitecture)12; + return arch == ProcessorArchitecture.Arm || arch == arm64; + } +#else + get => RuntimeInformation.ProcessArchitecture is Architecture.Arm or Architecture.Arm64; +#endif } + public static bool Is64Bit => IntPtr.Size == 8; + private static string linuxFlavor; public static string LinuxFlavor