~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
	crystal build --no-debug -Dkernel --cross-compile --target i386-unknown-linux-elf --prelude=../prelude.cr -o ./build/kernel ./kernel/kernel.cr
	# The object file should be at ./build/kernel.o, now link it with the entry point
	ld -m elf_i386 -nostdlib -T ./kernel/linker.ld -o ./bin/kernel.bin ./build/kernelEntryPoint.o ./build/kernel.o || exit 1
else
	clang++ $CC_FLAGS $LD_FLAGS $CPP_FILES || exit 1
fi

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

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

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

exit 0