CFLAGS = -ffreestanding -nostdlib -fno-builtin -fno-stack-protector -Wall -Wextra
LDFLAGS = -ffreestanding -nostdlib -lgcc
ASFLAGS =

CC = i686-elf-gcc
AS = i686-elf-as
LD = i686-elf-gcc
GRUB_FILE = grub-file
GRUB_MKRESCUE = grub-mkrescue

KERNEL = myos.bin
ISO = myos.iso

SOURCES_C = kernel.c gdt.c
SOURCES_ASM = boot.s idt_load.s gdt_flush.s pit_handler.s
OBJECTS = $(SOURCES_C:.c=.o) $(SOURCES_ASM:.s=.o)

ISO_DIR = isodir
GRUB_DIR = $(ISO_DIR)/boot/grub

.PHONY: all clean iso

all: $(ISO)

$(KERNEL): $(OBJECTS) linker.ld
	$(LD) -T linker.ld -o $(KERNEL) $(OBJECTS) $(LDFLAGS)

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

%.o: %.s
	$(AS) $(ASFLAGS) $< -o $@

iso: $(ISO)

$(ISO): $(KERNEL) grub.cfg
	mkdir -p $(GRUB_DIR)
	cp $(KERNEL) $(ISO_DIR)/boot/$(KERNEL)
	cp grub.cfg $(GRUB_DIR)/grub.cfg
	$(GRUB_MKRESCUE) -o $(ISO) $(ISO_DIR)

check-multiboot: $(KERNEL)
	$(GRUB_FILE) --is-x86-multiboot $(KERNEL) && echo "multiboot confirmed" || echo "the file is not multiboot"

clean:
	rm -f $(OBJECTS) $(KERNEL) $(ISO)
	rm -rf $(ISO_DIR)
