1
+ using System ;
2
+ using System . Runtime . InteropServices ;
3
+
1
4
namespace Python . Runtime . Platform
2
5
{
3
6
public enum MachineType
@@ -20,4 +23,170 @@ public enum OperatingSystemType
20
23
Linux ,
21
24
Other
22
25
}
26
+
27
+
28
+ static class SystemInfo
29
+ {
30
+ public static MachineType GetMachineType ( )
31
+ {
32
+ return Runtime . IsWindows ? GetMachineType_Windows ( ) : GetMachineType_Unix ( ) ;
33
+ }
34
+
35
+ public static string GetArchitecture ( )
36
+ {
37
+ return Runtime . IsWindows ? GetArchName_Windows ( ) : GetArchName_Unix ( ) ;
38
+ }
39
+
40
+ public static OperatingSystemType GetSystemType ( )
41
+ {
42
+ if ( Runtime . IsWindows )
43
+ {
44
+ return OperatingSystemType . Windows ;
45
+ }
46
+ switch ( PythonEngine . Platform )
47
+ {
48
+ case "linux" :
49
+ return OperatingSystemType . Linux ;
50
+
51
+ case "darwin" :
52
+ return OperatingSystemType . Darwin ;
53
+
54
+ default :
55
+ return OperatingSystemType . Other ;
56
+ }
57
+ }
58
+
59
+ #region WINDOWS
60
+
61
+ static string GetArchName_Windows ( )
62
+ {
63
+ // https://docs.microsoft.com/en-us/windows/win32/winprog64/wow64-implementation-details
64
+ return Environment . GetEnvironmentVariable ( "PROCESSOR_ARCHITECTURE" ) ;
65
+ }
66
+
67
+ static MachineType GetMachineType_Windows ( )
68
+ {
69
+ if ( Runtime . Is32Bit )
70
+ {
71
+ return MachineType . i386 ;
72
+ }
73
+ switch ( GetArchName_Windows ( ) )
74
+ {
75
+ case "AMD64" :
76
+ return MachineType . x86_64 ;
77
+ case "ARM64" :
78
+ return MachineType . aarch64 ;
79
+ default :
80
+ return MachineType . Other ;
81
+ }
82
+ }
83
+
84
+ #endregion
85
+
86
+ #region UNIX
87
+
88
+
89
+ [ StructLayout ( LayoutKind . Sequential ) ]
90
+ unsafe struct utsname_linux
91
+ {
92
+ const int NameLength = 65 ;
93
+
94
+ /* Name of the implementation of the operating system. */
95
+ public fixed byte sysname [ NameLength ] ;
96
+
97
+ /* Name of this node on the network. */
98
+ public fixed byte nodename [ NameLength ] ;
99
+
100
+ /* Current release level of this implementation. */
101
+ public fixed byte release [ NameLength ] ;
102
+
103
+ /* Current version level of this release. */
104
+ public fixed byte version [ NameLength ] ;
105
+
106
+ /* Name of the hardware type the system is running on. */
107
+ public fixed byte machine [ NameLength ] ;
108
+
109
+ // GNU extension
110
+ fixed byte domainname [ NameLength ] ; /* NIS or YP domain name */
111
+ }
112
+
113
+ [ StructLayout ( LayoutKind . Sequential ) ]
114
+ unsafe struct utsname_darwin
115
+ {
116
+ const int NameLength = 256 ;
117
+
118
+ /* Name of the implementation of the operating system. */
119
+ public fixed byte sysname [ NameLength ] ;
120
+
121
+ /* Name of this node on the network. */
122
+ public fixed byte nodename [ NameLength ] ;
123
+
124
+ /* Current release level of this implementation. */
125
+ public fixed byte release [ NameLength ] ;
126
+
127
+ /* Current version level of this release. */
128
+ public fixed byte version [ NameLength ] ;
129
+
130
+ /* Name of the hardware type the system is running on. */
131
+ public fixed byte machine [ NameLength ] ;
132
+ }
133
+
134
+ [ DllImport ( "libc" ) ]
135
+ static extern int uname ( IntPtr buf ) ;
136
+
137
+
138
+ static unsafe string GetArchName_Unix ( )
139
+ {
140
+ switch ( GetSystemType ( ) )
141
+ {
142
+ case OperatingSystemType . Linux :
143
+ {
144
+ var buf = stackalloc utsname_linux [ 1 ] ;
145
+ if ( uname ( ( IntPtr ) buf ) != 0 )
146
+ {
147
+ return null ;
148
+ }
149
+ return Marshal . PtrToStringAnsi ( ( IntPtr ) buf ->machine ) ;
150
+ }
151
+
152
+ case OperatingSystemType . Darwin :
153
+ {
154
+ var buf = stackalloc utsname_darwin [ 1 ] ;
155
+ if ( uname ( ( IntPtr ) buf ) != 0 )
156
+ {
157
+ return null ;
158
+ }
159
+ return Marshal . PtrToStringAnsi ( ( IntPtr ) buf ->machine ) ;
160
+ }
161
+
162
+ default :
163
+ return null ;
164
+ }
165
+ }
166
+
167
+ static unsafe MachineType GetMachineType_Unix ( )
168
+ {
169
+ switch ( GetArchName_Unix ( ) )
170
+ {
171
+ case "x86_64" :
172
+ case "em64t" :
173
+ return Runtime . Is32Bit ? MachineType . i386 : MachineType . x86_64 ;
174
+ case "i386" :
175
+ case "i686" :
176
+ return MachineType . i386 ;
177
+
178
+ case "armv7l" :
179
+ return MachineType . armv7l ;
180
+ case "armv8" :
181
+ return Runtime . Is32Bit ? MachineType . armv7l : MachineType . armv8 ;
182
+ case "aarch64" :
183
+ return Runtime . Is32Bit ? MachineType . armv7l : MachineType . aarch64 ;
184
+
185
+ default :
186
+ return MachineType . Other ;
187
+ }
188
+ }
189
+
190
+ #endregion
191
+ }
23
192
}
0 commit comments