Skip to content

Building multiple libcs for testing

Alex Rønne Petersen edited this page May 6, 2025 · 4 revisions

When working on Zig, especially in low-level areas, it can be useful to build many copies of libc for different architectures so that zig build test will run tests for all of them. After completing one or more of the sections below, you can do e.g. zig build test -fqemu --libc-runtimes $HOME/.local/libc.

glibc

This will take multiple hours even on a beefy machine:

git clone https://sourceware.org/git/glibc.git
cd glibc
git checkout $(git describe $(git rev-list -n 1 --tags='glibc-?.??'))
mkdir build
cd build
../scripts/build-many-glibcs.py . checkout
pushd src/glibc
git checkout $(git describe $(git rev-list -n 1 --tags='glibc-?.??'))
popd
../scripts/build-many-glibcs.py . host-libraries -j $(nproc)
../scripts/build-many-glibcs.py . compilers -j $(nproc)
../scripts/build-many-glibcs.py . glibcs -j $(nproc)
mkdir -p $HOME/.local/libc
mv install/glibcs/* $HOME/.local/libc

musl

This should take around 5-10 minutes:

git clone https://github.com/alexrp/musl.git
cd musl
# Tracks the latest musl release with Hexagon patches from: https://github.com/quic/musl
git checkout hexagon
# Workaround for: https://www.openwall.com/lists/musl/2025/01/24/1
sed -i 's/\tbrasl %r14, __tls_get_addr/.hidden __tls_get_addr\n\tbrasl %r14, __tls_get_addr/' src/thread/s390x/__tls_get_offset.s
mkdir build
cd build
for triple in arm-linux-musleabi \
              arm-linux-musleabihf \
              armeb-linux-musleabi \
              armeb-linux-musleabihf \
              thumb-linux-musleabi \
              thumb-linux-musleabihf \
              thumbeb-linux-musleabi \
              thumbeb-linux-musleabihf \
              aarch64-linux-musl \
              aarch64_be-linux-musl \
              hexagon-linux-musl \
              loongarch64-linux-musl \
              mips-linux-musleabi \
              mips-linux-musleabihf \
              mipsel-linux-musleabi \
              mipsel-linux-musleabihf \
              mips64-linux-muslabi64 \
              mips64-linux-muslabin32 \
              mips64el-linux-muslabi64 \
              mips64el-linux-muslabin32 \
              powerpc-linux-musleabi \
              powerpc-linux-musleabihf \
              powerpc64-linux-musl \
              powerpc64le-linux-musl \
              riscv32-linux-musl \
              riscv64-linux-musl \
              s390x-linux-musl \
              x86_64-linux-musl; do
    echo -e '#!/bin/sh\nzig cc -target '$triple' $@' > zcc-$triple
done
chmod +x zcc-*
for zcc in $(echo zcc-* | sort); do
    target=${zcc#zcc-}
    target=${target/x86-/i386-}
    target=${target/thumb-/arm-}
    target=${target/thumbeb-/armeb-}
    ../configure --prefix=$HOME/.local/libc/${zcc#zcc-} --syslibdir=$HOME/.local/libc/${zcc#zcc-}/lib --target=$target --enable-debug AR="zig ar" CC=$PWD/$zcc RANLIB="zig ranlib" && \
        rm -f conf*-*-*.o && \
        make clean && \
        make -j $(nproc) && \
        make install || break
done

Note that x86-linux-musl and x86_64-linux-muslx32 are currently broken.