2023-03-28 23:04:07 +01:00
|
|
|
# Nuke built-in rules and variables.
|
|
|
|
override MAKEFLAGS += -rR
|
|
|
|
|
2024-08-14 20:19:14 +01:00
|
|
|
# This is the name that our final executable will have.
|
2022-08-20 00:54:34 +01:00
|
|
|
# Change as needed.
|
2024-08-14 20:19:14 +01:00
|
|
|
override OUTPUT := kernel
|
2022-08-20 00:54:34 +01:00
|
|
|
|
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
|
|
|
|
|
2024-08-14 20:19:14 +01:00
|
|
|
# User controllable C compiler command.
|
|
|
|
override DEFAULT_KCC := cc
|
|
|
|
$(eval $(call DEFAULT_VAR,KCC,$(DEFAULT_KCC)))
|
2022-08-20 00:54:34 +01:00
|
|
|
|
2024-08-14 20:19:14 +01:00
|
|
|
# User controllable linker command.
|
|
|
|
override DEFAULT_KLD := ld
|
|
|
|
$(eval $(call DEFAULT_VAR,KLD,$(DEFAULT_KLD)))
|
2022-08-20 00:54:34 +01:00
|
|
|
|
2023-03-28 23:04:07 +01:00
|
|
|
# User controllable C flags.
|
2024-08-14 20:19:14 +01:00
|
|
|
override DEFAULT_KCFLAGS := -g -O2 -pipe
|
|
|
|
$(eval $(call DEFAULT_VAR,KCFLAGS,$(DEFAULT_KCFLAGS)))
|
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.
|
2024-08-14 20:19:14 +01:00
|
|
|
override DEFAULT_KCPPFLAGS :=
|
|
|
|
$(eval $(call DEFAULT_VAR,KCPPFLAGS,$(DEFAULT_KCPPFLAGS)))
|
2022-08-20 00:54:34 +01:00
|
|
|
|
|
|
|
# User controllable nasm flags.
|
2024-08-14 20:19:14 +01:00
|
|
|
override DEFAULT_KNASMFLAGS := -F dwarf -g
|
|
|
|
$(eval $(call DEFAULT_VAR,KNASMFLAGS,$(DEFAULT_KNASMFLAGS)))
|
2022-08-20 00:54:34 +01:00
|
|
|
|
|
|
|
# User controllable linker flags. We set none by default.
|
2024-08-14 20:19:14 +01:00
|
|
|
override DEFAULT_KLDFLAGS :=
|
|
|
|
$(eval $(call DEFAULT_VAR,KLDFLAGS,$(DEFAULT_KLDFLAGS)))
|
2022-08-20 00:54:34 +01:00
|
|
|
|
|
|
|
# Internal C flags that should not be changed by the user.
|
2024-08-14 20:19:14 +01:00
|
|
|
override KCFLAGS += \
|
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-PIC \
|
2024-08-14 20:19:14 +01:00
|
|
|
-ffunction-sections \
|
|
|
|
-fdata-sections \
|
2023-03-28 23:04:07 +01:00
|
|
|
-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.
|
2024-08-14 20:19:14 +01:00
|
|
|
override KCPPFLAGS := \
|
2023-09-28 08:31:52 +01:00
|
|
|
-I src \
|
|
|
|
-I src/lib \
|
2024-08-14 20:19:14 +01:00
|
|
|
$(KCPPFLAGS) \
|
2023-03-28 23:04:07 +01:00
|
|
|
-MMD \
|
|
|
|
-MP
|
2022-08-20 00:54:34 +01:00
|
|
|
|
2024-08-14 20:19:14 +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.
|
2024-08-14 20:19:14 +01:00
|
|
|
override KLDFLAGS += \
|
2023-07-15 07:12:16 +01:00
|
|
|
-m elf_x86_64 \
|
2023-03-28 23:04:07 +01:00
|
|
|
-nostdlib \
|
|
|
|
-static \
|
2022-08-20 00:54:34 +01:00
|
|
|
-z max-page-size=0x1000 \
|
2024-08-14 20:19:14 +01:00
|
|
|
-gc-sections \
|
2022-08-20 00:54:34 +01:00
|
|
|
-T linker.ld
|
|
|
|
|
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.
|
2024-08-14 20:19:14 +01:00
|
|
|
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)
|
2023-09-28 08:31:52 +01:00
|
|
|
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
|
2024-08-14 20:19:14 +01:00
|
|
|
all: bin/$(OUTPUT)
|
2022-08-20 00:54:34 +01:00
|
|
|
|
2023-09-28 08:31:52 +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
|
|
|
|
2024-08-14 20:19:14 +01:00
|
|
|
# Link rules for the final executable.
|
|
|
|
bin/$(OUTPUT): GNUmakefile linker.ld $(OBJ)
|
2023-09-28 08:31:52 +01:00
|
|
|
mkdir -p "$$(dirname $@)"
|
2024-08-14 20:19:14 +01:00
|
|
|
$(KLD) $(OBJ) $(KLDFLAGS) -o $@
|
2022-08-20 00:54:34 +01:00
|
|
|
|
|
|
|
# Include header dependencies.
|
|
|
|
-include $(HEADER_DEPS)
|
|
|
|
|
|
|
|
# Compilation rules for *.c files.
|
2023-09-28 08:31:52 +01:00
|
|
|
obj/%.c.o: src/%.c GNUmakefile src/limine.h
|
|
|
|
mkdir -p "$$(dirname $@)"
|
2024-08-14 20:19:14 +01:00
|
|
|
$(KCC) $(KCFLAGS) $(KCPPFLAGS) -c $< -o $@
|
2022-08-20 00:54:34 +01:00
|
|
|
|
|
|
|
# Compilation rules for *.S files.
|
2023-09-28 08:31:52 +01:00
|
|
|
obj/%.S.o: src/%.S GNUmakefile src/limine.h
|
|
|
|
mkdir -p "$$(dirname $@)"
|
2024-08-14 20:19:14 +01:00
|
|
|
$(KCC) $(KCFLAGS) $(KCPPFLAGS) -c $< -o $@
|
2022-08-20 00:54:34 +01:00
|
|
|
|
|
|
|
# Compilation rules for *.asm (nasm) files.
|
2023-09-28 08:31:52 +01:00
|
|
|
obj/%.asm.o: src/%.asm GNUmakefile
|
|
|
|
mkdir -p "$$(dirname $@)"
|
2024-08-14 20:19:14 +01:00
|
|
|
nasm $(KNASMFLAGS) $< -o $@
|
2022-08-20 00:54:34 +01:00
|
|
|
|
|
|
|
# Remove object files and the final executable.
|
|
|
|
.PHONY: clean
|
|
|
|
clean:
|
2023-09-28 08:31:52 +01:00
|
|
|
rm -rf bin obj
|
2022-08-20 00:54:34 +01:00
|
|
|
|
|
|
|
.PHONY: distclean
|
|
|
|
distclean: clean
|
2023-09-28 08:31:52 +01:00
|
|
|
rm -f src/limine.h
|