Skip to content

Commit 20e00fd

Browse files
committed
Minor refactoring of QemuOptions
Make the qemu_arch_sufffix member private and fix the spelling error. Also improve get_qemu_binary() to be able to pass a list of search dirs instead of always using $PATH
1 parent 3fc9afb commit 20e00fd

File tree

3 files changed

+26
-25
lines changed

3 files changed

+26
-25
lines changed

pycheribuild/boot_cheribsd/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,7 @@ def _main(
15241524
else:
15251525
args.qemu_cmd = qemu_options.get_qemu_binary()
15261526
if args.qemu_cmd is None:
1527-
failure("ERROR: Cannot find QEMU binary for target ", qemu_options.qemu_arch_sufffix, exit=True)
1527+
failure("ERROR: Cannot find QEMU binary for target ", qemu_options.xtarget, exit=True)
15281528

15291529
global INTERACT_ON_KERNEL_PANIC # noqa: PLW0603
15301530
if args.interact_on_kernel_panic:

pycheribuild/projects/run_qemu.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,21 +110,17 @@ def setup(self, launch):
110110
assert launch.use_qemu == QEMUType.SYSTEM or launch.use_qemu == QEMUType.DEFAULT, (
111111
"Unexpected use_qemu for lazy binary location: " + str(launch.use_qemu)
112112
)
113-
binary_name = "qemu-system-" + launch.qemu_options.qemu_arch_sufffix
114-
if (launch.config.qemu_bindir / binary_name).is_file() and launch.use_qemu != QEMUType.SYSTEM:
113+
qemu_binary = launch.qemu_options.get_qemu_binary(search_dirs=[launch.config.qemu_bindir])
114+
if qemu_binary is not None and qemu_binary.is_file() and launch.use_qemu != QEMUType.SYSTEM:
115115
# Only CHERI QEMU supports more than one SMB share
116116
self._can_provide_src_via_smb = True
117-
self._binary = launch.config.qemu_bindir / binary_name
118117
else:
119-
# Only CHERI QEMU supports more than one SMB share; conservatively
120-
# guess what kind of QEMU this is
118+
# Only CHERI QEMU supports more than one SMB share; conservatively guess what kind of QEMU this is
121119
self._can_provide_src_via_smb = launch.crosscompile_target.is_hybrid_or_purecap_cheri()
122-
launch.check_required_system_tool(binary_name)
123-
binary_path = shutil.which(binary_name)
124-
if not binary_path:
125-
launch.fatal("Could not find system QEMU", binary_name)
126-
binary_path = "/could/not/find/qemu"
127-
self._binary = Path(binary_path)
120+
if qemu_binary is None:
121+
launch.fatal("Could not find system QEMU for target:", launch.qemu_options.xtarget)
122+
qemu_binary = Path("/could/not/find/qemu")
123+
self._binary = qemu_binary
128124

129125

130126
class LaunchQEMUBase(SimpleProject):
@@ -243,8 +239,8 @@ def targets_reset(cls):
243239
cls._cached_chosen_qemu = None
244240

245241
@classmethod
246-
def get_chosen_qemu(cls, config: CheriConfig):
247-
if cls._cached_chosen_qemu:
242+
def get_chosen_qemu(cls, config: CheriConfig) -> ChosenQEMU:
243+
if cls._cached_chosen_qemu is not None:
248244
return cls._cached_chosen_qemu
249245

250246
xtarget = cls.get_crosscompile_target()
@@ -273,8 +269,7 @@ def get_chosen_qemu(cls, config: CheriConfig):
273269
assert False, "Unknown target " + str(xtarget)
274270

275271
if cls.use_qemu == QEMUType.CUSTOM:
276-
# Only CHERI QEMU supports more than one SMB share; conservatively
277-
# guess what kind of QEMU this is
272+
# Only CHERI QEMU supports more than one SMB share; conservatively guess what kind of QEMU this is
278273
can_provide_src_via_smb = xtarget.is_hybrid_or_purecap_cheri()
279274
if not cls.custom_qemu_path:
280275
fatal_error(

pycheribuild/qemu_utils.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def __init__(self, xtarget: CrossCompileTarget, want_debugger=False) -> None:
4747
self.memory_size = "2048"
4848
self.has_default_nic = False
4949
if xtarget.is_hybrid_or_purecap_cheri([CPUArchitecture.AARCH64]):
50-
self.qemu_arch_sufffix = "morello"
50+
self._qemu_arch_suffix = "morello"
5151
self.can_boot_kernel_directly = False # boot from disk
5252
# XXX: Use a CHERI-aware firmware. EL3 is disabled by default for
5353
# virt, so CPTR_EL3 doesn't exist and CheriBSD can enable
@@ -56,19 +56,19 @@ def __init__(self, xtarget: CrossCompileTarget, want_debugger=False) -> None:
5656
self.machine_flags = ["-M", "virt,gic-version=3", "-cpu", "morello", "-bios", "edk2-aarch64-code.fd"]
5757
elif xtarget.is_mips(include_purecap=True):
5858
# Note: we always use the CHERI QEMU
59-
self.qemu_arch_sufffix = "mips64cheri128"
59+
self._qemu_arch_suffix = "mips64cheri128"
6060
self.machine_flags = ["-M", "malta"]
6161
self.virtio_disk = False # broken for MIPS?
6262
self.can_boot_kernel_directly = True
6363
self.has_default_nic = True # MALTA board has a default pcnet at 0x0b
6464
elif xtarget.is_riscv(include_purecap=True):
6565
# Note: we always use the CHERI QEMU
66-
self.qemu_arch_sufffix = "riscv32cheri" if xtarget.is_riscv32(include_purecap=True) else "riscv64cheri"
66+
self._qemu_arch_suffix = "riscv32cheri" if xtarget.is_riscv32(include_purecap=True) else "riscv64cheri"
6767
self.machine_flags = ["-M", "virt"]
6868
self.can_boot_kernel_directly = True
6969
elif xtarget.is_any_x86():
7070
# We boot i386 FreeBSD in a x86_64 QEMU. This avoids having to build another version of QEMU.
71-
self.qemu_arch_sufffix = "x86_64"
71+
self._qemu_arch_suffix = "x86_64"
7272
self.can_boot_kernel_directly = False # boot from disk
7373
# Try to use KVM instead of TCG if possible to speed up emulation
7474
if not want_debugger:
@@ -83,11 +83,11 @@ def __init__(self, xtarget: CrossCompileTarget, want_debugger=False) -> None:
8383
# We have to use the ancient default instead to avoid kernel panics.
8484
# See https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=253617 for details.
8585
elif xtarget.is_aarch64(include_purecap=False):
86-
self.qemu_arch_sufffix = "aarch64"
86+
self._qemu_arch_suffix = "aarch64"
8787
self.can_boot_kernel_directly = False # boot from disk
8888
self.machine_flags = ["-M", "virt,gic-version=3", "-cpu", "cortex-a72", "-bios", "edk2-aarch64-code.fd"]
8989
elif xtarget.is_arm32(include_purecap=False):
90-
self.qemu_arch_sufffix = "arm"
90+
self._qemu_arch_suffix = "arm"
9191
self.can_boot_kernel_directly = False # boot from disk
9292
self.machine_flags = ["-M", "virt", "-cpu", "cortex-15"]
9393
else:
@@ -156,9 +156,15 @@ def user_network_args(self, extra_options) -> "list[str]":
156156
network_device_kind = self._qemu_network_config()[0]
157157
return ["-device", network_device_kind + ",netdev=net0", "-netdev", "user,id=net0" + extra_options]
158158

159-
def get_qemu_binary(self) -> "Optional[Path]":
160-
found_in_path = shutil.which("qemu-system-" + self.qemu_arch_sufffix)
161-
return Path(found_in_path) if found_in_path is not None else None
159+
def get_qemu_binary(self, search_dirs: "Optional[list[Path]]" = None) -> "Optional[Path]":
160+
if search_dirs is None:
161+
found_in_path = shutil.which("qemu-system-" + self._qemu_arch_suffix)
162+
return Path(found_in_path) if found_in_path is not None else None
163+
for d in search_dirs:
164+
candidate = d / f"qemu-system-{self._qemu_arch_suffix}"
165+
if candidate.exists():
166+
return candidate
167+
return None
162168

163169
def get_commandline(
164170
self,

0 commit comments

Comments
 (0)