From 6cbd8270ca267e954fcb965eea6dcfeb7e5c450e Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Wed, 30 Dec 2020 23:03:15 +0100 Subject: [PATCH] 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 --- .gitignore | 8 ++++ build.sh | 63 +++++++++++++++++++++++++++ configure.sh | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++ fxsdk.make | 18 ++++++++ install.sh | 55 ++++++++++++++++++++++++ uninstall.sh | 7 +++ 6 files changed, 268 insertions(+) create mode 100644 .gitignore create mode 100755 build.sh create mode 100755 configure.sh create mode 100644 fxsdk.make create mode 100755 install.sh create mode 100755 uninstall.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6f35c45 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +# Everything but the scripts +* +!fxsdk.make +!configure.sh +!build.sh +!install.sh +!uninstall.sh +!.gitignore diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..b1720a9 --- /dev/null +++ b/build.sh @@ -0,0 +1,63 @@ +#! /usr/bin/env bash + +binutils_version=$1 +gcc_version=$2 +tag="" +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 diff --git a/configure.sh b/configure.sh new file mode 100755 index 0000000..4136556 --- /dev/null +++ b/configure.sh @@ -0,0 +1,117 @@ +#! /usr/bin/env bash + +binutils_version=$1 +gcc_version=$2 +tag="" + +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 diff --git a/fxsdk.make b/fxsdk.make new file mode 100644 index 0000000..d21a004 --- /dev/null +++ b/fxsdk.make @@ -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 diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..eab04ef --- /dev/null +++ b/install.sh @@ -0,0 +1,55 @@ +#! /usr/bin/env bash + +path=$1/bin +tag="" + +# 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 < " 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 + diff --git a/uninstall.sh b/uninstall.sh new file mode 100755 index 0000000..90c6353 --- /dev/null +++ b/uninstall.sh @@ -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 " You can now remove $1/bin from your PATH."