CFLAGS = -Iinclude -Iefi/inc -ffreestanding -fno-stack-protector -fno-stack-check -fshort-wchar -mno-red-zone -std=c17 -Wwrite-strings -fcf-protection=none -g
CXXFLAGS = -Iinclude -Iefi/inc -ffreestanding -fno-stack-protector -fno-stack-check -fshort-wchar -mno-red-zone -maccumulate-outgoing-args -std=c++17 -Wwrite-strings -fcf-protection=none -g
LDFLAGS = -shared -Bsymbolic -Tefi/gnuefi/elf_x86_64_efi.lds
LDLIBS = --no-undefined

KERNEL_CXXFLAGS = $(CXXFLAGS) -DGNU_EFI_USE_MS_ABI -fno-pic
KERNEL_LDFLAGS = -Tkernel/kernel.ld -no-pie -z max-page-size=0x1000

EFI_CFLAGS = -Iefi/inc -Iefi/inc/x86_64 -Iefi/inc/protocol \
             -ffreestanding -fno-stack-protector -fno-stack-check \
             -fshort-wchar -mno-red-zone -maccumulate-outgoing-args \
             -fPIC -fno-strict-aliasing -Wall -Wextra -Wstrict-prototypes \
             -DGNU_EFI_USE_MS_ABI -DCONFIG_x86_64 -std=c11 -O2 -g \
             -fcf-protection=none

BOOT_OBJ = build/boot.o

KERNEL_ASMFLAGS = -Iinclude -Iefi/inc -ffreestanding -fno-stack-protector -fno-stack-check \
                  -fshort-wchar -mno-red-zone -fcf-protection=none -g

KERNEL_CPP := $(shell find kernel graphics fonts -name '*.cpp' -type f)
KERNEL_ASM := $(shell find kernel -name '*.S' -type f)

KERNEL_OBJ := $(KERNEL_CPP:%.cpp=build/%.o) $(KERNEL_ASM:%.S=build/%.o)
KERNEL_DIRS := $(sort $(dir $(KERNEL_OBJ)))

EFI_TOP_C = $(wildcard efi/lib/*.c)
EFI_TOP_S = $(wildcard efi/lib/*.S)
EFI_X86_C = $(wildcard efi/lib/x86_64/*.c)
EFI_X86_S = $(wildcard efi/lib/x86_64/*.S)
EFI_RT_C  = $(wildcard efi/lib/runtime/*.c)

EFI_CRT0_OBJ = build/efi/gnuefi/crt0-efi-x86_64.o
EFI_RELOC_OBJ = build/efi/gnuefi/reloc_x86_64.o
EFI_TOP_OBJ = $(EFI_TOP_C:efi/lib/%.c=build/efi/lib/%.o) $(EFI_TOP_S:efi/lib/%.S=build/efi/lib/%.o)
EFI_X86_OBJ = $(EFI_X86_C:efi/lib/x86_64/%.c=build/efi/lib/x86_64/%.o) $(EFI_X86_S:efi/lib/x86_64/%.S=build/efi/lib/x86_64/%.o)
EFI_RT_OBJ  = $(EFI_RT_C:efi/lib/runtime/%.c=build/efi/lib/runtime/%.o)
EFI_OBJ = $(EFI_CRT0_OBJ) $(EFI_RELOC_OBJ) $(EFI_TOP_OBJ) $(EFI_X86_OBJ) $(EFI_RT_OBJ)

all: _bd $(EFI_OBJ) $(BOOT_OBJ) $(KERNEL_OBJ)
	@echo "* Linking bootloader..."
	@ld $(LDFLAGS) $(EFI_OBJ) $(BOOT_OBJ) -o build/boot.so $(LDLIBS)
	@objcopy -j .text -j .sdata -j .data -j .rodata -j .dynamic -j .dynsym -j .rel -j .rela -j .rel.* -j .rela.* -j .reloc --output-target efi-app-x86_64 --subsystem=10 build/boot.so build/BOOTX64.EFI
	@echo "* Linking Kernel.elf..."
	@ld $(KERNEL_LDFLAGS) $(KERNEL_OBJ) -o build/Kernel.elf
	@echo "Done."

_bd:
	@mkdir -p $(KERNEL_DIRS) build/efi/gnuefi build/efi/lib build/efi/lib/x86_64 build/efi/lib/runtime

$(EFI_CRT0_OBJ): efi/gnuefi/crt0-efi-x86_64.S | _bd
	@echo "Compile AS $<"
	@gcc $(EFI_CFLAGS) -c $< -o $@

$(EFI_RELOC_OBJ): efi/gnuefi/reloc_x86_64.c | _bd
	@echo "Compile C $<"
	@gcc $(EFI_CFLAGS) -c $< -o $@

build/efi/lib/%.o: efi/lib/%.c | _bd
	@echo "Compile C $<"
	@gcc $(EFI_CFLAGS) -c $< -o $@

build/efi/lib/%.o: efi/lib/%.S | _bd
	@echo "Compile AS $<"
	@gcc $(EFI_CFLAGS) -c $< -o $@

build/efi/lib/x86_64/%.o: efi/lib/x86_64/%.c | _bd
	@echo "Compile C $<"
	@gcc $(EFI_CFLAGS) -c $< -o $@

build/efi/lib/x86_64/%.o: efi/lib/x86_64/%.S | _bd
	@echo "Compile AS $<"
	@gcc $(EFI_CFLAGS) -c $< -o $@

build/efi/lib/runtime/%.o: efi/lib/runtime/%.c | _bd
	@echo "Compile C $<"
	@gcc $(EFI_CFLAGS) -c $< -o $@

build/%.o: %.c
	@echo "Compile C $<"
	@gcc $(CFLAGS) -c $< -o $@

build/%.o: %.cpp | _bd
	@echo "Compile CPP $<"
	@g++ $(KERNEL_CXXFLAGS) -c $< -o $@

build/%.o: %.S | _bd
	@echo "Compile AS $<"
	@gcc $(KERNEL_ASMFLAGS) -c $< -o $@

vdir: all
	@mkdir -p vdir/EFI/BOOT vdir/sys
	@cp build/BOOTX64.EFI vdir/EFI/BOOT
	@cp build/Kernel.elf vdir/
	@cp -r resources vdir/sys/

disk: vdir
	@echo "* Building FAT32 disk image (128 MiB)..."
	@dd if=/dev/zero of=build/disk.img bs=1M count=128 status=none
	@mformat -i build/disk.img -F -T 262144 -h 16 -s 32 ::
	@mmd -i build/disk.img ::/EFI ::/EFI/BOOT ::/sys
	@mcopy -i build/disk.img -s vdir/EFI/BOOT/BOOTX64.EFI ::/EFI/BOOT/
	@mcopy -i build/disk.img -s vdir/Kernel.elf ::/
	@mcopy -i build/disk.img -s vdir/sys/resources ::/sys/

run: disk
	@echo "Launching QEMU"
	@qemu-system-x86_64 -bios /usr/share/ovmf/OVMF.fd -net none -drive file=build/disk.img,index=0,format=raw -serial stdio -serial file:serial.log -s -S

clean:
	@echo "Cleaning old files"
	@rm -rf build vdir

.PHONY: all vdir disk run clean _bd
