|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +from pathlib import Path |
| 5 | +import subprocess as sp |
| 6 | +import sys |
| 7 | + |
| 8 | +## Configurable settings |
| 9 | +# Target to build for. |
| 10 | +TARGET = 'x86_64-uefi' |
| 11 | +# Configuration to build. |
| 12 | +CONFIG = 'debug' |
| 13 | + |
| 14 | +# Xargo executable. |
| 15 | +XARGO = 'xargo' |
| 16 | +# Additional flags passed to Xargo. |
| 17 | +XARGO_FLAGS = [ |
| 18 | + '--target', TARGET, |
| 19 | +] |
| 20 | + |
| 21 | +# A linker for PE/COFF files. |
| 22 | +LINKER = 'lld' |
| 23 | +LINKER_FLAGS = [ |
| 24 | + # Use LLD in `link.exe` mode. |
| 25 | + '-flavor', 'link', |
| 26 | + # Create 64-bit executables. |
| 27 | + '/Machine:x64', |
| 28 | + # Create UEFI apps. |
| 29 | + '/Subsystem:EFI_Application', |
| 30 | + # Customizable entry point name. |
| 31 | + '/Entry:uefi_start', |
| 32 | +] |
| 33 | + |
| 34 | +BUILD_DIR = Path('target') / TARGET / CONFIG |
| 35 | +ESP_DIR = BUILD_DIR / 'esp' |
| 36 | + |
| 37 | +def run_xargo(verb, *flags): |
| 38 | + sp.run([XARGO, verb, *XARGO_FLAGS, *flags]).check_returncode() |
| 39 | + |
| 40 | +def build(): |
| 41 | + run_xargo('build', '--package', 'tests') |
| 42 | + |
| 43 | + input = BUILD_DIR / 'libtests.a' |
| 44 | + |
| 45 | + boot_dir = ESP_DIR / 'EFI' / 'Boot' |
| 46 | + boot_dir.mkdir(parents=True, exist_ok=True) |
| 47 | + |
| 48 | + output = boot_dir / 'BootX64.efi' |
| 49 | + |
| 50 | + sp.run([LINKER, *LINKER_FLAGS, input, f'-Out:{output}']).check_returncode() |
| 51 | + |
| 52 | +def doc(): |
| 53 | + run_xargo('doc', '--no-deps', '--package', 'uefi') |
| 54 | + |
| 55 | +def clippy(): |
| 56 | + run_xargo('clippy') |
| 57 | + |
| 58 | + |
| 59 | +def run_qemu(): |
| 60 | + qemu = 'qemu-system-x86_64' |
| 61 | + |
| 62 | + # TODO: download and extract automatically from Kraxel's repo. |
| 63 | + ovmf_dir = Path('.') |
| 64 | + ovmf_code, ovmf_vars = ovmf_dir / 'OVMF_CODE.fd', ovmf_dir / 'OVMF_VARS.fd' |
| 65 | + |
| 66 | + qemu_flags = [ |
| 67 | + # Disable default devices. |
| 68 | + '-nodefaults', |
| 69 | + # Use a standard VGA for graphics. |
| 70 | + '-vga', 'std', |
| 71 | + # Use a modern machine, with acceleration if possible. |
| 72 | + '-machine', 'q35,accel=kvm:zen:hax:tcg', |
| 73 | + # Allocate some memory. |
| 74 | + '-m', '128M', |
| 75 | + # Set up OVMF. |
| 76 | + '-drive', f'if=pflash,format=raw,file={ovmf_code},readonly=on', |
| 77 | + '-drive', f'if=pflash,format=raw,file={ovmf_vars}', |
| 78 | + # Create AHCI controller. |
| 79 | + '-device', 'ahci,id=ahci,multifunction=on', |
| 80 | + # Mount a local directory as a FAT partition. |
| 81 | + '-drive', f'if=none,format=raw,file=fat:rw:{ESP_DIR},id=esp', |
| 82 | + '-device', 'ide-drive,bus=ahci.0,drive=esp', |
| 83 | + # Only enable when debugging UEFI boot: |
| 84 | + #'-debugcon', 'file:debug.log', '-global', 'isa-debugcon.iobase=0x402', |
| 85 | + ] |
| 86 | + |
| 87 | + sp.run([qemu, *qemu_flags]).check_returncode() |
| 88 | + |
| 89 | + |
| 90 | +def main(args) -> int: |
| 91 | + # Clear any Rust flags which might affect the build. |
| 92 | + os.environ['RUSTFLAGS'] = '' |
| 93 | + |
| 94 | + if len(args) < 2: |
| 95 | + print("Expected at least one parameter (the commands to run): build / doc / run / clippy") |
| 96 | + return 1 |
| 97 | + |
| 98 | + cmds = args[1:] |
| 99 | + |
| 100 | + KNOWN_CMDS = { |
| 101 | + 'build': build, |
| 102 | + 'doc': doc, |
| 103 | + 'run': run_qemu, |
| 104 | + } |
| 105 | + |
| 106 | + for cmd in cmds: |
| 107 | + if cmd in KNOWN_CMDS: |
| 108 | + KNOWN_CMDS[cmd]() |
| 109 | + else: |
| 110 | + print("Unknown verb:", cmd) |
| 111 | + return 1 |
| 112 | + |
| 113 | +if __name__ == '__main__': |
| 114 | + sys.exit(main(sys.argv)) |
0 commit comments