| | | 1 | | using System.Runtime.InteropServices; |
| | | 2 | | |
| | | 3 | | namespace InferenceEngine.Core.Diagnostics; |
| | | 4 | | |
| | | 5 | | /// <summary>Identifies the host platform and architecture.</summary> |
| | | 6 | | public enum HostPlatform |
| | | 7 | | { |
| | | 8 | | Unknown = 0, |
| | | 9 | | LinuxX64, |
| | | 10 | | LinuxArm64, |
| | | 11 | | WinX64, |
| | | 12 | | WinArm64, |
| | | 13 | | OSX64, |
| | | 14 | | OSXArm64, |
| | | 15 | | } |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Detects the host platform at runtime. Provided for convenience; the primary |
| | | 19 | | /// API for platform information is <see cref="InferenceEngineInfo.Platform"/>. |
| | | 20 | | /// </summary> |
| | | 21 | | public static class HostPlatformDetector |
| | | 22 | | { |
| | | 23 | | // Test seam: the OS/architecture probes are injectable so tests can drive every |
| | | 24 | | // branch of Current() hermetically on any host (the real RuntimeInformation calls |
| | | 25 | | // answer for the host OS only, leaving the other branches permanently uncovered). |
| | | 26 | | // Internal and nullable-probe-free: production callers always go through Current(), |
| | | 27 | | // whose public surface is unchanged. Tests must restore the defaults afterwards. |
| | 222 | 28 | | internal static Func<string, bool> IsOSPlatformProbe { get; set; } = |
| | 170 | 29 | | static name => RuntimeInformation.IsOSPlatform(OSPlatform.Create(name)); |
| | 207 | 30 | | internal static Func<Architecture> ProcessArchitectureProbe { get; set; } = |
| | 170 | 31 | | static () => RuntimeInformation.ProcessArchitecture; |
| | | 32 | | |
| | | 33 | | /// <summary>Detects the current host platform. See <see cref="HostPlatform"/>.</summary> |
| | | 34 | | public static HostPlatform Current() |
| | | 35 | | { |
| | 183 | 36 | | if (IsOSPlatformProbe("Linux")) |
| | | 37 | | { |
| | 175 | 38 | | return ProcessArchitectureProbe() switch |
| | 175 | 39 | | { |
| | 172 | 40 | | Architecture.X64 => HostPlatform.LinuxX64, |
| | 1 | 41 | | Architecture.Arm64 => HostPlatform.LinuxArm64, |
| | 2 | 42 | | _ => HostPlatform.Unknown, |
| | 175 | 43 | | }; |
| | | 44 | | } |
| | 8 | 45 | | if (IsOSPlatformProbe("OSX")) |
| | | 46 | | { |
| | 3 | 47 | | return ProcessArchitectureProbe() switch |
| | 3 | 48 | | { |
| | 1 | 49 | | Architecture.X64 => HostPlatform.OSX64, |
| | 1 | 50 | | Architecture.Arm64 => HostPlatform.OSXArm64, |
| | 1 | 51 | | _ => HostPlatform.Unknown, |
| | 3 | 52 | | }; |
| | | 53 | | } |
| | 5 | 54 | | if (IsOSPlatformProbe("Windows")) |
| | | 55 | | { |
| | 3 | 56 | | return ProcessArchitectureProbe() switch |
| | 3 | 57 | | { |
| | 1 | 58 | | Architecture.X64 => HostPlatform.WinX64, |
| | 1 | 59 | | Architecture.Arm64 => HostPlatform.WinArm64, |
| | 1 | 60 | | _ => HostPlatform.Unknown, |
| | 3 | 61 | | }; |
| | | 62 | | } |
| | 2 | 63 | | return HostPlatform.Unknown; |
| | | 64 | | } |
| | | 65 | | } |