AS=yasm -felf32
CC=i386-elf-gcc
QEMU=qemu-system-x86_64 -serial stdio -d guest_errors

CFLAGS=-std=gnu99 -ffreestanding -O2 -W -Wall -Wextra -I ./

KERNEL_CC_LIST:=\
kernel/kernel.o \
kernel/kernel_sh.o \
kernel/k_utils/k_utils.o \
kernel/drivers/tty/tty.o \
kernel/drivers/keyboard/keyboard.o \
kernel/drivers/cmos/cmos.o \
kernel/lib/io/io.o \
kernel/lib/strings/strings.o \
kernel/lib/time/time.o \

default: kernel

all: kernel

.PHONY: kernel
kernel: $(KERNEL_CC_LIST)
	$(AS) boot/boot.asm -o ./build/boot.o
	$(CC) -T linker/linker.ld -o build/kernel.bin -ffreestanding -O2 -nostdlib ./build/boot.o $^ -lgcc

%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

qemu: kernel
	 $(QEMU) -kernel build/kernel.bin

iso: kernel
	cp build/kernel.bin build/isodir/boot/kernel.bin
	cp boot/grub.cfg build/isodir/boot/grub/grub.cfg
	vagrant ssh -c 'cd /vagrant/build && grub-mkrescue -o kernel.iso isodir 2>/dev/null 1>&2'

qemu-iso: iso
	$(QEMU) -cdrom build/kernel.iso

.PHONY: clean
clean: $(KERNEL_CC_LIST)
	rm $^
	rm build/*.*
