#! /usr/bin/env bash source util.sh VERSION=$1 PREFIX="$2" URL="https://ftp.gnu.org/gnu/binutils/binutils-$VERSION.tar.xz" ARCHIVE=$(basename "$URL") # Final location of ld in the sysroot SYSROOT_LD="$SYSROOT/bin/sh-elf-ld" # Version string of any existing sh-elf-ld in the sysroot SYSROOT_LD_VERSION= #--- # Determine what versions are already here and decide whether to skip #--- if [[ -f "$SYSROOT_LD" ]]; then SYSROOT_LD_VERSION=$("$SYSROOT_LD" --version | head -n 1 | grep -Eo '[0-9.]+$') fi if [[ "$SYSROOT_LD_VERSION" = "$VERSION" ]]; then echo "$TAG binutils $VERSION already installed, skipping rebuild" [[ -d build ]] && touch "build/giteapc-skip-rebuild.txt" exit 0 fi if [[ ! -z "$ACCEPT_ANY" && ! -z "$SYSROOT_LD_VERSION" ]]; then echo "$TAG binutils $SYSROOT_LD_VERSION is available; skipping as per ACCEPT_ANY" [[ -d build ]] && touch "build/giteapc-skip-rebuild.txt" exit 0 fi #--- # Check dependencies for building binutils and GCC, and offer to install them #--- if command -v pkg >/dev/null 2>&1; then deps="libmpfr libmpc libgmp libpng flex clang git texinfo libisl bison xz-utils" pm=pkg pm_has="dpkg -s" pm_install="pkg install" elif command -v apt >/dev/null 2>&1; then deps="libmpfr-dev libmpc-dev libgmp-dev libpng-dev libppl-dev flex g++ git texinfo xz-utils" pm=apt pm_has="dpkg -s" pm_install="sudo apt install" elif command -v pacman >/dev/null 2>&1; then deps="mpfr libmpc gmp libpng ppl flex gcc git texinfo xz" pm=pacman pm_has="pacman -Qi" pm_install="sudo pacman -S" else trust_deps=1 fi missing="" if [[ -z "$trust_deps" ]]; then for d in $deps; do if ! $pm_has $d >/dev/null 2>&1; then missing="$missing $d" fi done fi if [[ ! -z "$missing" ]]; then echo "$TAG Based on $pm, some dependencies are missing: $missing" echo -n "$TAG Do you want to run '$pm_install $missing' to install them (Y/n)? " read do_install if [[ "$do_install" = "y" || "$do_install" = "Y" || "$do_install" = "" ]]; then $pm_install $missing else echo "$TAG Skipping dependencies, hoping it will build anyway." fi fi #--- # 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) echo "$TAG Extracting $ARCHIVE..." unxz -c < "$ARCHIVE" | tar -xf - # Touch intl/plural.c to avoid regenerating it from intl/plural.y with recent # versions of bison, which is subject to the following known bug. # * https://sourceware.org/bugzilla/show_bug.cgi?id=22941 # * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92008 touch "binutils-$VERSION/intl/plural.c" #--- # Configure #--- # We install in the sysroot and then symlink to the PREFIX folder in the path [[ -d "build" ]] && rm -rf build mkdir build && cd build echo "$TAG Configuring binutils..." if command -v termux-setup-storage >/dev/null 2>&1; then # Since the __ANDROID_API__ flag is hardcoded as 24 in clang, and # doesn't prototype some functions when this flag is too low, fixes it's # version by checking system's properties so as to prevent from missing prototypes # of existing functions such as fgets_unlocked (only if API >= 28) # See the following issues : # * https://github.com/termux/termux-packages/issues/6176 # * https://github.com/termux/termux-packages/issues/2469 export CFLAGS="-D__ANDROID_API__=$(getprop ro.build.version.sdk) -g -O2" \ CXXFLAGS="-D__ANDROID_API__=$(getprop ro.build.version.sdk) -g -O2" fi run_quietly giteapc-configure.log \ ../binutils-$VERSION/configure \ --prefix="$SYSROOT" \ --target="sh3eb-elf" \ --with-multilib-list="m3,m4-nofpu" \ --program-prefix="sh-elf-" \ --enable-libssp \ --enable-lto