# Nuke built-in rules and variables. MAKEFLAGS += -rR .SUFFIXES: # This is the name that our final executable will have. # Change as needed. override OUTPUT := kernel # User controllable C compiler command. CC := cc # User controllable archiver command. AR := ar # User controllable C flags. CFLAGS := -g -O2 -pipe # User controllable C preprocessor flags. We set none by default. CPPFLAGS := # User controllable nasm flags. NASMFLAGS := -F dwarf -g # User controllable linker flags. We set none by default. LDFLAGS := # Check if CC is Clang. override CC_IS_CLANG := $(shell ! $(CC) --version 2>/dev/null | grep 'clang' >/dev/null 2>&1; echo $$?) # If the C compiler is Clang, set the target as needed. ifeq ($(CC_IS_CLANG),1) override CC += \ -target x86_64-unknown-none endif # Internal C flags that should not be changed by the user. override CFLAGS += \ -Wall \ -Wextra \ -std=gnu11 \ -ffreestanding \ -fno-stack-protector \ -fno-stack-check \ -fno-PIC \ -ffunction-sections \ -fdata-sections \ -m64 \ -march=x86-64 \ -mno-80387 \ -mno-mmx \ -mno-sse \ -mno-sse2 \ -mno-red-zone \ -mcmodel=kernel # Internal C preprocessor flags that should not be changed by the user. override CPPFLAGS := \ -I src \ -I src/lib \ $(CPPFLAGS) \ -DLIMINE_API_REVISION=3 \ -MMD \ -MP # Internal nasm flags that should not be changed by the user. override NASMFLAGS += \ -Wall \ -f elf64 # Internal linker flags that should not be changed by the user. override LDFLAGS += \ -Wl,-m,elf_x86_64 \ -Wl,--build-id=none \ -nostdlib \ -static \ -z max-page-size=0x1000 \ -Wl,--gc-sections \ -T linker.ld # Use "find" to glob all *.c, *.S, and *.asm files in the tree and obtain the # object and header dependency file names. override SRCFILES := $(shell cd src && find -L * -type f | LC_ALL=C sort) override CFILES := $(filter %.c,$(SRCFILES)) override ASFILES := $(filter %.S,$(SRCFILES)) override NASMFILES := $(filter %.asm,$(SRCFILES)) 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. This must come first, before header dependencies. .PHONY: all all: bin/$(OUTPUT) # Include header dependencies. -include $(HEADER_DEPS) src/limine.h: curl -Lo $@ https://github.com/limine-bootloader/limine/raw/trunk/limine.h || cp ../build/limine/limine.h src/limine.h || echo "ERROR" # Link rules for the final executable. bin/$(OUTPUT): GNUmakefile linker.ld $(OBJ) mkdir -p "$$(dirname $@)" $(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) -o $@ # Compilation rules for *.c files. obj/%.c.o: src/%.c GNUmakefile src/limine.h mkdir -p "$$(dirname $@)" $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@ # Compilation rules for *.S files. obj/%.S.o: src/%.S GNUmakefile src/limine.h mkdir -p "$$(dirname $@)" $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@ # Compilation rules for *.asm (nasm) files. obj/%.asm.o: src/%.asm GNUmakefile mkdir -p "$$(dirname $@)" nasm $(NASMFLAGS) $< -o $@ # Remove object files and the final executable. .PHONY: clean clean: rm -rf bin obj # Remove everything built and generated including downloaded dependencies. .PHONY: distclean distclean: clean rm -f src/limine.h