diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3459638 --- /dev/null +++ b/Makefile @@ -0,0 +1,73 @@ +# === Config === +TARGET := boot/espresso.bin +ISO := boot/espresso.iso +CC := i686-elf-gcc +AS := i686-elf-as +NASM := nasm +QEMU_MKE_IMG := qemu-img create -f raw espresso.img 64M +NASMFLAGS := -f elf32 +CFLAGS := -std=gnu99 -ffreestanding -O2 -Wall -Wextra -msse +LDFLAGS := -T linker.ld -ffreestanding -O2 -nostdlib +SRC_DIRS := kernel drivers src lib +INCLUDE_DIRS := include +INCLUDES := $(addprefix -I, $(INCLUDE_DIRS)) +ISO_DIR := isodir +BOOT_DIR := $(ISO_DIR)/boot +GRUB_DIR := $(BOOT_DIR)/grub +GRUB_CFG := grub.cfg + +# === File collection === +C_SRCS := $(foreach dir, $(SRC_DIRS), $(shell find $(dir) -name '*.c')) +S_SRCS := boot.s crti.s crtn.s misc_asm.s +NASM_SRCS := idt.asm isr128.asm gdt.asm isr_stub_table.asm +CRTI_OBJ := crti.o +CRTBEGIN_OBJ := $(shell $(CC) $(CFLAGS) -print-file-name=crtbegin.o) +CRTEND_OBJ := $(shell $(CC) $(CFLAGS) -print-file-name=crtend.o) +CRTN_OBJ := crtn.o +OBJS := $(C_SRCS:.c=.o) $(S_SRCS:.s=.o) $(NASM_SRCS:.asm=.o) + +OBJ_LINK_LIST := $(CRTI_OBJ) $(CRTBEGIN_OBJ) $(OBJS) $(CRTEND_OBJ) $(CRTN_OBJ) +INTERNAL_OBJS := $(CRTI_OBJ) $(OBJS) $(CRTN_OBJ) + +# === Build all targets === +build: all iso run clean + +# === Default target === +all: $(TARGET) + +# === Linking === +$(TARGET): boot.o $(filter-out boot.o, $(OBJ_LINK_LIST)) + @mkdir -p $(dir $@) + $(CC) $(LDFLAGS) -o $@ $^ -lgcc + +# === Compiling C files === +%.o: %.c + $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ + +# === Assembling S files === +%.o: %.s + $(AS) $< -o $@ + +%.o: %.asm + $(NASM) $(NASMFLAGS) $< -o $@ + +# === ISO generation === +iso: $(TARGET) + @grub-file --is-x86-multiboot $(TARGET) && echo "multiboot confirmed" || (echo "not multiboot" && exit 1) + mkdir -p $(GRUB_DIR) + cp $(TARGET) $(BOOT_DIR)/ + cp $(GRUB_CFG) $(GRUB_DIR)/ + grub-mkrescue -o $(ISO) $(ISO_DIR) + +# === Run in QEMU === +run: iso + $(QEMU_MKE_IMG) + qemu-system-i386 -kernel $(TARGET) -drive file=espresso.img,format=raw,if=ide,readonly=off,rerror=report,werror=report -cpu qemu32,sse2 #-singlestep + +# === Clean all build artifacts === +clean: + rm -f $(INTERNAL_OBJS) $(TARGET) $(ISO) + rm -rf $(ISO_DIR) + +.PHONY: all clean iso run +