sh-elf-gcc/configure.sh

118 lines
2.4 KiB
Bash
Executable File

#! /usr/bin/env bash
binutils_version=$1
gcc_version=$2
tag="<sh-elf-gcc>"
has() {
which $1 >/dev/null 2>&1
}
pacman_has() {
pacman -Qi $1 >/dev/null 2>&1
}
apt_has() {
apt show $1 >/dev/null 2>&1
}
download() {
url=$1
out=$2
if [ -f "$out" ]; then
echo "$tag Found $out in current directory, skipping download"
return
fi
echo "$tag Downloading $url..."
if has curl; then
curl $url -o $out
elif has wget; then
wget -q --show-progress $url -O $out
else
echo "$tag error: no curl or wget; install one or download archives yourself" >&2
return 1
fi
}
# Check dependencies
if has pacman; then
deps="mpfr mpc gmp libpng ppl flex gcc git texinfo"
pm=pacman
pm_has=pacman_has
install_cmd="sudo pacman -S"
elif has apt; then
deps="libmpfr-dev libmpc-dev libgmp-dev libpng-dev libppl-dev flex g++ git texinfo"
pm=apt
pm_has=apt_has
install_cmd="sudo apt install"
else
trust_deps=1
fi
missing=""
if [[ -z "$trust_deps" ]]; then
for d in $deps; do
if ! $pm_has $d; then
missing="$missing $d"
fi
done
fi
# Offer to install dependencies
if [[ ! -z "$missing" ]]; then
echo "$tag Based on $pm, some dependencies are missing: $missing"
echo -n "$tag Do you want to run '$missing_cmd' to install them (Y/n)? "
read do_install
if [[ "$do_install" == "y" || "$do_install" == "Y" || "$do_install" == "" ]]; then
$missing_cmd
else
echo "$tag Skipping dependencies, hoping it will build anyway."
fi
fi
# Determine which compression to use (either gzip or xzip)
if has xz; then
format="tar.xz"
decompress="-J"
elif has gzip; then
format="tar.gz"
decompress="-z"
else
echo "error: gcc archives need gzip or xzip to decompress!" >&2
exit 1
fi
# Deduce source URL and file names
binutils_url="https://ftp.gnu.org/gnu/binutils/binutils-$binutils_version.$format"
binutils_archive=$(basename $binutils_url)
gcc_url="https://ftp.gnu.org/gnu/gcc/gcc-$gcc_version/gcc-$gcc_version.$format"
gcc_archive="$(basename $gcc_url)"
# Download archives
download $binutils_url $binutils_archive
download $gcc_url $gcc_archive
# Extract archive
echo "$tag Extracting $binutils_archive..."
tar -x $decompress -f $binutils_archive
echo "$tag Extracting $gcc_archive..."
tar -x $decompress -f $gcc_archive
# Create empty build folders
[[ -d "build-binutils" ]] && rm -rf build-binutils
mkdir build-binutils
[[ -d "build-gcc" ]] && rm -rf build-gcc
mkdir build-gcc