~build_image.sh
.sh
Shell
(text/x-sh)
#!/bin/bash

# Flags

CC_FLAGS="-ffreestanding -nostdinc -nostdinc++ \
					-Wall -Wextra \
					-o ./bin/kernel.bin -target i386-pc-none-elf \
					-I ./kernel/"
LD_FLAGS="-nostdlib -Wl,--oformat=binary,-T./kernel/linker.ld"
CPP_FILES="./kernel/unityBuild.cpp ./build/kernelEntryPoint.o"

# Build

mkdir {bin,build} 2> /dev/null

echo -e "Cleaning..."
rm *.bin *.img *.iso *.o *.vmdk 2> /dev/null

echo -e "\nBuilding..."
nasm ./boot/bootloader.asm -I ./boot/ -o ./bin/bootloader.bin || exit 1
nasm -felf32 ./kernel/entry.asm -o ./build/kernelEntryPoint.o || exit 1

use_crystal_kernel=true

if [ "$use_crystal_kernel" = true ]
then
	# Build crystal kernel without stdlib, using the custom prelude, and targeting i386-unknown-linux-elf
	crystal build \
		--no-debug -Dkernel \
		--cross-compile --target i386-unknown-linux-elf \
		--prelude=../prelude.cr \
		-o ./build/kernel ./kernel/kernel.cr

	# Link the kernel into an ELF executable, then convert it to a flat binary for the bootloader.
	ld -m elf_i386 -nostdlib -T ./kernel/linker.ld -o ./bin/kernel.elf ./build/kernelEntryPoint.o ./build/kernel.o || exit 1
	objcopy -O binary ./bin/kernel.elf ./bin/kernel.bin || exit 1
else
	clang++ $CC_FLAGS $LD_FLAGS $CPP_FILES || exit 1
fi

cp ./bin/kernel.bin ./iso/boot/kernel.bin || exit 1

./tools/genVDisk --output "crystalos.img" --floppy \
				  --bootloader ./bin/bootloader.bin --kernel ./bin/kernel.bin

# Run

echo -e "\nRunning..."
qemu-system-i386 -drive if=floppy,index=0,format=raw,file=crystalos.img || exit 1

exit 0