CC = gcc
LD = ld
CFLAGS = -Wall -Wextra -std=c11 -ffreestanding -fno-stack-protector -fno-stack-check -fno-lto -fPIE -m64 -march=x86-64 -Isrc
LDFLAGS = -T linker.ld -static -nostdlib -no-pie -z max-page-size=0x1000

# Explicitly define the objects
OBJS = build/kernel.o build/drivers/tty.o build/drivers/keyboard.o

all: Fflorid.iso

# Rule to make the build directories
build_dirs:
	mkdir -p build/drivers build/shell

# Rule to compile C files
build/%.o: src/%.c | build_dirs
	$(CC) $(CFLAGS) -c $< -o $@

# THE MISSING RULE: Link the kernel
build/kernel.bin: $(OBJS)
	$(LD) $(LDFLAGS) $(OBJS) -o build/kernel.bin

# Build the ISO
Fflorid.iso: build/kernel.bin
	rm -rf iso_root
	mkdir -p iso_root
	cp build/kernel.bin iso_root/kernel
	cp limine.cfg iso_root/limine.cfg
	cp limine/limine-bios.sys iso_root/limine-bios.sys
	cp limine/limine-bios-cd.bin iso_root/limine-bios-cd.bin
	xorriso -as mkisofs -b limine-bios-cd.bin \
		-no-emul-boot -boot-load-size 4 -boot-info-table \
		iso_root -o Fflorid.iso
	./limine/limine bios-install Fflorid.iso

run: Fflorid.iso
	qemu-system-x86_64 -cdrom Fflorid.iso

clean:
	rm -rf build iso_root Fflorid.iso