#!/bin/sh set -e ALPINE_ROOT="/build/alpine" COMPONENTS_DIR="/build/components" CACHE_DIR="/build/cache/source" echo "[+] Compiling Zero-OS components from git submodules..." # Create build directories mkdir -p "$CACHE_DIR" # Check if submodules are available if [ ! -d "$COMPONENTS_DIR" ]; then echo "Error: Components directory not found. Run setup-submodules.sh first." exit 1 fi # Function to prepare component for building prepare_component() { local component_name="$1" local component_dir="$COMPONENTS_DIR/$component_name" if [ ! -d "$component_dir" ]; then echo " Warning: $component_name submodule not found at $component_dir" return 1 fi echo " Preparing $component_name..." cd "$component_dir" # Update submodule if needed if [ -d ".git" ]; then git pull origin main 2>/dev/null || git pull origin master 2>/dev/null || echo " Using existing checkout" fi return 0 } # Function to build Rust project from submodule build_rust_component() { local component_name="$1" local binary_name="$2" local install_path="$3" echo " Building Rust component: $component_name" if ! prepare_component "$component_name"; then echo " Skipping $component_name (not available)" return 1 fi # Build the project echo " Compiling with cargo..." if command -v cargo >/dev/null; then cargo build --release # Install binary to Alpine root mkdir -p "$ALPINE_ROOT$install_path" cp "target/release/$binary_name" "$ALPINE_ROOT$install_path/" chmod +x "$ALPINE_ROOT$install_path/$binary_name" echo " Success: $install_path/$binary_name installed to Alpine root" return 0 else echo " Error: cargo not found - installing Rust..." apk add --no-cache rust cargo cargo build --release mkdir -p "$ALPINE_ROOT$install_path" cp "target/release/$binary_name" "$ALPINE_ROOT$install_path/" chmod +x "$ALPINE_ROOT$install_path/$binary_name" echo " Success: $install_path/$binary_name installed to Alpine root" return 0 fi } # Function to build Go project from submodule build_go_component() { local component_name="$1" local binary_name="$2" local install_path="$3" local build_cmd="$4" echo " Building Go component: $component_name" if ! prepare_component "$component_name"; then echo " Skipping $component_name (not available)" return 1 fi # Build the project echo " Compiling with go..." if command -v go >/dev/null; then if [ -n "$build_cmd" ]; then eval "$build_cmd" else go build -o "$binary_name" fi # Install binary to Alpine root mkdir -p "$ALPINE_ROOT$install_path" cp "$binary_name" "$ALPINE_ROOT$install_path/" chmod +x "$ALPINE_ROOT$install_path/$binary_name" echo " Success: $install_path/$binary_name installed to Alpine root" return 0 else echo " Error: go not found - installing Go..." apk add --no-cache go if [ -n "$build_cmd" ]; then eval "$build_cmd" else go build -o "$binary_name" fi mkdir -p "$ALPINE_ROOT$install_path" cp "$binary_name" "$ALPINE_ROOT$install_path/" chmod +x "$ALPINE_ROOT$install_path/$binary_name" echo " Success: $install_path/$binary_name installed to Alpine root" return 0 fi } # Install build dependencies if needed echo " Checking build dependencies..." if ! command -v git >/dev/null; then echo " Installing git..." apk add --no-cache git fi echo "" # Ensure Alpine root directory exists if [ ! -d "$ALPINE_ROOT" ]; then echo "Error: Alpine root not found at $ALPINE_ROOT" echo "Make sure Alpine system is built first" exit 1 fi # Build each Zero-OS component from submodules echo "Building Zero-OS components from submodules:" # 1. Zinit - Init system (Go) if build_go_component "zinit" "zinit" "/sbin" "go build -o zinit"; then echo " Zinit built successfully" else echo " Warning: Failed to build zinit" fi # 2. RFS - Rust filesystem (Rust) if build_rust_component "rfs" "rfs" "/usr/bin"; then echo " RFS built successfully" else echo " Warning: Failed to build rfs" fi # 3. Seektime - Disk detection (Go) if build_go_component "seektime" "seektime" "/usr/bin" "go build -o seektime"; then echo " Seektime built successfully" else echo " Warning: Failed to build seektime" fi # 4. Core-X - Container control (Go) if build_go_component "core-x" "core-x" "/usr/bin" "go build -o core-x"; then echo " Core-X built successfully" else echo " Warning: Failed to build core-x" fi # 5. Mycelium - Networking layer (Rust) if build_rust_component "mycelium" "mycelium" "/usr/bin"; then echo " Mycelium built successfully" else echo " Warning: Failed to build mycelium" fi echo "" echo "[+] Zero-OS component compilation completed" # Show installed binaries info echo "" echo "[+] Compiled Zero-OS components in Alpine root:" for binary in "/sbin/zinit" "/usr/bin/rfs" "/usr/bin/seektime" "/usr/bin/core-x" "/usr/bin/mycelium"; do if [ -x "$ALPINE_ROOT$binary" ]; then size=$(stat -c%s "$ALPINE_ROOT$binary" 2>/dev/null || echo "unknown") echo " $binary (${size} bytes)" else echo " $binary (NOT BUILT)" fi done echo "[+] Zero-OS binaries installed to Alpine root filesystem"