From: Tulio A M Mendes Date: Fri, 13 Mar 2026 23:45:00 +0000 (-0300) Subject: fix: build system cleanup — replace non-freestanding with utils.h, paramet... X-Git-Url: https://projects.tadryanom.me/docs/POSIX_ROADMAP.md?a=commitdiff_plain;h=2deaf85c56a2ca398fa877529e7306203b337d6e;p=AdrOS.git fix: build system cleanup — replace non-freestanding with utils.h, parameterize toolchain, fix header conflicts - Kernel: replace #include (not freestanding) with #include "utils.h" in src/arch/x86/fpu.c, src/kernel/fpu.c, src/rump/rumpuser_adros.c - Makefile: define USER_CC/USER_LD/USER_AR variables for userspace cross-compilation, replacing all hardcoded i686-elf-gcc/ld references in FULLTEST, DYN_CC, DYN_LD, LDSO, PIE_SO, PIE_ELF rules - Makefile: pass USER_CC/USER_LD/USER_AR to ulibc sub-make for consistent toolchain - Makefile: add missing .PHONY targets (test-battery, run-arm, run-riscv, run-mips) - ulibc Makefile: use ?= for CC/AS/AR/LD so parent can override - doom Makefile: use ?= for CC/LD so parent can override - ulibc: remove duplicate stat/fstat declarations from unistd.h (conflicted with sys/stat.h when both included); proper declarations remain in sys/stat.h per POSIX --- diff --git a/Makefile b/Makefile index 60b72bb..02f123e 100644 --- a/Makefile +++ b/Makefile @@ -73,6 +73,11 @@ ifeq ($(ARCH),x86) ASM_SOURCES := $(wildcard $(SRC_DIR)/arch/x86/*.S) C_SOURCES += $(wildcard $(SRC_DIR)/arch/x86/*.c) + # Userspace cross-compiler (always i686-elf, even when kernel CC differs) + USER_CC ?= i686-elf-gcc + USER_LD ?= i686-elf-ld + USER_AR ?= i686-elf-ar + FULLTEST_ELF := user/fulltest.elf ECHO_ELF := user/echo.elf SH_ELF := user/sh.elf @@ -213,18 +218,18 @@ ULIBC_DIR := user/ulibc ULIBC_LIB := $(ULIBC_DIR)/libulibc.a $(ULIBC_LIB): - @$(MAKE) -C $(ULIBC_DIR) --no-print-directory + @$(MAKE) -C $(ULIBC_DIR) CC="$(USER_CC)" AS="$(USER_CC:gcc=as)" AR="$(USER_AR)" LD="$(USER_LD)" --no-print-directory $(ULIBC_SO): - @$(MAKE) -C $(ULIBC_DIR) libc.so --no-print-directory + @$(MAKE) -C $(ULIBC_DIR) CC="$(USER_CC)" AS="$(USER_CC:gcc=as)" AR="$(USER_AR)" LD="$(USER_LD)" libc.so --no-print-directory $(FULLTEST_ELF): user/fulltest.c user/linker.ld - @i686-elf-gcc -m32 -I include -ffreestanding -fno-pie -no-pie -nostdlib -Wl,-T,user/linker.ld -o $(FULLTEST_ELF) user/fulltest.c user/errno.c + @$(USER_CC) -m32 -I include -ffreestanding -fno-pie -no-pie -nostdlib -Wl,-T,user/linker.ld -o $(FULLTEST_ELF) user/fulltest.c user/errno.c # --- Dynamic linking helper: compile .c to PIC .o, link as PIE with crt0 + libc.so --- ULIBC_CRT0 := $(ULIBC_DIR)/src/crt0.o -DYN_CC := i686-elf-gcc -m32 -ffreestanding -nostdlib -O2 -Wall -Wextra -fPIC -fno-plt -I$(ULIBC_DIR)/include -DYN_LD := i686-elf-ld -m elf_i386 --dynamic-linker=/lib/ld.so -T user/dyn_linker.ld -L$(ULIBC_DIR) -rpath /lib +DYN_CC := $(USER_CC) -m32 -ffreestanding -nostdlib -O2 -Wall -Wextra -fPIC -fno-plt -I$(ULIBC_DIR)/include +DYN_LD := $(USER_LD) -m elf_i386 --dynamic-linker=/lib/ld.so -T user/dyn_linker.ld -L$(ULIBC_DIR) -rpath /lib $(ECHO_ELF): user/echo.c user/dyn_linker.ld $(ULIBC_SO) $(ULIBC_LIB) @$(DYN_CC) -c user/echo.c -o user/echo.o @@ -435,15 +440,15 @@ $(WHICH_ELF): user/which.c user/dyn_linker.ld $(ULIBC_SO) $(ULIBC_LIB) @$(DYN_LD) -o $@ $(ULIBC_CRT0) user/which.o -lc $(LDSO_ELF): user/ldso.c user/ldso_linker.ld - @i686-elf-gcc -m32 -ffreestanding -fno-pie -no-pie -nostdlib -Wl,-T,user/ldso_linker.ld -o $(LDSO_ELF) user/ldso.c + @$(USER_CC) -m32 -ffreestanding -fno-pie -no-pie -nostdlib -Wl,-T,user/ldso_linker.ld -o $(LDSO_ELF) user/ldso.c $(PIE_SO): user/pie_func.c - @i686-elf-gcc -m32 -fPIC -fno-plt -c user/pie_func.c -o user/pie_func.o - @i686-elf-ld -m elf_i386 -shared -soname libpietest.so -o $(PIE_SO) user/pie_func.o + @$(USER_CC) -m32 -fPIC -fno-plt -c user/pie_func.c -o user/pie_func.o + @$(USER_LD) -m elf_i386 -shared -soname libpietest.so -o $(PIE_SO) user/pie_func.o $(PIE_ELF): user/pie_main.c user/pie_linker.ld $(PIE_SO) - @i686-elf-gcc -m32 -fPIC -c user/pie_main.c -o user/pie_main.o - @i686-elf-ld -m elf_i386 -pie --dynamic-linker=/lib/ld.so -T user/pie_linker.ld -o $(PIE_ELF) user/pie_main.o $(PIE_SO) -rpath /lib + @$(USER_CC) -m32 -fPIC -c user/pie_main.c -o user/pie_main.o + @$(USER_LD) -m elf_i386 -pie --dynamic-linker=/lib/ld.so -T user/pie_linker.ld -o $(PIE_ELF) user/pie_main.o $(PIE_SO) -rpath /lib # All dynamically-linked user commands USER_CMDS := $(ECHO_ELF) $(SH_ELF) $(CAT_ELF) $(LS_ELF) $(MKDIR_ELF) $(RM_ELF) \ @@ -634,4 +639,4 @@ $(BUILD_DIR)/%.o: $(SRC_DIR)/%.S clean: rm -rf build $(KERNEL_NAME) -.PHONY: all clean iso run cppcheck sparse analyzer check test test-1cpu test-host test-gdb test-all scan-build mkinitrd-asan +.PHONY: all clean iso run run-arm run-riscv run-mips cppcheck sparse analyzer check test test-1cpu test-battery test-host test-gdb test-all scan-build mkinitrd-asan diff --git a/src/arch/x86/fpu.c b/src/arch/x86/fpu.c index 0d8ee55..a74b16f 100644 --- a/src/arch/x86/fpu.c +++ b/src/arch/x86/fpu.c @@ -3,7 +3,7 @@ #include "hal/cpu_features.h" #include -#include +#include "utils.h" /* CR0 bits */ #define CR0_EM (1U << 2) /* Emulate coprocessor (must be CLEAR for real FPU) */ diff --git a/src/kernel/fpu.c b/src/kernel/fpu.c index 0796b11..c673728 100644 --- a/src/kernel/fpu.c +++ b/src/kernel/fpu.c @@ -1,6 +1,6 @@ #include "arch_fpu.h" #include "console.h" -#include +#include "utils.h" __attribute__((weak)) void arch_fpu_init(void) { diff --git a/src/rump/rumpuser_adros.c b/src/rump/rumpuser_adros.c index f5122bc..aa22e9c 100644 --- a/src/rump/rumpuser_adros.c +++ b/src/rump/rumpuser_adros.c @@ -22,8 +22,7 @@ #include "process.h" #include "sync.h" #include "hal/cpu.h" - -#include +#include "utils.h" /* ------------------------------------------------------------------ */ /* Rump hypercall version */ diff --git a/user/doom/Makefile b/user/doom/Makefile index ab51cd2..ba56605 100644 --- a/user/doom/Makefile +++ b/user/doom/Makefile @@ -8,8 +8,8 @@ # # The output is doom.elf which gets packaged into the AdrOS initrd. -CC := i686-elf-gcc -LD := i686-elf-ld +CC ?= i686-elf-gcc +LD ?= i686-elf-ld ULIBC_DIR := ../ulibc ULIBC_INC := $(ULIBC_DIR)/include diff --git a/user/ulibc/Makefile b/user/ulibc/Makefile index a8203a3..844ea41 100644 --- a/user/ulibc/Makefile +++ b/user/ulibc/Makefile @@ -1,8 +1,8 @@ # ulibc — Minimal C library for AdrOS userspace -CC := i686-elf-gcc -AS := i686-elf-as -AR := i686-elf-ar -LD := i686-elf-ld +CC ?= i686-elf-gcc +AS ?= i686-elf-as +AR ?= i686-elf-ar +LD ?= i686-elf-ld CFLAGS := -m32 -ffreestanding -fno-pie -no-pie -nostdlib -O2 -Wall -Wextra -Iinclude CFLAGS_PIC := -m32 -ffreestanding -nostdlib -O2 -Wall -Wextra -Iinclude -fPIC -fno-plt diff --git a/user/ulibc/include/unistd.h b/user/ulibc/include/unistd.h index 41f4867..085b7be 100644 --- a/user/ulibc/include/unistd.h +++ b/user/ulibc/include/unistd.h @@ -59,8 +59,6 @@ void* brk(void* addr); int waitpid(int pid, int* status, int options); int getdents(int fd, void* buf, size_t count); -int stat(const char* path, void* buf); /* use sys/stat.h for typed version */ -int fstat(int fd, void* buf); /* use sys/stat.h for typed version */ int chmod(const char* path, int mode); int chown(const char* path, int owner, int group); int link(const char* oldpath, const char* newpath);