2023-03-28 23:04:07 +01:00
|
|
|
# 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 := zealbooter.elf
|
|
|
|
|
2023-03-28 23:04:07 +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 highly recommended to use a custom built cross toolchain to build a kernel.
|
|
|
|
# We are only using "cc" as a placeholder here. It may work by using
|
|
|
|
# the host system's toolchain, but this is not guaranteed.
|
|
|
|
$(eval $(call DEFAULT_VAR,CC,cc))
|
|
|
|
|
|
|
|
# Same thing for "ld" (the linker).
|
|
|
|
$(eval $(call DEFAULT_VAR,LD,ld))
|
|
|
|
|
2023-03-28 23:04:07 +01:00
|
|
|
# User controllable C flags.
|
2023-04-17 01:20:19 +01:00
|
|
|
$(eval $(call DEFAULT_VAR,CFLAGS,-g -O2 -pipe))
|
2022-08-20 00:54:34 +01:00
|
|
|
|
2023-03-28 23:04:07 +01:00
|
|
|
# User controllable C preprocessor flags. We set none by default.
|
|
|
|
$(eval $(call DEFAULT_VAR,CPPFLAGS,))
|
2022-08-20 00:54:34 +01:00
|
|
|
|
|
|
|
# User controllable nasm flags.
|
2023-03-28 23:04:07 +01:00
|
|
|
$(eval $(call DEFAULT_VAR,NASMFLAGS,-F dwarf -g))
|
2022-08-20 00:54:34 +01:00
|
|
|
|
|
|
|
# User controllable linker flags. We set none by default.
|
2023-03-28 23:04:07 +01:00
|
|
|
$(eval $(call DEFAULT_VAR,LDFLAGS,))
|
2022-08-20 00:54:34 +01:00
|
|
|
|
|
|
|
# Internal C flags that should not be changed by the user.
|
2023-03-28 23:04:07 +01:00
|
|
|
override CFLAGS += \
|
2023-04-17 01:20:19 +01:00
|
|
|
-Wall \
|
|
|
|
-Wextra \
|
2023-03-28 23:04:07 +01:00
|
|
|
-std=gnu11 \
|
|
|
|
-ffreestanding \
|
2022-08-20 00:54:34 +01:00
|
|
|
-fno-stack-protector \
|
2023-03-28 23:04:07 +01:00
|
|
|
-fno-stack-check \
|
|
|
|
-fno-lto \
|
2023-04-17 01:20:19 +01:00
|
|
|
-fno-PIE \
|
|
|
|
-fno-PIC \
|
2023-03-28 23:04:07 +01:00
|
|
|
-m64 \
|
|
|
|
-march=x86-64 \
|
|
|
|
-mabi=sysv \
|
|
|
|
-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. \
|
|
|
|
-I./lib \
|
|
|
|
$(CPPFLAGS) \
|
|
|
|
-MMD \
|
|
|
|
-MP
|
2022-08-20 00:54:34 +01:00
|
|
|
|
|
|
|
# Internal linker flags that should not be changed by the user.
|
2023-03-28 23:04:07 +01:00
|
|
|
override LDFLAGS += \
|
|
|
|
-nostdlib \
|
|
|
|
-static \
|
|
|
|
-m elf_x86_64 \
|
2022-08-20 00:54:34 +01:00
|
|
|
-z max-page-size=0x1000 \
|
|
|
|
-T linker.ld
|
|
|
|
|
2023-03-28 23:04:07 +01:00
|
|
|
# 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 += \
|
2023-04-17 01:20:19 +01:00
|
|
|
-Wall \
|
2022-08-20 00:54:34 +01:00
|
|
|
-f elf64
|
|
|
|
|
2023-03-28 23:04:07 +01:00
|
|
|
# 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 find -L . -type f -name '*.c')
|
|
|
|
override ASFILES := $(shell find -L . -type f -name '*.S')
|
|
|
|
override NASMFILES := $(shell find -L . -type f -name '*.asm')
|
2022-08-20 00:54:34 +01:00
|
|
|
override OBJ := $(CFILES:.c=.o) $(ASFILES:.S=.o) $(NASMFILES:.asm=.o)
|
|
|
|
override HEADER_DEPS := $(CFILES:.c=.d) $(ASFILES:.S=.d)
|
|
|
|
|
|
|
|
# Default target.
|
|
|
|
.PHONY: all
|
|
|
|
all: $(KERNEL)
|
|
|
|
|
|
|
|
limine.h:
|
2023-03-28 23:04:07 +01:00
|
|
|
curl -Lo $@ https://github.com/limine-bootloader/limine/raw/trunk/limine.h || cp ../build/limine/limine.h limine.h || echo "ERROR"
|
2022-08-20 00:54:34 +01:00
|
|
|
|
|
|
|
# Link rules for the final kernel executable.
|
|
|
|
$(KERNEL): $(OBJ)
|
|
|
|
$(LD) $(OBJ) $(LDFLAGS) -o $@
|
|
|
|
|
|
|
|
# Include header dependencies.
|
|
|
|
-include $(HEADER_DEPS)
|
|
|
|
|
|
|
|
# Compilation rules for *.c files.
|
|
|
|
%.o: %.c limine.h
|
2023-03-28 23:04:07 +01:00
|
|
|
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
|
2022-08-20 00:54:34 +01:00
|
|
|
|
|
|
|
# Compilation rules for *.S files.
|
|
|
|
%.o: %.S limine.h
|
2023-03-28 23:04:07 +01:00
|
|
|
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
|
2022-08-20 00:54:34 +01:00
|
|
|
|
|
|
|
# Compilation rules for *.asm (nasm) files.
|
|
|
|
%.o: %.asm
|
|
|
|
nasm $(NASMFLAGS) $< -o $@
|
|
|
|
|
|
|
|
# Remove object files and the final executable.
|
|
|
|
.PHONY: clean
|
|
|
|
clean:
|
|
|
|
rm -rf $(KERNEL) $(OBJ) $(HEADER_DEPS)
|
|
|
|
|
|
|
|
.PHONY: distclean
|
|
|
|
distclean: clean
|
|
|
|
rm -f limine.h
|