#!/bin/bash # Rumpk Build Script # Builds Zig L0 + Nim L1 into a single ELF set -e RUMPK_DIR="$(cd "$(dirname "$0")" && pwd)" BUILD_DIR="$RUMPK_DIR/build" echo "╔═══════════════════════════════════════╗" echo "║ RUMPK BUILD SYSTEM v0.1 ║" echo "╚═══════════════════════════════════════╝" echo "" # Create build directory mkdir -p "$BUILD_DIR" mkdir -p "$BUILD_DIR/nimcache" # ========================================================= # Step 1: Compile Zig L0 (HAL) # ========================================================= echo "[1/3] Compiling Zig L0 (HAL)..." zig build-obj \ "$RUMPK_DIR/hal/main.zig" \ -target aarch64-freestanding-none \ -O ReleaseSmall \ -femit-bin="$BUILD_DIR/hal.o" echo " → $BUILD_DIR/hal.o" # ========================================================= # Step 2: Compile Nim L1 (Kernel) # ========================================================= echo "[2/3] Compiling Nim L1 (Kernel)..." # Note: This requires careful Nim configuration for freestanding # For now, we'll try direct compilation with clang 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 \ --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/4] Compiling Nim C files..." # First compile cstubs.c echo " Compiling cstubs.c..." zig cc \ -target aarch64-freestanding-none \ -ffreestanding \ -fno-builtin \ -I"$RUMPK_DIR/core/include" \ -c "$RUMPK_DIR/core/cstubs.c" \ -o "$BUILD_DIR/cstubs.o" # Now compile 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 \ -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 # ========================================================= echo "[4/4] Linking..." # 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/cstubs.o" \ $NIM_OBJS \ -o "$BUILD_DIR/rumpk.elf" echo " → $BUILD_DIR/rumpk.elf" # ========================================================= # Done # ========================================================= echo "" echo "✅ Build complete!" echo "" echo "Run with:" echo " qemu-system-aarch64 -M virt -cpu cortex-a57 -nographic -kernel $BUILD_DIR/rumpk.elf"