Files
darkforge/toolchain/scripts/023a-chroot-build-all.sh
2026-03-20 14:33:48 +01:00

137 lines
4.8 KiB
Bash

#!/bin/bash
# ============================================================================
# DarkForge Linux — Phase 0, Chapter 7: Run All Chroot Build Scripts
# ============================================================================
# Purpose: Enters the chroot and runs scripts 024-031 in sequence.
# This is a convenience wrapper — run it on the HOST as root,
# AFTER 023-chroot-setup.sh has mounted virtual filesystems.
# Usage: sudo bash /mnt/darkforge/sources/toolchain-scripts/023a-chroot-build-all.sh
# Inputs: LFS environment variable (default: /mnt/darkforge)
# Outputs: Completed chroot tools (gettext, bison, perl, python, texinfo, util-linux)
# Assumes: 023-chroot-setup.sh already ran, virtual filesystems mounted
# ============================================================================
set -euo pipefail
LFS="${LFS:-/mnt/darkforge}"
SCRIPTS="/sources/toolchain-scripts"
# Color output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
ok() { echo -e "${GREEN}>>> $*${NC}"; }
warn() { echo -e "${YELLOW}>>> $*${NC}"; }
fail() { echo -e "${RED}>>> $*${NC}"; exit 1; }
[ "$(id -u)" -eq 0 ] || fail "This script must be run as root."
# Verify chroot is ready (virtual filesystems mounted)
if ! mountpoint -q "${LFS}/proc" 2>/dev/null; then
fail "${LFS}/proc is not mounted. Run 023-chroot-setup.sh first."
fi
echo "============================================================"
echo " DarkForge Linux — Chroot Build Phase (Chapter 7)"
echo "============================================================"
echo ""
# --- Bootstrap libz.so.1 from host -------------------------------------------
# Chicken-and-egg problem: the linker (ld) from binutils pass 2 was built with
# zlib support and dynamically links against libz.so.1. But zlib isn't installed
# in the chroot yet. Without libz.so.1, ld can't run, so nothing can link, so
# we can't even build zlib properly. Fix: copy the host's libz into the chroot
# temporarily. The real zlib build (024a-zlib.sh) will replace it.
warn "Bootstrapping libz.so.1 from host into chroot..."
HOST_LIBZ=$(find /usr/lib /usr/lib64 /lib /lib64 -name 'libz.so.1*' -type f 2>/dev/null | head -1)
if [ -z "${HOST_LIBZ}" ]; then
# Try ldconfig cache
HOST_LIBZ=$(ldconfig -p 2>/dev/null | grep 'libz.so.1 ' | awk '{print $NF}' | head -1)
fi
if [ -n "${HOST_LIBZ}" ]; then
cp -Lv "${HOST_LIBZ}" "${LFS}/usr/lib/libz.so.1"
# Also create the .so symlink for the linker
ln -sfv libz.so.1 "${LFS}/usr/lib/libz.so"
ok "Copied host libz.so.1 → ${LFS}/usr/lib/libz.so.1"
ok "ld should now be able to link inside the chroot"
else
warn "Could not find host libz.so.1 — zlib build in chroot may fail"
warn "Install zlib on the host: sudo pacman -S zlib"
fi
echo ""
# Build a script that runs inside the chroot
# We write it to a temp file inside $LFS so the chroot can see it
cat > "${LFS}/tmp/chroot-build-runner.sh" << 'CHROOT_EOF'
#!/bin/bash
set -euo pipefail
SCRIPTS="/sources/toolchain-scripts"
LOG_DIR="/sources/logs"
mkdir -p "${LOG_DIR}"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
run_script() {
local script="$1"
local name
name=$(basename "${script}" .sh)
local logfile="${LOG_DIR}/${name}.log"
echo -e "${YELLOW}>>> [$(date '+%H:%M:%S')] Starting: ${name}${NC}"
if bash "${script}" 2>&1 | tee "${logfile}"; then
echo -e "${GREEN}>>> [$(date '+%H:%M:%S')] PASSED: ${name}${NC}"
echo ""
else
echo -e "${RED}>>> [$(date '+%H:%M:%S')] FAILED: ${name}${NC}"
echo "Log file: ${logfile}"
exit 1
fi
}
echo "=== Chapter 7: Chroot Build Scripts ==="
echo ""
run_script "${SCRIPTS}/024-chroot-essentials.sh"
run_script "${SCRIPTS}/024a-zlib.sh"
run_script "${SCRIPTS}/025-gettext.sh"
run_script "${SCRIPTS}/026-bison.sh"
run_script "${SCRIPTS}/027-perl.sh"
run_script "${SCRIPTS}/028-python.sh"
run_script "${SCRIPTS}/029-texinfo.sh"
run_script "${SCRIPTS}/030-util-linux.sh"
run_script "${SCRIPTS}/031-cleanup.sh"
echo ""
echo "============================================================"
echo " Chapter 7 chroot builds complete!"
echo "============================================================"
CHROOT_EOF
chmod +x "${LFS}/tmp/chroot-build-runner.sh"
# Enter chroot and run the build script
# PATH includes /tools/bin where the cross-compiled gcc lives
chroot "${LFS}" /usr/bin/env -i \
HOME=/root \
TERM="${TERM}" \
PS1='(darkforge chroot) \u:\w\$ ' \
PATH=/usr/bin:/usr/sbin:/tools/bin \
MAKEFLAGS="-j32" \
/bin/bash /tmp/chroot-build-runner.sh
# Clean up
rm -f "${LFS}/tmp/chroot-build-runner.sh"
echo ""
echo -e "${GREEN}Phase 0 is FULLY COMPLETE.${NC}"
echo ""
echo "The toolchain chroot environment is ready."
echo "Next: Phase 1 (dpack) or Phase 3 (base system packages)."