ZealOS/zealbooter/GNUmakefile

136 lines
3.8 KiB
Makefile
Raw Permalink Normal View History

# Nuke built-in rules and variables.
override MAKEFLAGS += -rR
2022-08-20 00:54:34 +01:00
# This is the name that our final kernel executable will have.
# Change as needed.
override KERNEL := kernel
2022-08-20 00:54:34 +01:00
# Convenience macro to reliably declare user overridable variables.
2022-08-20 00:54:34 +01:00
define DEFAULT_VAR =
2022-08-24 07:53:44 +01:00
ifeq ($(origin $1),default)
2022-08-20 00:54:34 +01:00
override $(1) := $(2)
endif
2022-08-24 07:53:44 +01:00
ifeq ($(origin $1),undefined)
2022-08-20 00:54:34 +01:00
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
2022-08-20 00:54:34 +01:00
# the host system's toolchain, but this is not guaranteed.
override DEFAULT_CC := cc
$(eval $(call DEFAULT_VAR,CC,$(DEFAULT_CC)))
2022-08-20 00:54:34 +01:00
# Same thing for "ld" (the linker).
override DEFAULT_LD := ld
$(eval $(call DEFAULT_VAR,LD,$(DEFAULT_LD)))
2022-08-20 00:54:34 +01:00
# User controllable C flags.
override DEFAULT_CFLAGS := -g -O2 -pipe
$(eval $(call DEFAULT_VAR,CFLAGS,$(DEFAULT_CFLAGS)))
2022-08-20 00:54:34 +01:00
# User controllable C preprocessor flags. We set none by default.
override DEFAULT_CPPFLAGS :=
$(eval $(call DEFAULT_VAR,CPPFLAGS,$(DEFAULT_CPPFLAGS)))
2022-08-20 00:54:34 +01:00
# User controllable nasm flags.
override DEFAULT_NASMFLAGS := -F dwarf -g
$(eval $(call DEFAULT_VAR,NASMFLAGS,$(DEFAULT_NASMFLAGS)))
2022-08-20 00:54:34 +01:00
# User controllable linker flags. We set none by default.
override DEFAULT_LDFLAGS :=
$(eval $(call DEFAULT_VAR,LDFLAGS,$(DEFAULT_LDFLAGS)))
2022-08-20 00:54:34 +01:00
# Internal C flags that should not be changed by the user.
override CFLAGS += \
-Wall \
-Wextra \
-std=gnu11 \
-ffreestanding \
2022-08-20 00:54:34 +01:00
-fno-stack-protector \
-fno-stack-check \
-fno-lto \
-fno-PIE \
-fno-PIC \
-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) \
-MMD \
-MP
2022-08-20 00:54:34 +01:00
# Internal linker flags that should not be changed by the user.
override LDFLAGS += \
-m elf_x86_64 \
-nostdlib \
-static \
2022-08-20 00:54:34 +01:00
-z max-page-size=0x1000 \
-T linker.ld
# Check if the linker supports -no-pie and enable it if it does.
2022-09-05 05:42:29 +01:00
ifeq ($(shell $(LD) --help 2>&1 | grep 'no-pie' >/dev/null 2>&1; echo $$?),0)
override LDFLAGS += -no-pie
endif
2022-08-20 00:54:34 +01:00
# Internal nasm flags that should not be changed by the user.
override NASMFLAGS += \
-Wall \
2022-08-20 00:54:34 +01:00
-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')
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))
2022-08-20 00:54:34 +01:00
# Default target.
.PHONY: all
all: bin/$(KERNEL)
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 kernel executable.
bin/$(KERNEL): GNUmakefile linker.ld $(OBJ)
mkdir -p "$$(dirname $@)"
2022-08-20 00:54:34 +01:00
$(LD) $(OBJ) $(LDFLAGS) -o $@
# Include header dependencies.
-include $(HEADER_DEPS)
# Compilation rules for *.c files.
obj/%.c.o: src/%.c GNUmakefile src/limine.h
mkdir -p "$$(dirname $@)"
$(CC) $(CFLAGS) $(CPPFLAGS) -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 $@)"
$(CC) $(CFLAGS) $(CPPFLAGS) -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 $@)"
2022-08-20 00:54:34 +01:00
nasm $(NASMFLAGS) $< -o $@
# 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