os/Makefile
2021-08-21 20:32:20 -05:00

55 lines
1.5 KiB
Makefile

BUILD_DIR ?= ./build
SRC_DIRS ?= ./src
BOOTLOADER_DIR ?= ./asm/boot
SRCS := $(shell find $(SRC_DIRS) -name *.cpp -or -name *.c -or -name *.s)
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
DEPS := $(OBJS:.o=.d)
INC_DIRS := $(shell find $(SRC_DIRS) -type d)
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
CPPFLAGS ?= $(INC_FLAGS) -MMD -MP -g -std=c++11 -Wall
# Default makefile target
all: os-image
run: os-image
qemu-system-x86_64 -hda build/os-image.bin
# Builds a bootable x86 image that loads the kernel
os-image: $(BUILD_DIR)/kernel.bin $(BUILD_DIR)/bootloader.bin
cat $(BUILD_DIR)/bootloader.bin $(BUILD_DIR)/kernel.bin > build/os-image.bin
# Builds the binary kernel image with the entry prefix
$(BUILD_DIR)/kernel.bin: $(BUILD_DIR)/kernel_entry.o $(OBJS)
ld -m elf_i386 -o $@ -Ttext 0x1000 --oformat binary $(BUILD_DIR)/kernel_entry.o $(OBJS)
# Builds the x86 -> 32-bit bootloader
$(BUILD_DIR)/bootloader.bin: $(BOOTLOADER_DIR)/main16.asm
cd $(BOOTLOADER_DIR); nasm -f bin -o ../../build/bootloader.bin main16.asm
# Builds the kernel entry object file
$(BUILD_DIR)/kernel_entry.o: asm/boot.asm
$(MKDIR_P) $(dir $@)
nasm asm/boot.asm -f elf -o $@
# Build C++ sources
$(BUILD_DIR)/%.cpp.o: %.cpp
$(MKDIR_P) $(dir $@)
g++ $(CPPFLAGS) $(CXXFLAGS) -m32 -ffreestanding -c $< -o $@ -fno-pie
# Build C sources
$(BUILD_DIR)/%.c.o: %.c
$(MKDIR_P) $(dir $@)
gcc $(CFLAGS) $(CXXFLAGS) -m32 -ffreestanding -c $< -o $@ -fno-pie
# Clean all build objects
.PHONY: clean
clean:
$(RM) -rf $(BUILD_DIR)
-include $(DEPS)
MKDIR_P ?= mkdir -p