arch ?= x86_64
kernel := build/kernel-$(arch).bin
iso := build/os-$(arch).iso
target ?= $(arch)-unknown-linux-gnu
rust_os := target/$(target)/debug/libmahdi_os.a

linker_script = src/arch/$(arch)/linker.ld
grub_cfg = src/arch/$(arch)/grub.cfg
assembly_source_files = $(wildcard src/arch/$(arch)/*.asm)
assembly_object_files = $(patsubst src/arch/$(arch)/%.asm, build/arch/$(arch)/%.o, $(assembly_source_files))

.PHONY: all clean run iso

all: $(kernel)

clean:
			@rm -r build

run: $(iso)
				@qemu-system-x86_64 -curses -cdrom $(iso)

iso: $(iso)

$(iso): $(kernel) $(grub_cfg)
					@mkdir -p build/isofiles/boot/grub
					@cp $(kernel) build/isofiles/boot/kernel.bin
					@cp $(grub_cfg) build/isofiles/boot/grub
					@grub-mkrescue -o $(iso) build/isofiles -d /usr/lib/grub/i386-pc 2> /dev/null
					@rm -r build/isofiles

$(kernel): cargo $(rust_os) $(assembly_object_files) $(linker_script)
						@ld -n --gc-sections -T $(linker_script) -o $(kernel) $(assembly_object_files) $(rust_os)

cargo:
	@cargo build --target $(target)

build/arch/$(arch)/%.o: src/arch/$(arch)/%.asm
					@mkdir -p $(shell dirname $@)
					@nasm -felf64 $< -o $@