From 11bd62882151036c426f7c18bd031ec2ef6d89bb Mon Sep 17 00:00:00 2001 From: furtest Date: Sun, 15 Mar 2026 16:47:16 +0100 Subject: [PATCH 1/5] Extract CC and LD to variables. This allows to change the name of the compiler or linker when calling make. --- Makefile | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 59505c3..8720455 100644 --- a/Makefile +++ b/Makefile @@ -1,21 +1,24 @@ SOURCES = src/sched/spinlock.c src/debug/misc.c src/io/term/flanterm_backends/fb.c src/io/term/flanterm.c src/debug/panic.c src/debug/stacktrace.c src/boot/boot.c src/sched/scheduler.c src/sched/process.c src/mem/heap/kheap.c src/mem/paging/vmm.c src/mem/paging/paging.c src/mem/paging/pmm.c src/string/string.c src/io/kbd/ps2.c src/io/serial/serial.c src/io/term/term.c src/idt/idt.c src/mem/gdt/gdt.c src/mem/misc/utils.c src/time/timer.c src/kmain.c +CC := x86_64-elf-gcc CC_FLAGS=-Wall -Wextra -std=gnu99 -nostdlib -ffreestanding -fno-stack-protector -fno-omit-frame-pointer -fno-stack-check -fno-PIC -ffunction-sections -fdata-sections -mcmodel=kernel CC_PROBLEMATIC_FLAGS=-Wno-unused-parameter -Wno-unused-variable +LD := x86_64-elf-ld + .PHONY: build build-iso debug debug2 run clean build: mkdir -p build rm -f *.o build/*.o - x86_64-elf-gcc -g -c -Isrc $(SOURCES) $(CC_PROBLEMATIC_FLAGS) $(CC_FLAGS) + $(CC) -g -c -Isrc $(SOURCES) $(CC_PROBLEMATIC_FLAGS) $(CC_FLAGS) mv *.o build/ nasm -f elf64 src/idt/idt.S -o build/idt_stub.o - x86_64-elf-ld -o pepperk -T linker.ld build/*.o + $(LD) -o pepperk -T linker.ld build/*.o nm -n pepperk | awk '$$2 ~ /[TtDdBbRr]/ {print $$1, $$3}' > symbols.map python3 symbols.py nasm -f elf64 symbols.S -o build/symbols.o - x86_64-elf-ld -o pepperk -T linker.ld build/*.o + $(LD) -o pepperk -T linker.ld build/*.o limine/limine: rm -rf limine From 9fc55f98d8ff34e8fca979c978cdd5256b06ecc1 Mon Sep 17 00:00:00 2001 From: furtest Date: Sun, 15 Mar 2026 16:51:55 +0100 Subject: [PATCH 2/5] Use variables for build and pepperk and rename build target. Instead of hardcoding the names set them using a variable. Also rename the target build to the name of the file it builds which is in the ELFFILE variable. --- Makefile | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 8720455..8fb5756 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,6 @@ + +BUILDDIR := build +ELFFILE := pepperk SOURCES = src/sched/spinlock.c src/debug/misc.c src/io/term/flanterm_backends/fb.c src/io/term/flanterm.c src/debug/panic.c src/debug/stacktrace.c src/boot/boot.c src/sched/scheduler.c src/sched/process.c src/mem/heap/kheap.c src/mem/paging/vmm.c src/mem/paging/paging.c src/mem/paging/pmm.c src/string/string.c src/io/kbd/ps2.c src/io/serial/serial.c src/io/term/term.c src/idt/idt.c src/mem/gdt/gdt.c src/mem/misc/utils.c src/time/timer.c src/kmain.c CC := x86_64-elf-gcc @@ -8,17 +11,17 @@ LD := x86_64-elf-ld .PHONY: build build-iso debug debug2 run clean -build: - mkdir -p build - rm -f *.o build/*.o +$(ELFFILE): + mkdir -p $(BUILDDIR) + rm -f *.o $(BUILDDIR)/*.o $(CC) -g -c -Isrc $(SOURCES) $(CC_PROBLEMATIC_FLAGS) $(CC_FLAGS) - mv *.o build/ - nasm -f elf64 src/idt/idt.S -o build/idt_stub.o - $(LD) -o pepperk -T linker.ld build/*.o - nm -n pepperk | awk '$$2 ~ /[TtDdBbRr]/ {print $$1, $$3}' > symbols.map + mv *.o $(BUILDDIR)/ + nasm -f elf64 src/idt/idt.S -o $(BUILDDIR)/idt_stub.o + $(LD) -o $(ELFFILE) -T linker.ld $(BUILDDIR)/*.o + nm -n $(ELFFILE) | awk '$$2 ~ /[TtDdBbRr]/ {print $$1, $$3}' > symbols.map python3 symbols.py - nasm -f elf64 symbols.S -o build/symbols.o - $(LD) -o pepperk -T linker.ld build/*.o + nasm -f elf64 symbols.S -o $(BUILDDIR)/symbols.o + $(LD) -o $(ELFFILE) -T linker.ld $(BUILDDIR)/*.o limine/limine: rm -rf limine @@ -28,7 +31,7 @@ limine/limine: build-iso: limine/limine build rm -rf iso_root mkdir -p iso_root/boot - cp -v pepperk iso_root/boot + cp -v $(ELFFILE) iso_root/boot mkdir -p iso_root/boot/limine cp -v limine.conf iso_root/boot/limine mkdir -p iso_root/EFI/BOOT @@ -44,14 +47,14 @@ build-iso: limine/limine build debug: /usr/bin/qemu-system-x86_64 -drive file=pepper.iso -s -S -d int -D qemu.log -no-reboot -no-shutdown & - gdb pepperk --command=debug.gdb + gdb $(ELFFILE) --command=debug.gdb debug2: /usr/bin/qemu-system-x86_64 -drive file=pepper.iso -s -S -d int -no-reboot -no-shutdown & - pwndbg pepperk --command=debug.gdb + pwndbg $(ELFFILE) --command=debug.gdb run: build-iso /usr/bin/qemu-system-x86_64 -cdrom pepper.iso -serial stdio clean: - rm -rf *.o symbols.map symbols.S pepperk iso_root pepper.iso limine build/*.o + rm -rf *.o symbols.map symbols.S $(ELFFILE) iso_root pepper.iso limine $(BUILDDIR)/*.o From 803ac0879b32c66473f4f9b7317581d62bff0f66 Mon Sep 17 00:00:00 2001 From: furtest Date: Sun, 15 Mar 2026 17:56:26 +0100 Subject: [PATCH 3/5] Auto find source files check for changes Previously the build process removed everything and did all the build again on each make invocation. This fixes this behaviour with two changes. First dynamically find the list of files to build using find instead of a manually written list. Then use implicit rules to only build files that need to be built again instead of recompiling everything. --- Makefile | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 8fb5756..e113160 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,9 @@ BUILDDIR := build ELFFILE := pepperk -SOURCES = src/sched/spinlock.c src/debug/misc.c src/io/term/flanterm_backends/fb.c src/io/term/flanterm.c src/debug/panic.c src/debug/stacktrace.c src/boot/boot.c src/sched/scheduler.c src/sched/process.c src/mem/heap/kheap.c src/mem/paging/vmm.c src/mem/paging/paging.c src/mem/paging/pmm.c src/string/string.c src/io/kbd/ps2.c src/io/serial/serial.c src/io/term/term.c src/idt/idt.c src/mem/gdt/gdt.c src/mem/misc/utils.c src/time/timer.c src/kmain.c +SRC := src +SOURCES := $(shell find src -name '*.c') +OBJFILES := $(patsubst $(SRC)/%.c, $(BUILDDIR)/%.o, $(SOURCES)) CC := x86_64-elf-gcc CC_FLAGS=-Wall -Wextra -std=gnu99 -nostdlib -ffreestanding -fno-stack-protector -fno-omit-frame-pointer -fno-stack-check -fno-PIC -ffunction-sections -fdata-sections -mcmodel=kernel @@ -11,17 +13,20 @@ LD := x86_64-elf-ld .PHONY: build build-iso debug debug2 run clean -$(ELFFILE): - mkdir -p $(BUILDDIR) - rm -f *.o $(BUILDDIR)/*.o - $(CC) -g -c -Isrc $(SOURCES) $(CC_PROBLEMATIC_FLAGS) $(CC_FLAGS) - mv *.o $(BUILDDIR)/ +$(ELFFILE): $(BUILDDIR) $(OBJFILES) nasm -f elf64 src/idt/idt.S -o $(BUILDDIR)/idt_stub.o - $(LD) -o $(ELFFILE) -T linker.ld $(BUILDDIR)/*.o + $(LD) -o $(ELFFILE) -T linker.ld $(OBJFILES) $(BUILDDIR)/idt_stub.o nm -n $(ELFFILE) | awk '$$2 ~ /[TtDdBbRr]/ {print $$1, $$3}' > symbols.map python3 symbols.py nasm -f elf64 symbols.S -o $(BUILDDIR)/symbols.o - $(LD) -o $(ELFFILE) -T linker.ld $(BUILDDIR)/*.o + $(LD) -o $(ELFFILE) -T linker.ld $(OBJFILES) $(BUILDDIR)/idt_stub.o + +$(BUILDDIR): + @mkdir -p $(BUILDDIR) + +$(BUILDDIR)/%.o: $(SRC)/%.c + mkdir -p $(dir $@) + $(CC) -g -c -Isrc $< $(CC_PROBLEMATIC_FLAGS) $(CC_FLAGS) -o $@ limine/limine: rm -rf limine From 32f388956520f85101fe5e16dc2abdc1f602bcf9 Mon Sep 17 00:00:00 2001 From: furtest Date: Sun, 15 Mar 2026 18:01:35 +0100 Subject: [PATCH 4/5] Move PHONY tags and fix clean Move the PHONY tags to make them clearer to read. Fix the clean rule so it deletes the build directory. --- Makefile | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index e113160..38218a7 100644 --- a/Makefile +++ b/Makefile @@ -11,8 +11,6 @@ CC_PROBLEMATIC_FLAGS=-Wno-unused-parameter -Wno-unused-variable LD := x86_64-elf-ld -.PHONY: build build-iso debug debug2 run clean - $(ELFFILE): $(BUILDDIR) $(OBJFILES) nasm -f elf64 src/idt/idt.S -o $(BUILDDIR)/idt_stub.o $(LD) -o $(ELFFILE) -T linker.ld $(OBJFILES) $(BUILDDIR)/idt_stub.o @@ -50,16 +48,20 @@ build-iso: limine/limine build iso_root -o pepper.iso ./limine/limine bios-install pepper.iso +.PHONY: debug debug: /usr/bin/qemu-system-x86_64 -drive file=pepper.iso -s -S -d int -D qemu.log -no-reboot -no-shutdown & gdb $(ELFFILE) --command=debug.gdb +.PHONY: debug2 debug2: /usr/bin/qemu-system-x86_64 -drive file=pepper.iso -s -S -d int -no-reboot -no-shutdown & pwndbg $(ELFFILE) --command=debug.gdb +.PHONY: run run: build-iso /usr/bin/qemu-system-x86_64 -cdrom pepper.iso -serial stdio +.PHONY: clean clean: - rm -rf *.o symbols.map symbols.S $(ELFFILE) iso_root pepper.iso limine $(BUILDDIR)/*.o + rm -rf $(BUILDDIR) symbols.map symbols.S $(ELFFILE) iso_root pepper.iso limine From b02a4b5284d542c1c8babd553ea662a28655a0ca Mon Sep 17 00:00:00 2001 From: furtest Date: Sun, 15 Mar 2026 18:05:24 +0100 Subject: [PATCH 5/5] Fix build-iso prerequisites --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 38218a7..5891c9b 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,7 @@ LD := x86_64-elf-ld $(ELFFILE): $(BUILDDIR) $(OBJFILES) nasm -f elf64 src/idt/idt.S -o $(BUILDDIR)/idt_stub.o $(LD) -o $(ELFFILE) -T linker.ld $(OBJFILES) $(BUILDDIR)/idt_stub.o + # Get the symbols for debugging nm -n $(ELFFILE) | awk '$$2 ~ /[TtDdBbRr]/ {print $$1, $$3}' > symbols.map python3 symbols.py nasm -f elf64 symbols.S -o $(BUILDDIR)/symbols.o @@ -31,7 +32,7 @@ limine/limine: git clone https://github.com/limine-bootloader/limine.git --branch=v9.x-binary --depth=1 $(MAKE) -C limine -build-iso: limine/limine build +build-iso: limine/limine $(ELFFILE) rm -rf iso_root mkdir -p iso_root/boot cp -v $(ELFFILE) iso_root/boot