Set up Loongson GCC Toolchain

1 Download and Unpack Loongson GCC 4.9

1
2
3
4
$ sudo -i # or `su root -l`
$ cd /opt
$ wget http://ftp.loongnix.org/toolchain/gcc/release/mips-loongson-gcc4.9-2019.08-05.linux-gnu.tar.gz
$ tar xvf mips-loongson-gcc4.9-2019.08-05.linux-gnu.tar.gz

2 Download Supplementary Setup Script

1
$ wget https://pzhang.net/2020/06/04/Setup-Loongson-GCC-Toolchain/gs264e-setup -O /opt/mips-loongson-gcc4.9-linux-gnu/gs264e-setup.sh

This supplementary setup script helps update the PATH environment variable, so you can use the toolchain commands directly in your shell without ugly absolute paths. Moreover, it sets the correct DEB_CFLAGS for Loongson 2K1000 processor (GS264E core), so when you run dpkg-buildpackage to cross-build a package, the optimizing CFLAGS are applied.

Setting DEB_CFLAGS directly may be an ugly workaround, as it will affect other arch package builds (since this environment variable is applied regardless of host and target architecture). Let me know if there is a better way to set the CFLAGS just for mips64el!

3 Apply the Script

1
$ source /opt/mips-loongson-gcc4.9-linux-gnu/gs264e-setup.sh

4 Test the Toolchain

1
2
3
4
5
6
7
8
$ mips-linux-gnu-gcc -v
Using built-in specs.
COLLECT_GCC=mips-linux-gnu-gcc
COLLECT_LTO_WRAPPER=/opt/mips-loongson-gcc4.9-linux-gnu/2019.08-05/bin/../libexec/gcc/mips-linux-gnu/4.9.4/lto-wrapper
Target: mips-linux-gnu
Configured with: /home/loongson/chenglulu/loongson-4.9/release-13.4/src/gcc-4.9-2015.05/configure --build=x86_64-unknown-linux-gnu --host=x86_64-unknown-linux-gnu --target=mips-linux-gnu --enable-threads --disable-libmudflap --disable-libssp --disable-libstdcxx-pch --with-arch-32=mips32r2 --with-arch-64=mips64r2 --with-float=hard --with-mips-plt --enable-extra-sgxxlite-multilibs --with-gnu-as --with-gnu-ld --with-fix-loongson3-llsc --with-specs='%{save-temps: -fverbose-asm}' --enable-languages=c,c++,fortran --enable-shared --enable-lto --enable-symvers=gnu --enable-__cxa_atexit --with-glibc-version=2.20 --with-pkgversion='Loongson gcc4.9' --with-bugurl=http://bugs.loongnix.org/ --disable-nls --prefix=/opt/codesourcery --with-sysroot=/opt/codesourcery/mips-linux-gnu/libc --with-build-sysroot=/home/loongson/chenglulu/loongson-4.9/release-13.4/install/opt/codesourcery/mips-linux-gnu/libc --with-gmp=/home/loongson/chenglulu/loongson-4.9/release-13.4/obj/pkg-2015.05-18-mips-linux-gnu/mips-2015.05-18-mips-linux-gnu.extras/host-libs-i686-pc-linux-gnu/usr --with-mpfr=/home/loongson/chenglulu/loongson-4.9/release-13.4/obj/pkg-2015.05-18-mips-linux-gnu/mips-2015.05-18-mips-linux-gnu.extras/host-libs-i686-pc-linux-gnu/usr --with-mpc=/home/loongson/chenglulu/loongson-4.9/release-13.4/obj/pkg-2015.05-18-mips-linux-gnu/mips-2015.05-18-mips-linux-gnu.extras/host-libs-i686-pc-linux-gnu/usr --with-isl=/home/loongson/chenglulu/loongson-4.9/release-13.4/obj/pkg-2015.05-18-mips-linux-gnu/mips-2015.05-18-mips-linux-gnu.extras/host-libs-i686-pc-linux-gnu/usr --with-cloog=/home/loongson/chenglulu/loongson-4.9/release-13.4/obj/pkg-2015.05-18-mips-linux-gnu/mips-2015.05-18-mips-linux-gnu.extras/host-libs-i686-pc-linux-gnu/usr --with-libelf=/home/loongson/chenglulu/loongson-4.9/release-13.4/obj/pkg-2015.05-18-mips-linux-gnu/mips-2015.05-18-mips-linux-gnu.extras/host-libs-i686-pc-linux-gnu/usr --enable-libgomp --disable-libitm --enable-libatomic --disable-libssp --enable-poison-system-directories --with-build-time-tools=/home/loongson/chenglulu/loongson-4.9/release-13.4/install/opt/codesourcery/mips-linux-gnu/bin --with-build-time-tools=/home/loongson/chenglulu/loongson-4.9/release-13.4/install/opt/codesourcery/mips-linux-gnu/bin SED=sed
Thread model: posix
gcc version 4.9.4 (Loongson gcc4.9)

Now you can start cross-building Debian packages, see Cross-build Debian Packages for mips64el Architecture.