Files
zosbuilder/scripts/build-smart.sh

106 lines
3.1 KiB
Bash
Executable File

#!/bin/sh
set -e
CACHE_DIR="/build/cache"
KERNEL_VERSION=$(cat /build/configs/kernel-version)
echo "[+] Smart build with Docker layer caching..."
# Function to check if we need to rebuild a stage
needs_rebuild() {
local stage="$1"
local trigger_files="$2"
local cache_marker="$CACHE_DIR/${stage}-marker"
# If cache marker doesn't exist, rebuild
if [ ! -f "$cache_marker" ]; then
echo " $stage: No cache marker found, rebuild needed"
return 0
fi
# Check if any trigger files are newer than cache marker
for file in $trigger_files; do
if [ -f "$file" ] && [ "$file" -nt "$cache_marker" ]; then
echo " $stage: $file changed, rebuild needed"
return 0
fi
done
echo " $stage: Cache valid, skipping"
return 1
}
# Function to mark stage as completed
mark_completed() {
local stage="$1"
mkdir -p "$CACHE_DIR"
touch "$CACHE_DIR/${stage}-marker"
}
echo " Checking cache status..."
# Check each build stage
REBUILD_COMPONENTS=false
REBUILD_KERNEL=false
REBUILD_INITRAMFS=false
# Components need rebuild if:
# - Component source repos have updates (we'll assume weekly refresh)
# - compile-components.sh script changed
if needs_rebuild "components" "/build/scripts/compile-components.sh"; then
REBUILD_COMPONENTS=true
fi
# Kernel needs rebuild if:
# - Kernel version changed
# - Kernel config changed
# - build-kernel.sh script changed
if needs_rebuild "kernel" "/build/configs/kernel-version /build/configs/kernel-config-generic /build/scripts/build-kernel.sh"; then
REBUILD_KERNEL=true
fi
# Initramfs always rebuilds (it's fast and contains user scripts)
REBUILD_INITRAMFS=true
echo ""
echo " Build plan:"
echo " Components: $([ "$REBUILD_COMPONENTS" = "true" ] && echo "REBUILD" || echo "CACHED")"
echo " Kernel: $([ "$REBUILD_KERNEL" = "true" ] && echo "REBUILD" || echo "CACHED")"
echo " Initramfs: REBUILD (always)"
echo ""
# Phase 1: Components (if needed)
if [ "$REBUILD_COMPONENTS" = "true" ]; then
echo "[+] Building Zero-OS components from source..."
/build/scripts/compile-components.sh
mark_completed "components"
else
echo "[+] Using cached Zero-OS components"
fi
# Phase 2: Kernel (if needed)
if [ "$REBUILD_KERNEL" = "true" ]; then
echo "[+] Building kernel ${KERNEL_VERSION}..."
# Only download and build kernel, not embed initramfs yet
/build/scripts/build-kernel.sh --kernel-only
mark_completed "kernel"
else
echo "[+] Using cached kernel ${KERNEL_VERSION}"
fi
# Phase 3: Initramfs (always - fast)
echo "[+] Installing Alpine packages..."
/build/scripts/install-packages.sh
echo "[+] Setting up initramfs structure..."
/build/scripts/setup-initramfs.sh
# Phase 4: Final kernel assembly (embed initramfs)
echo "[+] Embedding initramfs into kernel..."
/build/scripts/build-kernel.sh --embed-only
echo ""
echo "[+] Smart build completed!"
echo " Components: $([ "$REBUILD_COMPONENTS" = "true" ] && echo "rebuilt" || echo "cached")"
echo " Kernel: $([ "$REBUILD_KERNEL" = "true" ] && echo "rebuilt" || echo "cached")"
echo " Total time saved by caching: significant!"