From 92ec440af86d29c3ba6953a3df384b710b3a204d Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Fri, 19 Aug 2022 15:03:06 +0200 Subject: [PATCH] new build system for fxSDK sysroot * Configure and install so stuff ends up in the fxSDK sysroot * Don't clean by default, make it a configuration called :clean * :any now only skips build if the compiler is in the sysroot * Redo README with proper tutorial Details: * Proper .gitignore --- .gitignore | 15 ++----- README.md | 68 +++++++++++++++++++++++++----- build.sh | 10 +---- configure.sh | 87 ++++++++++++++++++++------------------- giteapc-config-clean.make | 4 ++ giteapc.make | 1 + install.sh | 26 +++++------- uninstall.sh | 15 +++---- util.sh | 16 +++++++ 9 files changed, 149 insertions(+), 93 deletions(-) create mode 100644 giteapc-config-clean.make diff --git a/.gitignore b/.gitignore index 792f670..6900ba2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,4 @@ -# Everything but the scripts -* -!giteapc.make -!giteapc-config-any.make -!configure.sh -!build.sh -!install.sh -!uninstall.sh -!util.sh -!.gitignore -!README.md +/binutils-*/ +/binutils-*.tar.* +/build*/ +/giteapc-config.make diff --git a/README.md b/README.md index 9599551..b4e0eeb 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,67 @@ -# Automatic `sh-elf-binutils` installer +# SuperH toolchain: `sh-elf-binutils` -This script can be used to automatically compile and install an SH3/SH4 binutils suite. The normal use is with GiteaPC: +This repository provides a guide and automated scripts to install an SH3/SH4-compatible [binutils](https://www.gnu.org/software/binutils/) suite. binutils is a collection of tools to assemble, edit and link binary programs, mainly object files. It is the main dependency for GCC, which compiles C down to assembler and relies on binutils to assemble and link this assembler output. -``` +The following three methods all install binutils with different degrees of automation. + +## Method 1: Using GiteaPC + +The most common way to install binutils (for the fxSDK) is by using [GiteaPC](https://gitea.planet-casio.com/Lephenixnoir/GiteaPC): + +```bash % giteapc install Lephenixnoir/sh-elf-binutils ``` -You can also install binutils manually by running the GiteaPC Makefile with a manually-specified install prefix: +This will take care of all the steps and install binutils in the fxSDK's SuperH system root. -``` -% make -f giteapc.make configure build install PREFIX=$HOME/.local -``` +A `:any` configuration is provided in case you already have another version of binutils installed in the fxSDK sysroot and want to keep using it (ie. skip a version upgrade). This will mark this repository as installed, so other repositories depending on it can build, without actually compiling binutils. Having binutils installed outside the fxSDK sysroot is not sufficient for some libraries. -An `any` configuration is provided in case you already have binutils installed somewhere and want to keep using it. This will turn this repository into a no-op while still satisfying requirements for other repositories. You can do this as long as you have `sh-elf-as` in your PATH: - -``` +```bash % giteapc install Lephenixnoir/sh-elf-binutils:any ``` + +A `:clean` configuration is also provided if you want to clean up the source and build files automatically after installing. This frees up some disk space. + +```bash +% giteapc install Lephenixnoir/sh-elf-binutils:clean +``` + +## Method 2: Manually running the scripts + +You can also install binutils by directly running this repository's scripts. In terms of dependencies, you will need: + +* [fxSDK](https://gitea.planet-casio.com/Lephenixnoir/fxsdk) ≥ 2.9 (provides the sysroot) +* Most of the [requirements for GCC](https://gcc.gnu.org/install/prerequisites.html) are checked by `configure.sh` + +You can run `fxsdk path sysroot` to show the SuperH sysroot where binutils will be installed. You should also specify a PATH-reachable `PREFIX` where the binaries of the toolchain will be symlinked. (The `PREFIX` is *only* for symlinks to executables, the toolchain will always be installed in the fxSDK's sysroot.) Once you're ready, run the main scripts with `make`: + +```bash +% make -f giteapc.make configure build install PREFIX="$HOME/.local" +``` + +## Method 3: Fully manually + +Get your version of choice from [`ftp.gnu.org`](https://ftp.gnu.org/gnu/binutils/) and extract the archive. Touch `binutils-$VERSION/intl/plural.c` to avoid autotools rebuilding it with bison, which no longer works ([[1]](https://sourceware.org/bugzilla/show_bug.cgi?id=22941),[[2]](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92008)). + +If you want to use the fxSDK, you must install the SDK's SuperH sysroot and set the `PREFIX` to `$(fxsdk path sysroot)`. Otherwise, anything that properly isolates the cross-compiler from the host is fine. + +Create and move into a build folder, then configure: + +```bash +% ../binutils-$VERSION/configure \ + --prefix="$PREFIX" \ + --target="sh3eb-elf" \ + --with-multilib-list="m3,m4-nofpu" \ + --program-prefix="sh-elf-" \ + --enable-libssp \ + --enable-lto +``` + +Finally, build and install: + +```bash +% make -j$(nproc) +% make install-strip +``` + +You should add the SuperH sysroot's `bin` to your PATH, or symlink them somewhere else in the PATH. You can then remove the build folder, source folder, and source archive if you need to free up disk space. diff --git a/build.sh b/build.sh index 28b3efd..b755cf5 100755 --- a/build.sh +++ b/build.sh @@ -1,7 +1,7 @@ #! /usr/bin/env bash # Avoid rebuilds of the same version -[[ ! -d build ]] && exit 0 +[[ -e "build/giteapc-skip-rebuild.txt" ]] && exit 0 source util.sh cd build @@ -15,11 +15,5 @@ fi echo "$TAG Compiling binutils (usually 5-10 minutes)..." -if command -v gmake >/dev/null 2>&1; then - make_command=gmake -else - make_command=make -fi - run_quietly giteapc-build.log \ -$make_command -j"$cores" +$MAKE_COMMAND -j"$cores" diff --git a/configure.sh b/configure.sh index d604e75..4025992 100755 --- a/configure.sh +++ b/configure.sh @@ -7,34 +7,35 @@ PREFIX="$2" URL="https://ftp.gnu.org/gnu/binutils/binutils-$VERSION.tar.xz" ARCHIVE=$(basename "$URL") -# Avoid rebuilds of the same version +# 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= -existing_as="$PREFIX/bin/sh-elf-as" +#--- +# Determine what versions are already here and decide whether to skip +#--- -if [[ -f "$existing_as" ]]; then - existing_version=$($existing_as --version | head -n 1 | grep -Eo '[0-9.]+$') - if [[ $existing_version == $VERSION ]]; then - echo "$TAG Version $VERSION already installed, skipping rebuild" - if [[ -e build ]]; then - rm -rf build - fi - exit 0 - fi +if [[ -f "$SYSROOT_LD" ]]; then + SYSROOT_LD_VERSION=$("$SYSROOT_LD" --version | head -n 1 | grep -Eo '[0-9.]+$') fi -# Aggressive parameter to avoid rebuilds - -if [[ ! -z "$ACCEPT_ANY" ]]; then - if command -v sh-elf-as >/dev/null 2>&1; then - echo "$TAG Found sh-elf-as in PATH and ACCEPT_ANY is set, skipping build" - if [[ -e build ]]; then - rm -rf build - fi - exit 0 - 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 -# Check dependencies for binutils and GCC +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 @@ -63,57 +64,53 @@ if [[ -z "$trust_deps" ]]; then 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 '$pm_install $missing' to install them (Y/n)? " read do_install - if [[ "$do_install" == "y" || "$do_install" == "Y" || "$do_install" == "" ]]; then + 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 -# Download archive +#--- +# 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 + curl "$URL" -o "$ARCHIVE" elif command -v wget >/dev/null 2>&1; then - wget -q --show-progress $URL -O $ARCHIVE + 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 (OpenBDS-compliant version) - +# Extract archive (OpenBSD-compliant version) echo "$TAG Extracting $ARCHIVE..." -unxz -c < $ARCHIVE | tar -xf - +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 +touch "binutils-$VERSION/intl/plural.c" -# Create build folder +#--- +# Configure +#--- +# We install in the sysroot and then symlink to the PREFIX folder in the path [[ -d "build" ]] && rm -rf build -mkdir build - -# Configure. binutils does not support the uninstall target (wow) so we just -# install in this directory and later symlink executables to $PREFIX/bin. - -PREFIX="$(pwd)" -cd build +mkdir build && cd build echo "$TAG Configuring binutils..." @@ -130,5 +127,11 @@ if command -v termux-setup-storage >/dev/null 2>&1; then CXXFLAGS="-D__ANDROID_API__=$(getprop ro.build.version.sdk) -g -O2" fi -run_quietly giteapc-configure.log \ -../binutils-$VERSION/configure --prefix="$PREFIX" --target=sh3eb-elf --with-multilib-list=m3,m4-nofpu --program-prefix=sh-elf- --enable-libssp --enable-lto +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 diff --git a/giteapc-config-clean.make b/giteapc-config-clean.make new file mode 100644 index 0000000..572345d --- /dev/null +++ b/giteapc-config-clean.make @@ -0,0 +1,4 @@ +# Clean the build products after install +CONFIG_CLEAN=1 + +export CONFIG_CLEAN diff --git a/giteapc.make b/giteapc.make index e33defd..8626cb7 100644 --- a/giteapc.make +++ b/giteapc.make @@ -1,4 +1,5 @@ # giteapc: version=1 +# giteapc: depends=Lephenixnoir/fxsdk PREFIX ?= $(GITEAPC_PREFIX) VERSION = 2.39 diff --git a/install.sh b/install.sh index 1e3d699..0e61322 100755 --- a/install.sh +++ b/install.sh @@ -1,30 +1,26 @@ #! /usr/bin/env bash # Avoid rebuilds of the same version -[[ ! -d build ]] && exit 0 +[[ -e "build/giteapc-skip-rebuild.txt" ]] && exit 0 source util.sh PREFIX="$1" -if command -v gmake >/dev/null 2>&1; then - make_command=gmake -else - make_command=make -fi - cd build -echo "$TAG Installing to local folder..." +echo "$TAG Installing binutils to the SuperH sysroot..." run_quietly giteapc-install.log \ -$make_command install-strip +$MAKE_COMMAND install-strip cd .. # Symbolic link executables to $PREFIX/bin -echo "$TAG Symlinking binaries..." -for x in bin/*; do - ln -sf "$(pwd)/$x" "$PREFIX/$x" +echo "$TAG Symlinking sysroot binaries to $PREFIX/bin..." +mkdir -p "$PREFIX/bin" +for f in "$SYSROOT/bin"/*; do + ln -sf "$f" "$PREFIX/${f#$SYSROOT/}" done # Cleanup build files -echo "$TAG Cleaning up build files..." -rm -rf binutils-*/ binutils-*.tar.* -rm -rf build/ +if [[ ! -z "$CONFIG_CLEAN" ]]; then + echo "$TAG Cleaning up build files..." + rm -rf binutils-*/ binutils-*.tar.* build/ +fi diff --git a/uninstall.sh b/uninstall.sh index d1665f4..51e03d0 100755 --- a/uninstall.sh +++ b/uninstall.sh @@ -3,12 +3,13 @@ source util.sh PREFIX="$1" -# Remove symlinks -echo "$TAG Removing symlinks to binaries..." -for x in bin/*; do - rm "$PREFIX/$x" +echo "$TAG Removing symlinks to $PREFIX/bin..." +TOOLS="addr2line ar as c++filt elfedit gprof ld ld.bfd nm objcopy objdump \ + ranlib readelf size strings strip" + +for t in $TOOLS; do + [[ -L "$PREFIX/bin/sh-elf-$t" ]] && rm "$PREFIX/bin/sh-elf-$t" done -# Remove local files -echo "$TAG Removing installed files..." -rm -rf bin/ sh3eb-elf/ share/ +echo "$TAG Other files are managed by the fxSDK's SuperH sysroot" +echo "$TAG Uninstall Lephenixnoir/fxsdk to clean up the sysroot" diff --git a/util.sh b/util.sh index 8ecd195..1e701ca 100644 --- a/util.sh +++ b/util.sh @@ -1,5 +1,21 @@ TAG="" +if command -v gmake >/dev/null 2>&1; then + MAKE_COMMAND=gmake +else + MAKE_COMMAND=make +fi + +if ! command -v fxsdk >/dev/null 2>&1; then + echo "$TAG error: fxSDK is not installed" + exit 1 +elif ! fxsdk path sysroot >/dev/null 2>&1; then + echo "$TAG error: need fxSDK ≥ 2.9 with 'path' command" + exit 1 +fi + +SYSROOT="$(fxsdk path sysroot)" + run_quietly() { out="$1" shift 1