Files
ClassicOS/Makefile

59 lines
1.9 KiB
Makefile

AS = nasm
ASFLAGS = -f elf32 -g -F dwarf
CC = gcc
LD = ld
QEMU= qemu-system-i386
BUILD_DIR = build
DISK_IMG = $(BUILD_DIR)/disk.img
STAGE2_SIZE = 2048
KERNEL_C_SRC = $(wildcard kernel/*.c)
KERNEL_ASM_SRC = $(wildcard kernel/*.asm)
KERNEL_OBJ = $(patsubst kernel/%.c, $(BUILD_DIR)/%.o, $(KERNEL_C_SRC))
KERNEL_OBJ += $(patsubst kernel/%.asm, $(BUILD_DIR)/asm_%.o, $(KERNEL_ASM_SRC))
all: $(DISK_IMG)
.PHONY: stage1 stage2 kernel run gdb clean
stage1: $(BUILD_DIR)
$(AS) $(ASFLAGS) -o $(BUILD_DIR)/$@.o bootloader/$@.asm
$(LD) -Ttext=0x7c00 -melf_i386 -o $(BUILD_DIR)/$@.elf $(BUILD_DIR)/$@.o
objcopy -O binary $(BUILD_DIR)/$@.elf $(BUILD_DIR)/$@.bin
# NOTE: Stage2 final size should be checked against `$(STAGE2_SIZE)` by the build system to avoid an overflow.
# Alternatively, convey the final stage2 size through other means to stage1.
stage2: $(BUILD_DIR)
$(AS) $(ASFLAGS) -o $(BUILD_DIR)/stage2.o bootloader/stage2.asm
$(CC) -std=c11 -ffreestanding -nostdlib -fno-stack-protector -m32 -g -c -o $(BUILD_DIR)/stage2_load.o bootloader/stage2_load.c
$(LD) -Tbootloader/stage2.ld -melf_i386 -o $(BUILD_DIR)/$@.elf $(BUILD_DIR)/stage2.o $(BUILD_DIR)/stage2_load.o
objcopy -O binary $(BUILD_DIR)/$@.elf $(BUILD_DIR)/$@.bin
truncate -s $(STAGE2_SIZE) $(BUILD_DIR)/$@.bin
$(BUILD_DIR)/asm_%.o: kernel/%.asm
$(AS) $(ASFLAGS) -o $@ $<
$(BUILD_DIR)/%.o: kernel/%.c
$(CC) -std=c11 -ffreestanding -nostdlib -fno-stack-protector -m32 -g -c -o $@ $<
kernel: $(KERNEL_OBJ) | $(BUILD_DIR)
$(LD) -melf_i386 -Tbootloader/linker.ld -o $(BUILD_DIR)/kernel.elf $(KERNEL_OBJ)
$(DISK_IMG): stage1 stage2 kernel
dd if=$(BUILD_DIR)/stage1.bin of=$@
dd if=$(BUILD_DIR)/stage2.bin of=$@ oflag=append conv=notrunc
dd if=$(BUILD_DIR)/kernel.elf of=$@ oflag=append conv=notrunc
truncate -s 1M $@
$(BUILD_DIR):
mkdir -p $@
run:
qemu-system-i386 -s -S $(DISK_IMG)
gdb:
gdb -x gdb.txt
clean:
rm -rf $(BUILD_DIR)