# Nuke built-in rules.
.SUFFIXES:

# This is the name that our final executable will have.
# Change as needed.
override OUTPUT := kirkos

# User controllable toolchain and toolchain prefix.
TOOLCHAIN :=
TOOLCHAIN_PREFIX :=
ifneq ($(TOOLCHAIN),)
	ifeq ($(TOOLCHAIN_PREFIX),)
		TOOLCHAIN_PREFIX := $(TOOLCHAIN)-
	endif
endif

# User controllable C compiler command.
ifneq ($(TOOLCHAIN_PREFIX),)
	CC := $(TOOLCHAIN_PREFIX)gcc
else
	CC := cc
endif

# User controllable linker command.
LD := $(TOOLCHAIN_PREFIX)ld

# Defaults overrides for variables if using "llvm" as toolchain.
ifeq ($(TOOLCHAIN),llvm)
	CC := clang
	LD := ld.lld
endif

# User controllable C flags.
CFLAGS := -g -O2 -pipe

# User controllable C preprocessor flags. We set none by default.
CPPFLAGS :=

# User controllable nasm flags.
NASMFLAGS := -g

ISO_ROOT := iso_root
ISO := image.iso

EXT2_ROOT := ext2_root
DISK_IMG := disk.img

# User controllable linker flags. We set none by default.
LDFLAGS :=

# Check if CC is Clang.
override CC_IS_CLANG := $(shell ! $(CC) --version 2>/dev/null | grep -q '^Target: '; echo $$?)

# If the C compiler is Clang, set the target as needed.
ifeq ($(CC_IS_CLANG),1)
	override CC += \
		-target x86_64-unknown-none-elf
endif

# Internal C flags that should not be changed by the user.
override CFLAGS += \
	-Wall \
	-Wextra \
	-std=gnu11 \
	-ffreestanding \
	-fno-stack-protector \
	-fno-stack-check \
	-fno-lto \
	-fno-PIC \
	-ffunction-sections \
	-fdata-sections \
	-m64 \
	-march=x86-64 \
	-mabi=sysv \
	-mno-80387 \
	-mno-mmx \
	-mno-sse \
	-mno-sse2 \
	-mno-red-zone \
	-mcmodel=kernel

# Internal C preprocessor flags that should not be changed by the user.
override CPPFLAGS := \
	-I src \
	$(CPPFLAGS) \
	-MMD \
	-MP

# Internal nasm flags that should not be changed by the user.
override NASMFLAGS := \
	-f elf64 \
	$(patsubst -g,-g -F dwarf,$(NASMFLAGS)) \
	-Wall

# Internal linker flags that should not be changed by the user.
override LDFLAGS += \
	-m elf_x86_64 \
	-nostdlib \
	-static \
	-z max-page-size=0x1000 \
	--gc-sections \
	-T linker.lds

# Use "find" to glob all *.c, *.S, and *.asm files in the tree and obtain the
# object and header dependency file names.
override SRCFILES := $(shell find -L src -type f 2>/dev/null | LC_ALL=C sort)
override CFILES := $(filter %.c,$(SRCFILES))
override ASFILES := $(filter %.S,$(SRCFILES))
override NASMFILES := $(filter %.asm,$(SRCFILES))
override OBJ := $(addprefix obj/,$(CFILES:.c=.c.o) $(ASFILES:.S=.S.o) $(NASMFILES:.asm=.asm.o))
override HEADER_DEPS := $(addprefix obj/,$(CFILES:.c=.c.d) $(ASFILES:.S=.S.d))

# Default target. This must come first, before header dependencies.
.PHONY: all run clean-iso iso limine debug
all: bin/$(OUTPUT)

LIMINE_DIR := limine

# Include header dependencies.
-include $(HEADER_DEPS)

# Link rules for the final executable.
bin/$(OUTPUT): GNUmakefile linker.lds $(OBJ)
	mkdir -p "$(dir $@)"
	$(LD) $(LDFLAGS) $(OBJ) -o $@

# Compilation rules for *.c files.
obj/%.c.o: %.c GNUmakefile
	mkdir -p "$(dir $@)"
	$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@

# Compilation rules for *.S files.
obj/%.S.o: %.S GNUmakefile
	mkdir -p "$(dir $@)"
	$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@

# Compilation rules for *.asm (nasm) files.
obj/%.asm.o: %.asm GNUmakefile
	mkdir -p "$(dir $@)"
	nasm $(NASMFLAGS) $< -o $@


$(DISK_IMG): $(EXT2_ROOT)
	@echo "Creating ext2 disk image from $(EXT2_ROOT)..."
	dd if=/dev/zero of=$(DISK_IMG) bs=1M count=32
	mkfs.ext2 -F -L KIRKOS $(DISK_IMG)
	mkdir -p mnt
	sudo mount -o loop $(DISK_IMG) mnt
	sudo cp -r $(EXT2_ROOT)/* mnt/
	sudo umount mnt
	rmdir mnt
	@echo "Done: $(DISK_IMG) created"

# Build ISO directory structure
$(ISO_ROOT):
	mkdir -p $(ISO_ROOT)/boot
	mkdir -p $(ISO_ROOT)/boot/limine
	mkdir -p $(ISO_ROOT)/EFI/BOOT

# Copy kernel
$(ISO_ROOT)/boot/$(OUTPUT): bin/$(OUTPUT) | $(ISO_ROOT)
	cp -v bin/$(OUTPUT) $(ISO_ROOT)/boot/


# Copy limine BIOS/UEFI files
limine-files: | $(ISO_ROOT)
	cp -v limine.conf \
		$(LIMINE_DIR)/limine-bios.sys \
		$(LIMINE_DIR)/limine-bios-cd.bin \
		$(LIMINE_DIR)/limine-uefi-cd.bin \
		$(ISO_ROOT)/boot/limine

	cp -v $(LIMINE_DIR)/BOOTX64.EFI $(ISO_ROOT)/EFI/BOOT/
	cp -v $(LIMINE_DIR)/BOOTIA32.EFI $(ISO_ROOT)/EFI/BOOT/

# Build ISO
iso: all $(ISO_ROOT)/boot/$(OUTPUT) limine-files
	xorriso -as mkisofs -R -r -J \
		-b boot/limine/limine-bios-cd.bin \
		-no-emul-boot -boot-load-size 4 -boot-info-table \
		-hfsplus \
		-apm-block-size 2048 \
		--efi-boot boot/limine/limine-uefi-cd.bin \
		-efi-boot-part --efi-boot-image \
		--protective-msdos-label \
		$(ISO_ROOT) -o $(ISO)

	./$(LIMINE_DIR)/limine bios-install $(ISO)

# Run in QEMU
run: $(DISK_IMG) iso
	qemu-system-x86_64 -cdrom $(ISO) -hda $(DISK_IMG) -debugcon stdio -m 8G -smp 2 -device ich9-intel-hda -device hda-duplex


debug: iso $(DISK_IMG)
	qemu-system-x86_64 \
		-cdrom $(ISO) -hda $(DISK_IMG) \
		-s -S -debugcon stdio -d int,cpu_reset -m 8G -smp 2 -device ich9-intel-hda,id=hda -device hda-duplex,id=codec0,bus=hda.0,cad=0

# Clean ISO artifacts
clean-iso:
	rm -rf $(ISO_ROOT) $(ISO)

# Remove object files and the final executable.
.PHONY: clean
clean:
	rm -rf bin obj $(DISK_IMG)