ZealOS/zealbooter/GNUmakefile

128 lines
3.4 KiB
Makefile
Raw Permalink Normal View History

# Nuke built-in rules and variables.
MAKEFLAGS += -rR
.SUFFIXES:
# This is the name that our final executable will have.
2022-08-20 00:54:34 +01:00
# Change as needed.
override OUTPUT := kernel
2022-08-20 00:54:34 +01:00
# Convenience macro to reliably declare user overridable variables.
override USER_VARIABLE = $(if $(filter $(origin $(1)),default undefined),$(eval override $(1) := $(2)))
2022-08-20 00:54:34 +01:00
# User controllable C compiler command.
$(call USER_VARIABLE,KCC,cc)
2022-08-20 00:54:34 +01:00
# User controllable linker command.
$(call USER_VARIABLE,KLD,ld)
2022-08-20 00:54:34 +01:00
# User controllable C flags.
$(call USER_VARIABLE,KCFLAGS,-g -O2 -pipe)
2022-08-20 00:54:34 +01:00
# User controllable C preprocessor flags. We set none by default.
$(call USER_VARIABLE,KCPPFLAGS,)
2022-08-20 00:54:34 +01:00
# User controllable nasm flags.
$(call USER_VARIABLE,KNASMFLAGS,-F dwarf -g)
2022-08-20 00:54:34 +01:00
# User controllable linker flags. We set none by default.
$(call USER_VARIABLE,KLDFLAGS,)
# Check if KCC is Clang.
override KCC_IS_CLANG := $(shell ! $(KCC) --version 2>/dev/null | grep 'clang' >/dev/null 2>&1; echo $$?)
# If the C compiler is Clang, set the target as needed.
ifeq ($(KCC_IS_CLANG),1)
override KCC += \
-target x86_64-unknown-none
endif
2022-08-20 00:54:34 +01:00
# Internal C flags that should not be changed by the user.
override KCFLAGS += \
-Wall \
-Wextra \
-std=gnu11 \
-ffreestanding \
2022-08-20 00:54:34 +01:00
-fno-stack-protector \
-fno-stack-check \
-fno-lto \
-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 KCPPFLAGS := \
-I src \
-I src/lib \
$(KCPPFLAGS) \
-MMD \
-MP
2022-08-20 00:54:34 +01:00
# Internal nasm flags that should not be changed by the user.
override KNASMFLAGS += \
-Wall \
-f elf64
2022-08-20 00:54:34 +01:00
# Internal linker flags that should not be changed by the user.
override KLDFLAGS += \
-m elf_x86_64 \
-nostdlib \
-static \
2022-08-20 00:54:34 +01:00
-z max-page-size=0x1000 \
-gc-sections \
2022-08-20 00:54:34 +01:00
-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 CFILES := $(shell cd src && find -L * -type f -name '*.c' | LC_ALL=C sort)
override ASFILES := $(shell cd src && find -L * -type f -name '*.S' | LC_ALL=C sort)
override NASMFILES := $(shell cd src && find -L * -type f -name '*.asm' | LC_ALL=C sort)
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))
2022-08-20 00:54:34 +01:00
# Default target.
.PHONY: all
all: bin/$(OUTPUT)
2022-08-20 00:54:34 +01:00
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"
2022-08-20 00:54:34 +01:00
# Link rules for the final executable.
bin/$(OUTPUT): GNUmakefile linker.ld $(OBJ)
mkdir -p "$$(dirname $@)"
$(KLD) $(OBJ) $(KLDFLAGS) -o $@
2022-08-20 00:54:34 +01:00
# Include header dependencies.
-include $(HEADER_DEPS)
# Compilation rules for *.c files.
obj/%.c.o: src/%.c GNUmakefile src/limine.h
mkdir -p "$$(dirname $@)"
$(KCC) $(KCFLAGS) $(KCPPFLAGS) -c $< -o $@
2022-08-20 00:54:34 +01:00
# Compilation rules for *.S files.
obj/%.S.o: src/%.S GNUmakefile src/limine.h
mkdir -p "$$(dirname $@)"
$(KCC) $(KCFLAGS) $(KCPPFLAGS) -c $< -o $@
2022-08-20 00:54:34 +01:00
# Compilation rules for *.asm (nasm) files.
obj/%.asm.o: src/%.asm GNUmakefile
mkdir -p "$$(dirname $@)"
nasm $(KNASMFLAGS) $< -o $@
2022-08-20 00:54:34 +01:00
# Remove object files and the final executable.
.PHONY: clean
clean:
rm -rf bin obj
2022-08-20 00:54:34 +01:00
.PHONY: distclean
distclean: clean
rm -f src/limine.h