corn/Makefile
2024-01-27 02:23:28 -05:00

51 lines
1.3 KiB
Makefile

SRC_DIR=src
INCLUDE_DIR=include
BUILD_DIR=build
K_BIN_NAME=kernel.bin
ISO_NAME=os_image.iso
CC=cc
LD=ld
CFLAGS=-std=c2x -ffreestanding -g -Wall -Wextra -pedantic -lgcc -isystem $(INCLUDE_DIR)
C_SRC=$(shell find $(SRC_DIR) -type f -name "*.c")
C_OBJ=$(patsubst %.c,$(BUILD_DIR)/%.o,$(C_SRC))
H_SRC=$(shell find $(SRC_DIR) -type f -name "*.h")
H_INCLUDE=$(shell find $(INCLUDE_DIR) -type f -name "*.h")
A_SRC=$(shell find $(SRC_DIR) -type f -name "*.S")
A_OBJ=$(patsubst %.S,$(BUILD_DIR)/%.S.o,$(A_SRC))
.PHONY: all clean
all: $(BUILD_DIR)/$(ISO_NAME)
clean:
rm -rf $(BUILD_DIR)/*
$(A_OBJ): $(BUILD_DIR)/%.S.o : %.S
@mkdir -p $(@D)
nasm $< -f elf64 -o $@
$(BUILD_DIR)/$(SRC_DIR)/arch/amd64/shim.o: $(SRC_DIR)/arch/amd64/shim.c
@mkdir -p $(@D)
$(CC) -c $(CFLAGS) -o $@ $< -m32
$(C_OBJ): $(BUILD_DIR)/%.o : %.c
@mkdir -p $(@D)
$(CC) -c $(CFLAGS) -o $@ $<
$(BUILD_DIR)/$(K_BIN_NAME): $(SRC_DIR)/arch/amd64/linker.ld $(A_OBJ) $(C_OBJ) $(H_SRC) $(H_INCLUDE)
@mkdir -p $(@D)
$(LD) -nmagic -o $(BUILD_DIR)/$(K_BIN_NAME) -T $(SRC_DIR)/arch/amd64/linker.ld $(A_OBJ) $(C_OBJ)
$(BUILD_DIR)/$(ISO_NAME): $(BUILD_DIR)/$(K_BIN_NAME) grub.cfg
@mkdir -p $(@D)
@mkdir -p $(BUILD_DIR)/iso/boot/grub
cp grub.cfg $(BUILD_DIR)/iso/boot/grub
cp $(BUILD_DIR)/$(K_BIN_NAME) $(BUILD_DIR)/iso/boot
grub-mkrescue -o $(BUILD_DIR)/$(ISO_NAME) $(BUILD_DIR)/iso