LD=../compiler/indigo_gcc/bin/x86_64-elf-ld
CC=../compiler/indigo_gcc/bin/x86_64-elf-gcc
OBJCPY=../compiler/indigo_gcc/bin/x86_64-elf-objcopy
INC=-Ikernel/include

EMU_CORES=4
EMU_RAM=2G
XRES=1024
YRES=768

#TODO clean up make vs debug

make: 
	nasm -g -felf64 bootloader/bootloader.asm -o objects/bootloader.o
	$(CC) $(INC) -g -ffreestanding -c kernel/kernel.c -o objects/kernel.o
	$(CC) $(INC) -g -ffreestanding -c kernel/libs/acpi.c -o objects/acpi.o
	$(CC) $(INC) -g -ffreestanding -c kernel/libs/drivers/serial.c -o objects/serial.o
	$(CC) $(INC) -g -ffreestanding -c kernel/libs/drivers/video.c -o objects/video.o
	$(CC) $(INC) -g -ffreestanding -c kernel/libs/printf.c -o objects/printf.o
	$(CC) $(INC) -g -ffreestanding -c kernel/libs/page.c -o objects/page.o
	$(CC) $(INC) -g -ffreestanding -c kernel/libs/libc.c -o objects/libc.o
	$(LD) -o indigo_os.elf --oformat=elf64-x86-64 -T link.ld
	$(OBJCPY) --only-keep-debug indigo_os.elf debug/debug_syms.o
	$(OBJCPY) -O binary --strip-all indigo_os.elf indigo_os
ifneq ("$(wildcard $(./debug/serial.in))","")
	mkfifo debug/serial.in
endif
ifneq ("$(wildcard $(./debug/serial.out))","")
	mkfifo debug/serial.out
endif
	rm -f indigo_os.elf


run:
	qemu-system-x86_64 -smp $(EMU_CORES) -m $(EMU_RAM) -nographic -drive format=raw,file=./indigo_os
	

gdb: indigo_os
	tmux new-session -s os_gdb "qemu-system-x86_64 -smp $(EMU_CORES) -nographic -S -s -drive format=raw,file=./indigo_os -m $(EMU_RAM)"\;\
		split-window -h "gdb -x debug/gdbinit.gdb; killall qemu-system-x86_64"

run-graphical:
	qemu-system-x86_64 -S -s -smp $(EMU_CORES) -serial pipe:debug/serial -device VGA,edid=on,xres=$(XRES),yres=$(YRES) -drive format=raw,file=./indigo_os &\
		gdb -x debug/gdbinit.gdb; killall qemu-system-x86_64

clean:
	rm -f objects/*
	rm -f indigo_os
	rm -f bin/*
	rm -f debug/debug_syms.o
	rm -f debug/serial.in
	rm -f debug/serial.out
	rm -f indigo_os.elf
