[corlib] Simplify RuntimeInformation by getting OS name from the runtime (#13164)
[mono-project.git] / mcs / class / corlib / System.Runtime.InteropServices.RuntimeInformation / RuntimeInformation.cs
blob8e2c82b19efa0bfe8449835d71d66b97316d20da
1 //
2 // RuntimeInformation.cs
3 //
4 // Author:
5 // Alexander Köplinger (alexander.koeplinger@xamarin.com)
6 //
7 // (C) 2016 Xamarin, Inc.
8 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.IO;
32 using System.Reflection;
33 using System.Runtime.CompilerServices;
35 namespace System.Runtime.InteropServices
37 public static class RuntimeInformation
39 static readonly Architecture _osArchitecture;
40 static readonly Architecture _processArchitecture;
41 static readonly OSPlatform _osPlatform;
43 static RuntimeInformation ()
45 // we can use the runtime's compiled config options for DllMaps here
46 // process architecure for us is runtime architecture
47 // see for values: mono-config.c
48 var runtimeArchitecture = GetRuntimeArchitecture ();
49 var osName = GetOSName ();
51 // check OS/process architecture
52 switch (runtimeArchitecture) {
53 case "arm":
54 _osArchitecture = Environment.Is64BitOperatingSystem ? Architecture.Arm64 : Architecture.Arm;
55 _processArchitecture = Architecture.Arm;
56 break;
57 case "armv8":
58 _osArchitecture = Environment.Is64BitOperatingSystem ? Architecture.Arm64 : Architecture.Arm;
59 _processArchitecture = Architecture.Arm64;
60 break;
61 case "x86":
62 _osArchitecture = Environment.Is64BitOperatingSystem ? Architecture.X64 : Architecture.X86;
63 _processArchitecture = Architecture.X86;
64 break;
65 case "x86-64":
66 _osArchitecture = Environment.Is64BitOperatingSystem ? Architecture.X64 : Architecture.X86;
67 _processArchitecture = Architecture.X64;
68 break;
69 // upstream only has these values; try to pretend we're x86 if nothing matches
70 // want more? bug: https://github.com/dotnet/corefx/issues/30706
71 default:
72 _osArchitecture = Environment.Is64BitOperatingSystem ? Architecture.X64 : Architecture.X86;
73 _processArchitecture = Environment.Is64BitProcess ? Architecture.X64 : Architecture.X86;
74 break;
77 // check OS platform
78 switch (osName) {
79 case "linux":
80 _osPlatform = OSPlatform.Linux;
81 break;
82 case "osx":
83 _osPlatform = OSPlatform.OSX;
84 break;
85 case "windows":
86 _osPlatform = OSPlatform.Windows;
87 break;
88 case "solaris":
89 _osPlatform = OSPlatform.Create ("SOLARIS");
90 break;
91 case "freebsd":
92 _osPlatform = OSPlatform.Create ("FREEBSD");
93 break;
94 case "netbsd":
95 _osPlatform = OSPlatform.Create ("NETBSD");
96 break;
97 case "openbsd":
98 _osPlatform = OSPlatform.Create ("OPENBSD");
99 break;
100 case "aix":
101 _osPlatform = OSPlatform.Create ("AIX");
102 break;
103 case "hpux":
104 _osPlatform = OSPlatform.Create ("HPUX");
105 break;
106 case "haiku":
107 _osPlatform = OSPlatform.Create ("HAIKU");
108 break;
109 case "wasm":
110 _osPlatform = OSPlatform.Create ("WEBASSEMBLY");
111 break;
112 default:
113 _osPlatform = OSPlatform.Create ("UNKNOWN");
114 break;
118 /* gets the runtime's arch from the value it uses for DllMap */
119 [MethodImpl (MethodImplOptions.InternalCall)]
120 static extern string GetRuntimeArchitecture ();
122 /* gets the runtime's OS from the value it uses for DllMap */
123 [MethodImpl (MethodImplOptions.InternalCall)]
124 static extern string GetOSName ();
126 public static string FrameworkDescription {
127 get {
128 return "Mono " + Mono.Runtime.GetDisplayName ();
132 public static bool IsOSPlatform (OSPlatform osPlatform)
134 return _osPlatform == osPlatform;
137 public static string OSDescription
141 #if WASM
142 return "web"; //yes, hardcoded as right now we don't really support other environments
143 #else
144 return Environment.OSVersion.VersionString;
145 #endif
149 public static Architecture OSArchitecture
153 return _osArchitecture;
157 public static Architecture ProcessArchitecture
161 return _processArchitecture;