-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
env_generic.py
374 lines (319 loc) · 11.9 KB
/
env_generic.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
from collections import OrderedDict
from configparser import ConfigParser
import locale
from pathlib import Path
import shutil
import subprocess
import tempfile
from typing import Callable, Optional, Mapping, Sequence
from . import winenv
from .machine_file import strv_to_meson
from .machine_spec import MachineSpec
def init_machine_config(machine: MachineSpec,
build_machine: MachineSpec,
is_cross_build: bool,
environ: dict[str, str],
toolchain_prefix: Optional[Path],
sdk_prefix: Optional[Path],
call_selected_meson: Callable,
config: ConfigParser,
outpath: list[str],
outenv: dict[str, str],
outdir: Path):
allow_undefined_symbols = machine.os == "freebsd"
options = config["built-in options"]
options["c_args"] = "c_like_flags"
options["cpp_args"] = "c_like_flags + cxx_like_flags"
options["c_link_args"] = "linker_flags"
options["cpp_link_args"] = "linker_flags + cxx_link_flags"
options["b_lundef"] = str(not allow_undefined_symbols).lower()
binaries = config["binaries"]
cc = None
common_flags = []
c_like_flags = []
linker_flags = []
cxx_like_flags = []
cxx_link_flags = []
triplet = machine.triplet
if triplet is not None:
try:
cc, gcc_binaries = resolve_gcc_binaries(toolprefix=triplet + "-")
binaries.update(gcc_binaries)
except CompilerNotFoundError:
pass
diagnostics = None
if cc is None:
with tempfile.TemporaryDirectory() as raw_prober_dir:
prober_dir = Path(raw_prober_dir)
machine_file = prober_dir / "machine.txt"
argv = [
"env2mfile",
"-o", machine_file,
"--native" if machine == build_machine else "--cross",
]
if machine != build_machine:
argv += [
"--system", machine.system,
"--subsystem", machine.subsystem,
"--kernel", machine.kernel,
"--cpu-family", machine.cpu_family,
"--cpu", machine.cpu,
"--endian", machine.endian,
]
process = call_selected_meson(argv,
cwd=raw_prober_dir,
env=environ,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
encoding=locale.getpreferredencoding())
if process.returncode == 0:
mcfg = ConfigParser()
mcfg.read(machine_file)
for section in mcfg.sections():
copy = config[section] if section in config else OrderedDict()
for key, val in mcfg.items(section):
if section == "binaries":
argv = eval(val.replace("\\", "\\\\"))
if not Path(argv[0]).is_absolute():
path = shutil.which(argv[0])
if path is None:
raise BinaryNotFoundError(f"unable to locate {argv[0]}")
argv[0] = path
val = strv_to_meson(argv)
if key in {"c", "cpp"}:
val += " + common_flags"
if key in copy and section == "built-in options" and key.endswith("_args"):
val = val + " + " + copy[key]
copy[key] = val
config[section] = copy
raw_cc = binaries.get("c", None)
if raw_cc is not None:
cc = eval(raw_cc.replace("\\", "\\\\"), None, {"common_flags": []})
else:
diagnostics = process.stdout
linker_flavor = None
if cc is not None \
and machine.os == "windows" \
and machine.toolchain_is_msvc:
linker_flavor = detect_linker_flavor(cc)
detected_wrong_toolchain = linker_flavor != "msvc"
if detected_wrong_toolchain:
cc = None
linker_flavor = None
if cc is None:
if machine.os == "windows":
detect_tool_path = lambda name: winenv.detect_msvs_tool_path(machine, build_machine, name, toolchain_prefix)
cc = [str(detect_tool_path("cl.exe"))]
lib = [str(detect_tool_path("lib.exe"))]
link = [str(detect_tool_path("link.exe"))]
assembler_name = MSVC_ASSEMBLER_NAMES[machine.arch]
assembler_tool = [str(detect_tool_path(assembler_name + ".exe"))]
raw_cc = strv_to_meson(cc) + " + common_flags"
binaries["c"] = raw_cc
binaries["cpp"] = raw_cc
binaries["lib"] = strv_to_meson(lib) + " + common_flags"
binaries["link"] = strv_to_meson(link) + " + common_flags"
binaries[assembler_name] = strv_to_meson(assembler_tool) + " + common_flags"
runtime_dirs = winenv.detect_msvs_runtime_path(machine, build_machine, toolchain_prefix)
outpath.extend(runtime_dirs)
vs_dir = winenv.detect_msvs_installation_dir(toolchain_prefix)
outenv["VSINSTALLDIR"] = str(vs_dir) + "\\"
outenv["VCINSTALLDIR"] = str(vs_dir / "VC") + "\\"
outenv["Platform"] = machine.msvc_platform
outenv["INCLUDE"] = ";".join([str(path) for path in winenv.detect_msvs_include_path(toolchain_prefix)])
outenv["LIB"] = ";".join([str(path) for path in winenv.detect_msvs_library_path(machine, toolchain_prefix)])
elif machine != build_machine \
and "CC" not in environ \
and "CFLAGS" not in environ \
and machine.os == build_machine.os \
and machine.os == "linux" \
and machine.pointer_size == 4 \
and build_machine.pointer_size == 8:
try:
cc, gcc_binaries = resolve_gcc_binaries()
binaries.update(gcc_binaries)
common_flags += ["-m32"]
except CompilerNotFoundError:
pass
if cc is None:
suffix = ":\n" + diagnostics if diagnostics is not None else ""
raise CompilerNotFoundError("no C compiler found" + suffix)
if "cpp" not in binaries:
raise CompilerNotFoundError("no C++ compiler found")
if linker_flavor is None:
linker_flavor = detect_linker_flavor(cc)
strip_binary = binaries.get("strip", None)
if strip_binary is not None:
strip_arg = "-Sx" if linker_flavor == "apple" else "--strip-all"
binaries["strip"] = strip_binary[:-1] + f", '{strip_arg}']"
if linker_flavor == "msvc":
for gnu_tool in ["ar", "as", "ld", "nm", "objcopy", "objdump",
"ranlib", "readelf", "size", "strip", "windres"]:
binaries.pop(gnu_tool, None)
c_like_flags += [
"/GS-",
"/Gy",
"/Zc:inline",
"/fp:fast",
]
if machine.arch == "x86":
c_like_flags += ["/arch:SSE2"]
# Relax C++11 compliance for XP compatibility.
cxx_like_flags += ["/Zc:threadSafeInit-"]
else:
if machine.os == "qnx":
common_flags += ARCH_COMMON_FLAGS_QNX.get(machine.arch, [])
else:
common_flags += ARCH_COMMON_FLAGS_UNIX.get(machine.arch, [])
c_like_flags += ARCH_C_LIKE_FLAGS_UNIX.get(machine.arch, [])
c_like_flags += [
"-ffunction-sections",
"-fdata-sections",
]
if linker_flavor.startswith("gnu-"):
linker_flags += ["-static-libgcc"]
if machine.os != "windows":
linker_flags += [
"-Wl,-z,relro",
"-Wl,-z,noexecstack",
]
cxx_link_flags += ["-static-libstdc++"]
if linker_flavor == "apple":
linker_flags += ["-Wl,-dead_strip"]
else:
linker_flags += ["-Wl,--gc-sections"]
if linker_flavor == "gnu-gold":
linker_flags += ["-Wl,--icf=all"]
constants = config["constants"]
constants["common_flags"] = strv_to_meson(common_flags)
constants["c_like_flags"] = strv_to_meson(c_like_flags)
constants["linker_flags"] = strv_to_meson(linker_flags)
constants["cxx_like_flags"] = strv_to_meson(cxx_like_flags)
constants["cxx_link_flags"] = strv_to_meson(cxx_link_flags)
def resolve_gcc_binaries(toolprefix: str = "") -> tuple[list[str], dict[str, str]]:
cc = None
binaries = OrderedDict()
for identifier in GCC_TOOL_IDS:
name = GCC_TOOL_NAMES.get(identifier, identifier)
full_name = toolprefix + name
val = shutil.which(full_name)
if val is None:
raise CompilerNotFoundError(f"missing {full_name}")
# QNX SDP 6.5 gcc-* tools are broken, erroring out with:
# > sorry - this program has been built without plugin support
# We detect this and use the tool without the gcc-* prefix.
if name.startswith("gcc-"):
p = subprocess.run([val, "--version"], capture_output=True)
if p.returncode != 0:
full_name = toolprefix + name[4:]
val = shutil.which(full_name)
if val is None:
raise CompilerNotFoundError(f"missing {full_name}")
if identifier == "c":
cc = [val]
extra = " + common_flags" if identifier in {"c", "cpp"} else ""
binaries[identifier] = strv_to_meson([val]) + extra
return (cc, binaries)
def detect_linker_flavor(cc: list[str]) -> str:
linker_version = subprocess.run(cc + ["-Wl,--version"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
encoding=locale.getpreferredencoding()).stdout
if "Microsoft " in linker_version:
return "msvc"
if "GNU ld " in linker_version:
return "gnu-ld"
if "GNU gold " in linker_version:
return "gnu-gold"
if linker_version.startswith("LLD "):
return "lld"
if linker_version.startswith("ld: "):
return "apple"
excerpt = linker_version.split("\n")[0].rstrip()
raise LinkerDetectionError(f"unknown linker: '{excerpt}'")
class CompilerNotFoundError(Exception):
pass
class BinaryNotFoundError(Exception):
pass
class LinkerDetectionError(Exception):
pass
ARCH_COMMON_FLAGS_UNIX = {
"x86": [
"-march=pentium4",
],
"arm": [
"-march=armv5t",
],
"armbe8": [
"-march=armv6",
"-mbe8",
],
"armhf": [
"-march=armv7-a",
],
"arm64": [
"-march=armv8-a",
],
"mips": [
"-march=mips1",
"-mfp32",
],
"mipsel": [
"-march=mips1",
"-mfp32",
],
"mips64": [
"-march=mips64r2",
"-mabi=64",
],
"mips64el": [
"-march=mips64r2",
"-mabi=64",
],
"s390x": [
"-march=z10",
"-m64",
],
}
ARCH_COMMON_FLAGS_QNX = {
"x86": [
"-march=i686",
],
"arm": [
"-march=armv6",
"-mno-unaligned-access",
],
"armeabi": [
"-march=armv7-a",
"-mno-unaligned-access",
],
}
ARCH_C_LIKE_FLAGS_UNIX = {
"x86": [
"-mfpmath=sse",
"-mstackrealign",
],
}
GCC_TOOL_IDS = [
"c",
"cpp",
"ar",
"nm",
"ranlib",
"strip",
"readelf",
"objcopy",
"objdump",
]
GCC_TOOL_NAMES = {
"c": "gcc",
"cpp": "g++",
"ar": "gcc-ar",
"nm": "gcc-nm",
"ranlib": "gcc-ranlib",
}
MSVC_ASSEMBLER_NAMES = {
"x86": "ml",
"x86_64": "ml64",
"arm64": "armasm64",
}