BUILD_DIR="./target" BIN="$BUILD_DIR/poppin.bin" ISO="$BUILD_DIR/poppin.iso" set -e function cmd_exists() { if ! command -v $1 &> /dev/null then echo "The command '$1' could not be found please install it" exit 1 fi } function pre_build() { cmd_exists cargo cmd_exists xorriso cmd_exists grub-file cmd_exists ld cmd_exists grub-mkrescue cmd_exists qemu-system-x86_64 } function build_kernel() { echo "INFO: Building poppin-kernel" pushd kernel > /dev/null case $1 in dev) cargo build --target-dir ../${BUILD_DIR} ;; *) cargo build --release --target-dir ../${BUILD_DIR} ;; esac popd > /dev/null } function build_boot() { echo "INFO: Building boot code" nasm -felf64 -o $BUILD_DIR/long_mode.o boot/long_mode.s nasm -felf64 -o $BUILD_DIR/boot.o boot/boot.s nasm -felf64 -o $BUILD_DIR/multiboot.o boot/multiboot.s } function link(){ echo "INFO: Linking" ld -melf_x86_64 -n \ -T linker.ld \ -o ${BIN} \ ${BUILD_DIR}/multiboot.o \ ${BUILD_DIR}/boot.o \ ${BUILD_DIR}/long_mode.o \ $1 # kernel path } function check_mb() { if grub-file --is-x86-multiboot2 $BIN; then echo "INFO: Kernel conatins multiboot2" else echo "ERROR: Kernel doesnt contain multiboot2" exit 1 fi } function make_iso() { echo "INFO: Building ISO" mkdir -p ${BUILD_DIR}/iso/boot/grub cp ${BUILD_DIR}/poppin.bin ${BUILD_DIR}/iso/boot/poppin.bin cp boot/grub.cfg ${BUILD_DIR}/iso/boot/grub/grub.cfg grub-mkrescue -o ${ISO} ${BUILD_DIR}/iso > /dev/null 2>&1 } function run_qemu() { qemu-system-x86_64 \ -cdrom $ISO \ -serial stdio \ -device VGA # -d int \ } function run_qemu_gdb { qemu-system-x86_64 \ -cdrom $ISO \ -device VGA \ -d int \ -s -S -monitor stdio } function print_help() { echo "Usage: ./build.sh [command]" echo "Commands:" echo " run-qemu - Compiles and Runs the built iso with qemu" echo " build - Compiles everything" } function build() { pre_build build_kernel build_boot link ${BUILD_DIR}/x86_64-unknown-none/release/libkernel.a check_mb make_iso } function build_dev() { pre_build build_kernel dev build_boot link ${BUILD_DIR}/x86_64-unknown-none/debug/libkernel.a check_mb make_iso } case $1 in run-qemu) build run_qemu ;; build) build ;; build-dev) build_dev ;; run-qemu-gdb) build run_qemu_gdb ;; *) if [ $# -gt 0 ]; then echo "ERROR: Unknown option '$1'" fi print_help exit 1 ;; esac