Skip to content

Instantly share code, notes, and snippets.

@27justin
Last active April 20, 2021 07:15
Show Gist options
  • Save 27justin/5599de1946c8b5a63403490176e7e39b to your computer and use it in GitHub Desktop.
Save 27justin/5599de1946c8b5a63403490176e7e39b to your computer and use it in GitHub Desktop.
Build a cross-compiler for x86_64-sun-solaris2.11
#!/bin/bash
# Tested against SmartOS (build: 20200117T180335Z)
# Should work on any x86_64-sun-solaris2.11 operating system tho
# Fail on any error
set -e
PREFIX=~/Dokumente/cross/x86_64-sun-solaris2.11
SYSROOT=$PREFIX/sysroot
BUILD_DIR=$PREFIX/build
PATH=$PREFIX/bin:$PATH
TARGET="x86_64-sun-solaris2.11"
# This variable is later used to extract libraries from the operating system to build our local SYSROOT
# It is used as `ssh $DONOR_SERVER ...`
# (Note for people looking to build a cross-compiler for SmartOS: i used the libraries from a base-64 zone, i haven't tried whether it'll work with libraries from the global zone tho) (cross-compiler works on SmartOS (build: 20200117T180335Z))
DONOR_SERVER="-p 26 root@192.168.0.143"
# Dependencies
# ------------
# Confirmed to work on:
# * binutils-2.36.1 + gcc-10.3.0
BINUTILS="binutils-2.36.1"
GCC="gcc-10.3.0"
# Misc Variables
# --------------
HOST=$(gcc -dumpmachine)
MAKE_ARGS="-j16"
# -------------
mkdir -p $PREFIX
mkdir -p $SYSROOT
mkdir -p $BUILD_DIR
# Download system headers
cd $SYSROOT
# Shamelessly taken from:
# https://gist.githubusercontent.com/AlainODea/6679613/raw/69ed7c94fde4585b0842d1f433f5b838c547038f/setup_cross_gcc_linux_illumos.sh
echo "Downloading libraries from donor..."
ssh $DONOR_SERVER "pkgin -y up && pkgin -y ug && pkgin -y in gmp"
ssh $DONOR_SERVER "tar -cf - /usr/include" | tar -xf -
ssh $DONOR_SERVER "tar -cf - /opt/local/include" | tar -xf -
ssh $DONOR_SERVER "tar -cf - /opt/local/gcc47/include" | tar -xf -
ssh $DONOR_SERVER "tar -cf - /lib" | tar -xf -
ssh $DONOR_SERVER "tar -cf - /usr/lib" | tar -xf -
ssh $DONOR_SERVER "tar -cf - /usr/gnu/lib" | tar -xf -
echo "Finished, changing ownership to $USER"
sudo chown -R $USER:$USER .
# Download and compile GNU binutils
cd $BUILD_DIR
echo "Downloading $BINUTILS..."
wget https://ftp.gnu.org/gnu/binutils/$BINUTILS.tar.gz
tar xf $BINUTILS.tar.gz
mkdir build-binutils && cd build-binutils
../$BINUTILS/configure --host=$HOST --target=$TARGET --prefix=$PREFIX --disable-nls --with-sysroot=$SYSROOT -v --without-headers
echo "Compiling $binutils"
make $MAKE_ARGS
make install
# Download and compile GCC
cd $BUILD_DIR
echo "Downloading $GCC..."
wget https://ftp.gnu.org/gnu/gcc/$GCC/$GCC.tar.gz
tar xf $GCC.tar.gz
# Download libgmp, libmpc and libmpfr
cd $GCC
./contrib/download_prerequisites
cd $BUILD_DIR
# -----------------------------------
mkdir build-gcc && cd build-gcc
# Apparently the whole --with-gnu-as, --with-as, --with-gnu-ld and --with-ld business is pretty much mandatory:
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=38693
# (I can confirm out of first hand experience, that it doesn't work without these arguments)
../$GCC/configure --host=$HOST --prefix=$PREFIX --target=$TARGET --disable-nls --with-sysroot=$SYSROOT --enable-languages=c,c++ --with-gnu-as --with-as=$PREFIX/bin/$TARGET-as --with-gnu-ld --with-ld=$PREFIX/bin/$TARGET-ld --without-headers
echo "Compiling gcc"
make $MAKE_ARGS all-gcc
make install-gcc
echo "Compiling libgcc"
make $MAKE_ARGS all-target-libgcc
make install-target-libgcc
echo "Finished."
cd $PREFIX
echo "Cross-compiler installed at $PREFIX/bin/$TARGET-gcc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment