-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathenv.py
420 lines (339 loc) · 13.3 KB
/
env.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
from collections import OrderedDict
from configparser import ConfigParser
from dataclasses import dataclass
import os
from pathlib import Path
import platform
import pprint
import shlex
import shutil
import subprocess
import sys
from typing import Callable, Literal, Optional
from . import env_android, env_apple, env_generic, machine_file
from .machine_file import bool_to_meson, str_to_meson, strv_to_meson
from .machine_spec import MachineSpec
@dataclass
class MachineConfig:
machine_file: Path
binpath: list[Path]
environ: dict[str, str]
def make_merged_environment(self, source_environ: dict[str, str]) -> dict[str, str]:
menv = {**source_environ}
menv.update(self.environ)
if self.binpath:
old_path = menv.get("PATH", "")
old_dirs = old_path.split(os.pathsep) if old_path else []
menv["PATH"] = os.pathsep.join([str(p) for p in self.binpath] + old_dirs)
return menv
DefaultLibrary = Literal["shared", "static"]
def call_meson(argv, use_submodule, *args, **kwargs):
return subprocess.run(query_meson_entrypoint(use_submodule) + argv, *args, **kwargs)
def query_meson_entrypoint(use_submodule):
if use_submodule:
return [sys.executable, str(INTERNAL_MESON_ENTRYPOINT)]
return ["meson"]
def load_meson_config(machine: MachineSpec, flavor: str, build_dir: Path):
return machine_file.load(query_machine_file_path(machine, flavor, build_dir))
def query_machine_file_path(machine: MachineSpec, flavor: str, build_dir: Path) -> Path:
return build_dir / f"frida{flavor}-{machine.identifier}.txt"
def detect_default_prefix() -> Path:
if platform.system() == "Windows":
return Path(os.environ["ProgramFiles"]) / "Frida"
return Path("/usr/local")
def generate_machine_configs(build_machine: MachineSpec,
host_machine: MachineSpec,
environ: dict[str, str],
toolchain_prefix: Optional[Path],
build_sdk_prefix: Optional[Path],
host_sdk_prefix: Optional[Path],
call_selected_meson: Callable,
default_library: DefaultLibrary,
outdir: Path) -> tuple[MachineConfig, MachineConfig]:
is_cross_build = host_machine != build_machine
if is_cross_build:
build_environ = {build_envvar_to_host(k): v for k, v in environ.items() if k not in TOOLCHAIN_ENVVARS}
else:
build_environ = environ
build_config = \
generate_machine_config(build_machine,
build_machine,
is_cross_build,
build_environ,
toolchain_prefix,
build_sdk_prefix,
call_selected_meson,
default_library,
outdir)
if is_cross_build:
host_config = generate_machine_config(host_machine,
build_machine,
is_cross_build,
environ,
toolchain_prefix,
host_sdk_prefix,
call_selected_meson,
default_library,
outdir)
else:
host_config = build_config
return (build_config, host_config)
def generate_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,
default_library: DefaultLibrary,
outdir: Path) -> MachineConfig:
config = ConfigParser(dict_type=OrderedDict)
config["constants"] = OrderedDict()
config["binaries"] = OrderedDict()
config["built-in options"] = OrderedDict()
config["properties"] = OrderedDict()
config["host_machine"] = OrderedDict([
("system", str_to_meson(machine.system)),
("subsystem", str_to_meson(machine.subsystem)),
("kernel", str_to_meson(machine.kernel)),
("cpu_family", str_to_meson(machine.cpu_family)),
("cpu", str_to_meson(machine.cpu)),
("endian", str_to_meson(machine.endian)),
])
binaries = config["binaries"]
builtin_options = config["built-in options"]
properties = config["properties"]
outpath = []
outenv = OrderedDict()
outdir.mkdir(parents=True, exist_ok=True)
if machine.is_apple:
impl = env_apple
elif machine.os == "android":
impl = env_android
else:
impl = env_generic
impl.init_machine_config(machine,
build_machine,
is_cross_build,
environ,
toolchain_prefix,
sdk_prefix,
call_selected_meson,
config,
outpath,
outenv,
outdir)
if machine.toolchain_is_msvc:
builtin_options["b_vscrt"] = str_to_meson(machine.config)
pkg_config = None
vala_compiler = None
if toolchain_prefix is not None:
toolchain_bindir = toolchain_prefix / "bin"
exe_suffix = build_machine.executable_suffix
ninja_binary = toolchain_bindir / f"ninja{exe_suffix}"
if ninja_binary.exists():
outenv["NINJA"] = str(ninja_binary)
for (tool_name, filename_suffix) in {("gdbus-codegen", ""),
("gio-querymodules", exe_suffix),
("glib-compile-resources", exe_suffix),
("glib-compile-schemas", exe_suffix),
("glib-genmarshal", ""),
("glib-mkenums", ""),
("flex", exe_suffix),
("bison", exe_suffix),
("nasm", exe_suffix)}:
tool_path = toolchain_bindir / (tool_name + filename_suffix)
if tool_path.exists():
if tool_name == "bison":
outenv["BISON_PKGDATADIR"] = str(toolchain_prefix / "share" / "bison")
outenv["M4"] = str(toolchain_bindir / f"m4{exe_suffix}")
else:
tool_path = shutil.which(tool_name)
if tool_path is not None:
binaries[tool_name] = strv_to_meson([str(tool_path)])
pkg_config_binary = toolchain_bindir / f"pkg-config{exe_suffix}"
if not pkg_config_binary.exists():
pkg_config_binary = shutil.which("pkg-config")
if pkg_config_binary is not None:
pkg_config = [
str(pkg_config_binary),
]
if default_library == "static":
pkg_config += ["--static"]
if sdk_prefix is not None:
pkg_config += [f"--define-variable=frida_sdk_prefix={sdk_prefix}"]
binaries["pkg-config"] = strv_to_meson(pkg_config)
vala_compiler = detect_toolchain_vala_compiler(toolchain_prefix, build_machine)
pkg_config_path = shlex.split(environ.get("PKG_CONFIG_PATH", "").replace("\\", "\\\\"))
if sdk_prefix is not None:
builtin_options["vala_args"] = strv_to_meson([
"--vapidir=" + str(sdk_prefix / "share" / "vala" / "vapi")
])
pkg_config_path += [str(sdk_prefix / machine.libdatadir / "pkgconfig")]
sdk_bindir = sdk_prefix / "bin" / build_machine.os_dash_arch
if sdk_bindir.exists():
for f in sdk_bindir.iterdir():
binaries[f.stem] = strv_to_meson([str(f)])
if vala_compiler is not None:
valac, vapidir = vala_compiler
vala = [
str(valac),
f"--vapidir={vapidir}",
]
if pkg_config is not None:
wrapper = outdir / "frida-pkg-config.py"
wrapper.write_text(make_pkg_config_wrapper(pkg_config, pkg_config_path), encoding="utf-8")
vala += [f"--pkg-config={quote(sys.executable)} {quote(str(wrapper))}"]
binaries["vala"] = strv_to_meson(vala)
qmake6 = shutil.which("qmake6")
if qmake6 is not None:
binaries["qmake6"] = strv_to_meson([qmake6])
builtin_options["pkg_config_path"] = strv_to_meson(pkg_config_path)
needs_wrapper = needs_exe_wrapper(build_machine, machine, environ)
properties["needs_exe_wrapper"] = bool_to_meson(needs_wrapper)
if needs_wrapper:
wrapper = find_exe_wrapper(machine, environ)
if wrapper is not None:
binaries["exe_wrapper"] = strv_to_meson(wrapper)
machine_file = outdir / f"frida-{machine.identifier}.txt"
with machine_file.open("w", encoding="utf-8") as f:
config.write(f)
return MachineConfig(machine_file, outpath, outenv)
def needs_exe_wrapper(build_machine: MachineSpec,
host_machine: MachineSpec,
environ: dict[str, str]) -> bool:
return not can_run_host_binaries(build_machine, host_machine, environ)
def can_run_host_binaries(build_machine: MachineSpec,
host_machine: MachineSpec,
environ: dict[str, str]) -> bool:
if host_machine == build_machine:
return True
build_os = build_machine.os
build_arch = build_machine.arch
host_os = host_machine.os
host_arch = host_machine.arch
if host_os == build_os:
if build_os == "windows":
return build_arch == "arm64" or host_arch != "arm64"
if build_os == "macos":
if build_arch == "arm64" and host_arch == "x86_64":
return True
if build_os == "linux" and host_machine.config == build_machine.config:
if build_arch == "x86_64" and host_arch == "x86":
return True
return environ.get("FRIDA_CAN_RUN_HOST_BINARIES", "no") == "yes"
def find_exe_wrapper(machine: MachineSpec,
environ: dict[str, str]) -> Optional[list[str]]:
qemu_sysroot = environ.get("FRIDA_QEMU_SYSROOT")
if qemu_sysroot is None:
return None
qemu_flavor = "qemu-" + QEMU_ARCHS.get(machine.arch, machine.arch)
qemu_binary = shutil.which(qemu_flavor)
if qemu_binary is None:
raise QEMUNotFoundError(f"unable to find {qemu_flavor}, needed due to FRIDA_QEMU_SYSROOT being set")
return [qemu_binary, "-L", qemu_sysroot]
def make_pkg_config_wrapper(pkg_config: list[str], pkg_config_path: list[str]) -> str:
return "\n".join([
"import os",
"import subprocess",
"import sys",
"",
"args = [",
f" {pprint.pformat(pkg_config, indent=4)[1:-1]},",
" *sys.argv[1:],",
"]",
"env = {",
" **os.environ,",
f" 'PKG_CONFIG_PATH': {repr(os.pathsep.join(pkg_config_path))},",
"}",
f"p = subprocess.run(args, env=env)",
"sys.exit(p.returncode)"
])
def detect_toolchain_vala_compiler(toolchain_prefix: Path,
build_machine: MachineSpec) -> Optional[tuple[Path, Path]]:
datadir = next((toolchain_prefix / "share").glob("vala-*"), None)
if datadir is None:
return None
api_version = datadir.name.split("-", maxsplit=1)[1]
valac = toolchain_prefix / "bin" / f"valac-{api_version}{build_machine.executable_suffix}"
vapidir = datadir / "vapi"
return (valac, vapidir)
def build_envvar_to_host(name: str) -> str:
if name.endswith("_FOR_BUILD"):
return name[:-10]
return name
def quote(s: str) -> str:
if " " not in s:
return s
return "\"" + s.replace("\"", "\\\"") + "\""
class QEMUNotFoundError(Exception):
pass
INTERNAL_MESON_ENTRYPOINT = Path(__file__).resolve().parent / "meson" / "meson.py"
# Based on mesonbuild/envconfig.py and mesonbuild/compilers/compilers.py
TOOLCHAIN_ENVVARS = {
# Compilers
"CC",
"CXX",
"CSC",
"CYTHON",
"DC",
"FC",
"OBJC",
"OBJCXX",
"RUSTC",
"VALAC",
"NASM",
# Linkers
"CC_LD",
"CXX_LD",
"DC_LD",
"FC_LD",
"OBJC_LD",
"OBJCXX_LD",
"RUSTC_LD",
# Binutils
"AR",
"AS",
"LD",
"NM",
"OBJCOPY",
"OBJDUMP",
"RANLIB",
"READELF",
"SIZE",
"STRINGS",
"STRIP",
"WINDRES",
# Other tools
"CMAKE",
"QMAKE",
"PKG_CONFIG",
"PKG_CONFIG_PATH",
"MAKE",
"VAPIGEN",
"LLVM_CONFIG",
# Deprecated
"D_LD",
"F_LD",
"RUST_LD",
"OBJCPP_LD",
# Flags
"CFLAGS",
"CXXFLAGS",
"CUFLAGS",
"OBJCFLAGS",
"OBJCXXFLAGS",
"FFLAGS",
"DFLAGS",
"VALAFLAGS",
"RUSTFLAGS",
"CYTHONFLAGS",
"CSFLAGS",
"LDFLAGS",
}
QEMU_ARCHS = {
"armeabi": "arm",
"armhf": "arm",
"armbe8": "armeb",
"arm64": "aarch64",
}