initial building script

* Try to check dependencies with pacman or apt
* Download and extract archives (supporting gzip and xz)
* Configure and build GCC and binutils (redirecting output to file,
  hoping that everything goes well)
* Clean up uneeded files after building
* Check for conflicting builds of sh-elf-gcc in the PATH
* Offer to update the profile to set the PATH at login
This commit is contained in:
Lephenixnoir 2020-12-30 23:03:15 +01:00
commit 6cbd8270ca
Signed by untrusted user: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
6 changed files with 268 additions and 0 deletions

8
.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Everything but the scripts
*
!fxsdk.make
!configure.sh
!build.sh
!install.sh
!uninstall.sh
!.gitignore

63
build.sh Executable file
View File

@ -0,0 +1,63 @@
#! /usr/bin/env bash
binutils_version=$1
gcc_version=$2
tag="<sh-elf-gcc>"
root=$(pwd)
run_with_guard() {
out="$1"
shift 1
"$@" >$out 2>&1
if [[ "$?" != 0 ]]; then
echo "$tag error: build failed, please check $(pwd)/$out o(x_x)o"
exit 1
fi
}
# Configure binutils
echo "$tag Configuring binutils..."
cd build-binutils
run_with_guard fxsdk-configure.log \
../binutils-$binutils_version/configure --prefix="$root" --target=sh3eb-elf --with-multilib-list=m3,m4-nofpu --program-prefix=sh-elf- --enable-libssp --enable-lto
# Build binutils
echo "$tag Compiling binutils (can take a couple of minutes)..."
run_with_guard fxsdk-build.log \
make -j$(nproc)
run_with_guard fxsdk-install.log \
make install
cd ..
# Configure GCC
echo "$tag Configuring gcc..."
cd build-gcc
run_with_guard fxsdk-configure.log \
../gcc-$gcc_version/configure --prefix="$root" --target=sh3eb-elf --with-multilib-list=m3,m4-nofpu --enable-languages=c,c++ --without-headers --with-newlib --program-prefix=sh-elf- --enable-libssp --enable-lto
# Build GCC
echo "$tag Compiling gcc (often takes 10-20 minutes)..."
run_with_guard fxsdk-build.log \
make -j$(nproc) all-gcc all-target-libgcc
run_with_guard fxsdk-install.log \
make install-gcc install-target-libgcc
cd ..
# Cleaning up everything
echo "$tag Cleaning up..."
rm -rf binutils-$binutils_version/ build-binutils/
rm -rf gcc-$gcc_version/ build-gcc

117
configure.sh Executable file
View File

@ -0,0 +1,117 @@
#! /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

18
fxsdk.make Normal file
View File

@ -0,0 +1,18 @@
# fxsdk: version=1
binutils_version=2.35.1
gcc_version=10.2.0
configure:
@ ./configure.sh $(binutils_version) $(gcc_version)
build:
@ ./build.sh $(binutils_version) $(gcc_version)
install:
@ ./install.sh $(CURDIR)
uninstall:
@ ./uninstall.sh $(CURDIR)
.PHONY: configure build install uninstall

55
install.sh Executable file
View File

@ -0,0 +1,55 @@
#! /usr/bin/env bash
path=$1/bin
tag="<sh-elf-gcc>"
# Check whether there is already such a binutils in the PATH
existing=$(which sh-elf-gcc)
rc=$?
if [[ $rc == 0 && "$existing" != "$path/sh-elf-gcc" ]]; then
echo "$tag WARNING: there seems to be another sh-elf-gcc in the PATH, be careful!" >&2
fi
if [[ $rc == 0 && "$existing" == "$path/sh-elf-gcc" ]]; then
echo "$tag This build is already in the PATH, there is nothing to do." >&2
exit 0
fi
# Try to find a suitable default
default="$HOME/.profile"
candidates="$HOME/.zprofile $HOME/.profile $HOME/.bash_profile $HOME/.zshrc $HOME/.bashrc"
for c in $candidates; do
if [[ -f $c ]]; then
default=$c
break
fi
done
# Suggest to add the path to binaries to the PATH at startup
cat <<EOF
===============================================================================
sh-elf-gcc has been compiled but is not in the PATH, so it can't be used yet.
I can add this line to your profile to update the PATH when you log in:
export PATH="\$PATH:$path"
* Press Enter to add this line to $default
* Or type "-" to skip this step
* Or type in another file name
EOF
read -p "> " startup_file
[[ -z "$startup_file" ]] && startup_file=$default
if [[ "$startup_file" == "-" ]]; then
echo "$tag Skipped setting the PATH."
else
echo "export PATH=\"\$PATH:$path\"" >> $startup_file
echo "$tag Set the PATH in $startup_file, this will take effect next login."
fi

7
uninstall.sh Executable file
View File

@ -0,0 +1,7 @@
#! /usr/bin/env bash
rm -rf bin/ include/ lib/ libexec/ sh3eb-elf/ share/
rm -rf binutils-*/ binutils-*.tar.* gcc-*/ gcc-*.tar.*
rm -rf build-binutils/ build-gcc/
echo "<sh-elf-gcc> You can now remove $1/bin from your PATH."