feat: make kernel architecture-agnostic; primitive work on arm port

This commit is contained in:
daniel committed 2026-10-24 21:25:18 +02:00
1 parent dab7d5427b
commit 1bae77ec84
164 files changed
+3664 -1641

No files matched your search

+48 -1
View File
@@ -27,6 +27,20 @@ ifeq ($(ARCH),x86_64)
endif
endif
# The other architectures have no in-tree cross GCC: default to LLVM, which
# cross-compiles to every target (linking needs ld.lld; see docs/porting.md).
ifneq ($(ARCH),x86_64)
ifeq ($(filter command line environment,$(origin CC)),)
CC := clang
endif
ifeq ($(filter command line environment,$(origin CXX)),)
CXX := clang++
endif
ifeq ($(filter command line environment,$(origin AR)),)
AR := ar
endif
endif
# User controllable C compiler command.
CC ?= cc
@@ -197,8 +211,10 @@ override CXXFLAGS += \
-std=gnu++20 \
-fno-rtti \
-fno-exceptions \
-fmodules-ts \
$(SHARED_FLAGS)
ifneq ($(CXX_IS_CLANG),1)
override CXXFLAGS += -fmodules-ts
endif
# Internal linker flags that should not be changed by the user.
override LDFLAGS += \
@@ -212,6 +228,9 @@ override LDFLAGS += \
# Use "find" to glob all *.c, *.cpp, *.S, and *.asm files in the tree and obtain the
# object and header dependency file names.
override SRCFILES := $(shell cd src && find -L * -type f | LC_ALL=C sort)
# Architecture-specific code lives in src/Arch/<arch>/; only the target's
# directory is built. Everything else in src/ is architecture-neutral.
override SRCFILES := $(filter-out Arch/%,$(SRCFILES)) $(filter Arch/$(ARCH)/%,$(SRCFILES))
override CFILES := $(filter %.c,$(SRCFILES))
override CXXFILES := $(filter %.cpp,$(SRCFILES))
override OBJCFILES := $(filter %.mm,$(SRCFILES))
@@ -253,6 +272,11 @@ $(BUILDNO_HDR): $(filter-out $(BUILDNO_OBJ),$(OBJ)) $(PROG_SRC)
# on a clean build (where no .d dependency exists yet).
$(BUILDNO_OBJ): $(BUILDNO_HDR)
# Compile everything without linking (used by scripts/check-arch-neutral.sh
# to verify a port's objects before a cross linker is available).
.PHONY: objects
objects: $(OBJ) cc-runtime-$(ARCH)/cc-runtime.a
# Link rules for building the C compiler runtime.
cc-runtime-$(ARCH)/cc-runtime.a: GNUmakefile cc-runtime/*
rm -rf cc-runtime-$(ARCH)
@@ -263,10 +287,33 @@ cc-runtime-$(ARCH)/cc-runtime.a: GNUmakefile cc-runtime/*
CFLAGS="$(CFLAGS)" \
CPPFLAGS='-isystem ../freestnd-c-hdrs/$(ARCH)/include -DCC_RUNTIME_NO_FLOAT'
# Clang's generic-ELF driver links cross targets through the host's GCC,
# so a clang-built non-x86 kernel is linked by ld.lld directly.
ifneq ($(ARCH),x86_64)
ifeq ($(CXX_IS_CLANG),1)
KERNEL_LD ?= ld.lld
override LD_EMULATION_aarch64 := aarch64elf
override LD_EMULATION_riscv64 := elf64lriscv
override LD_EMULATION_loongarch64 := elf64loongarch
override KERNEL_LDFLAGS := \
-m $(LD_EMULATION_$(ARCH)) \
--build-id=none \
--no-relax \
-static \
-z max-page-size=0x1000 \
--gc-sections \
-T linker-$(ARCH).ld
endif
endif
# Link rules for the final executable.
bin-$(ARCH)/$(OUTPUT): GNUmakefile linker-$(ARCH).ld $(OBJ) cc-runtime-$(ARCH)/cc-runtime.a
mkdir -p "$$(dirname $@)"
ifeq ($(KERNEL_LD),)
$(CXX) $(CXXFLAGS) $(LDFLAGS) $(OBJ) cc-runtime-$(ARCH)/cc-runtime.a -o $@
else
$(KERNEL_LD) $(KERNEL_LDFLAGS) $(OBJ) cc-runtime-$(ARCH)/cc-runtime.a -o $@
endif
# Compilation rules for *.c files.
obj-$(ARCH)/%.c.o: src/%.c GNUmakefile