Files
Espresso/Makefile

77 lines
2.3 KiB
Makefile
Raw Normal View History

2025-05-28 14:41:02 -05:00
# === 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
WNOFLAGS := -Wno-discarded-qualifiers
CFLAGS := -std=gnu99 -ffreestanding -O2 -Wall -Wextra -msse $(WNOFLAGS)
LDFLAGS := -T linker.ld -ffreestanding -O2 -nostdlib
QEMUFLAGS := -device isa-debug-exit -serial stdio -kernel $(TARGET) -drive file=espresso.img,format=raw,if=ide,readonly=off,rerror=report,werror=report -cpu qemu32,sse2 #-singlestep
SRC_DIRS := kernel drivers 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 pit.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
# === 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 $(QEMUFLAGS)
# === Clean all build artifacts ===
clean:
rm -f $(INTERNAL_OBJS) $(TARGET) $(ISO)
rm -rf $(ISO_DIR)
rm -f espresso.img
.PHONY: all clean iso run