-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
machine_spec.py
294 lines (239 loc) · 7.25 KB
/
machine_spec.py
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
from __future__ import annotations
from dataclasses import dataclass
import platform
import re
from typing import Optional
@dataclass
class MachineSpec:
os: str
arch: str
config: Optional[str] = None
triplet: Optional[str] = None
@staticmethod
def make_from_local_system() -> MachineSpec:
return MachineSpec(detect_os(), detect_arch())
@staticmethod
def parse(raw_spec: str) -> MachineSpec:
os = None
arch = None
config = None
triplet = None
tokens = raw_spec.split("-")
if len(tokens) in {3, 4}:
arch = tokens[0]
m = TARGET_TRIPLET_ARCH_PATTERN.match(arch)
if m is not None:
kernel = tokens[-2]
system = tokens[-1]
if kernel == "w64":
os = "windows"
elif kernel == "nto":
os = "qnx"
else:
os = kernel
if arch[0] == "i":
arch = "x86"
elif arch == "arm":
if system == "gnueabihf":
arch = "armhf"
elif os == "qnx" and system.endswith("eabi"):
arch = "armeabi"
elif arch == "aarch64":
arch = "arm64"
if system.startswith("musl"):
config = "musl"
elif kernel == "w64":
config = "mingw"
triplet = raw_spec
if os is None:
os, arch, *rest = tokens
if rest:
assert len(rest) == 1
config = rest[0]
return MachineSpec(os, arch, config, triplet)
def evolve(self,
os: Optional[str] = None,
arch: Optional[str] = None,
config: Optional[str] = None,
triplet: Optional[str] = None) -> MachineSpec:
return MachineSpec(
os if os is not None else self.os,
arch if arch is not None else self.arch,
config if config is not None else self.config,
triplet if triplet is not None else self.triplet,
)
def default_missing(self, recommended_vscrt: Optional[str] = None) -> MachineSpec:
config = self.config
if config is None and self.toolchain_is_msvc:
if recommended_vscrt is not None:
config = recommended_vscrt
else:
config = "mt"
return self.evolve(config=config)
def maybe_adapt_to_host(self, host_machine: MachineSpec) -> MachineSpec:
if self.identifier == host_machine.identifier and host_machine.triplet is not None:
return host_machine
if self.os == "windows":
if host_machine.arch in {"x86_64", "x86"}:
return host_machine
if self.arch == host_machine.arch:
return host_machine
return self
@property
def identifier(self) -> str:
parts = [self.os, self.arch]
if self.config is not None:
parts += [self.config]
return "-".join(parts)
@property
def os_dash_arch(self) -> str:
return f"{self.os}-{self.arch}"
@property
def os_dash_config(self) -> str:
parts = [self.os]
if self.config is not None:
parts += [self.config]
return "-".join(parts)
@property
def config_is_optimized(self) -> bool:
if self.toolchain_is_msvc:
return self.config in {"md", "mt"}
return True
@property
def meson_optimization_options(self) -> list[str]:
if self.config_is_optimized:
optimization = "s"
ndebug = "true"
else:
optimization = "0"
ndebug = "false"
return [
f"-Doptimization={optimization}",
f"-Db_ndebug={ndebug}",
]
@property
def executable_suffix(self) -> str:
return ".exe" if self.os == "windows" else ""
@property
def msvc_platform(self) -> str:
return "x64" if self.arch == "x86_64" else self.arch
@property
def is_apple(self) -> str:
return self.os in {"macos", "ios", "watchos", "tvos"}
@property
def system(self) -> str:
return "darwin" if self.is_apple else self.os
@property
def subsystem(self) -> str:
return self.os_dash_config if self.is_apple else self.os
@property
def kernel(self) -> str:
return KERNELS.get(self.os, self.os)
@property
def cpu_family(self) -> str:
arch = self.arch
return CPU_FAMILIES.get(arch, arch)
@property
def cpu(self) -> str:
arch = self.arch
mappings_to_search = [
CPU_TYPES_PER_OS_OVERRIDES.get(self.os, {}),
CPU_TYPES,
]
for m in mappings_to_search:
cpu = m.get(arch, None)
if cpu is not None:
return cpu
return arch
@property
def endian(self) -> str:
return "big" if self.arch in BIG_ENDIAN_ARCHS else "little"
@property
def pointer_size(self) -> int:
arch = self.arch
if arch in {"x86_64", "s390x"}:
return 8
if arch.startswith("arm64") or arch.startswith("mips64"):
return 8
return 4
@property
def libdatadir(self) -> str:
return "libdata" if self.os == "freebsd" else "lib"
@property
def toolchain_is_msvc(self) -> bool:
return self.os == "windows" and self.config != "mingw"
@property
def toolchain_can_strip(self) -> bool:
return not self.toolchain_is_msvc
def __eq__(self, other):
if isinstance(other, MachineSpec):
return other.identifier == self.identifier
return False
def detect_os() -> str:
os = platform.system().lower()
if os == "darwin":
os = "macos"
return os
def detect_arch() -> str:
arch = platform.machine().lower()
return ARCHS.get(arch, arch)
ARCHS = {
"amd64": "x86_64",
"armv7l": "armhf",
"aarch64": "arm64",
}
KERNELS = {
"windows": "nt",
"macos": "xnu",
"ios": "xnu",
"watchos": "xnu",
"tvos": "xnu",
"qnx": "nto",
}
CPU_FAMILIES = {
"armbe8": "arm",
"armeabi": "arm",
"armhf": "arm",
"arm64": "aarch64",
"arm64e": "aarch64",
"arm64eoabi": "aarch64",
"mipsel": "mips",
"mips64el": "mips64",
"powerpc": "ppc"
}
CPU_TYPES = {
"arm": "armv7",
"armbe8": "armv6",
"armhf": "armv7hf",
"armeabi": "armv7eabi",
"arm64": "aarch64",
"arm64e": "aarch64",
"arm64eoabi": "aarch64",
}
CPU_TYPES_PER_OS_OVERRIDES = {
"linux": {
"arm": "armv5t",
"armbe8": "armv6t",
"armhf": "armv7a",
"mips": "mips1",
"mipsel": "mips1",
"mips64": "mips64r2",
"mips64el": "mips64r2",
},
"android": {
"x86": "i686",
},
"qnx": {
"arm": "armv6",
"armeabi": "armv7",
},
}
BIG_ENDIAN_ARCHS = {
"armbe8",
"mips",
"mips64",
"ppc",
"ppc64",
"s390x",
}
TARGET_TRIPLET_ARCH_PATTERN = re.compile(r"^(i.86|x86_64|arm\w*|aarch64|mips\w*|powerpc|s390x)$")