Skip to content
Vojtech Horky edited this page Jan 16, 2018 · 3 revisions

My local notes for HelenOS

Static cross-compilation toolchain

On top of MUSL (expects system-installation with musl-gcc wrapper).

#!/bin/sh

XX_BINUTILS_VERSION=2.28
XX_GMP_VERSION=6.1.2
XX_MPFR_VERSION=3.1.6
XX_MPC_VERSION=1.0.3
XX_ISL_VERSION=0.18
XX_GCC_VERSION=7.1.0

XX_DOWNLOAD_DIR="$PWD/sources"

XX_GNU_MIRROR="ftp://ftp.gnu.org/gnu/"

xx_download_one() {
    if ! [ -e "$XX_DOWNLOAD_DIR/$2" ]; then
        wget -c "$1" -O "$XX_DOWNLOAD_DIR/$2" || exit 1
    fi
}

xx_download_all() {
    xx_download_one "$XX_GNU_MIRROR/binutils/binutils-$XX_BINUTILS_VERSION.tar.bz2" "binutils-$XX_BINUTILS_VERSION.tar.bz2"
    xx_download_one "https://gmplib.org/download/gmp/gmp-$XX_GMP_VERSION.tar.bz2" "gmp-$XX_GMP_VERSION.tar.bz2"
    xx_download_one "http://www.mpfr.org/mpfr-$XX_MPFR_VERSION/mpfr-$XX_MPFR_VERSION.tar.bz2" "mpfr-$XX_MPFR_VERSION.tar.bz2"
    xx_download_one "http://www.multiprecision.org/mpc/download/mpc-$XX_MPC_VERSION.tar.gz" "mpc-$XX_MPC_VERSION.tar.gz"
    xx_download_one "http://isl.gforge.inria.fr/isl-$XX_ISL_VERSION.tar.bz2" "isl-$XX_ISL_VERSION.tar.bz2"
    xx_download_one "$XX_GNU_MIRROR/gcc/releases/gcc-$XX_GCC_VERSION/gcc-$XX_GCC_VERSION.tar.bz2" "gcc-$XX_GCC_VERSION.tar.bz2"
}

xx_build_binutils() {
    TARGET="$1"
    
    tar xjf "$XX_DOWNLOAD_DIR/binutils-$XX_BINUTILS_VERSION.tar.bz2" || exit 1
    cd "binutils-$XX_BINUTILS_VERSION" || exit 1
    
    export CC=musl-gcc
    export LD=musl-gcc
    export CFLAGS=-static
    export LDFLAGS=-static

    ./configure \
        "--target=$TARGET" \
        "--prefix=$XX_PREFIX" \
        "--program-prefix=$TARGET-" \
        "--with-gmp=$XX_DEP_DESTDIR/$XX_PREFIX/" \
        "--with-mpfr=$XX_DEP_DESTDIR/$XX_PREFIX/" \
        "--with-mpc=$XX_DEP_DESTDIR/$XX_PREFIX/" \
        "--with-isl=$XX_DEP_DESTDIR/$XX_PREFIX/" \
        --disable-nls \
        --disable-werror \
        --disable-gold \
        --enable-deterministic-archives \
        --enable-static \
        --disable-shared || exit 1

    make configure-host || exit 1
    
    # -all-static is a flag for libtool
    # This flag is not recognized by a linker thus we
    # cannot pass it in when running configure as it
    # fails on conftest.c-style files
    make all LDFLAGS="-all-static" || exit 1
    #make all-gas all-ld LDFLAGS="-all-static" || exit 1
    make install "DESTDIR=$XX_DESTDIR" || exit 1
    touch ../ok.binutils
}

