Files
zosbuilder/scripts/compile-components.sh
Jan De Landtsheer 23180a1b0e Fix component build issues
- Add binary detection logic for RFS build to handle different binary names
- Remove non-existent 'vendored-openssl' feature from mycelium build
- Add debugging output to show actual build results
2025-08-16 20:23:25 +02:00

251 lines
8.0 KiB
Bash
Executable File

#!/bin/bash
set -e
ALPINE_ROOT="/build/initramfs"
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 for musl target..."
# Source cargo environment
. ~/.cargo/env
# Build with musl target for static linking
cargo build --release --target x86_64-unknown-linux-musl
# Debug: Check what was actually built
echo " Checking build results..."
ls -la target/x86_64-unknown-linux-musl/release/ | grep -v "\.d$" | grep -v "\.rlib$" || true
# Try to find the actual binary (might have different name)
local actual_binary=""
if [ -f "target/x86_64-unknown-linux-musl/release/$binary_name" ]; then
actual_binary="$binary_name"
else
# Look for executable files that might be our binary
actual_binary=$(find target/x86_64-unknown-linux-musl/release/ -maxdepth 1 -type f -executable ! -name "*.d" | head -1 | xargs basename 2>/dev/null || echo "")
fi
if [ -n "$actual_binary" ] && [ -f "target/x86_64-unknown-linux-musl/release/$actual_binary" ]; then
# Install binary to Alpine root
mkdir -p "$ALPINE_ROOT$install_path"
cp "target/x86_64-unknown-linux-musl/release/$actual_binary" "$ALPINE_ROOT$install_path/$binary_name"
chmod +x "$ALPINE_ROOT$install_path/$binary_name"
echo " Success: $install_path/$binary_name installed to Alpine root (musl static, actual binary: $actual_binary)"
else
echo " Error: No executable binary found in target/x86_64-unknown-linux-musl/release/"
return 1
fi
return 0
}
# 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
}
# Function to build mycelium (special case - builds from myceliumd subdirectory)
build_mycelium_component() {
local component_name="$1"
local binary_name="$2"
local install_path="$3"
echo " Building Mycelium component: $component_name"
if ! prepare_component "$component_name"; then
echo " Skipping $component_name (not available)"
return 1
fi
# Change to myceliumd subdirectory
if [ ! -d "myceliumd" ]; then
echo " Error: myceliumd subdirectory not found in $component_name"
return 1
fi
echo " Building from myceliumd subdirectory..."
cd myceliumd
# Source cargo environment
. ~/.cargo/env
# Build with musl target for static linking from myceliumd directory
# Note: Removed --features vendored-openssl as it doesn't exist in current version
cargo build --release --target x86_64-unknown-linux-musl
# Debug: Check what was actually built
echo " Checking build results..."
ls -la target/x86_64-unknown-linux-musl/release/ | grep -v "\.d$" | grep -v "\.rlib$" || true
# The binary is in target/x86_64-unknown-linux-musl/release/ (relative to myceliumd directory)
if [ ! -f "target/x86_64-unknown-linux-musl/release/$binary_name" ]; then
echo " Error: Binary not found at target/x86_64-unknown-linux-musl/release/$binary_name"
echo " Available files:"
ls -la target/x86_64-unknown-linux-musl/release/ || true
return 1
fi
# Install binary to Alpine root
mkdir -p "$ALPINE_ROOT$install_path"
cp "target/x86_64-unknown-linux-musl/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 (musl static)"
return 0
}
# 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 "Creating Alpine initramfs root at $ALPINE_ROOT"
mkdir -p "$ALPINE_ROOT"
fi
# Build each Zero-OS component from submodules
echo "Building Zero-OS components from submodules:"
# 1. Zinit - Init system (Rust) - master branch
if build_rust_component "zinit" "zinit" "/sbin"; then
echo " Zinit built successfully"
else
echo " Warning: Failed to build zinit"
fi
# 2. RFS - Rust filesystem (Rust) - v2.0.6 with musl
if build_rust_component "rfs" "rfs" "/usr/bin"; then
echo " RFS built successfully"
else
echo " Warning: Failed to build rfs"
fi
# 3. CoreX - Container control (static binary) - v2.1.4
echo " Installing CoreX static binary..."
if [ -f "$COMPONENTS_DIR/corex/corex" ]; then
mkdir -p "$ALPINE_ROOT/usr/bin"
cp "$COMPONENTS_DIR/corex/corex" "$ALPINE_ROOT/usr/bin/"
chmod +x "$ALPINE_ROOT/usr/bin/corex"
echo " CoreX installed successfully"
else
echo " Warning: CoreX binary not found"
fi
# 4. Mycelium - Networking layer (Rust) - 0.6.1 with musl (special build from myceliumd subdirectory)
if build_mycelium_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/corex" "/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"