LIMINE_DIR = $(abspath dependencies/limine)
TOOLCHAIN_DIR = $(abspath toolchain)

HILBERT_NASM = nasm -f elf64
HILBERT_CC = ${TOOLCHAIN_DIR}/usr/bin/x86_64-elf-c++ -std=c++17 -Wall \
  -Wextra -O3 -ggdb -static -fno-exceptions -fno-rtti -mno-sse -I include \
  -I $(abspath dependencies/mintsuki-headers) -I $(abspath euler/include) \
  -I $(abspath libraries/daguerre/include)
HILBERT_AR = ${TOOLCHAIN_DIR}/usr/bin/x86_64-elf-ar
HILBERT_LD = ${TOOLCHAIN_DIR}/usr/bin/x86_64-elf-ld -z noexecstack

.EXPORT_ALL_VARIABLES:

LIB_DIR = toolchain/usr/x86_64-elf/lib/

LIMINE_DEP = dependencies/.limine-done
MINTSUKI_HEADERS_DEP = dependencies/.mintsuki-headers-done
BINUTILS_DEP = toolchain/.binutils-done
GCC_DEP = toolchain/.gcc-done
LIBGCC_DEP = toolchain/.libgcc-done

EULER_DEP = toolchain/.euler-done
DAGUERRE_DEP = toolchain/.daguerre-done

APP_DEPS = ${GCC_DEP} ${LIBGCC_DEP} ${EULER_DEP} ${MINTSUKI_HEADERS_DEP}
LIBRARY_DEPS = ${GCC_DEP} ${MINTSUKI_HEADERS_DEP}

.PHONY: default run clean clean-dependencies

default: build/disk.iso

run: build/disk.iso
	gdb -x qemu.gdb

clean:
	rm -rf build ${EULER_DEP} ${DAGUERRE_DEP}
	make -C euler clean
	make -C kernel clean
	make -C applications/init clean
	make -C libraries/daguerre clean

clean-dependencies: clean
	rm -rf toolchain dependencies

${LIMINE_DEP}:
	mkdir -p dependencies
	test -e dependencies/limine || git clone --depth 1 -b v7.5.1 https://github.com/limine-bootloader/limine dependencies/limine
	cd ${LIMINE_DIR} && ./bootstrap
	cd ${LIMINE_DIR} && ./configure --enable-bios --enable-bios-cd
	+make -C ${LIMINE_DIR}
	touch $@

${MINTSUKI_HEADERS_DEP}:
	mkdir -p dependencies
	test -e dependencies/mintsuki-headers || git clone --depth 1 https://github.com/mintsuki/freestanding-headers dependencies/mintsuki-headers
	touch $@

${BINUTILS_DEP}:
	mkdir -p dependencies toolchain/usr
	test -e dependencies/binutils || (git clone --depth 1 -b binutils-2_42 https://sourceware.org/git/binutils-gdb dependencies/binutils && cd dependencies/binutils && git apply ../../patches/binutils.txt)
	mkdir -p dependencies/binutils/build
	cd dependencies/binutils/build && ../configure --disable-gdb \
	  --target=x86_64-elf --prefix=${TOOLCHAIN_DIR}/usr
	+make -C dependencies/binutils/build
	+make -C dependencies/binutils/build install
	touch $@

${GCC_DEP}: ${BINUTILS_DEP}
	mkdir -p toolchain/usr/include
	test -e dependencies/gcc || git clone --depth 1 -b releases/gcc-14.1.0 https://gcc.gnu.org/git/gcc dependencies/gcc
	mkdir -p dependencies/gcc/build
	cd dependencies/gcc/build && ../configure --disable-fixed-point \
	  --disable-gcov --disable-multilib --disable-shared --enable-languages=c++ \
	  --target=x86_64-elf --prefix=${TOOLCHAIN_DIR}/usr --without-headers
	+make -C dependencies/gcc/build all-gcc
	+make -C dependencies/gcc/build install-gcc
	touch $@

${LIBGCC_DEP}: ${GCC_DEP}
	+make -C dependencies/gcc/build all-target-libgcc
	+make -C dependencies/gcc/build install-target-libgcc
	touch $@

${EULER_DEP}: ${GCC_DEP} ${MINTSUKI_HEADERS_DEP}
	+make -C euler build/crt0.o build/libc.a build/libg.a build/libm.a \
	  build/libstdc++.a
	mkdir -p toolchain/usr/x86_64-elf/lib
	cp euler/build/crt0.o euler/build/libc.a euler/build/libg.a \
	  euler/build/libm.a euler/build/libstdc++.a ${LIB_DIR}
	touch $@

${DAGUERRE_DEP}: ${LIBRARY_DEPS}
	+make -C libraries/daguerre build/libdaguerre.a
	cp libraries/daguerre/build/libdaguerre.a ${LIB_DIR}
	touch $@

kernel/build/kernel.elf: ${GCC_DEP} ${MINTSUKI_HEADERS_DEP} ${LIMINE_DEP}
	+make -C kernel build/kernel.elf

applications/init/build/init.elf: ${APP_DEPS} ${DAGUERRE_DEP}
	+make -C applications/init build/init.elf

build/initfs.tgz: applications/init/build/init.elf
	@mkdir -p build
	rm -rf build/initfs
	cp -r skeleton build/initfs
	mkdir build/initfs/bin
	cp applications/init/build/init.elf build/initfs/bin/init
	cd build/initfs && tar czf ../initfs.tgz .

build/disk.iso: kernel/build/kernel.elf build/initfs.tgz ${LIMINE_DEP}
	@mkdir -p build
	rm -rf build/iso
	mkdir build/iso
	cp kernel/build/kernel.elf ${LIMINE_DIR}/bin/limine-bios.sys \
	  ${LIMINE_DIR}/bin/limine-bios-cd.bin build/initfs.tgz build/iso/
	echo 'TIMEOUT=0' > build/iso/limine.cfg
	echo ':Hilbert OS' >> build/iso/limine.cfg
	echo 'PROTOCOL=limine' >> build/iso/limine.cfg
	echo 'KERNEL_PATH=boot:///kernel.elf' >> build/iso/limine.cfg
	echo 'MODULE_PATH=$$boot:///initfs.tgz' >> build/iso/limine.cfg
	echo 'MODULE_CMDLINE=initfs' >> build/iso/limine.cfg
	xorriso -as mkisofs -b limine-bios-cd.bin -no-emul-boot -boot-load-size 4 \
	  -boot-info-table --protective-msdos-label build/iso -o $@
