-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
meson_configure.py
506 lines (419 loc) · 19.2 KB
/
meson_configure.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
import argparse
import os
from pathlib import Path
import pickle
import platform
import re
import shlex
import shutil
import subprocess
import sys
from typing import Any, Callable, Optional
RELENG_DIR = Path(__file__).resolve().parent
SCRIPTS_DIR = RELENG_DIR / "meson-scripts"
sys.path.insert(0, str(RELENG_DIR / "meson"))
import mesonbuild.interpreter
from mesonbuild.coredata import UserArrayOption, UserBooleanOption, \
UserComboOption, UserFeatureOption, UserOption, UserStringOption
from . import deps, env
from .machine_spec import MachineSpec
from .progress import ProgressCallback, print_progress
def main():
default_sourcedir = Path(sys.argv.pop(1))
sourcedir = Path(os.environ.get("MESON_SOURCE_ROOT", default_sourcedir)).resolve()
workdir = Path(os.getcwd())
if workdir == sourcedir:
default_builddir = sourcedir / "build"
else:
default_builddir = workdir
builddir = Path(os.environ.get("MESON_BUILD_ROOT", default_builddir)).resolve()
parser = argparse.ArgumentParser(prog="configure",
add_help=False)
opts = parser.add_argument_group(title="generic options")
opts.add_argument("-h", "--help",
help="show this help message and exit",
action="help")
opts.add_argument("--prefix",
help="install files in PREFIX",
metavar="PREFIX",
type=parse_prefix)
opts.add_argument("--build",
help="configure for building on BUILD",
metavar="BUILD",
type=MachineSpec.parse)
opts.add_argument("--host",
help="cross-compile to build binaries to run on HOST",
metavar="HOST",
type=MachineSpec.parse)
opts.add_argument("--enable-symbols",
help="build binaries with debug symbols included (default: disabled)",
action="store_true")
opts.add_argument("--enable-shared",
help="enable building shared libraries (default: disabled)",
action="store_true")
opts.add_argument("--with-meson",
help="which Meson implementation to use (default: internal)",
choices=["internal", "system"],
dest="meson",
default="internal")
opts.add_argument(f"--without-prebuilds",
help="do not make use of prebuilt bundles",
metavar="{" + ",".join(query_supported_bundle_types(include_wildcards=True)) + "}",
type=parse_bundle_type_set,
default=set())
opts.add_argument("extra_meson_options",
nargs="*",
help=argparse.SUPPRESS)
meson_options_file = sourcedir / "meson.options"
if not meson_options_file.exists():
meson_options_file = sourcedir / "meson_options.txt"
if meson_options_file.exists():
meson_group = parser.add_argument_group(title="project-specific options")
meson_opts = register_meson_options(meson_options_file, meson_group)
options = parser.parse_args()
if builddir.exists():
if (builddir / "build.ninja").exists():
print(f"Already configured. Wipe .{os.sep}{builddir.relative_to(workdir)} to reconfigure.",
file=sys.stderr)
sys.exit(1)
default_library = "shared" if options.enable_shared else "static"
allowed_prebuilds = set(query_supported_bundle_types(include_wildcards=False)) - options.without_prebuilds
try:
configure(sourcedir,
builddir,
options.prefix,
options.build,
options.host,
os.environ,
"included" if options.enable_symbols else "stripped",
default_library,
allowed_prebuilds,
options.meson,
collect_meson_options(options))
except Exception as e:
print(e, file=sys.stderr)
if isinstance(e, subprocess.CalledProcessError):
for label, data in [("Output", e.output),
("Stderr", e.stderr)]:
if data:
print(f"{label}:\n\t| " + "\n\t| ".join(data.strip().split("\n")), file=sys.stderr)
sys.exit(1)
def configure(sourcedir: Path,
builddir: Path,
prefix: Optional[str] = None,
build_machine: Optional[MachineSpec] = None,
host_machine: Optional[MachineSpec] = None,
environ: dict[str, str] = os.environ,
debug_symbols: str = "stripped",
default_library: str = "static",
allowed_prebuilds: set[str] = None,
meson: str = "internal",
extra_meson_options: list[str] = [],
call_meson: Callable = env.call_meson,
on_progress: ProgressCallback = print_progress):
if prefix is None:
prefix = env.detect_default_prefix()
project_vscrt = detect_project_vscrt(sourcedir)
if build_machine is None:
build_machine = MachineSpec.make_from_local_system()
build_machine = build_machine.default_missing(recommended_vscrt=project_vscrt)
if host_machine is None:
host_machine = build_machine
else:
host_machine = host_machine.default_missing(recommended_vscrt=project_vscrt)
if host_machine.os == "windows":
vs_arch = environ.get("VSCMD_ARG_TGT_ARCH")
if vs_arch == "x86":
host_machine = host_machine.evolve(arch=vs_arch)
build_machine = build_machine.maybe_adapt_to_host(host_machine)
if allowed_prebuilds is None:
allowed_prebuilds = set(query_supported_bundle_types(include_wildcards=False))
call_selected_meson = lambda argv, *args, **kwargs: call_meson(argv,
use_submodule=meson == "internal",
*args,
**kwargs)
meson_options = [
f"-Dprefix={prefix}",
f"-Ddefault_library={default_library}",
*host_machine.meson_optimization_options,
]
if debug_symbols == "stripped" and host_machine.toolchain_can_strip:
meson_options += ["-Dstrip=true"]
deps_dir = deps.detect_cache_dir(sourcedir)
allow_prebuilt_toolchain = "toolchain" in allowed_prebuilds
if allow_prebuilt_toolchain:
try:
toolchain_prefix, _ = deps.ensure_toolchain(build_machine, deps_dir, on_progress=on_progress)
except deps.BundleNotFoundError as e:
raise_toolchain_not_found(e)
else:
if project_depends_on_vala_compiler(sourcedir):
toolchain_prefix = deps.query_toolchain_prefix(build_machine, deps_dir)
vala_compiler = env.detect_toolchain_vala_compiler(toolchain_prefix, build_machine)
if vala_compiler is None:
build_vala_compiler(toolchain_prefix, deps_dir, call_selected_meson)
else:
toolchain_prefix = None
is_cross_build = host_machine != build_machine
build_sdk_prefix = None
required = {"sdk:build"}
if not is_cross_build:
required.add("sdk:host")
if allowed_prebuilds.issuperset(required):
try:
build_sdk_prefix, _ = deps.ensure_sdk(build_machine, deps_dir, on_progress=on_progress)
except deps.BundleNotFoundError as e:
raise_sdk_not_found(e, "build", build_machine)
host_sdk_prefix = None
if is_cross_build and "sdk:host" in allowed_prebuilds:
try:
host_sdk_prefix, _ = deps.ensure_sdk(host_machine, deps_dir, on_progress=on_progress)
except deps.BundleNotFoundError as e:
raise_sdk_not_found(e, "host", host_machine)
build_config, host_config = \
env.generate_machine_configs(build_machine,
host_machine,
environ,
toolchain_prefix,
build_sdk_prefix,
host_sdk_prefix,
call_selected_meson,
default_library,
builddir)
meson_options += [f"--native-file={build_config.machine_file}"]
if host_config is not build_config:
meson_options += [f"--cross-file={host_config.machine_file}"]
setup_env = host_config.make_merged_environment(environ)
setup_env["FRIDA_ALLOWED_PREBUILDS"] = ",".join(allowed_prebuilds)
call_selected_meson(["setup"] + meson_options + extra_meson_options + [builddir],
cwd=sourcedir,
env=setup_env,
check=True)
shutil.copy(SCRIPTS_DIR / "BSDmakefile", builddir)
(builddir / "Makefile").write_text(generate_out_of_tree_makefile(sourcedir), encoding="utf-8")
if platform.system() == "Windows":
(builddir / "make.bat").write_text(generate_out_of_tree_make_bat(sourcedir), encoding="utf-8")
(builddir / "frida-env.dat").write_bytes(pickle.dumps({
"meson": meson,
"build": build_config,
"host": host_config if host_config is not build_config else None,
"allowed_prebuilds": allowed_prebuilds,
"deps": deps_dir,
}))
def parse_prefix(raw_prefix: str) -> Path:
prefix = Path(raw_prefix)
if not prefix.is_absolute():
prefix = Path(os.getcwd()) / prefix
return prefix
def query_supported_bundle_types(include_wildcards: bool) -> list[str]:
for e in deps.Bundle:
identifier = e.name.lower()
if e == deps.Bundle.SDK:
if include_wildcards:
yield identifier
yield identifier + ":build"
yield identifier + ":host"
else:
yield identifier
def query_supported_bundle_type_values() -> list[deps.Bundle]:
return [e for e in deps.Bundle]
def parse_bundle_type_set(raw_array: str) -> list[str]:
supported_types = list(query_supported_bundle_types(include_wildcards=True))
result = set()
for element in raw_array.split(","):
bundle_type = element.strip()
if bundle_type not in supported_types:
pretty_choices = "', '".join(supported_types)
raise argparse.ArgumentTypeError(f"invalid bundle type: '{bundle_type}' (choose from '{pretty_choices}')")
if bundle_type == "sdk":
result.add("sdk:build")
result.add("sdk:host")
else:
result.add(bundle_type)
return result
def raise_toolchain_not_found(e: Exception):
raise ToolchainNotFoundError("\n".join([
f"Unable to download toolchain: {e}",
"",
"Specify --without-prebuilds=toolchain to only use tools on your PATH.",
"",
"Another option is to do what Frida's CI does:",
"",
" ./releng/deps.py build --bundle=toolchain",
"",
"This produces a tarball in ./deps which gets picked up if you retry `./configure`.",
"You may also want to make a backup of it for future reuse.",
]))
def raise_sdk_not_found(e: Exception, kind: str, machine: MachineSpec):
raise SDKNotFoundError("\n".join([
f"Unable to download SDK: {e}",
"",
f"Specify --without-prebuilds=sdk:{kind} to build dependencies from source code.",
"",
"Another option is to do what Frida's CI does:",
"",
f" ./releng/deps.py build --bundle=sdk --host={machine.identifier}",
"",
"This produces a tarball in ./deps which gets picked up if you retry `./configure`.",
"You may also want to make a backup of it for future reuse.",
]))
def generate_out_of_tree_makefile(sourcedir: Path) -> str:
m = ((SCRIPTS_DIR / "Makefile").read_text(encoding="utf-8")
.replace("sys.argv[1]", "r'" + str(RELENG_DIR.parent) + "'")
.replace('"$(shell pwd)"', shlex.quote(str(sourcedir)))
.replace("./build", "."))
return re.sub(r"git-submodules:.+?(?=\.PHONY:)", "", m, flags=re.MULTILINE | re.DOTALL)
def generate_out_of_tree_make_bat(sourcedir: Path) -> str:
m = ((SCRIPTS_DIR / "make.bat").read_text(encoding="utf-8")
.replace("sys.argv[1]", "r'" + str(RELENG_DIR.parent) + "'")
.replace('"%dp0%"', '"' + str(sourcedir) + '"')
.replace('.\\build', "\"%dp0%\""))
return re.sub(r"if not exist .+?(?=endlocal)", "", m, flags=re.MULTILINE | re.DOTALL)
def register_meson_options(meson_option_file: Path, group: argparse._ArgumentGroup):
interpreter = mesonbuild.optinterpreter.OptionInterpreter(subproject="")
interpreter.process(meson_option_file)
for key, opt in interpreter.options.items():
name = key.name
pretty_name = name.replace("_", "-")
if isinstance(opt, UserFeatureOption):
if opt.value != "enabled":
action = "enable"
value_to_set = "enabled"
else:
action = "disable"
value_to_set = "disabled"
group.add_argument(f"--{action}-{pretty_name}",
action="append_const",
const=f"-D{name}={value_to_set}",
dest="main_meson_options",
**parse_option_meta(name, action, opt))
if opt.value == "auto":
group.add_argument(f"--disable-{pretty_name}",
action="append_const",
const=f"-D{name}=disabled",
dest="main_meson_options",
**parse_option_meta(name, "disable", opt))
elif isinstance(opt, UserBooleanOption):
if not opt.value:
action = "enable"
value_to_set = "true"
else:
action = "disable"
value_to_set = "false"
group.add_argument(f"--{action}-{pretty_name}",
action="append_const",
const=f"-D{name}={value_to_set}",
dest="main_meson_options",
**parse_option_meta(name, action, opt))
elif isinstance(opt, UserComboOption):
group.add_argument(f"--with-{pretty_name}",
choices=opt.choices,
dest="meson_option:" + name,
**parse_option_meta(name, "with", opt))
elif isinstance(opt, UserArrayOption):
group.add_argument(f"--with-{pretty_name}",
dest="meson_option:" + name,
type=make_array_option_value_parser(opt),
**parse_option_meta(name, "with", opt))
else:
group.add_argument(f"--with-{pretty_name}",
dest="meson_option:" + name,
**parse_option_meta(name, "with", opt))
def parse_option_meta(name: str,
action: str,
opt: UserOption[Any]):
params = {}
if isinstance(opt, UserStringOption):
default_value = repr(opt.value)
metavar = name.upper()
elif isinstance(opt, UserArrayOption):
default_value = ",".join(opt.value)
metavar = "{" + ",".join(opt.choices) + "}"
elif isinstance(opt, UserComboOption):
default_value = opt.value
metavar = "{" + "|".join(opt.choices) + "}"
else:
default_value = str(opt.value).lower()
metavar = name.upper()
if not (isinstance(opt, UserFeatureOption) \
and opt.value == "auto" \
and action == "disable"):
text = f"{help_text_from_meson(opt.description)} (default: {default_value})"
if action == "disable":
text = "do not " + text
params["help"] = text
params["metavar"] = metavar
return params
def help_text_from_meson(description: str) -> str:
if description:
return description[0].lower() + description[1:]
return description
def collect_meson_options(options: argparse.Namespace) -> list[str]:
result = []
for raw_name, raw_val in vars(options).items():
if raw_val is None:
continue
if raw_name == "main_meson_options":
result += raw_val
if raw_name.startswith("meson_option:"):
name = raw_name[13:]
val = raw_val if isinstance(raw_val, str) else ",".join(raw_val)
result += [f"-D{name}={val}"]
result += options.extra_meson_options
return result
def make_array_option_value_parser(opt: UserOption[Any]) -> Callable[[str], list[str]]:
return lambda v: parse_array_option_value(v, opt)
def parse_array_option_value(v: str, opt: UserArrayOption) -> list[str]:
vals = [v.strip() for v in v.split(",")]
choices = opt.choices
for v in vals:
if v not in choices:
pretty_choices = "', '".join(choices)
raise argparse.ArgumentTypeError(f"invalid array value: '{v}' (choose from '{pretty_choices}')")
return vals
def detect_project_vscrt(sourcedir: Path) -> Optional[str]:
m = next(re.finditer(r"project\(([^)]+\))", read_meson_build(sourcedir)), None)
if m is not None:
project_args = m.group(1)
m = next(re.finditer("'b_vscrt=([^']+)'", project_args), None)
if m is not None:
return m.group(1)
return None
def project_depends_on_vala_compiler(sourcedir: Path) -> bool:
return "'vala'" in read_meson_build(sourcedir)
def read_meson_build(sourcedir: Path) -> str:
return (sourcedir / "meson.build").read_text(encoding="utf-8")
def build_vala_compiler(toolchain_prefix: Path, deps_dir: Path, call_selected_meson: Callable):
print("Building Vala compiler...", flush=True)
workdir = deps_dir / "src"
workdir.mkdir(parents=True, exist_ok=True)
git = lambda *args, **kwargs: subprocess.run(["git", *args],
**kwargs,
capture_output=True,
encoding="utf-8")
vala_checkout = workdir / "vala"
if vala_checkout.exists():
shutil.rmtree(vala_checkout)
vala_pkg = deps.load_dependency_parameters().packages["vala"]
deps.clone_shallow(vala_pkg, vala_checkout, git)
run_kwargs = {
"stdout": subprocess.PIPE,
"stderr": subprocess.STDOUT,
"encoding": "utf-8",
"check": True,
}
call_selected_meson([
"setup",
f"--prefix={toolchain_prefix}",
"-Doptimization=2",
"build",
],
cwd=vala_checkout,
**run_kwargs)
call_selected_meson(["install"],
cwd=vala_checkout / "build",
**run_kwargs)
class ToolchainNotFoundError(Exception):
pass
class SDKNotFoundError(Exception):
pass