From f33d27f8fa1d48fbc01b04cab718d8e87b2e85ec Mon Sep 17 00:00:00 2001 From: Matthew Brush Date: Sun, 10 Aug 2025 12:38:30 -0700 Subject: [PATCH 1/6] Use ARCH make variable in dump target --- Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 282b54e6..e7dfd903 100644 --- a/Makefile +++ b/Makefile @@ -63,11 +63,11 @@ run: debug: $(MAKE) $(MODE) ./rundebug MODE=$(MODE) $(ARGS) - + dump: $(OBJCOPY) -O binary kernel.elf kernel.img - aarch64-none-elf-objdump -D kernel.elf > dump - + $(ARCH)-objdump -D kernel.elf > dump + install: $(MAKE) clean $(MAKE) LOAD_ADDR=0x80000 XHCI_CTX_SIZE=64 QEMU=false all From 939a9999218017e3d60ad98647600d4e2b5b7c35 Mon Sep 17 00:00:00 2001 From: Matthew Brush Date: Sun, 10 Aug 2025 12:42:27 -0700 Subject: [PATCH 2/6] Use building make variable RM (for `rm -f`) --- Makefile | 4 ++-- kernel/Makefile | 2 +- shared/Makefile | 2 +- user/Makefile | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index e7dfd903..11de4c4c 100644 --- a/Makefile +++ b/Makefile @@ -46,9 +46,9 @@ clean: $(MAKE) -C user clean $(MAKE) -C kernel clean @echo "removing fs dirs" - rm -rf $(FS_DIRS) + $(RM) -r $(FS_DIRS) @echo "removing images" - rm -f kernel.img kernel.elf disk.img dump + $(RM) kernel.img kernel.elf disk.img dump raspi: $(MAKE) LOAD_ADDR=0x80000 XHCI_CTX_SIZE=64 QEMU=true all diff --git a/kernel/Makefile b/kernel/Makefile index 8d936996..c1d2f4e6 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -34,6 +34,6 @@ $(TARGET): ../shared/libshared.a $(OBJ) $(CC) $(CFLAGS) -fno-rtti -c -MMD -MP $< -o $@ clean: - rm -f $(CLEAN_OBJS) $(CLEAN_DEPS) $(ELF) $(TARGET) + $(RM) $(CLEAN_OBJS) $(CLEAN_DEPS) $(ELF) $(TARGET) -include $(DEP) diff --git a/shared/Makefile b/shared/Makefile index d815120d..c9af41dd 100644 --- a/shared/Makefile +++ b/shared/Makefile @@ -28,6 +28,6 @@ $(TARGET): $(OBJ) $(CC) $(CFLAGS) -fno-rtti -c -MMD -MP $< -o $@ clean: - rm -f $(CLEAN_OBJS) $(CLEAN_DEPS) $(TARGET) + $(RM) $(CLEAN_OBJS) $(CLEAN_DEPS) $(TARGET) -include $(DEP) diff --git a/user/Makefile b/user/Makefile index 3f9ce25e..818baaa9 100644 --- a/user/Makefile +++ b/user/Makefile @@ -32,6 +32,6 @@ $(LOCATION)$(TARGET): $(OBJ) $(CC) $(CFLAGS) -fno-rtti -c -MMD -MP $< -o $@ clean: - rm -f $(CLEAN_OBJS) $(CLEAN_DEPS) $(TARGET) + $(RM) $(CLEAN_OBJS) $(CLEAN_DEPS) $(TARGET) -include $(DEP) From e832de3ec1f279ada60ddeedcc345af613e933ee Mon Sep 17 00:00:00 2001 From: Matthew Brush Date: Sun, 10 Aug 2025 13:21:11 -0700 Subject: [PATCH 3/6] Split C and C++ compilers and flags Instead of using `gcc` to compile both C and C++, use `gcc` for C and `g++` for C++. This allows to split flags more cleanly. Re-organize flag variables a bit, adding `CPPFLAGS` for C preprocessor flags, `CFLAGS` for C compiler flags and `CXXFLAGS` for C++ compiler flags. --- Makefile | 16 ++++++++++------ kernel/Makefile | 15 ++++++++++----- shared/Makefile | 8 +++++--- user/Makefile | 10 ++++++---- 4 files changed, 31 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 11de4c4c..ab1b652c 100644 --- a/Makefile +++ b/Makefile @@ -1,21 +1,25 @@ ARCH ?= aarch64-none-elf CC := $(ARCH)-gcc +CXX := $(ARCH)-g++ LD := $(ARCH)-ld AR := $(ARCH)-ar OBJCOPY := $(ARCH)-objcopy -CFLAGS_BASE ?= -g -O0 -nostdlib -ffreestanding \ - -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables \ - -Wall -Wextra -Wno-unused-parameter -Wno-address-of-packed-member -mcpu=cortex-a72 -CONLY_FLAGS_BASE ?= -std=c17 -LDFLAGS_BASE ?= +COMMON_FLAGS ?= -ffreestanding -nostdlib -fno-exceptions -fno-unwind-tables \ + -fno-asynchronous-unwind-tables -g -O0 -Wall -Wextra \ + -Wno-unused-parameter -Wno-address-of-packed-member \ + -mcpu=cortex-a72 + +CFLAGS_BASE ?= $(COMMON_FLAGS) -std=c17 +CXXFLAGS_BASE ?= $(COMMON_FLAGS) -fno-rtti +LDFLAGS_BASE ?= LOAD_ADDR ?= 0x41000000 XHCI_CTX_SIZE ?= 32 QEMU ?= true MODE ?= virt -export ARCH CC LD AR OBJCOPY CFLAGS_BASE CONLY_FLAGS_BASE LDFLAGS_BASE LOAD_ADDR XHCI_CTX_SIZE QEMU +export ARCH CC CXX LD AR OBJCOPY COMMON_FLAGS CFLAGS_BASE CXXFLAGS_BASE LDFLAGS_BASE LOAD_ADDR XHCI_CTX_SIZE QEMU OS := $(shell uname) FS_DIRS := fs/redos/user diff --git a/kernel/Makefile b/kernel/Makefile index c1d2f4e6..a32a8108 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -1,12 +1,15 @@ #kernel # toolchain (inherited from top-level) -CFLAGS := $(CFLAGS_BASE) -I. -I../shared -I../user -DXHCI_CTX_SIZE=$(XHCI_CTX_SIZE) +CPPFLAGS := -I. -I../shared -I../user -DXHCI_CTX_SIZE=$(XHCI_CTX_SIZE) + ifeq ($(QEMU),true) - CFLAGS += -DQEMU + CPPFLAGS += -DQEMU endif -LDFLAGS := $(LDFLAGS_BASE) -T $(shell ls *.ld) --defsym=LOAD_ADDR=$(LOAD_ADDR) +CFLAGS := $(CFLAGS_BASE) $(CPPFLAGS) +CXXFLAGS := $(CXXFLAGS_BASE) $(CPPFLAGS) +LDFLAGS := $(LDFLAGS_BASE) -T $(shell ls *.ld) --defsym=LOAD_ADDR=$(LOAD_ADDR) CLEAN_OBJS := $(shell find . -name '*.o') CLEAN_DEPS := $(shell find . -name '*.d') @@ -28,10 +31,12 @@ $(TARGET): ../shared/libshared.a $(OBJ) %.o: %.S $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ + %.o: %.c - $(CC) $(CFLAGS) $(CONLY_FLAGS_BASE) -c -MMD -MP $< -o $@ + $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ + %.o: %.cpp - $(CC) $(CFLAGS) -fno-rtti -c -MMD -MP $< -o $@ + $(CXX) $(CXXFLAGS) -c -MMD -MP $< -o $@ clean: $(RM) $(CLEAN_OBJS) $(CLEAN_DEPS) $(ELF) $(TARGET) diff --git a/shared/Makefile b/shared/Makefile index c9af41dd..7814cced 100644 --- a/shared/Makefile +++ b/shared/Makefile @@ -1,5 +1,7 @@ #shared -CFLAGS := $(CFLAGS_BASE) -I. -I../kernel -Wno-unused-parameter +CPPFLAGS := -I. -I../kernel +CFLAGS := $(CFLAGS_BASE) $(CPPFLAGS) +CXXFLAGS := $(CXXFLAGS_BASE) $(CPPFLAGS) CLEAN_OBJS := $(shell find . -name '*.o') CLEAN_DEPS := $(shell find . -name '*.d') @@ -22,10 +24,10 @@ $(TARGET): $(OBJ) $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ %.o: %.c - $(CC) $(CFLAGS) $(CONLY_FLAGS_BASE) -c -MMD -MP $< -o $@ + $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ %.o: %.cpp - $(CC) $(CFLAGS) -fno-rtti -c -MMD -MP $< -o $@ + $(CXX) $(CXXFLAGS) -c -MMD -MP $< -o $@ clean: $(RM) $(CLEAN_OBJS) $(CLEAN_DEPS) $(TARGET) diff --git a/user/Makefile b/user/Makefile index 818baaa9..5c1c88a7 100644 --- a/user/Makefile +++ b/user/Makefile @@ -1,6 +1,8 @@ #user -CFLAGS := $(CFLAGS_BASE) -I. -I../shared -Wno-unused-parameter -LDFLAGS := -T $(shell ls *.ld) +CPPFLAGS := -I. -I../shared +CFLAGS := $(CFLAGS_BASE) $(CPPFLAGS) +CXXFLAGS := $(CXXFLAGS_BASE) $(CPPFLAGS) +LDFLAGS := -T $(shell ls *.ld) CLEAN_OBJS := $(shell find . -name '*.o') CLEAN_DEPS := $(shell find . -name '*.d') @@ -26,10 +28,10 @@ $(LOCATION)$(TARGET): $(OBJ) $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ %.o: %.c - $(CC) $(CFLAGS) $(CONLY_FLAGS_BASE) -c -MMD -MP $< -o $@ + $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ %.o: %.cpp - $(CC) $(CFLAGS) -fno-rtti -c -MMD -MP $< -o $@ + $(CXX) $(CXXFLAGS) -c -MMD -MP $< -o $@ clean: $(RM) $(CLEAN_OBJS) $(CLEAN_DEPS) $(TARGET) From 4070d03c632f005a159d1186d236fdcdd4ea031c Mon Sep 17 00:00:00 2001 From: Matthew Brush Date: Sun, 10 Aug 2025 13:46:44 -0700 Subject: [PATCH 4/6] Implement "silent" Make rules by default This makes it easier to see the build output/warnings. Full build output can be enabled with `make V=1`. --- Makefile | 22 ++++++++++++++++++++-- kernel/Makefile | 8 ++++---- shared/Makefile | 8 ++++---- user/Makefile | 8 ++++---- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index ab1b652c..152dbeb9 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,23 @@ XHCI_CTX_SIZE ?= 32 QEMU ?= true MODE ?= virt -export ARCH CC CXX LD AR OBJCOPY COMMON_FLAGS CFLAGS_BASE CXXFLAGS_BASE LDFLAGS_BASE LOAD_ADDR XHCI_CTX_SIZE QEMU +ifeq ($(V), 1) + VAR = $(AR) + VAS = $(CC) + VCC = $(CC) + VCXX = $(CXX) + VLD = $(LD) +else + VAR = @echo " [AR] $@" && $(AR) + VAS = @echo " [AS] $@" && $(CC) + VCC = @echo " [CC] $@" && $(CC) + VCXX = @echo " [CXX] $@" && $(CXX) + VLD = @echo " [LD] $@" && $(LD) +endif + +export AR AS CC CXX LD OBJCOPY +export VAR VAS VCC VCXX VLD +export ARCH COMMON_FLAGS CFLAGS_BASE CXXFLAGS_BASE LDFLAGS_BASE LOAD_ADDR XHCI_CTX_SIZE QEMU OS := $(shell uname) FS_DIRS := fs/redos/user @@ -93,4 +109,6 @@ help: make debug build and run with debugger\n\ make dump disassemble kernel.elf\n\ make install create raspi kernel and mount it on a bootable partition\n\ - make prepare-fs create directories for the filesystem\n\n" + make prepare-fs create directories for the filesystem\n\n"\ + \n\ + Use 'make V=1' for verbose build output. diff --git a/kernel/Makefile b/kernel/Makefile index a32a8108..fb7ffb5c 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -26,17 +26,17 @@ TARGET := ../kernel.img all: $(TARGET) $(TARGET): ../shared/libshared.a $(OBJ) - $(LD) $(LDFLAGS) -o $(ELF) $(OBJL) ../shared/libshared.a + $(VLD) $(LDFLAGS) -o $(ELF) $(OBJL) ../shared/libshared.a $(OBJCOPY) -O binary $(ELF) $@ %.o: %.S - $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ + $(VAS) $(CFLAGS) -c $< -o $@ %.o: %.c - $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ + $(VCC) $(CFLAGS) -c -MMD -MP $< -o $@ %.o: %.cpp - $(CXX) $(CXXFLAGS) -c -MMD -MP $< -o $@ + $(VCXX) $(CXXFLAGS) -c -MMD -MP $< -o $@ clean: $(RM) $(CLEAN_OBJS) $(CLEAN_DEPS) $(ELF) $(TARGET) diff --git a/shared/Makefile b/shared/Makefile index 7814cced..0c1f4f98 100644 --- a/shared/Makefile +++ b/shared/Makefile @@ -18,16 +18,16 @@ TARGET := libshared.a all: $(TARGET) $(TARGET): $(OBJ) - $(AR) rcs $@ $^ + $(VAR) rcs $@ $^ %.o: %.S - $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ + $(VAS) $(CFLAGS) -c $< -o $@ %.o: %.c - $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ + $(VCC) $(CFLAGS) -c -MMD -MP $< -o $@ %.o: %.cpp - $(CXX) $(CXXFLAGS) -c -MMD -MP $< -o $@ + $(VCXX) $(CXXFLAGS) -c -MMD -MP $< -o $@ clean: $(RM) $(CLEAN_OBJS) $(CLEAN_DEPS) $(TARGET) diff --git a/user/Makefile b/user/Makefile index 5c1c88a7..e479a372 100644 --- a/user/Makefile +++ b/user/Makefile @@ -21,17 +21,17 @@ LOCATION := ../fs/redos/user/ all: $(LOCATION)$(TARGET) $(LOCATION)$(TARGET): $(OBJ) - $(LD) $(LDFLAGS) -o $(LOCATION)$(ELF) $(OBJ) ../shared/libshared.a + $(VLD) $(LDFLAGS) -o $(LOCATION)$(ELF) $(OBJ) ../shared/libshared.a $(OBJCOPY) -O binary $(LOCATION)$(ELF) $@ %.o: %.S - $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ + $(VAS) $(CFLAGS) -c $< -o $@ %.o: %.c - $(CC) $(CFLAGS) -c -MMD -MP $< -o $@ + $(VCC) $(CFLAGS) -c -MMD -MP $< -o $@ %.o: %.cpp - $(CXX) $(CXXFLAGS) -c -MMD -MP $< -o $@ + $(VCXX) $(CXXFLAGS) -c -MMD -MP $< -o $@ clean: $(RM) $(CLEAN_OBJS) $(CLEAN_DEPS) $(TARGET) From af5c969b1c9cfe4218bfd1f73d430524328d12e4 Mon Sep 17 00:00:00 2001 From: Matthew Brush Date: Sun, 10 Aug 2025 13:53:08 -0700 Subject: [PATCH 5/6] Use instead of repeating targets --- Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 152dbeb9..8809e711 100644 --- a/Makefile +++ b/Makefile @@ -62,9 +62,9 @@ kernel: $(MAKE) -C kernel LOAD_ADDR=$(LOAD_ADDR) XHCI_CTX_SIZE=$(XHCI_CTX_SIZE) QEMU=$(QEMU) clean: - $(MAKE) -C shared clean - $(MAKE) -C user clean - $(MAKE) -C kernel clean + $(MAKE) -C shared $@ + $(MAKE) -C user $@ + $(MAKE) -C kernel $@ @echo "removing fs dirs" $(RM) -r $(FS_DIRS) @echo "removing images" From d66a325fe36c22ee6923117f7c001a6c4ea4c548 Mon Sep 17 00:00:00 2001 From: Matthew Brush Date: Sun, 10 Aug 2025 14:01:52 -0700 Subject: [PATCH 6/6] Extract common variables into common.mk Include common.mk from the other Makefiles instead of exporting variables from the top-level Makefile so they get expanded correctly. --- Makefile | 39 +-------------------------------------- common.mk | 34 ++++++++++++++++++++++++++++++++++ kernel/Makefile | 2 ++ shared/Makefile | 3 +++ user/Makefile | 3 +++ 5 files changed, 43 insertions(+), 38 deletions(-) create mode 100644 common.mk diff --git a/Makefile b/Makefile index 8809e711..3255c719 100644 --- a/Makefile +++ b/Makefile @@ -1,41 +1,4 @@ -ARCH ?= aarch64-none-elf -CC := $(ARCH)-gcc -CXX := $(ARCH)-g++ -LD := $(ARCH)-ld -AR := $(ARCH)-ar -OBJCOPY := $(ARCH)-objcopy - -COMMON_FLAGS ?= -ffreestanding -nostdlib -fno-exceptions -fno-unwind-tables \ - -fno-asynchronous-unwind-tables -g -O0 -Wall -Wextra \ - -Wno-unused-parameter -Wno-address-of-packed-member \ - -mcpu=cortex-a72 - -CFLAGS_BASE ?= $(COMMON_FLAGS) -std=c17 -CXXFLAGS_BASE ?= $(COMMON_FLAGS) -fno-rtti -LDFLAGS_BASE ?= - -LOAD_ADDR ?= 0x41000000 -XHCI_CTX_SIZE ?= 32 -QEMU ?= true -MODE ?= virt - -ifeq ($(V), 1) - VAR = $(AR) - VAS = $(CC) - VCC = $(CC) - VCXX = $(CXX) - VLD = $(LD) -else - VAR = @echo " [AR] $@" && $(AR) - VAS = @echo " [AS] $@" && $(CC) - VCC = @echo " [CC] $@" && $(CC) - VCXX = @echo " [CXX] $@" && $(CXX) - VLD = @echo " [LD] $@" && $(LD) -endif - -export AR AS CC CXX LD OBJCOPY -export VAR VAS VCC VCXX VLD -export ARCH COMMON_FLAGS CFLAGS_BASE CXXFLAGS_BASE LDFLAGS_BASE LOAD_ADDR XHCI_CTX_SIZE QEMU +include common.mk OS := $(shell uname) FS_DIRS := fs/redos/user diff --git a/common.mk b/common.mk new file mode 100644 index 00000000..676b839e --- /dev/null +++ b/common.mk @@ -0,0 +1,34 @@ +ARCH ?= aarch64-none-elf +CC := $(ARCH)-gcc +CXX := $(ARCH)-g++ +LD := $(ARCH)-ld +AR := $(ARCH)-ar +OBJCOPY := $(ARCH)-objcopy + +COMMON_FLAGS ?= -ffreestanding -nostdlib -fno-exceptions -fno-unwind-tables \ + -fno-asynchronous-unwind-tables -g -O0 -Wall -Wextra \ + -Wno-unused-parameter -Wno-address-of-packed-member \ + -mcpu=cortex-a72 + +CFLAGS_BASE ?= $(COMMON_FLAGS) -std=c17 +CXXFLAGS_BASE ?= $(COMMON_FLAGS) -fno-rtti +LDFLAGS_BASE ?= + +LOAD_ADDR ?= 0x41000000 +XHCI_CTX_SIZE ?= 32 +QEMU ?= true +MODE ?= virt + +ifeq ($(V), 1) + VAR = $(AR) + VAS = $(CC) + VCC = $(CC) + VCXX = $(CXX) + VLD = $(LD) +else + VAR = @echo " [AR] $@" && $(AR) + VAS = @echo " [AS] $@" && $(CC) + VCC = @echo " [CC] $@" && $(CC) + VCXX = @echo " [CXX] $@" && $(CXX) + VLD = @echo " [LD] $@" && $(LD) +endif diff --git a/kernel/Makefile b/kernel/Makefile index fb7ffb5c..db1c553c 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -1,6 +1,8 @@ #kernel # toolchain (inherited from top-level) +include ../common.mk + CPPFLAGS := -I. -I../shared -I../user -DXHCI_CTX_SIZE=$(XHCI_CTX_SIZE) ifeq ($(QEMU),true) diff --git a/shared/Makefile b/shared/Makefile index 0c1f4f98..cd1b61bc 100644 --- a/shared/Makefile +++ b/shared/Makefile @@ -1,4 +1,7 @@ #shared + +include ../common.mk + CPPFLAGS := -I. -I../kernel CFLAGS := $(CFLAGS_BASE) $(CPPFLAGS) CXXFLAGS := $(CXXFLAGS_BASE) $(CPPFLAGS) diff --git a/user/Makefile b/user/Makefile index e479a372..5e5cabf1 100644 --- a/user/Makefile +++ b/user/Makefile @@ -1,4 +1,7 @@ #user + +include ../common.mk + CPPFLAGS := -I. -I../shared CFLAGS := $(CFLAGS_BASE) $(CPPFLAGS) CXXFLAGS := $(CXXFLAGS_BASE) $(CPPFLAGS)