#!/bin/sh onerr() { echo -e "\x1b[1;31merror: '$action' failed\x1b[m" exit 1 } trap onerr ERR name="uff" build_mode=debug action=help target=x86_64 target_name="$target-$name" target_path="target/" rust_target_path="$target_path/$target_name/$build_mode/" iso_path="$target_path/iso/" obj_path="$iso_path/obj/" src_path="src/" asm_path="$src_path/" link_script="$src_path/linker.ld" grub_cfg="$src_path/grub.cfg" print_help() { echo "usage: $0 (options) [action]" echo "options:" echo " -mode= set build mode (standard: debug)" exit } get_rust_bin() { case "$action" in "test") echo "$rust_target_path/$(ls -t1 $rust_target_path | grep -P '^kernel-[a-fA-F0-9]+$' | head -n1)";; *) echo "$rust_target_path/libkernel.a";; esac } prepare_iso() { mkdir "$target_path" &> /dev/null mkdir "$iso_path" &> /dev/null mkdir "$obj_path" &> /dev/null mkdir "$iso_path/isofiles" &> /dev/null mkdir "$iso_path/isofiles/boot" &> /dev/null mkdir "$iso_path/isofiles/boot/grub" &> /dev/null for f in "$asm_path"/*.asm; do nasm -felf64 "$f" -o "$obj_path/$(basename -s .asm $f).o" done cp "$grub_cfg" "$iso_path/isofiles/boot/grub/grub.cfg" } build_iso() { ld -n -o "$iso_path/isofiles/boot/kernel.bin" -gc-sections -T "$link_script" "$obj_path"/*.o "$(get_rust_bin)" grub-mkrescue -d /usr/lib/grub/i386-pc -o "$iso_path/uff.iso" "$iso_path/isofiles" } build() { if test ! -d "$iso_path/isofiles"; then prepare_iso fi cargo xbuild build_iso } build_test() { if test ! -d "$iso_path/isofiles"; then prepare_iso fi RUSTFLAGS="-Clink-arg=-r -Clink-dead-code" cargo xtest --no-run build_iso } run() { qemu-system-x86_64 -cdrom "$iso_path/uff.iso" } for arg in "$@"; do case "$arg" in -mode=*) build_mode="$(echo $arg | sed "s/^-mode=//")";; "run") action=run;; "build") action=build;; "test") action=test;; "help") action=help;; *) echo "warn: ignoring unknown option '$arg'";; esac done case "$action" in "help") print_help;; "build") build;; "test") build_test; run;; "run") build; run;; esac echo -e "\x1b[1;32mdone\x1b[m"