#! /usr/bin/env bash source util.sh VERSION=$1 PREFIX="$2" URL="https://ftp.gnu.org/gnu/gcc/gcc-$VERSION/gcc-$VERSION.tar.xz" ARCHIVE=$(basename "$URL") # Final location of gcc in the sysroot SYSROOT_GCC="$SYSROOT/bin/sh-elf-gcc" # Version string of any existing sh-elf-gcc in the sysroot SYSROOT_GCC_VERSION= # "1" if libstdc++-v3 is already in the install folder SYSROOT_HAS_LIBSTDCXX= # "1" if the conditions for building libstdc++-v3 are met CAN_BUILD_LIBSTDCXX= #--- # Determine what parts of GCC are already installed and decide whether to build #--- if [[ -f "$SYSROOT_GCC" ]]; then SYSROOT_GCC_VERSION=$("$SYSROOT_GCC" --version | head -n 1 | grep -Eo '[0-9.]+$') # TODO: Once we start using the full C++ library, check for libstdc++.a instead LIBSTDCXX_FILE=$("$SYSROOT_GCC" -print-file-name=libsupc++.a) LIBC_FILE=$("$SYSROOT_GCC" -print-file-name=libc.a) if [[ "$LIBSTDCXX_FILE" != "libsupc++.a" ]]; then SYSROOT_HAS_LIBSTDCXX=1 echo "$TAG libsupc++.a found, libstdc++-v3 is already built" fi if [[ "$LIBC_FILE" != "libc.a" ]]; then CAN_BUILD_LIBSTDCXX=1 [[ "$SYSROOT_HAS_LIBSTDCXX" != "1" ]] && \ echo "$TAG libc.a found, we can build libstdc++-v3" else echo "$TAG libc.a not found, we cannot build libstdc++-v3" fi else echo "$TAG After installing the libc, rebuild here for libstdc++-v3" fi if [[ "$SYSROOT_GCC_VERSION" = "$VERSION" ]]; then if [[ "$SYSROOT_HAS_LIBSTDCXX" = "1" ]]; then echo "$TAG GCC $VERSION with libstdc++-v3 already there; skipping rebuild" mkdir -p build touch build/giteapc-skip-rebuild.txt exit 0 elif [[ "$CAN_BUILD_LIBSTDCXX" != "1" ]]; then echo "$TAG Cannot build libstdc++-v3 yet; skipping rebuild until next time" mkdir -p build touch build/giteapc-skip-rebuild.txt exit 0 else echo "$TAG We will proceed with the second stage and build libstdc++-v3" fi fi if [[ ! -z "$ACCEPT_ANY" && ! -z "$SYSROOT_GCC_VERSION" ]]; then echo "$TAG GCC $VERSION already available; skipping rebuild as per ACCEPT_ANY" mkdir -p build touch build/giteapc-skip-rebuild.txt exit 0 fi [[ -e "build/giteapc-skip-rebuild.txt" ]] && rm build/giteapc-skip-rebuild.txt #--- # Get sources #--- if [[ -f "$ARCHIVE" ]]; then echo "$TAG Found $ARCHIVE, skipping download" else echo "$TAG Downloading $URL..." if command -v curl >/dev/null 2>&1; then curl $URL -o $ARCHIVE elif command -v wget >/dev/null 2>&1; then wget -q --show-progress $URL -O $ARCHIVE else echo "$TAG error: no curl or wget; install one or download archive yourself" >&2 exit 1 fi fi # Extract archive (OpenBSD-compliant version) if [[ -d "${ARCHIVE%.tar.*}" ]]; then echo "$TAG Found ${ARCHIVE%.tar.*}, skipping extraction" else echo "$TAG Extracting $ARCHIVE..." unxz -c < $ARCHIVE | tar -xf - if [[ "$VERSION" = "11.1.0" ]]; then echo "$TAG Applying patches/gcc-11.1.0-libstdc++-v3-skip-dlopen.patch..." patch -u -N -p0 < patches/gcc-11.1.0-libstdc++-v3-skip-dlopen.patch fi fi # Download prerequisites cd gcc-$VERSION ./contrib/download_prerequisites cd .. #--- # Configure #--- # We install in the sysroot and then symlink to the PREFIX folder in the path. # Symlink as, ld, ar and ranlib, which gcc will not find by itself (we renamed # them from sh3eb-elf-* to sh-elf-* with --program-prefix). mkdir -p "$SYSROOT/sh3eb-elf/bin" for TOOL in as ld ar ranlib; do if ! command -v sh-elf-$TOOL >/dev/null 2>&1; then echo "error: sh-elf-$TOOL not found in PATH!" >&2 exit 1 fi ln -sf $(command -v sh-elf-$TOOL) "$SYSROOT/sh3eb-elf/bin/$TOOL" done # OpenBSD apparently installs these in /usr/local if [[ $(uname) == "OpenBSD" ]]; then EXTRA_ARGS="--with-gmp=/usr/local --with-mpfr=/usr/local --with-mpc=/usr/local" else EXTRA_ARGS= fi # In libstdc++-v3 mode, don't reconfigure to avoid recompiling if [[ "$SYSROOT_GCC_VERSION" = "$VERSION" && "$SYSROOT_HAS_LIBSTDCXX" != "1" && "$CAN_BUILD_LIBSTDCXX" = "1" ]]; then touch build/giteapc-build-libstdcxx.txt elif [[ "$SYSROOT_GCC_VERSION" != "$VERSION" ]]; then echo "$TAG Configuring gcc..." [[ -d build ]] && rm -rf build mkdir build cd build run_quietly giteapc-configure.log \ ../gcc-$VERSION/configure \ --prefix="$SYSROOT" \ --target="sh3eb-elf" \ --with-multilib-list="m3,m4-nofpu" \ --enable-languages="c,c++" \ --without-headers \ --program-prefix="sh-elf-" \ --enable-libssp \ --enable-lto \ --enable-clocale="generic" \ --enable-libstdcxx-allocator \ --disable-threads \ --disable-libstdcxx-verbose \ --enable-cxx-flags="-fno-exceptions" \ $EXTRA_ARGS cd .. else echo "$TAG GCC already built, can't build libstdc++-v3; skipping!" touch build/giteapc-skip-rebuild.txt exit 0 fi