Repository navigation
Expand file tree
/
Copy pathbuild.py
More file actions
executable file
·836 lines (666 loc) · 26.6 KB
/
Copy pathbuild.py
File metadata and controls
executable file
·836 lines (666 loc) · 26.6 KB
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
#!/usr/bin/env python3
import argparse
import subprocess
import os
import platform
import re
import selectors
import shutil
import time
import signal
import sys
from contextlib import contextmanager
from typing import Optional, Dict, List, Callable, Iterator, Tuple
try:
import scripts.kconfiglib.kconfiglib as kc
# Make sure the rest of "import kconfiglib" always references this and not
# the kconfiglib from pip or other copies of the library
sys.modules["kconfiglib"] = kc
import scripts.build_utils.wsl_wrap as ww
import scripts.config_layers as cl
import scripts.build_utils.package_manager as pm
import scripts.build_utils.toolchain_builder as tb
import scripts.build_utils.toolchain_args as ta
import scripts.build_utils.download as dl
import scripts.image_utils.ultra as ultr
import scripts.image_utils.uefi as uefi
import scripts.image_utils.path_guesser as pg
import scripts.baremetal_ci.client as bmc
import scripts.lint as lint
except ImportError:
print("Unable to import one of submodule libraries!")
print("Please run 'git submodule update --init' to initialize submodules")
sys.exit(1)
GENERIC_DEPS = {
"apt": [
"xorriso",
"qemu-system-x86",
"qemu-system-arm",
"cmake",
"mtools",
"python3-pyelftools",
"python3-tk",
],
"pacman": [
"xorriso",
"qemu-system-x86",
"qemu-system-arm",
"cmake",
"mtools",
"python-pyelftools",
"tk",
],
"dnf": [
"xorriso",
"qemu-system-x86",
"qemu-system-aarch64",
"cmake",
"mtools",
"python3-pyelftools",
"python3-tkinter",
],
"brew": [
"xorriso",
"qemu",
"cmake",
"mtools",
"python-tk",
],
}
# How long --run-for waits for the first output before giving up
QEMU_SILENT_BOOT_SECONDS = 30
@contextmanager
def enter_work_dir(path: str) -> Iterator[None]:
old_dir = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(old_dir)
def get_toolchain_dir() -> str:
return pg.project_root_relative("toolchain")
def get_specific_toolchain_dir(type: str, arch: str) -> str:
return pg.project_root_relative(
get_toolchain_dir(), f"tools-{type}-{arch}"
)
def get_build_dir(suffix: str, toolchain: str) -> str:
return pg.project_root_relative(f"build-{toolchain}-{suffix}")
def get_tests_dir() -> str:
return pg.project_root_relative("tests")
def get_tests_build_dir(this_os: str) -> str:
return pg.project_root_relative(get_tests_dir(), f"build-{this_os}")
def platform_name_for_binary(binary: str, this_os: str) -> str:
if this_os == "windows":
binary += ".exe"
return binary
def test_runner_binary(this_os: str) -> str:
return os.path.join(
"tests", "bin", platform_name_for_binary("run_tests", this_os)
)
def build_toolchain(args: argparse.Namespace) -> None:
if not tb.is_supported_system():
sys.exit(1)
tc_root = get_specific_toolchain_dir(args.toolchain, args.arch)
tp = ta.params_from_args(args, "elf", tc_root, get_toolchain_dir())
if not args.skip_generic_dependencies:
pm.install_dependencies(GENERIC_DEPS)
tb.build_toolchain(tp)
def cmake_cache_get(build_dir: str, key: str) -> Optional[str]:
cmake_cache = os.path.join(build_dir, "CMakeCache.txt")
if not os.path.isfile(cmake_cache):
return None
with open(cmake_cache) as f:
for line in f:
name, sep, value = line.strip().partition("=")
if sep and name.split(":")[0] == key:
return value
return None
def make_supports_output_sync(make_program: str) -> bool:
try:
proc = subprocess.run([make_program, "--version"],
capture_output=True, text=True)
except OSError:
return False
match = re.match(r"GNU Make (\d+)", proc.stdout)
return match is not None and int(match.group(1)) >= 4
def cmake_native_build_args(build_dir: str) -> List[str]:
generator = cmake_cache_get(build_dir, "CMAKE_GENERATOR")
if generator is None or not generator.endswith("Makefiles"):
return []
make_program = cmake_cache_get(build_dir, "CMAKE_MAKE_PROGRAM")
if make_program is None or not make_supports_output_sync(make_program):
return []
# Parallel make interleaves the output of concurrent jobs otherwise
return ["--", "-Oline"]
def cmake_configure(
args: argparse.Namespace, build_dir: str, extra_args: List[str] = [],
reconfigure_cb: Optional[Callable[[], None]] = None,
refresh: bool = False
) -> None:
cmake_cache = os.path.join(build_dir, "CMakeCache.txt")
has_cache = os.path.isfile(cmake_cache)
if has_cache and refresh and not args.reconfigure:
subprocess.run(["cmake", "."], check=True, cwd=build_dir)
return
if has_cache and not args.reconfigure:
print("Not rerunning cmake since build directory already exists "
"(--reconfigure)")
return
if reconfigure_cb is not None:
reconfigure_cb()
os.makedirs(build_dir, exist_ok=True)
generator_args = []
if not has_cache and shutil.which("ninja"):
generator_args = ["-G", "Ninja"]
subprocess.run(["cmake", "..", *generator_args, *extra_args],
check=True, cwd=build_dir)
def cmake_run_build(build_dir: str) -> None:
subprocess.run(
["cmake", "--build", ".", "-j", str(os.cpu_count()),
*cmake_native_build_args(build_dir)],
cwd=build_dir, check=True
)
def cmake_build(args: argparse.Namespace, build_dir: str) -> None:
cmake_configure(args, build_dir)
cmake_run_build(build_dir)
def configure_ultra(
args: argparse.Namespace, build_dir: str, refresh: bool = False
) -> None:
def rebuild_toolchain() -> None:
# Only rerun toolchain builder if reconfigure is not artificial
if not args.reconfigure:
build_toolchain(args)
cmake_configure(
args, build_dir, [f"-DCONFIG_FILE={args.config}"],
rebuild_toolchain, refresh
)
def build_ultra(
args: argparse.Namespace, build_dir: str
) -> None:
configure_ultra(args, build_dir)
cmake_run_build(build_dir)
QEMU_HYPER_OPTIONS = """
cmdline = "log-level=debug earlycon=e9,colored{cmdline_extra}"
# Hardcode a resolution that's good enough for local testing,
# we don't need a huge QEMU window.
video-mode:
width = 1024
height = 768
"""
IMAGE_TARGETS = {
"qemu": QEMU_HYPER_OPTIONS,
"baremetal": "",
}
# The QEMU image gets a fixed command line, extra arguments are appended
def image_target_options(target: str, extra_cmdline: str) -> str:
if extra_cmdline:
extra_cmdline = " " + extra_cmdline
return IMAGE_TARGETS[target].replace("{cmdline_extra}", extra_cmdline)
def make_hyper_config(arch: str, target: str, extra_cmdline: str) -> str:
return \
f"""
default-entry = ultra-{arch}
[ultra-x86_64]
protocol = ultra
higher-half-exclusive = true
binary:
path = "/kernel-x86_64"
allocate-anywhere = true
page-table:
levels = 5
constraint = maximum
{image_target_options(target, extra_cmdline)}
[ultra-aarch64]
protocol = ultra
higher-half-exclusive = true
binary:
path = "/kernel-aarch64"
allocate-anywhere = true
page-table:
levels = 4
constraint = exactly
# We don't really need video for now
video-mode = unset
""" # noqa: E122
def get_kernel_path(execution_node: str, build_dir: str) -> str:
return os.path.join(build_dir, f"kernel-{execution_node}")
def make_hyper_image(
br_type: str, fs_type: str, arch: str, target: str, build_dir: str,
hyper_installer: Optional[str], hyper_iso_br: Optional[str],
hyper_uefi_binaries: List[str], image_path: str, force_regenerate: bool,
extra_cmdline: str
) -> Optional[ultr.DiskImage]:
kernel_path = get_kernel_path(arch, build_dir)
image_root_path = os.path.join(build_dir, "image-root")
try:
if (not force_regenerate and
os.path.getmtime(kernel_path) < os.path.getmtime(image_path)):
print("Image is newer than the kernel binary, not regenerating "
"(--make-image)")
return None
except OSError:
pass
os.makedirs(image_root_path, exist_ok=True)
shutil.copy(kernel_path, image_root_path)
return ultr.DiskImage(
image_root_path, br_type, fs_type,
hyper_config=make_hyper_config(arch, target, extra_cmdline),
hyper_uefi_binary_paths=hyper_uefi_binaries,
hyper_iso_br_path=hyper_iso_br,
hyper_installer_path=hyper_installer,
out_path=image_path,
)
def hyper_get_binary(name: str, optional: bool = False) -> Optional[str]:
hyper_version = "v0.12.0"
root = pg.project_root_relative(f"hyper-{hyper_version}")
binary_path = os.path.join(root, name)
if not os.path.isdir(root):
os.makedirs(root)
if not os.path.isfile(binary_path):
base_url = "https://github.com/UltraOS/Hyper/releases/download/"
base_url += hyper_version
file_url = f"{base_url}/{name}"
try:
dl.download([file_url], binary_path)
except Exception as ex:
print(f"Failed to retrieve {name}:", ex)
return None
os.chmod(binary_path, 0o777)
return binary_path
def hyper_get_installer() -> str:
ret = hyper_get_binary("hyper_install")
assert ret
return ret
def hyper_get_iso_br() -> str:
ret = hyper_get_binary("hyper_iso_boot")
assert ret
return ret
QEMU_VERDICT_TIMEOUT_SECONDS = 120
QEMU_DRAIN_SECONDS = 1
# The colored console wraps parts of a line in escape sequences
ANSI_ESCAPE = re.compile(rb"\x1b\[[0-9;]*m")
# Streams the kernel output, returns the first match of 'until' or None
# when QEMU exited or the wait ran out. With 'until' the run is bounded by
# 'run_for' seconds of total output, otherwise by 'run_for' seconds since
# the first output byte.
def qemu_watch(
qp: subprocess.Popen, run_for: float,
until: Optional[re.Pattern] = None
) -> Optional[re.Match]:
assert qp.stdout is not None
sel = selectors.DefaultSelector()
sel.register(qp.stdout, selectors.EVENT_READ)
deadline = None
silent_deadline = time.monotonic() + run_for + QEMU_SILENT_BOOT_SECONDS
tail = b""
match = None
while qp.poll() is None:
if deadline is not None:
timeout = deadline - time.monotonic()
else:
timeout = silent_deadline - time.monotonic()
if timeout <= 0:
if deadline is None:
print(
"No output from the kernel, giving up", file=sys.stderr
)
qp.terminate()
break
if not sel.select(timeout):
continue
data = os.read(qp.stdout.fileno(), 65536)
if not data:
break
sys.stdout.buffer.write(data)
sys.stdout.buffer.flush()
if deadline is None:
deadline = time.monotonic() + run_for
if until is not None and match is None:
tail = (tail + data)[-4096:]
match = until.search(ANSI_ESCAPE.sub(b"", tail))
if match is not None:
deadline = time.monotonic() + QEMU_DRAIN_SECONDS
if match is not None:
sys.stdout.buffer.write(b"\n")
sys.stdout.buffer.flush()
qp.wait()
return match
# A QEMU run is judged by the same criteria as a baremetal run
def verdict_pattern(criteria_name: Optional[str]) -> re.Pattern:
criteria = bmc.load_criteria(criteria_name)
failures = "|".join(criteria["failures"])
return re.compile(
f"(?P<success>{criteria['success']})|(?P<failure>{failures})".encode()
)
def run_qemu(
arch: str, image_path: str, image_type: str, debug: bool, uefi_boot: bool,
uefi_firmware: str, kvm: bool, la57: bool, headless: bool, dry: bool,
run_for: Optional[float] = None, verdict: Optional[re.Pattern] = None
) -> Optional[subprocess.Popen]:
extra_args = []
force_uefi = False
if arch == "aarch64":
extra_args.extend([
"-M", "virt", "-cpu", "max",
"-serial", "stdio"
])
force_uefi = True
elif arch == "x86_64":
extra_args.extend([
"-M", "q35", "-debugcon", "stdio"
])
else:
raise RuntimeError(f"Unknown/unsupported architecture '{arch}'")
disk_arg = "-cdrom" if image_type == "iso" else "-hda"
if debug:
extra_args.extend(["-s", "-S"])
if kvm:
extra_args.append("--enable-kvm")
if la57:
extra_args.extend(["-cpu", "qemu64,la57=on"])
if headless:
extra_args.extend(["-display", "none"])
args = [
f"qemu-system-{arch}",
disk_arg, image_path, "-m", "1G",
*extra_args
]
if uefi_boot:
if uefi_firmware is None:
uefi_firmware = uefi.get_path_to_qemu_uefi_firmware(arch)
if uefi_firmware is not None:
drive_opts = f"file={uefi_firmware}"
drive_opts += ",if=pflash,format=raw,readonly=on"
args.extend(["-drive", drive_opts])
elif force_uefi:
raise RuntimeError(
f"Unable to boot {arch} without UEFI firmware"
)
if dry:
print(" ".join(args))
return None
if verdict is not None:
qp = subprocess.Popen(args, stdout=subprocess.PIPE)
try:
match = qemu_watch(qp, QEMU_VERDICT_TIMEOUT_SECONDS, verdict)
except KeyboardInterrupt:
qp.terminate()
qp.wait()
sys.exit(130)
if match is None:
print("Run did not finish", file=sys.stderr)
sys.exit(2)
failure = match.group("failure")
if failure is not None:
print(f"Run failed: {failure.decode(errors='replace')}",
file=sys.stderr)
sys.exit(1)
sys.exit(0)
if run_for is not None and not debug:
qp = subprocess.Popen(args, stdout=subprocess.PIPE)
try:
qemu_watch(qp, run_for)
except KeyboardInterrupt:
qp.terminate()
qp.wait()
return qp
qp = subprocess.Popen(args, start_new_session=debug)
if not debug:
try:
qp.wait()
except KeyboardInterrupt:
pass
return qp
def hyper_pxe_loaders() -> Tuple[str, str]:
bootx64 = hyper_get_binary("BOOTX64.EFI")
# The upstream release ships the BIOS PXE binary as hyper_pxe,
# the deployment installs it under its proper hyper.pxe name
pxe = hyper_get_binary("hyper_pxe")
if bootx64 is None or pxe is None:
sys.exit("failed to fetch the hyper boot binaries")
return bootx64, pxe
def run_lint(args: argparse.Namespace, build_dir: str) -> int:
if args.toolchain != "clang":
sys.exit("--lint only works with --toolchain clang")
configure_ultra(args, build_dir, refresh=True)
return lint.run(build_dir)
def run_unit_tests(args: argparse.Namespace, this_os: str) -> int:
dir = get_tests_build_dir(this_os)
cmake_build(args, dir)
binary = pg.project_root_relative(test_runner_binary(this_os))
return subprocess.run([binary]).returncode
def main() -> None:
ww.relaunch_in_wsl_if_windows()
pg.set_project_root(os.path.dirname(os.path.abspath(__file__)))
parser = argparse.ArgumentParser(
description="Build, image and run the Ultra kernel"
)
build = parser.add_argument_group("Build")
build.add_argument("--arch", default="x86_64",
choices=["x86_64", "aarch64"],
help="CPU architecture to build the kernel for")
build.add_argument("--config",
help="Preset from configs/ or a config file to build "
f"(default: {cl.DEFAULT_PRESET})")
build.add_argument("--reset-config", action="store_true",
help="Recreate the build directory's config from "
"--config, discarding the changes made to it")
build.add_argument("--optimization-level", type=int, choices=range(4),
help="Override the optimization level of the config")
build.add_argument("--lto", action=argparse.BooleanOptionalAction,
help="Override whether the config uses link-time "
"optimization")
build.add_argument("--menuconfig", action="store_true",
help="Run menuconfig to edit the current config file")
build.add_argument("--guiconfig", action="store_true",
help="Run guiconfig to edit the current config file")
build.add_argument("--reconfigure", action="store_true",
help="Reconfigure cmake before building")
build.add_argument("--no-build", action="store_true",
help="Assume the kernel is already built")
build.add_argument("--skip-generic-dependencies", action="store_true",
help="Don't attempt to fetch the generic dependencies")
toolchain = parser.add_argument_group("Toolchain")
ta.add_base_args(toolchain)
toolchain.add_argument("--toolchain-only", action="store_true",
help="Build the toolchain and exit")
image = parser.add_argument_group("Image")
image.add_argument("--make-image", nargs="?", const="qemu",
choices=IMAGE_TARGETS.keys(), metavar="TARGET",
help="Produce a bootable image after building, "
"for QEMU or for real hardware with "
"'baremetal' (default: qemu)")
image.add_argument("--cmdline", type=str, default="",
help="Extra kernel command line arguments (implies "
"--make-image unless this is a baremetal run)")
image.add_argument("--image-type", choices=["iso", "raw"], default="iso",
help="Image type to produce (with --make-image)")
image.add_argument("--hyper-installer", type=str,
help="Path to the hyper installer")
image.add_argument("--hyper-iso-loader", type=str,
help="Path to the hyper iso boot record "
"(hyper_iso_boot)")
image.add_argument("--hyper-uefi-binary-paths", nargs='+',
help="Paths to the hyper UEFI binaries "
"(BOOT{X64,AA64}.EFI)")
boot_tests = parser.add_argument_group(
"Boot tests",
"Boot the kernel headless and exit with the verdict, in QEMU unless "
"--baremetal is given"
)
boot_tests.add_argument("--boot-test", action="store_true",
help="Check that the kernel boots until it idles "
"(the default for --baremetal)")
boot_tests.add_argument("--ktest", nargs="?", const="all",
metavar="TESTS",
help="Run the in-kernel tests selected by TESTS "
"(default: all)")
boot_tests.add_argument("--criteria", metavar="NAME",
help="Judge the run by criteria-NAME.json "
"instead of criteria.json")
qemu = parser.add_argument_group("QEMU")
qemu.add_argument("--run", action="store_true",
help="Run in QEMU after building")
qemu.add_argument("--kvm", action="store_true",
help="Run QEMU with KVM enabled (implies --run)")
qemu.add_argument("--la57", action="store_true",
help="Run QEMU with LA57 support (x86_64 only)")
qemu.add_argument("--headless", action="store_true",
help="Run QEMU without a display window (implies --run)")
qemu.add_argument("--uefi", action="store_true",
help="Boot in UEFI mode")
qemu.add_argument("--uefi-firmware-path",
help="Path to UEFI firmware to use with QEMU")
qemu.add_argument("--debug", action="store_true",
help="Start a debugging session after building "
"(implies --run)")
qemu.add_argument("--ide-debug", action="store_true",
help="Start QEMU in debug mode but don't start "
"a debugger")
qemu.add_argument("--dry", action="store_true",
help="Dump the QEMU command line instead of running")
qemu.add_argument("--run-for", type=float, metavar="SECONDS",
help="Stop this many seconds after the first"
" output, or after"
f" {QEMU_SILENT_BOOT_SECONDS} more if there is"
" none, also works for --baremetal")
baremetal = parser.add_argument_group(
"Baremetal",
"Boot the kernel on real hardware through the baremetal CI server"
)
bmc.add_build_args(baremetal)
tests = parser.add_argument_group("Tests")
tests.add_argument("--unit-tests", action="store_true",
help="Run the userspace test suite")
tests.add_argument("--lint", action="store_true",
help="Check the coding conventions without building,"
" needs the clang toolchain")
args = parser.parse_args()
this_os = platform.system()
if this_os not in ["Linux", "Darwin", "Windows"]:
print(f"{this_os} is not (yet) supported")
sys.exit(1)
if args.unit_tests:
if this_os != "Linux":
print("Unit tests only support Linux hosts")
sys.exit(1)
sys.exit(run_unit_tests(args, this_os.lower()))
rc = bmc.early_action(args, hyper_pxe_loaders)
if rc is not None:
sys.exit(rc)
if args.ktest is not None:
if args.config is not None:
sys.exit("--config cannot be combined with --ktest")
if args.criteria is not None:
sys.exit("--criteria cannot be combined with --ktest")
args.config = "ktest"
args.criteria = "ktest"
if args.cmdline:
args.cmdline += " "
args.cmdline += f"ktest.run={args.ktest}"
if args.criteria is not None:
args.boot_test = True
if args.boot_test:
args.headless = True
# A changed command line only reaches QEMU through a new image
if args.cmdline and args.machines is None and args.make_image is None:
args.make_image = "qemu"
config_layers = cl.build_layers(args.config, args.arch)
config_file = config_layers[-1]
default_preset = cl.preset_path(cl.DEFAULT_PRESET)
build_dir_name = f"build-{args.toolchain}-{args.arch}"
if os.path.abspath(config_file) != os.path.abspath(default_preset):
config_name = os.path.splitext(os.path.basename(config_file))[0]
if config_name.startswith("."):
config_name = "user-config"
build_dir_name += f"-{config_name}"
build_dir = pg.project_root_relative(build_dir_name)
os.makedirs(build_dir, exist_ok=True)
overrides: Dict[str, str] = {}
if args.optimization_level is not None:
overrides["OPTIMIZATION_LEVEL"] = str(args.optimization_level)
if args.lto is not None:
overrides["LTO"] = "y" if args.lto else "n"
args.config = os.path.join(build_dir, ".config")
cl.prepare_config(args.config, config_layers, overrides, args.toolchain,
args.arch, args.reset_config)
if args.menuconfig or args.guiconfig:
os.environ["KCONFIG_CONFIG"] = args.config
import scripts.kconfiglib.menuconfig as mc
import scripts.kconfiglib.guiconfig as gc
module = mc if args.menuconfig else gc
with enter_work_dir(pg.project_root()):
module.menuconfig(cl.root_kconfig())
sys.exit(0)
if args.toolchain_only:
build_toolchain(args)
sys.exit(0)
if args.lint:
sys.exit(run_lint(args, build_dir))
if not args.no_build:
build_ultra(args, build_dir)
rc = bmc.run_from_args(args, args.arch,
get_kernel_path(args.arch, build_dir), build_dir)
if rc is not None:
sys.exit(rc)
is_debug = args.debug or args.ide_debug
should_run = (args.run or args.kvm or args.la57 or args.headless or
is_debug or args.run_for is not None)
if args.run_for is not None and is_debug:
sys.exit("--run-for cannot be combined with a debug run")
image_target = args.make_image or "qemu"
if should_run and image_target != "qemu":
sys.exit(f"--make-image {image_target} cannot be combined "
"with a QEMU run")
if should_run or args.make_image:
hyper_installer = args.hyper_installer
hyper_iso_br = args.hyper_iso_loader
hyper_uefi_binary_paths = args.hyper_uefi_binary_paths
if args.image_type == "iso":
fs_type = "ISO9660"
image_ext = "iso"
else:
fs_type = "FAT32"
image_ext = "raw"
image_name = "image"
if image_target != "qemu":
image_name += f"-{image_target}"
image_name += f".{image_ext}"
if not hyper_installer:
hyper_installer = hyper_get_installer()
if not hyper_iso_br:
hyper_iso_br = hyper_get_iso_br()
if not hyper_uefi_binary_paths:
hyper_uefi_binary_paths = [
hyper_get_binary("BOOTX64.EFI", True),
hyper_get_binary("BOOTAA64.EFI", True),
]
hyper_uefi_binary_paths = filter(lambda x: x is not None,
hyper_uefi_binary_paths)
image_path = os.path.join(build_dir, image_name)
make_hyper_image(
"MBR", fs_type, args.arch, image_target, build_dir,
hyper_installer, hyper_iso_br, hyper_uefi_binary_paths,
image_path, args.make_image is not None, args.cmdline
)
if should_run:
if args.arch == "aarch64":
args.uefi = True
uefi_boot = hyper_uefi_binary_paths and args.uefi
if args.la57 and args.arch != "x86_64":
raise RuntimeError("--la57 is only supported on x86_64")
verdict: Optional[re.Pattern] = None
if args.boot_test:
verdict = verdict_pattern(args.criteria)
qp = run_qemu(args.arch, image_path, args.image_type, is_debug,
uefi_boot, args.uefi_firmware_path, args.kvm, args.la57,
args.headless, args.dry, args.run_for, verdict)
if args.debug:
assert qp
gdb_args = ["gdb", "--tui", get_kernel_path(args.arch, build_dir),
"--eval-command", "target remote localhost:1234"]
signal.signal(signal.SIGINT, signal.SIG_IGN)
subprocess.run(gdb_args)
qp.kill()
if __name__ == "__main__":
main()