crossing-the-boundary/chapters/01-freestanding/justfile
2026-11-22 09:33:26 -05:00

111 lines
No EOL
3.5 KiB
Makefile

# run `just` to list recipes. override a setting with `just <name>=<value> <recipe>`,
# e.g. `just profile=release run` or `just cmdline="foo=bar" run`
set shell := ["bash", "-euo", "pipefail", "-c"]
arch := "x86_64"
profile := "debug"
cmdline := ""
test_timeout := "60"
nasm := "nasm"
ld := "ld"
qemu := "qemu-system-" + arch
target := arch + "-unknown-none"
out := "build"
boot := "kernel/src/arch" / arch / "boot"
kernel_bin := out / "kernel.bin"
kernel_iso := out / "kernel.iso"
test_bin := out / "test.bin"
test_iso := out / "test.iso"
lib := "target" / target / profile / "libkernel.a"
linker := boot / "linker.ld"
cargo_flags := "--target " + target + if profile == "release" { " --release" } else { "" }
ldflags := "-n --gc-sections -z noexecstack --no-warn-rwx-segments"
qemu_flags := "-serial stdio -no-reboot -display none"
test_qemu_flags := qemu_flags + " -device isa-debug-exit,iobase=0xf4,iosize=0x04"
# list the recipes
default:
@just --list
# build and link the kernel binary
build: (_link "" kernel_bin)
# build a bootable ISO image
iso: build (_iso kernel_bin kernel_iso)
# build and boot in QEMU; extra arguments go to QEMU
run *args: iso
{{qemu}} -cdrom {{kernel_iso}} {{qemu_flags}} {{args}}
# boot halted, waiting for gdb on :1234
debug: iso
{{qemu}} -cdrom {{kernel_iso}} {{qemu_flags}} -s -S
# boot the test kernel and report the results
test: (_link "--features test" test_bin) (_iso test_bin test_iso)
#!/usr/bin/env bash
# isa-debug-exit makes QEMU exit with (code << 1) | 1, 33 is a pass
# timeout(1) exits 124 from a hanging test
set -uo pipefail
timeout --foreground {{test_timeout}} {{qemu}} -cdrom {{test_iso}} {{test_qemu_flags}} </dev/null
status=$?
case $status in
33) echo PASS ;;
124) echo TIMEOUT; exit 1 ;;
*) echo "FAIL (qemu exited $status)"; exit 1 ;;
esac
# rustfmt and clippy, with/without tests
check:
cargo fmt --all --check
cargo clippy {{cargo_flags}} --workspace -- -D warnings
cargo clippy {{cargo_flags}} -p kernel --features test -- -D warnings
# install the toolchain pinned in rust-toolchain.toml
toolchain:
rustup toolchain install
# remove build artifacts
clean:
rm -rf {{out}}
cargo clean
# assemble the boot code, build the kernel lib with `features`, and link
# them into `bin`. both builds share the library's path, so link after
_link features bin:
#!/usr/bin/env bash
set -euo pipefail
mkdir -p {{out}}/asm
objs=()
for src in {{boot}}/*.asm; do
obj="{{out}}/asm/$(basename "$src" .asm).o"
{{nasm}} -f elf64 "$src" -o "$obj"
objs+=("$obj")
done
cargo build {{cargo_flags}} -p kernel {{features}}
{{ld}} {{ldflags}} -T {{linker}} -o {{bin}} "${objs[@]}" --whole-archive {{lib}} --no-whole-archive
# wrap the kernei at `bin` in a bootable GRUB image at `image`
_iso bin image:
#!/usr/bin/env bash
set -euo pipefail
mkrescue="$(command -v grub2-mkrescue || command -v grub-mkrescue || true)"
if [[ -z "$mkrescue" ]]; then
echo "grub-mkrescue or grub2-mkrescue not found; install grub and xorriso" >&2
exit 1
fi
root={{out}}/isofiles
rm -rf "$root"
mkdir -p "$root/boot/grub"
cp {{bin}} "$root/boot/kernel.bin"
printf '%s\n' 'set timeout=0' 'set default=0' 'menuentry "Crossing the Boundary" {' \
' multiboot2 /boot/kernel.bin {{cmdline}}' \
' boot' '}' > "$root/boot/grub/grub.cfg"
"$mkrescue" -o {{image}} "$root" >{{out}}/grub.log 2>&1 || { cat {{out}}/grub.log; exit 1; }
rm -rf "$root"