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