xx_build_gmp() {
    tar xjf "$XX_DOWNLOAD_DIR/gmp-$XX_GMP_VERSION.tar.bz2" || exit 1
    cd "gmp-$XX_GMP_VERSION" || exit 1
    
    export CC=musl-gcc
    export LD=musl-gcc
    #export "CFLAGS=-static -I$XX_DESTDIR/$XX_PREFIX/include"
    #export "LDFLAGS=-static -L$XX_DESTDIR/$XX_PREFIX/lib"
    export "CFLAGS=-static"
    export "LDFLAGS=-static"

    ./configure \
        "--prefix=$XX_PREFIX" \
        --enable-static \
        --disable-shared || exit 1
    
    make all STATIC_ONLY=y || exit 1
    make install "DESTDIR=$XX_DEP_DESTDIR" || exit 1
    touch ../ok.gmp
}

xx_build_mpfr() {
    tar xjf "$XX_DOWNLOAD_DIR/mpfr-$XX_MPFR_VERSION.tar.bz2" || exit 1
    cd "mpfr-$XX_MPFR_VERSION" || exit 1
    
    export CC=musl-gcc
    export LD=musl-gcc
    export "CFLAGS=-static -I$XX_DEP_DESTDIR/$XX_PREFIX/include"
    export "LDFLAGS=-static -L$XX_DEP_DESTDIR/$XX_PREFIX/lib"

    ./configure \
        "--prefix=$XX_PREFIX" \
        --enable-static \
        --disable-shared || exit 1
    
    make all STATIC_ONLY=y || exit 1
    make install "DESTDIR=$XX_DEP_DESTDIR" || exit 1
    touch ../ok.mpfr
}

xx_build_mpc() {
    tar xzf "$XX_DOWNLOAD_DIR/mpc-$XX_MPC_VERSION.tar.gz" || exit 1
    cd "mpc-$XX_MPC_VERSION" || exit 1
    
    export CC=musl-gcc
    export LD=musl-gcc
    export "CFLAGS=-static -I$XX_DEP_DESTDIR/$XX_PREFIX/include"
    export "LDFLAGS=-static -L$XX_DEP_DESTDIR/$XX_PREFIX/lib"

    ./configure \
        "--prefix=$XX_PREFIX" \
        --enable-static \
        --disable-shared \
        --with-gmp=$XX_DEP_DESTDIR/$XX_PREFIX/ \
        --with-mpfr=$XX_DEP_DESTDIR/$XX_PREFIX/ \
        || exit 1
    
    make STATIC_ONLY=y LDFLAGS="-all-static" || exit 1
    make install "DESTDIR=$XX_DEP_DESTDIR" || exit 1
    touch ../ok.mpc
}

xx_build_isl() {
    tar xjf "$XX_DOWNLOAD_DIR/isl-$XX_ISL_VERSION.tar.bz2" || exit 1
    cd "isl-$XX_ISL_VERSION" || exit 1
    
    export CC=musl-gcc
    export LD=musl-gcc
    export "CFLAGS=-static -I$XX_DEP_DESTDIR/$XX_PREFIX/include"
    export "LDFLAGS=-static -L$XX_DEP_DESTDIR/$XX_PREFIX/lib"

    ./configure \
        "--prefix=$XX_PREFIX" \
        --with-gmp-prefix=$XX_DEP_DESTDIR/$XX_PREFIX/ \
        --enable-static \
        --disable-shared || exit 1
    
    make all STATIC_ONLY=y LDFLAGS="-all-static" || exit 1
    make install "DESTDIR=$XX_DEP_DESTDIR" || exit 1
    touch ../ok.isl
}

