-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdeps.py
executable file
·1135 lines (905 loc) · 42.8 KB
/
deps.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import base64
from configparser import ConfigParser
import dataclasses
from dataclasses import dataclass, field
from enum import Enum
import graphlib
import itertools
import json
import os
from pathlib import Path
import re
import shlex
import shutil
import subprocess
import sys
import tarfile
import tempfile
import time
from typing import Callable, Iterator, Optional, Mapping, Sequence, Union
import urllib.request
RELENG_DIR = Path(__file__).resolve().parent
ROOT_DIR = RELENG_DIR.parent
if __name__ == "__main__":
# TODO: Refactor
sys.path.insert(0, str(ROOT_DIR))
sys.path.insert(0, str(RELENG_DIR / "tomlkit"))
from tomlkit.toml_file import TOMLFile
from releng import env
from releng.progress import Progress, ProgressCallback, print_progress
from releng.machine_spec import MachineSpec
def main():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
default_machine = MachineSpec.make_from_local_system().identifier
bundle_opt_kwargs = {
"help": "bundle (default: sdk)",
"type": parse_bundle_option_value,
}
machine_opt_kwargs = {
"help": f"os/arch (default: {default_machine})",
"type": MachineSpec.parse,
}
command = subparsers.add_parser("sync", help="ensure prebuilt dependencies are up-to-date")
command.add_argument("bundle", **bundle_opt_kwargs)
command.add_argument("host", **machine_opt_kwargs)
command.add_argument("location", help="filesystem location", type=Path)
command.set_defaults(func=lambda args: sync(args.bundle, args.host, args.location.resolve()))
command = subparsers.add_parser("roll", help="build and upload prebuilt dependencies if needed")
command.add_argument("bundle", **bundle_opt_kwargs)
command.add_argument("host", **machine_opt_kwargs)
command.add_argument("--build", default=default_machine, **machine_opt_kwargs)
command.add_argument("--activate", default=False, action='store_true')
command.add_argument("--post", help="post-processing script")
command.set_defaults(func=lambda args: roll(args.bundle, args.build, args.host, args.activate,
Path(args.post) if args.post is not None else None))
command = subparsers.add_parser("build", help="build prebuilt dependencies")
command.add_argument("--bundle", default=Bundle.SDK, **bundle_opt_kwargs)
command.add_argument("--build", default=default_machine, **machine_opt_kwargs)
command.add_argument("--host", default=default_machine, **machine_opt_kwargs)
command.add_argument("--only", help="only build packages A, B, and C", metavar="A,B,C",
type=parse_set_option_value)
command.add_argument("--exclude", help="exclude packages A, B, and C", metavar="A,B,C",
type=parse_set_option_value, default=set())
command.add_argument("-v", "--verbose", help="be verbose", action="store_true")
command.set_defaults(func=lambda args: build(args.bundle, args.build, args.host,
args.only, args.exclude, args.verbose))
command = subparsers.add_parser("wait", help="wait for prebuilt dependencies if needed")
command.add_argument("bundle", **bundle_opt_kwargs)
command.add_argument("host", **machine_opt_kwargs)
command.set_defaults(func=lambda args: wait(args.bundle, args.host))
command = subparsers.add_parser("bump", help="bump dependency versions")
command.set_defaults(func=lambda args: bump())
args = parser.parse_args()
if 'func' in args:
try:
args.func(args)
except CommandError as e:
print(e, file=sys.stderr)
sys.exit(1)
else:
parser.print_usage(file=sys.stderr)
sys.exit(1)
def parse_bundle_option_value(raw_bundle: str) -> Bundle:
try:
return Bundle[raw_bundle.upper()]
except KeyError:
choices = "', '".join([e.name.lower() for e in Bundle])
raise argparse.ArgumentTypeError(f"invalid choice: {raw_bundle} (choose from '{choices}')")
def parse_set_option_value(v: str) -> set[str]:
return set([v.strip() for v in v.split(",")])
def query_toolchain_prefix(machine: MachineSpec,
cache_dir: Path) -> Path:
if machine.os == "windows":
identifier = "windows-x86" if machine.arch in {"x86", "x86_64"} else machine.os_dash_arch
else:
identifier = machine.identifier
return cache_dir / f"toolchain-{identifier}"
def ensure_toolchain(machine: MachineSpec,
cache_dir: Path,
version: Optional[str] = None,
on_progress: ProgressCallback = print_progress) -> tuple[Path, SourceState]:
toolchain_prefix = query_toolchain_prefix(machine, cache_dir)
state = sync(Bundle.TOOLCHAIN, machine, toolchain_prefix, version, on_progress)
return (toolchain_prefix, state)
def query_sdk_prefix(machine: MachineSpec,
cache_dir: Path) -> Path:
return cache_dir / f"sdk-{machine.identifier}"
def ensure_sdk(machine: MachineSpec,
cache_dir: Path,
version: Optional[str] = None,
on_progress: ProgressCallback = print_progress) -> tuple[Path, SourceState]:
sdk_prefix = query_sdk_prefix(machine, cache_dir)
state = sync(Bundle.SDK, machine, sdk_prefix, version, on_progress)
return (sdk_prefix, state)
def detect_cache_dir(sourcedir: Path) -> Path:
raw_location = os.environ.get("FRIDA_DEPS", None)
if raw_location is not None:
location = Path(raw_location)
else:
location = sourcedir / "deps"
return location
def sync(bundle: Bundle,
machine: MachineSpec,
location: Path,
version: Optional[str] = None,
on_progress: ProgressCallback = print_progress) -> SourceState:
state = SourceState.PRISTINE
if version is None:
version = load_dependency_parameters().deps_version
bundle_nick = bundle.name.lower() if bundle != Bundle.SDK else bundle.name
if location.exists():
try:
cached_version = (location / "VERSION.txt").read_text(encoding="utf-8").strip()
if cached_version == version:
return state
except:
pass
shutil.rmtree(location)
state = SourceState.MODIFIED
(url, filename) = compute_bundle_parameters(bundle, machine, version)
local_bundle = location.parent / filename
if local_bundle.exists():
on_progress(Progress("Deploying local {}".format(bundle_nick)))
archive_path = local_bundle
archive_is_temporary = False
else:
if bundle == Bundle.SDK:
on_progress(Progress(f"Downloading SDK {version} for {machine.identifier}"))
else:
on_progress(Progress(f"Downloading {bundle_nick} {version}"))
try:
with urllib.request.urlopen(url) as response, \
tempfile.NamedTemporaryFile(delete=False) as archive:
shutil.copyfileobj(response, archive)
archive_path = Path(archive.name)
archive_is_temporary = True
on_progress(Progress(f"Extracting {bundle_nick}"))
except urllib.error.HTTPError as e:
if e.code == 404:
raise BundleNotFoundError(f"missing bundle at {url}") from e
raise e
try:
staging_dir = location.parent / f"_{location.name}"
if staging_dir.exists():
shutil.rmtree(staging_dir)
staging_dir.mkdir(parents=True)
with tarfile.open(archive_path, "r:xz") as tar:
tar.extractall(staging_dir)
suffix_len = len(".frida.in")
raw_location = location.as_posix()
for f in staging_dir.rglob("*.frida.in"):
target = f.parent / f.name[:-suffix_len]
f.write_text(f.read_text(encoding="utf-8").replace("@FRIDA_TOOLROOT@", raw_location),
encoding="utf-8")
f.rename(target)
staging_dir.rename(location)
finally:
if archive_is_temporary:
archive_path.unlink()
return state
def roll(bundle: Bundle,
build_machine: MachineSpec,
host_machine: MachineSpec,
activate: bool,
post: Optional[Path]):
params = load_dependency_parameters()
version = params.deps_version
if activate and bundle == Bundle.SDK:
configure_bootstrap_version(version)
(public_url, filename) = compute_bundle_parameters(bundle, host_machine, version)
# First do a quick check to avoid hitting S3 in most cases.
request = urllib.request.Request(public_url)
request.get_method = lambda: "HEAD"
try:
with urllib.request.urlopen(request) as r:
return
except urllib.request.HTTPError as e:
if e.code != 404:
raise CommandError("network error") from e
s3_url = "s3://build.frida.re/deps/{version}/{filename}".format(version=version, filename=filename)
# We will most likely need to build, but let's check S3 to be certain.
r = subprocess.run(["aws", "s3", "ls", s3_url], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding="utf-8")
if r.returncode == 0:
return
if r.returncode != 1:
raise CommandError(f"unable to access S3: {r.stdout.strip()}")
artifact = build(bundle, build_machine, host_machine)
if post is not None:
post_script = RELENG_DIR / post
if not post_script.exists():
raise CommandError("post-processing script not found")
subprocess.run([
sys.executable, post_script,
"--bundle=" + bundle.name.lower(),
"--host=" + host_machine.identifier,
"--artifact=" + str(artifact),
"--version=" + version,
],
check=True)
subprocess.run(["aws", "s3", "cp", artifact, s3_url], check=True)
# Use the shell for Windows compatibility, where npm generates a .bat script.
subprocess.run("cfcli purge " + public_url, shell=True, check=True)
if activate and bundle == Bundle.TOOLCHAIN:
configure_bootstrap_version(version)
def build(bundle: Bundle,
build_machine: MachineSpec,
host_machine: MachineSpec,
only_packages: Optional[set[str]] = None,
excluded_packages: set[str] = set(),
verbose: bool = False) -> Path:
builder = Builder(bundle, build_machine, host_machine, verbose)
try:
return builder.build(only_packages, excluded_packages)
except subprocess.CalledProcessError as e:
print(e, file=sys.stderr)
if e.stdout is not None:
print("\n=== stdout ===\n" + e.stdout, file=sys.stderr)
if e.stderr is not None:
print("\n=== stderr ===\n" + e.stderr, file=sys.stderr)
sys.exit(1)
class Builder:
def __init__(self,
bundle: Bundle,
build_machine: MachineSpec,
host_machine: MachineSpec,
verbose: bool):
self._bundle = bundle
self._host_machine = host_machine.default_missing()
self._build_machine = build_machine.default_missing().maybe_adapt_to_host(self._host_machine)
self._verbose = verbose
self._default_library = "static"
self._params = load_dependency_parameters()
self._cachedir = detect_cache_dir(ROOT_DIR)
self._workdir = self._cachedir / "src"
self._toolchain_prefix: Optional[Path] = None
self._build_config: Optional[env.MachineConfig] = None
self._host_config: Optional[env.MachineConfig] = None
self._build_env: dict[str, str] = {}
self._host_env: dict[str, str] = {}
self._ansi_supported = os.environ.get("TERM") != "dumb" \
and (self._build_machine.os != "windows" or "WT_SESSION" in os.environ)
def build(self,
only_packages: Optional[list[str]],
excluded_packages: set[str]) -> Path:
started_at = time.time()
prepare_ended_at = None
clone_time_elapsed = None
build_time_elapsed = None
build_ended_at = None
packaging_ended_at = None
try:
all_packages = {i: self._resolve_package(p) for i, p in self._params.packages.items() \
if self._can_build(p)}
if only_packages is not None:
toplevel_packages = [all_packages[identifier] for identifier in only_packages]
selected_packages = self._resolve_dependencies(toplevel_packages, all_packages)
elif self._bundle is Bundle.TOOLCHAIN:
toplevel_packages = [p for p in all_packages.values() if p.scope == "toolchain"]
selected_packages = self._resolve_dependencies(toplevel_packages, all_packages)
else:
selected_packages = {i: p for i, p, in all_packages.items() if p.scope is None}
selected_packages = {i: p for i, p in selected_packages.items() if i not in excluded_packages}
packages = [selected_packages[i] for i in iterate_package_ids_in_dependency_order(selected_packages.values())]
all_deps = itertools.chain.from_iterable([pkg.dependencies for pkg in packages])
deps_for_build_machine = {dep.identifier for dep in all_deps if dep.for_machine == "build"}
self._prepare()
prepare_ended_at = time.time()
clone_time_elapsed = 0
build_time_elapsed = 0
for pkg in packages:
self._print_package_banner(pkg)
t1 = time.time()
self._clone_repo_if_needed(pkg)
t2 = time.time()
clone_time_elapsed += t2 - t1
machines = [self._host_machine]
if pkg.identifier in deps_for_build_machine:
machines += [self._build_machine]
self._build_package(pkg, machines)
t3 = time.time()
build_time_elapsed += t3 - t2
build_ended_at = time.time()
artifact_file = self._package()
packaging_ended_at = time.time()
finally:
ended_at = time.time()
if prepare_ended_at is not None:
self._print_summary_banner()
print(" Total: {}".format(format_duration(ended_at - started_at)))
if prepare_ended_at is not None:
print(" Prepare: {}".format(format_duration(prepare_ended_at - started_at)))
if clone_time_elapsed is not None:
print(" Clone: {}".format(format_duration(clone_time_elapsed)))
if build_time_elapsed is not None:
print(" Build: {}".format(format_duration(build_time_elapsed)))
if packaging_ended_at is not None:
print(" Packaging: {}".format(format_duration(packaging_ended_at - build_ended_at)))
print("", flush=True)
return artifact_file
def _can_build(self, pkg: PackageSpec) -> bool:
return self._evaluate_condition(pkg.when)
def _resolve_package(self, pkg: PackageSpec) -> bool:
resolved_opts = [opt for opt in pkg.options if self._evaluate_condition(opt.when)]
resolved_deps = [dep for dep in pkg.dependencies if self._evaluate_condition(dep.when)]
return dataclasses.replace(pkg,
options=resolved_opts,
dependencies=resolved_deps)
def _resolve_dependencies(self,
packages: Sequence[PackageSpec],
all_packages: Mapping[str, PackageSpec]) -> dict[str, PackageSpec]:
result = {p.identifier: p for p in packages}
for p in packages:
self._resolve_package_dependencies(p, all_packages, result)
return result
def _resolve_package_dependencies(self,
package: PackageSpec,
all_packages: Mapping[str, PackageSpec],
resolved_packages: Mapping[str, PackageSpec]):
for dep in package.dependencies:
identifier = dep.identifier
if identifier in resolved_packages:
continue
p = all_packages[identifier]
resolved_packages[identifier] = p
self._resolve_package_dependencies(p, all_packages, resolved_packages)
def _evaluate_condition(self, cond: Optional[str]) -> bool:
if cond is None:
return True
global_vars = {
"Bundle": Bundle,
"bundle": self._bundle,
"machine": self._host_machine,
}
return eval(cond, global_vars)
def _prepare(self):
self._toolchain_prefix, toolchain_state = \
ensure_toolchain(self._build_machine,
self._cachedir,
version=self._params.bootstrap_version)
if toolchain_state == SourceState.MODIFIED:
self._wipe_build_state()
envdir = self._get_builddir_container()
envdir.mkdir(parents=True, exist_ok=True)
menv = {**os.environ}
if self._bundle is Bundle.TOOLCHAIN:
extra_ldflags = []
if self._host_machine.is_apple:
symfile = envdir / "toolchain-executable.symbols"
symfile.write_text("# No exported symbols.\n", encoding="utf-8")
extra_ldflags += [f"-Wl,-exported_symbols_list,{symfile}"]
elif self._host_machine.os != "windows":
verfile = envdir / "toolchain-executable.version"
verfile.write_text("\n".join([
"{",
" global:",
" # FreeBSD needs these two:",
" __progname;",
" environ;",
"",
" local:",
" *;",
"};",
""
]),
encoding="utf-8")
extra_ldflags += [f"-Wl,--version-script,{verfile}"]
if extra_ldflags:
menv["LDFLAGS"] = shlex.join(extra_ldflags + shlex.split(menv.get("LDFLAGS", "")))
build_sdk_prefix = None
host_sdk_prefix = None
self._build_config, self._host_config = \
env.generate_machine_configs(self._build_machine,
self._host_machine,
menv,
self._toolchain_prefix,
build_sdk_prefix,
host_sdk_prefix,
self._call_meson,
self._default_library,
envdir)
self._build_env = self._build_config.make_merged_environment(os.environ)
self._host_env = self._host_config.make_merged_environment(os.environ)
def _clone_repo_if_needed(self, pkg: PackageSpec):
sourcedir = self._get_sourcedir(pkg)
git = lambda *args, **kwargs: subprocess.run(["git", *args],
**kwargs,
capture_output=True,
encoding="utf-8")
if sourcedir.exists():
self._print_status(pkg.name, "Reusing existing checkout")
current_rev = git("rev-parse", "FETCH_HEAD", cwd=sourcedir, check=True).stdout.strip()
if current_rev != pkg.version:
self._print_status(pkg.name, "WARNING: Checkout does not match version in deps.toml")
else:
self._print_status(pkg.name, "Cloning")
clone_shallow(pkg, sourcedir, git)
def _wipe_build_state(self):
for path in (self._get_outdir(), self._get_builddir_container()):
if path.exists():
self._print_status(path.relative_to(self._workdir).as_posix(), "Wiping")
shutil.rmtree(path)
def _build_package(self, pkg: PackageSpec, machines: Sequence[MachineSpec]):
for machine in machines:
manifest_path = self._get_manifest_path(pkg, machine)
action = "skip" if manifest_path.exists() else "build"
message = "Building" if action == "build" else "Already built"
message += f" for {machine.identifier}"
self._print_status(pkg.name, message)
if action == "build":
self._build_package_for_machine(pkg, machine)
assert manifest_path.exists()
def _build_package_for_machine(self, pkg: PackageSpec, machine: MachineSpec):
sourcedir = self._get_sourcedir(pkg)
builddir = self._get_builddir(pkg, machine)
prefix = self._get_prefix(machine)
libdir = prefix / "lib"
strip = "true" if machine.toolchain_can_strip else "false"
if builddir.exists():
shutil.rmtree(builddir)
machine_file_opts = [f"--native-file={self._build_config.machine_file}"]
pc_opts = [f"-Dpkg_config_path={prefix / machine.libdatadir / 'pkgconfig'}"]
if self._host_config is not self._build_config and machine is self._host_machine:
machine_file_opts += [f"--cross-file={self._host_config.machine_file}"]
pc_path_for_build = self._get_prefix(self._build_machine) / self._build_machine.libdatadir / "pkgconfig"
pc_opts += [f"-Dbuild.pkg_config_path={pc_path_for_build}"]
menv = self._host_env if machine is self._host_machine else self._build_env
meson_kwargs = {
"env": menv,
"check": True,
}
if not self._verbose:
meson_kwargs["capture_output"] = True
meson_kwargs["encoding"] = "utf-8"
self._call_meson([
"setup",
builddir,
*machine_file_opts,
f"-Dprefix={prefix}",
f"-Dlibdir={libdir}",
*pc_opts,
f"-Ddefault_library={self._default_library}",
f"-Dbackend=ninja",
*machine.meson_optimization_options,
f"-Dstrip={strip}",
*[opt.value for opt in pkg.options],
],
cwd=sourcedir,
**meson_kwargs)
self._call_meson(["install"],
cwd=builddir,
**meson_kwargs)
manifest_lines = []
install_locations = json.loads(self._call_meson(["introspect", "--installed"],
cwd=builddir,
capture_output=True,
encoding="utf-8",
env=menv).stdout)
for installed_path in install_locations.values():
manifest_lines.append(Path(installed_path).relative_to(prefix).as_posix())
manifest_lines.sort()
manifest_path = self._get_manifest_path(pkg, machine)
manifest_path.parent.mkdir(parents=True, exist_ok=True)
manifest_path.write_text("\n".join(manifest_lines) + "\n", encoding="utf-8")
def _call_meson(self, argv, *args, **kwargs):
if self._verbose and argv[0] in {"setup", "install"}:
vanilla_env = os.environ
meson_env = kwargs["env"]
changed_env = {k: v for k, v in meson_env.items() if k not in vanilla_env or v != vanilla_env[k]}
indent = " "
env_summary = f" \\\n{indent}".join([f"{k}={shlex.quote(v)}" for k, v in changed_env.items()])
argv_summary = f" \\\n{3 * indent}".join([str(arg) for arg in argv])
print(f"> {env_summary} \\\n{indent}meson {argv_summary}", flush=True)
return env.call_meson(argv, use_submodule=True, *args, **kwargs)
def _package(self):
outfile = self._cachedir / f"{self._bundle.name.lower()}-{self._host_machine.identifier}.tar.xz"
self._print_packaging_banner()
with tempfile.TemporaryDirectory(prefix="frida-deps") as raw_tempdir:
tempdir = Path(raw_tempdir)
self._print_status(outfile.name, "Staging files")
if self._bundle is Bundle.TOOLCHAIN:
self._stage_toolchain_files(tempdir)
else:
self._stage_sdk_files(tempdir)
self._adjust_manifests(tempdir)
self._adjust_files_containing_hardcoded_paths(tempdir)
(tempdir / "VERSION.txt").write_text(self._params.deps_version + "\n", encoding="utf-8")
self._print_status(outfile.name, "Assembling")
with tarfile.open(outfile, "w:xz") as tar:
tar.add(tempdir, ".")
self._print_status(outfile.name, "All done")
return outfile
def _stage_toolchain_files(self, location: Path) -> list[Path]:
if self._host_machine.os == "windows":
toolchain_prefix = self._toolchain_prefix
mixin_files = [f for f in self._walk_plain_files(toolchain_prefix)
if self._file_should_be_mixed_into_toolchain(f)]
copy_files(toolchain_prefix, mixin_files, location)
prefix = self._get_prefix(self._host_machine)
files = [f for f in self._walk_plain_files(prefix)
if self._file_is_toolchain_related(f)]
copy_files(prefix, files, location)
def _stage_sdk_files(self, location: Path) -> list[Path]:
prefix = self._get_prefix(self._host_machine)
files = [f for f in self._walk_plain_files(prefix)
if self._file_is_sdk_related(f)]
copy_files(prefix, files, location)
def _adjust_files_containing_hardcoded_paths(self, bundledir: Path):
prefix = self._get_prefix(self._host_machine)
raw_prefixes = [str(prefix)]
if self._host_machine.os == "windows":
raw_prefixes.append(prefix.as_posix())
for f in self._walk_plain_files(bundledir):
filepath = bundledir / f
try:
text = filepath.read_text(encoding="utf-8")
new_text = text
is_pcfile = filepath.suffix == ".pc"
replacement = "${frida_sdk_prefix}" if is_pcfile else "@FRIDA_TOOLROOT@"
for p in raw_prefixes:
new_text = new_text.replace(p, replacement)
if new_text != text:
filepath.write_text(new_text, encoding="utf-8")
if not is_pcfile:
filepath.rename(filepath.parent / f"{f.name}.frida.in")
except UnicodeDecodeError:
pass
@staticmethod
def _walk_plain_files(rootdir: Path) -> Iterator[Path]:
for dirpath, dirnames, filenames in os.walk(rootdir):
for filename in filenames:
f = Path(dirpath) / filename
if f.is_symlink():
continue
yield f.relative_to(rootdir)
@staticmethod
def _adjust_manifests(bundledir: Path):
for manifest_path in (bundledir / "manifest").glob("*.pkg"):
lines = []
prefix = manifest_path.parent.parent
for entry in manifest_path.read_text(encoding="utf-8").strip().split("\n"):
if prefix.joinpath(entry).exists():
lines.append(entry)
if lines:
lines.sort()
manifest_path.write_text("\n".join(lines) + "\n", encoding="utf-8")
else:
manifest_path.unlink()
def _file_should_be_mixed_into_toolchain(self, f: Path) -> bool:
parts = f.parts
if parts[0] == "VERSION.txt":
return False
if parts[0] == "bin":
stem = f.stem
return stem in {"bison", "flex", "m4", "nasm", "vswhere"} or stem.startswith("msys-")
if parts[0] == "manifest":
return False
if self._file_is_vala_toolchain_related(f):
return False
return True
def _file_is_toolchain_related(self, f: Path) -> bool:
if self._file_is_vala_toolchain_related(f):
return True
parts = f.parts
if parts[0] == "bin":
if f.suffix == ".pdb":
return False
stem = f.stem
if stem in {"gdbus", "gio", "gobject-query", "gsettings"}:
return False
if stem.startswith("gspawn-"):
return False
return True
if parts[0] == "manifest":
return True
return False
def _file_is_vala_toolchain_related(self, f: Path) -> bool:
if f.suffix in {".vapi", ".deps"}:
return True
name = f.name
if f.suffix == self._host_machine.executable_suffix:
return name.startswith("vala") or name.startswith("vapi") or name.startswith("gen-introspect")
if f.parts[0] == "bin" and name.startswith("vala-gen-introspect"):
return True
return False
def _file_is_sdk_related(self, f: Path) -> bool:
suffix = f.suffix
if suffix == ".pdb":
return False
if suffix in [".vapi", ".deps"]:
return True
parts = f.parts
if parts[0] == "bin":
return f.name.startswith("v8-mksnapshot-")
return "share" not in parts
def _get_outdir(self) -> Path:
return self._workdir / f"_{self._bundle.name.lower()}.out"
def _get_sourcedir(self, pkg: PackageSpec) -> Path:
return self._workdir / pkg.identifier
def _get_builddir(self, pkg: PackageSpec, machine: MachineSpec) -> Path:
return self._get_builddir_container() / machine.identifier / pkg.identifier
def _get_builddir_container(self) -> Path:
return self._workdir / f"_{self._bundle.name.lower()}.tmp"
def _get_prefix(self, machine: MachineSpec) -> Path:
return self._get_outdir() / machine.identifier
def _get_manifest_path(self, pkg: PackageSpec, machine: MachineSpec) -> Path:
return self._get_prefix(machine) / "manifest" / f"{pkg.identifier}.pkg"
def _print_package_banner(self, pkg: PackageSpec):
if self._ansi_supported:
print("\n".join([
"",
"╭────",
f"│ 📦 \033[1m{pkg.name}\033[0m",
"├───────────────────────────────────────────────╮",
f"│ URL: {pkg.url}",
f"│ CID: {pkg.version}",
"├───────────────────────────────────────────────╯",
]), flush=True)
else:
print("\n".join([
"",
f"# {pkg.name}",
f"- URL: {pkg.url}",
f"- CID: {pkg.version}",
]), flush=True)
def _print_packaging_banner(self):
if self._ansi_supported:
print("\n".join([
"",
"╭────",
f"│ 🏗️ \033[1mPackaging\033[0m",
"├───────────────────────────────────────────────╮",
]), flush=True)
else:
print("\n".join([
"",
f"# Packaging",
]), flush=True)
def _print_summary_banner(self):
if self._ansi_supported:
print("\n".join([
"",
"╭────",
f"│ 🎉 \033[1mDone\033[0m",
"├───────────────────────────────────────────────╮",
]), flush=True)
else:
print("\n".join([
"",
f"# Done",
]), flush=True)
def _print_status(self, scope: str, *args):
status = " ".join([str(arg) for arg in args])
if self._ansi_supported:
print(f"│ \033[1m{scope}\033[0m :: {status}", flush=True)
else:
print(f"# {scope} :: {status}", flush=True)
def wait(bundle: Bundle, machine: MachineSpec):
params = load_dependency_parameters()
(url, filename) = compute_bundle_parameters(bundle, machine, params.deps_version)
request = urllib.request.Request(url)
request.get_method = lambda: "HEAD"
started_at = time.time()
while True:
try:
with urllib.request.urlopen(request) as r:
return
except urllib.request.HTTPError as e:
if e.code != 404:
return
print("Waiting for: {} Elapsed: {} Retrying in 5 minutes...".format(url, int(time.time() - started_at)), flush=True)
time.sleep(5 * 60)
def bump():
def run(argv: list[str], **kwargs) -> subprocess.CompletedProcess:
return subprocess.run(argv,
capture_output=True,
encoding="utf-8",
check=True,
**kwargs)
packages = load_dependency_parameters().packages
for identifier in iterate_package_ids_in_dependency_order(packages.values()):
pkg = packages[identifier]
print(f"# Checking {pkg.name}")
assert pkg.url.startswith("https://github.com/frida/"), f"{pkg.url}: unhandled URL"
bump_wraps(identifier, packages, run)
latest = query_repo_commits(identifier)["sha"]
if pkg.version == latest:
print(f"\tdeps.toml is up-to-date")
else:
print(f"\tdeps.toml is outdated")
print(f"\t\tcurrent: {pkg.version}")
print(f"\t\t latest: {latest}")
f = TOMLFile(DEPS_TOML_PATH)
config = f.read()
config[identifier]["version"] = latest
f.write(config)
run(["git", "add", "deps.toml"], cwd=RELENG_DIR)
run(["git", "commit", "-m" f"deps: Bump {pkg.name} to {latest[:7]}"], cwd=RELENG_DIR)
packages = load_dependency_parameters().packages
print("")
def bump_wraps(identifier: str,
packages: Mapping[str, PackageSpec],
run: Callable):
root = query_repo_trees(identifier)
subp_dir = next((t for t in root["tree"] if t["path"] == "subprojects"), None)
if subp_dir is None or subp_dir["type"] != "tree":
print("\tno wraps to bump")
return
all_wraps = [(entry, identifier_from_wrap_filename(entry["path"]))
for entry in query_github_api(subp_dir["url"])["tree"]
if entry["type"] == "blob" and entry["path"].endswith(".wrap")]
relevant_wraps = [(blob, packages[identifier])
for blob, identifier in all_wraps
if identifier in packages]
if not relevant_wraps:
print(f"\tno relevant wraps, only: {', '.join([blob['path'] for blob, _ in all_wraps])}")
return
pending_wraps: list[tuple[str, str, PackageSpec]] = []
for blob, spec in relevant_wraps:
filename = blob["path"]
response = query_github_api(blob["url"])
assert response["encoding"] == "base64"
data = base64.b64decode(response["content"])
config = ConfigParser()
config.read_file(data.decode("utf-8").split("\n"))
if "wrap-git" not in config:
print(f"\tskipping {filename} as it's not wrap-git")
continue
source = config["wrap-git"]
url = source["url"]
if not url.startswith("https://github.com/frida/"):
print(f"\tskipping {filename} as URL is external: {url}")
continue
revision = source["revision"]
if revision == spec.version:
continue
pending_wraps.append((filename, revision, spec))
if not pending_wraps:
print(f"\tall wraps up-to-date")
return
workdir = detect_cache_dir(ROOT_DIR) / "src"
workdir.mkdir(parents=True, exist_ok=True)
sourcedir = workdir / identifier
if sourcedir.exists():
shutil.rmtree(sourcedir)
run(["git", "clone", "--depth", "1", f"[email protected]:frida/{identifier}.git"], cwd=workdir)
subpdir = sourcedir / "subprojects"
revision_pattern = re.compile(r"^(?P<key_equals>\s*revision\s*=\s*)\S+$", re.MULTILINE)
for filename, revision, dep in pending_wraps:
wrapfile = subpdir / filename
old_config = wrapfile.read_text(encoding="utf-8")
# Would be simpler to use ConfigParser to write it back out, but we
# want to preserve the particular style to keep our patches minimal.
new_config = revision_pattern.sub(fr"\g<key_equals>{dep.version}", old_config)
wrapfile.write_text(new_config, encoding="utf-8")
run(["git", "add", filename], cwd=subpdir)
action = "Pin" if revision == "main" else "Bump"
run(["git", "commit", "-m" f"subprojects: {action} {dep.name} to {dep.version[:7]}"], cwd=sourcedir)
print(f"\tdid {action.lower()} {filename} to {dep.version} (from {revision})")
run(["git", "push"], cwd=sourcedir)
def identifier_from_wrap_filename(filename: str) -> str:
return filename.split(".", maxsplit=1)[0]
def compute_bundle_parameters(bundle: Bundle,
machine: MachineSpec,
version: str) -> tuple[str, str]:
if bundle == Bundle.TOOLCHAIN and machine.os == "windows":
os_arch_config = "windows-x86" if machine.arch in {"x86", "x86_64"} else machine.os_dash_arch
else:
os_arch_config = machine.identifier
filename = f"{bundle.name.lower()}-{os_arch_config}.tar.xz"
url = BUNDLE_URL.format(version=version, filename=filename)
return (url, filename)
def load_dependency_parameters() -> DependencyParameters:
config = TOMLFile(DEPS_TOML_PATH).read()
packages = {}
for identifier, pkg in config.items():
if identifier == "dependencies":
continue
packages[identifier] = PackageSpec(identifier,
pkg["name"],
pkg["version"],
pkg["url"],
list(map(parse_option, pkg.get("options", []))),
list(map(parse_dependency, pkg.get("dependencies", []))),
pkg.get("scope"),
pkg.get("when"))
p = config["dependencies"]
return DependencyParameters(p["version"], p["bootstrap_version"], packages)
def iterate_package_ids_in_dependency_order(packages: Sequence[PackageSpec]) -> Iterator[str]:
ts = graphlib.TopologicalSorter({pkg.identifier: {dep.identifier for dep in pkg.dependencies}
for pkg in packages})