I have written this post as notes for myself on how to buld a GCC (with musl) toolchain for MIPS on macOS. Most of the steps apply for other operating systems, but there are some quirks to be done to do it on macOS.

First, you have to make sure that Xcode command-line tools are installed.

xcode-select --install

Now you have to install C headers in the /usr/include by running the following command:

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

Now we can start building the GCC. Let’s define all the variables we will use while building it.

export WDIR=/tmp
export BUILD=x86_64-apple-darwin$(uname -r)
export TARGET=mipsel-linux-musl
export PREFIX=/opt/cross/${TARGET}
cd ${WDIR}
mkdir -p ${TARGET}-src
mkdir -p ${TARGET}-toolchain

You also have to download and install GNU sed utility. The macOS built-in sed does not support the features needed. I usually install sed in /opt/sed/bin because it has to be used when building GNU projects.

cd ${WDIR}/${TARGET}-src
curl -L https://ftp.gnu.org/gnu/sed/sed-4.7.tar.xz | tar xJf -

cd ${WDIR}/${TARGET}-toolchain
mkdir -p build-sed
cd build-sed
${WDIR}/${TARGET}-src/sed-4.7/configure \
    --prefix=/opt/sed
make -j 8
sudo make install-strip
export PATH=/opt/sed/bin:${PATH}

Next we have to download and build binutils which includes utilities that are needed to build the gcc (assembler, linker etc.). We disable NLS (native language support) to speed up the build and pass the target architecture for which to build, in our case mipsellinuxmusl which means little endian MIPS running a Linux and using musl C library.

cd ${WDIR}/${TARGET}-src
curl -L http://ftp.gnu.org/gnu/binutils/binutils-2.32.tar.gz | tar xzf -

cd ${WDIR}/${TARGET}-toolchain
mkdir -p build-binutils
cd build-binutils
${WDIR}/${TARGET}-src/binutils-2.32/configure \
    --disable-nls \
    --prefix=${PREFIX} \
    --target=${TARGET}
make -j 8
sudo make install-strip

To build GCC we need GMP, MPFR, MPC, and ISL. Let’s download and build them.

cd ${WDIR}/${TARGET}-src
curl -L https://gmplib.org/download/gmp/gmp-6.1.2.tar.bz2 | tar xjf -
curl -L ftp://gcc.gnu.org/pub/gcc/infrastructure/mpfr-3.1.4.tar.bz2 | tar xjf -
curl -L ftp://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.0.3.tar.gz | tar xzf -
curl -L ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-0.18.tar.bz2 | tar xjf -

cd ${WDIR}/${TARGET}-toolchain
mkdir -p build-gmp
cd build-gmp
${WDIR}/${TARGET}-src/gmp-6.1.2/configure \
    --prefix=/opt/gmp \
    --enable-cxx
make -j 8
sudo make install-strip

cd ${WDIR}/${TARGET}-toolchain
mkdir -p build-mpfr
cd build-mpfr
${WDIR}/${TARGET}-src/mpfr-3.1.4/configure \
    --prefix=/opt/mpfr \
    --with-gmp=/opt/gmp
make -j 8
sudo make install-strip

cd ${WDIR}/${TARGET}-toolchain
mkdir -p build-mpc
cd build-mpc
${WDIR}/${TARGET}-src/mpc-1.0.3/configure \
    --prefix=/opt/mpc \
    --with-gmp=/opt/gmp \
    --with-mpfr=/opt/mpfr
make -j 8
sudo make install-strip

cd ${WDIR}/${TARGET}-toolchain
mkdir -p build-isl
cd build-isl
${WDIR}/${TARGET}-src/isl-0.18/configure \
    --prefix=/opt/isl \
    --with-gmp-prefix=/opt/gmp
make -j 8
sudo make install-strip

Now we can download GCC and build it. We will disable shared library support (you can enable it if you want but that will need some more configuration), enable C and C++ languages and build only the compiler (all-gcc). We can not build the rest of the GCC because we don’t have musl built yet, but we can cont build musl without GCC. Because our CPU does not support hardware floating point instructions we will enable floating point instruction software emulation with the “–with-float=soft” flag.

cd ${WDIR}/${TARGET}-src
curl -L https://ftpmirror.gnu.org/gcc/gcc-8.3.0/gcc-8.3.0.tar.xz | tar xJf -

cd ${WDIR}/${TARGET}-toolchain
mkdir -p build-gcc
cd build-gcc
${WDIR}/${TARGET}-src/gcc-8.3.0/configure \
    --enable-languages=c,c++ \
    --disable-nls \
    --prefix=${PREFIX} \
    --with-gmp=/opt/gmp \
    --with-mpfr=/opt/mpfr \
    --with-mpc=/opt/mpc \
    --with-isl=/opt/isl \
    --target=${TARGET} \
    --with-float=soft \
    --disable-shared # remove to enable shared libs
 make all-gcc -j8
 sudo make install-strip-gcc

When the GCC has been built we can download and build musl. You can replace musl with any other C library you need (e.g. glibc, uClibc, or newlib).

cd ${WDIR}/${TARGET}-src
curl -L https://www.musl-libc.org/releases/musl-1.1.21.tar.gz | tar xzf -

cd ${WDIR}/${TARGET}-toolchain
mkdir -p build-musl
cd build-musl
CROSS_COMPILE=${PREFIX}/bin/${TARGET}- \
${WDIR}/${TARGET}-src/musl-1.1.21/configure \
   --target=${TARGET} \
   --prefix=${PREFIX}/${TARGET} \
   --build=${BUILD} \
   --with-float=soft \
   --disable-shared # remove to enable shared libs
make -j8
sudo make install

The last step is to finish building the rest of the GCC.

cd ${WDIR}/${TARGET}-toolchain
cd build-gcc
make -j8
sudo make install-strip

Now the full GCC toolchain and musl should be in your /opt/cross/mipsellinux-musl directory. I hope that helps.