Files
darkforge/toolchain/scripts/127-gcc.sh
2026-03-20 15:09:30 +01:00

142 lines
5.0 KiB
Bash
Executable File

#!/bin/bash
# ============================================================================
# DarkForge Linux — Phase 3, Chapter 8.30: GCC (FINAL NATIVE COMPILER)
# ============================================================================
# Purpose: Build the FINAL NATIVE GCC compiler for the target system.
# This is a CRITICAL step — it compiles for the local hardware
# (AMD Ryzen 9 9950X3D) with Zen 5 optimizations.
# CRITICAL ARCHITECTURE DECISIONS:
# • Enable C and C++ languages for system tools and applications
# • Enable Position-Independent Executables (PIE) by default (security)
# • Enable Stack Smashing Protection (SSP) by default (security)
# • Disable multilib for now (32-bit support handled separately later)
# • Disable bootstrap (faster, safer, reuses earlier GCC Pass 2)
# • Apply the lib64→lib sed fix for x86_64 unified /usr/lib
# • Fix library paths after install to avoid searching lib64
#
# Inputs: /sources/gcc-15.2.0.tar.xz
# mpfr-4.2.2.tar.xz, gmp-6.3.0.tar.xz, mpc-1.3.1.tar.gz
# binutils, glibc, linux headers already installed
# Outputs: gcc, g++, cc (symlink) in /usr/bin
# GCC runtime libraries in /usr/lib
# Ref: LFS 13.0 §8.30
# ============================================================================
set -euo pipefail
source /sources/toolchain-scripts/100-chroot-env.sh
PACKAGE="gcc"
VERSION="15.2.0"
echo "=========================================================================="
echo "=== CRITICAL: Building FINAL NATIVE GCC-${VERSION} (Phase 3)"
echo "=== This compiler will target: AMD Zen 5 (9950X3D)"
echo "=== Compiler flags in use: ${CFLAGS}"
echo "=========================================================================="
pkg_extract "${PACKAGE}-${VERSION}.tar.xz"
cd "${PACKAGE}-${VERSION}"
# Extract arithmetic library dependencies (gmp, mpfr, mpc) into gcc tree
echo ">>> Embedding GMP, MPFR, MPC into GCC source tree..."
tar -xf "${SRCDIR}/mpfr-4.2.2.tar.xz"
mv -v mpfr-4.2.2 mpfr
tar -xf "${SRCDIR}/gmp-6.3.0.tar.xz"
mv -v gmp-6.3.0 gmp
tar -xf "${SRCDIR}/mpc-1.3.1.tar.gz"
mv -v mpc-1.3.1 mpc
# FIX FOR X86_64: lib64 → lib redirection
# On x86_64, GCC defaults to installing 64-bit libraries in lib64.
# DarkForge uses a unified /usr/lib directory. This sed changes GCC's default.
# CRITICAL: This must be done before configure.
case $(uname -m) in
x86_64)
echo ">>> Applying x86_64 lib64→lib fix..."
sed -e '/m64=/s/lib64/lib/' -i.orig gcc/config/i386/t-linux64
;;
esac
# Create build directory
mkdir -v build
cd build
# CONFIGURE THE FINAL NATIVE GCC
# This is the most important configuration in the entire toolchain.
echo ">>> Configuring GCC with full security hardening..."
echo ">>> Using Zen 5 flags: ${CFLAGS}"
../configure \
--prefix=/usr \
--sysconfdir=/etc \
--localstatedir=/var \
--disable-nls \
--enable-languages=c,c++ \
--enable-default-pie \
--enable-default-ssp \
--disable-multilib \
--disable-bootstrap \
--with-system-zlib \
--enable-gnu-unique-object
# COMPILE GCC
echo ">>> Building GCC (this will take several minutes)..."
make
# INSTALL GCC
echo ">>> Installing GCC to /usr..."
make install
# CREATE cc SYMLINK FOR COMPATIBILITY
# Many build scripts expect /usr/bin/cc to exist
ln -sv gcc /usr/bin/cc
# POST-INSTALL FIXES FOR LIB PATHS
echo ">>> Verifying and fixing library paths..."
# Ensure /usr/lib is in the search path, not /usr/lib64
mkdir -pv /usr/lib/gcc/$(gcc -dumpmachine)/15.2.0
# Run a sanity check: compile and link a hello world program
echo ">>> Running GCC sanity check (compile hello world)..."
echo '#include <stdio.h>
int main() {
printf("GCC hello world sanity check\\n");
return 0;
}' > /tmp/hello.c
gcc /tmp/hello.c -o /tmp/hello
if [ ! -f /tmp/hello ]; then
echo "[ERROR] GCC sanity check failed: could not compile hello.c"
exit 1
fi
# Check the dynamic linker path (should NOT have lib64)
echo ">>> Checking dynamic linker path..."
LINKER=$(/tmp/hello 2>&1 | head -1 2>/dev/null || ldd /tmp/hello 2>/dev/null | grep "ld-" || echo "OK")
if echo "${LINKER}" | grep -q "lib64"; then
echo "[WARNING] Dynamic linker path contains lib64 — may need manual fix"
else
echo ">>> Dynamic linker path is correct (no lib64)"
fi
rm -f /tmp/hello /tmp/hello.c
# Final verification
echo ""
echo ">>> GCC Final Verification:"
echo " GCC version: $(gcc --version | head -1)"
echo " G++ version: $(g++ --version | head -1)"
echo " cc symlink: $(ls -l /usr/bin/cc)"
echo " Default PIE: $(gcc -Q --help=code-generation | grep DEFAULT | grep pie)"
echo " Default SSP: $(gcc -Q --help=code-generation | grep DEFAULT | grep ssp)"
echo ""
cd "${SRCDIR}"
pkg_cleanup "${PACKAGE}-${VERSION}"
echo "=========================================================================="
echo "=== FINAL NATIVE GCC-${VERSION} BUILD COMPLETE"
echo "=== System now has a native compiler targeting Zen 5"
echo "=========================================================================="