corn/Makefile
2024-02-04 14:19:54 -05:00

87 lines
2.1 KiB
Makefile

#
# Makefile Configuration
#
CC=cc
LD=ld
AS=nasm
ARCH=amd64
ARCH_COMMON=x86_common
BUILD=build
CFLAGS += -std=c2x -ffreestanding -lgcc -isystem include -pipe
CFLAGS += -Wall -Wextra -pedantic
CFLAGS += -O2 -g -fno-stack-protector -fno-omit-frame-pointer
CFLAGS += -DPAGE_SIZE=4096
LDFLAGS += -nmagic --no-warn-rwx-segments -nostdlib
#
# Makefile
#
ARCH_COMMON += $(ARCH)
KERNEL=kernel.bin
ISO=os_image.iso
ARCH_DIRS = $(patsubst %,src/arch/%,$(ARCH_COMMON))
ARCH_INCLUDES = $(shell find $(ARCH_DIRS) -type d -name "include")
CFLAGS_ARCH = $(patsubst %, -isystem %,$(ARCH_INCLUDES))
H_SRC = $(shell find src include -type f -name "*.h" -not -path "src/arch/*")
HA_SRC = $(shell find $(ARCH_DIRS) -type f -name "*.h")
C_SRC = $(shell find src -type f -name "*.c" -not -path "src/arch/*")
C_OBJ = $(patsubst %.c,build/%.o,$(C_SRC))
CA_SRC = $(shell find $(ARCH_DIRS) -type f -name "*.c")
CA_OBJ = $(patsubst %.c,build/%.o,$(CA_SRC))
A_SRC = $(shell find $(ARCH_DIRS) -type f -name "*.S")
A_OBJ = $(patsubst %.S,build/%.S.o,$(A_SRC))
.PHONY: all clean
all: build/$(ISO)
clean:
@printf "\033[31m RM \033[0m%s\n" build
@rm -fr ./build
$(A_OBJ): build/%.S.o : %.S
@mkdir -p $(@D)
@printf "\033[33m AS \033[0m%s\n" $<
@nasm $< -f elf64 -o $@
$(C_OBJ): build/%.o : %.c
@mkdir -p $(@D)
@printf "\033[34m CC \033[0m%s\n" $<
@$(CC) -c $(CFLAGS) -o $@ $<
$(CA_OBJ): build/%.o : %.c
@mkdir -p $(@D)
@printf "\033[34m CC \033[0m%s\n" $<
@$(CC) -c $(CFLAGS) $(CFLAGS_ARCH) -o $@ $<
build/$(KERNEL): arch/$(ARCH)/linker.ld $(A_OBJ) $(C_OBJ) $(CA_OBJ) $(H_SRC) $(HA_SRC)
@mkdir -p $(@D)
@printf "\033[32m LD \033[0m%s\n" $@
@$(LD) $(LDFLAGS) -T arch/$(ARCH)/linker.ld -o build/$(KERNEL) $(A_OBJ) $(C_OBJ) $(CA_OBJ)
build/$(ISO): build/$(KERNEL) grub.cfg
@mkdir -p $(@D)
@mkdir -p build/iso/boot/grub
@cp grub.cfg build/iso/boot/grub
@cp build/$(KERNEL) build/iso/boot
@printf "\033[35m ISO \033[0m%s\n" build/$(ISO)
@grub-mkrescue -o build/$(ISO) build/iso 2> /dev/null
run: all
@qemu-system-x86_64 \
-cdrom build/$(ISO) \
-serial stdio \
-display sdl \
-m 1G \
-enable-kvm \
-name corn