rumpk/build.sh

137 lines
3.9 KiB
Bash
Executable File

#!/bin/bash
# Rumpk Build Script - Freestanding Doctrine
# Pure Zig L0 + Nim L1, no C stubs, no glibc
#
# We are the standard library now.
set -e
RUMPK_DIR="$(cd "$(dirname "$0")" && pwd)"
BUILD_DIR="$RUMPK_DIR/build"
echo "╔═══════════════════════════════════════╗"
echo "║ RUMPK FREESTANDING BUILD v0.2 ║"
echo "║ No glibc. Pure Sovereignty. ║"
echo "╚═══════════════════════════════════════╝"
echo ""
# Create build directory
mkdir -p "$BUILD_DIR"
mkdir -p "$BUILD_DIR/nimcache"
# =========================================================
# Step 1: Compile Zig L0 (HAL + Stubs + Context Switch)
# =========================================================
echo "[1/5] Compiling Zig L0 (HAL + libc stubs + context switch)..."
# Compile main.zig
zig build-obj \
"$RUMPK_DIR/hal/main.zig" \
-target aarch64-freestanding-none \
-O ReleaseSmall \
-femit-bin="$BUILD_DIR/hal.o"
# Compile stubs.zig (our libc replacement)
zig build-obj \
"$RUMPK_DIR/hal/stubs.zig" \
-target aarch64-freestanding-none \
-O ReleaseSmall \
-femit-bin="$BUILD_DIR/stubs.o"
# Compile switch.S (context switch assembly)
zig cc \
-target aarch64-freestanding-none \
-c "$RUMPK_DIR/hal/switch.S" \
-o "$BUILD_DIR/switch.o"
echo "$BUILD_DIR/hal.o"
echo "$BUILD_DIR/stubs.o"
echo "$BUILD_DIR/switch.o"
# =========================================================
# Step 2: Compile Nim L1 (Kernel + Fibers)
# =========================================================
echo "[2/5] Compiling Nim L1 (Kernel + Fibers)..."
nim c \
--cpu:arm64 \
--os:any \
--mm:arc \
--noMain:on \
--cc:clang \
--passC:"-target aarch64-unknown-none -ffreestanding -fno-stack-protector -fno-builtin" \
--passL:"-target aarch64-unknown-none -nostdlib -ffreestanding" \
--define:useMalloc \
--define:StandaloneHeapSize=65536 \
-d:release \
-d:danger \
--checks:off \
--assertions:off \
--boundChecks:off \
--overflowChecks:off \
--nimcache:"$BUILD_DIR/nimcache" \
--path:"$RUMPK_DIR/core" \
-c \
"$RUMPK_DIR/core/kernel.nim"
echo "$BUILD_DIR/nimcache/*.c"
# =========================================================
# Step 3: Compile Nim C files to objects
# =========================================================
echo "[3/5] Compiling Nim C files..."
for cfile in "$BUILD_DIR/nimcache"/*.c; do
ofile="${cfile%.c}.o"
zig cc \
-target aarch64-freestanding-none \
-ffreestanding \
-fno-stack-protector \
-fno-builtin \
-fno-sanitize=all \
-I"$RUMPK_DIR/core/include" \
-I/usr/lib/nim \
-I"$RUMPK_DIR/core" \
-c "$cfile" \
-o "$ofile" 2>&1 || true
done
echo "$BUILD_DIR/nimcache/*.o"
# =========================================================
# Step 4: Link Everything (NO LIBC!)
# =========================================================
echo "[4/5] Linking (freestanding, no libc)..."
# Collect all Nim object files
NIM_OBJS=$(find "$BUILD_DIR/nimcache" -name "*.o" 2>/dev/null | tr '\n' ' ')
if [ -z "$NIM_OBJS" ]; then
echo "ERROR: No Nim object files found!"
exit 1
fi
zig cc \
-target aarch64-freestanding-none \
-nostdlib \
-T "$RUMPK_DIR/boot/linker.ld" \
"$BUILD_DIR/hal.o" \
"$BUILD_DIR/stubs.o" \
"$BUILD_DIR/switch.o" \
$NIM_OBJS \
-o "$BUILD_DIR/rumpk.elf"
echo "$BUILD_DIR/rumpk.elf"
# =========================================================
# Done
# =========================================================
echo ""
echo "✅ Freestanding build complete!"
echo ""
echo "Verification:"
echo " file $BUILD_DIR/rumpk.elf"
echo ""
echo "Run with:"
echo " qemu-system-aarch64 -M virt -cpu cortex-a57 -nographic -kernel $BUILD_DIR/rumpk.elf"