11# Nuke built-in rules and variables.
22override MAKEFLAGS += -rR
33
4- # This is the name that our final kernel executable will have.
4+ # This is the name that our final executable will have.
55# Change as needed.
6- override KERNEL := kernel
6+ override OUTPUT := kernel
77
88# Convenience macro to reliably declare user overridable variables.
99define DEFAULT_VAR =
@@ -15,43 +15,42 @@ define DEFAULT_VAR =
1515 endif
1616endef
1717
18- # It is suggested to use a custom built cross toolchain to build a kernel.
19- # We are using the standard "cc" here, it may work by using
20- # the host system's toolchain, but this is not guaranteed.
21- override DEFAULT_CC := cc
22- $(eval $(call DEFAULT_VAR,CC,$(DEFAULT_CC)))
18+ # User controllable C compiler command.
19+ override DEFAULT_KCC := cc
20+ $(eval $(call DEFAULT_VAR,KCC,$(DEFAULT_KCC)))
2321
24- # Same thing for "ld" (the linker) .
25- override DEFAULT_LD := ld
26- $(eval $(call DEFAULT_VAR,LD ,$(DEFAULT_LD )))
22+ # User controllable linker command .
23+ override DEFAULT_KLD := ld
24+ $(eval $(call DEFAULT_VAR,KLD ,$(DEFAULT_KLD )))
2725
2826# User controllable C flags.
29- override DEFAULT_CFLAGS := -g -O2 -pipe
30- $(eval $(call DEFAULT_VAR,CFLAGS ,$(DEFAULT_CFLAGS )))
27+ override DEFAULT_KCFLAGS := -g -O2 -pipe
28+ $(eval $(call DEFAULT_VAR,KCFLAGS ,$(DEFAULT_KCFLAGS )))
3129
3230# User controllable C preprocessor flags. We set none by default.
33- override DEFAULT_CPPFLAGS :=
34- $(eval $(call DEFAULT_VAR,CPPFLAGS ,$(DEFAULT_CPPFLAGS )))
31+ override DEFAULT_KCPPFLAGS :=
32+ $(eval $(call DEFAULT_VAR,KCPPFLAGS ,$(DEFAULT_KCPPFLAGS )))
3533
3634# User controllable nasm flags.
37- override DEFAULT_NASMFLAGS := -F dwarf -g
38- $(eval $(call DEFAULT_VAR,NASMFLAGS ,$(DEFAULT_NASMFLAGS )))
35+ override DEFAULT_KNASMFLAGS := -F dwarf -g
36+ $(eval $(call DEFAULT_VAR,KNASMFLAGS ,$(DEFAULT_KNASMFLAGS )))
3937
4038# User controllable linker flags. We set none by default.
41- override DEFAULT_LDFLAGS :=
42- $(eval $(call DEFAULT_VAR,LDFLAGS ,$(DEFAULT_LDFLAGS )))
39+ override DEFAULT_KLDFLAGS :=
40+ $(eval $(call DEFAULT_VAR,KLDFLAGS ,$(DEFAULT_KLDFLAGS )))
4341
4442# Internal C flags that should not be changed by the user.
45- override CFLAGS += \
43+ override KCFLAGS += \
4644 -Wall \
4745 -Wextra \
4846 -std=gnu11 \
4947 -ffreestanding \
5048 -fno-stack-protector \
5149 -fno-stack-check \
5250 -fno-lto \
53- -fno-PIE \
5451 -fno-PIC \
52+ -ffunction-sections \
53+ -fdata-sections \
5554 -m64 \
5655 -march=x86-64 \
5756 -mno-80387 \
@@ -62,68 +61,64 @@ override CFLAGS += \
6261 -mcmodel=kernel
6362
6463# Internal C preprocessor flags that should not be changed by the user.
65- override CPPFLAGS := \
64+ override KCPPFLAGS := \
6665 -I src \
6766 -I src/lib \
68- $(CPPFLAGS ) \
67+ $(KCPPFLAGS ) \
6968 -MMD \
7069 -MP
7170
71+ # Internal nasm flags that should not be changed by the user.
72+ override KNASMFLAGS += \
73+ -Wall \
74+ -f elf64
75+
7276# Internal linker flags that should not be changed by the user.
73- override LDFLAGS += \
77+ override KLDFLAGS += \
7478 -m elf_x86_64 \
7579 -nostdlib \
7680 -static \
7781 -z max-page-size=0x1000 \
82+ -gc-sections \
7883 -T linker.ld
7984
80- # Check if the linker supports -no-pie and enable it if it does.
81- ifeq ($(shell $(LD ) --help 2>&1 | grep 'no-pie' >/dev/null 2>&1; echo $$? ) ,0)
82- override LDFLAGS += -no-pie
83- endif
84-
85- # Internal nasm flags that should not be changed by the user.
86- override NASMFLAGS += \
87- -Wall \
88- -f elf64
89-
9085# Use "find" to glob all *.c, *.S, and *.asm files in the tree and obtain the
9186# object and header dependency file names.
92- override CFILES := $(shell cd src && find -L . -type f -name '* .c')
93- override ASFILES := $(shell cd src && find -L . -type f -name '* .S')
94- override NASMFILES := $(shell cd src && find -L . -type f -name '* .asm')
87+ override CFILES := $(shell cd src && find -L * -type f -name '* .c' | LC_ALL=C sort )
88+ override ASFILES := $(shell cd src && find -L * -type f -name '* .S' | LC_ALL=C sort )
89+ override NASMFILES := $(shell cd src && find -L * -type f -name '* .asm' | LC_ALL=C sort )
9590override OBJ := $(addprefix obj/,$(CFILES:.c=.c.o ) $(ASFILES:.S=.S.o ) $(NASMFILES:.asm=.asm.o ) )
9691override HEADER_DEPS := $(addprefix obj/,$(CFILES:.c=.c.d ) $(ASFILES:.S=.S.d ) )
9792
9893# Default target.
9994.PHONY : all
100- all : bin/$(KERNEL )
95+ all : bin/$(OUTPUT )
10196
10297src/limine.h :
10398 curl -Lo $@ https://github.com/limine-bootloader/limine/raw/trunk/limine.h || cp ../build/limine/limine.h src/limine.h || echo " ERROR"
10499
105- # Link rules for the final kernel executable.
106- bin/$(KERNEL ) : GNUmakefile linker.ld $(OBJ )
100+ # Link rules for the final executable.
101+ bin/$(OUTPUT ) : GNUmakefile linker.ld $(OBJ )
107102 mkdir -p " $$ (dirname $@ )"
108- $(LD ) $(OBJ ) $(LDFLAGS ) -o $@
103+ $(KLD ) $(OBJ ) $(KLDFLAGS ) -o $@
109104
110105# Include header dependencies.
111106-include $(HEADER_DEPS )
112107
113108# Compilation rules for *.c files.
114109obj/% .c.o : src/% .c GNUmakefile src/limine.h
115110 mkdir -p " $$ (dirname $@ )"
116- $(CC ) $(CFLAGS ) $(CPPFLAGS ) -c $< -o $@
111+ $(KCC ) $(KCFLAGS ) $(KCPPFLAGS ) -c $< -o $@
117112
118113# Compilation rules for *.S files.
119114obj/% .S.o : src/% .S GNUmakefile src/limine.h
120115 mkdir -p " $$ (dirname $@ )"
121- $(CC ) $(CFLAGS ) $(CPPFLAGS ) -c $< -o $@
116+ $(KCC ) $(KCFLAGS ) $(KCPPFLAGS ) -c $< -o $@
122117
123118# Compilation rules for *.asm (nasm) files.
124119obj/% .asm.o : src/% .asm GNUmakefile
125120 mkdir -p " $$ (dirname $@ )"
126- nasm $(NASMFLAGS ) $< -o $@
121+ nasm $(KNASMFLAGS ) $< -o $@
127122
128123# Remove object files and the final executable.
129124.PHONY : clean
0 commit comments