xx_build_gcc() {
    TARGET="$1"
    
    tar xjf "$XX_DOWNLOAD_DIR/gcc-$XX_GCC_VERSION.tar.bz2" || exit 1
    cd "gcc-$XX_GCC_VERSION" || exit 1
    
    export CC=musl-gcc
    export LD=musl-gcc
    export "CFLAGS=-static -I$XX_DEP_DESTDIR/$XX_PREFIX/include"
    export "LDFLAGS=-static -L$XX_DEP_DESTDIR/$XX_PREFIX/lib"

    #,objc,c++,obj-c++

    ./configure \
        "--prefix=$XX_PREFIX" \
        "--target=$TARGET" \
        "--program-prefix=$TARGET-" \
        --with-gmp=$XX_DEP_DESTDIR/$XX_PREFIX/ \
        --with-mpfr=$XX_DEP_DESTDIR/$XX_PREFIX/ \
        --with-mpc=$XX_DEP_DESTDIR/$XX_PREFIX/ \
        --with-isl=$XX_DEP_DESTDIR/$XX_PREFIX/ \
        --with-gnu-as \
        --with-gnu-ld \
        --disable-nls \
        --enable-threads \
        --enable-languages=c \
        --disable-multilib \
        --disable-libgcj \
        --without-headers \
        --enable-lto \
        --disable-werror \
        --enable-static \
        --disable-shared || exit 1
    
    make all-gcc STATIC_ONLY=y || exit 1
    make install-gcc "DESTDIR=$XX_DESTDIR" || exit 1
    touch ../ok.gcc
}


xx_download_all

#rm -rf cross-static-musl

XX_DESTDIR=$PWD/cross-static
XX_BUILDDIR=$PWD/build-cross-static
XX_PREFIX=/usr/local/cross-static/

XX_TARGETS="amd64-unknown-elf arm-linux-gnueabi i686-pc-linux-gnu ia64-pc-linux-gnu mips-linux-gnu mips64el-linux-gnu mipsel-linux-gnu ppc-linux-gnu ppc64-linux-gnu riscv64-unknown-linux-gnu sparc64-linux-gnu"
#XX_TARGETS="amd64-unknown-elf arm-linux-gnueabi i686-pc-linux-gnu"

mkdir -p "$XX_DESTDIR" || exit 1
mkdir -p "$XX_BUILDDIR" || exit 1


for TARGET in $XX_TARGETS; do
    (
        set -x
        
        date
        
        export "XX_PREFIX=$XX_PREFIX/$TARGET/"
        export "XX_DEP_DESTDIR=$XX_BUILDDIR/$TARGET/deps/"
        
        mkdir -p "$XX_BUILDDIR/$TARGET" || exit 1
        cd "$XX_BUILDDIR/$TARGET" || exit 1

        
        if ! [ -e "ok.gmp" ]; then
            rm -rf "gmp-$XX_GMP_VERSION" || exit 1
            ( xx_build_gmp "$TARGET" ) || exit 1
        fi
        
        if ! [ -e "ok.mpfr" ]; then
            rm -rf "mpfr-$XX_MPFR_VERSION" || exit 1
            ( xx_build_mpfr "$TARGET" ) || exit 1
        fi
        
        if ! [ -e "ok.mpc" ]; then
            rm -rf "mpc-$XX_MPC_VERSION" || exit 1
            ( xx_build_mpc "$TARGET" ) || exit 1
        fi
        
        if ! [ -e "ok.isl" ]; then
            rm -rf "isl-$XX_ISL_VERSION" || exit 1
            ( xx_build_isl "$TARGET" ) || exit 1
        fi
        
        if ! [ -e "ok.binutils" ]; then
            rm -rf "binutils-$XX_BINUTILS_VERSION" || exit 1
            ( xx_build_binutils "$TARGET" ) || exit 1
        fi
        

        if ! [ -e "ok.gcc" ]; then
            rm -rf "gcc-$XX_GCC_VERSION" || exit 1
            ( xx_build_gcc "$TARGET" ) || exit 1
        fi
        
        cd "$XX_DESTDIR/$XX_PREFIX" || exit 1
        find . -type f -executable -not -name '*.sh' -not -name '*.la' -not -name 'mkinstalldirs' -not -name 'mkheaders' -exec strip {} \;
        
        date
    ) 2>&1 | tee "$XX_BUILDDIR/$TARGET.log"
done

The lost DEBUG macro

#define DEBUG(fmt, ...) \
	{ \
		char _buf[256]; \
		int _n = snprintf(_buf, sizeof(_buf), fmt, ##__VA_ARGS__); \
		if (_n > 0) \
			(void) __SYSCALL3(SYS_KIO, KIO_WRITE, (sysarg_t) _buf, str_size(_buf)); \
	}