radix-old/kernel/Makefile
2024-03-02 16:30:50 +13:00

137 lines
3.7 KiB
Makefile

# Nuke built-in rules and variables.
override MAKEFLAGS += -rR
# This is the name that our final kernel executable will have.
# Change as needed.
override KERNEL := kernel
# Convenience macro to reliably declare user overridable variables.
define DEFAULT_VAR =
ifeq ($(origin $1),default)
override $(1) := $(2)
endif
ifeq ($(origin $1),undefined)
override $(1) := $(2)
endif
endef
# It is suggested to use a custom built cross toolchain to build a kernel.
# We are using the standard "cc" here, it may work by using
# the host system's toolchain, but this is not guaranteed.
override DEFAULT_CC := cc
$(eval $(call DEFAULT_VAR,CC,$(DEFAULT_CC)))
# Same thing for "ld" (the linker).
override DEFAULT_LD := ld
$(eval $(call DEFAULT_VAR,LD,$(DEFAULT_LD)))
# User controllable C flags.
override DEFAULT_CFLAGS := -g -O2 -pipe
$(eval $(call DEFAULT_VAR,CFLAGS,$(DEFAULT_CFLAGS)))
# User controllable C preprocessor flags. We set none by default.
override DEFAULT_CPPFLAGS :=
$(eval $(call DEFAULT_VAR,CPPFLAGS,$(DEFAULT_CPPFLAGS)))
# User controllable nasm flags.
override DEFAULT_NASMFLAGS := -F dwarf -g
$(eval $(call DEFAULT_VAR,NASMFLAGS,$(DEFAULT_NASMFLAGS)))
# User controllable linker flags. We set none by default.
override DEFAULT_LDFLAGS :=
$(eval $(call DEFAULT_VAR,LDFLAGS,$(DEFAULT_LDFLAGS)))
# Internal C flags that should not be changed by the user.
override CFLAGS += \
-Wall \
-Werror \
-Wextra \
-std=gnu11 \
-ffreestanding \
-fno-stack-protector \
-fno-stack-check \
-fno-lto \
-fPIE \
-m64 \
-march=x86-64 \
-mno-80387 \
-mno-mmx \
-mno-sse \
-mno-sse2 \
-mno-red-zone
# Internal C preprocessor flags that should not be changed by the user.
override CPPFLAGS := \
-I src \
-I vendor \
$(CPPFLAGS) \
-MMD \
-MP
# Internal linker flags that should not be changed by the user.
override LDFLAGS += \
-m elf_x86_64 \
-nostdlib \
-static \
-pie \
--no-dynamic-linker \
-z text \
-z max-page-size=0x1000 \
-T linker.ld
# Internal nasm flags that should not be changed by the user.
override NASMFLAGS += \
-Wall \
-f elf64
# Use "find" to glob all *.c, *.S, and *.asm files in the tree and obtain the
# object and header dependency file names.
override CFILES := $(shell cd src && find -L * -type f -name '*.c') $(shell cd vendor && find -L * -type f -name '*.c')
override ASFILES := $(shell cd src && find -L * -type f -name '*.S')
override NASMFILES := $(shell cd src && find -L * -type f -name '*.asm')
override OBJ := $(addprefix obj/,$(CFILES:.c=.c.o) $(ASFILES:.S=.S.o) $(NASMFILES:.asm=.asm.o))
override HEADER_DEPS := $(addprefix obj/,$(CFILES:.c=.c.d) $(ASFILES:.S=.S.d))
# Default target.
.PHONY: all
all: bin/$(KERNEL)
src/limine.h:
curl -Lo $@ https://github.com/limine-bootloader/limine/raw/trunk/limine.h
# Link rules for the final kernel executable.
bin/$(KERNEL): Makefile linker.ld $(OBJ)
mkdir -p "$$(dirname $@)"
$(LD) $(OBJ) $(LDFLAGS) -o $@
# Include header dependencies.
-include $(HEADER_DEPS)
# Compilation rules for *.c files.
obj/%.c.o: src/%.c Makefile src/limine.h
mkdir -p "$$(dirname $@)"
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
obj/%.c.o: vendor/%.c Makefile src/limine.h
mkdir -p "$$(dirname $@)"
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
# Compilation rules for *.S files.
obj/%.S.o: src/%.S Makefile
mkdir -p "$$(dirname $@)"
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
# Compilation rules for *.asm (nasm) files.
obj/%.asm.o: src/%.asm Makefile
mkdir -p "$$(dirname $@)"
nasm $(NASMFLAGS) $< -o $@
# Remove object files and the final executable.
.PHONY: clean
clean:
rm -rf bin obj
.PHONY: distclean
distclean: clean
rm -f src/limine.h