#!/bin/bash set -eux ARCH="$1" shift sudo= if [ "${CI:-false}" = true ]; then sudo=sudo fi $sudo apt-get update -y if [ "${CI:-false}" != true ]; then $sudo apt-get upgrade -y fi build_packages() { local pkgs=() local checkcacert=false if ! dpkg -L ca-certificates >/dev/null 2>/dev/null; then checkcacert=true fi for pkg in "${@}"; do if ! dpkg -L "${pkg}" >/dev/null 2>/dev/null; then pkgs+=("${pkg}") fi done if [ "${#pkgs[@]}" -gt 0 ]; then $sudo apt-get install --assume-yes --no-install-recommends "${pkgs[@]}" $sudo apt-mark auto "${pkgs[@]}" fi if $checkcacert && dpkg -L ca-certificates >/dev/null 2>/dev/null; then $sudo update-ca-certificates --fresh fi } common_install() { build_packages wget git file texinfo ca-certificates $sudo apt-get install -y build-essential "$@" } install_musl() { local MUSLDIR MUSLDIR="$(mktemp -d)" git clone --depth=1 https://github.com/richfelker/musl-cross-make.git "$MUSLDIR" cd "$MUSLDIR" cat <config.mak TARGET = $1 OUTPUT = /usr GCC_VER = 11.2.0 BINUTILS_VER = 2.33.1 GCC_CONFIG += --enable-default-pie EOF make -j"$(nproc)" install cd rm -rf "$MUSLDIR" local sysroot="/usr/$1" local arch="$2" local ld_arch="${arch//_/-}" local dst mkdir -p "${sysroot}/usr/lib" local src="${sysroot}/lib/libc.so" for dst in \ "/lib/ld-musl-${arch}.so" \ "/lib/ld-musl-${arch}.so.1" \ "${sysroot}/lib/ld-musl-${arch}.so" \ "${sysroot}/lib/ld-musl-${arch}.so.1" \ "${sysroot}/usr/lib/ld.so" \ "${sysroot}/usr/lib/ld.so.1" \ "${sysroot}/usr/lib/libc.so" \ "${sysroot}/usr/lib/libc.so.1" \ "${sysroot}/lib/ld-linux-${ld_arch}.so" \ "${sysroot}/lib/ld-linux-${ld_arch}.so.1"; do if [ -L "$dst" ] && [ ! -a "$dst" ]; then ln -sf "${src}" "${dst}" elif [ ! -f "$dst" ]; then ln -s "${src}" "${dst}" fi done rm -f "${sysroot}/lib/libstdc++.so"* echo 'GROUP ( libstdc++.a AS_NEEDED( -lgcc -lc -lm ) )' >"${sysroot}/lib/libstdc++.so.6.0.29" ln -s libstdc++.so.6.0.29 "${sysroot}/lib/libstdc++.so.6" ln -s libstdc++.so.6.0.29 "${sysroot}/lib/libstdc++.so" echo "${sysroot}/lib" >>"/etc/ld-musl-${arch}.path" } musl_linker() { cat </armv5te-musl.sh chmod +x /armv5te-musl.sh compiler_rt_root install_android_ndk arm } setup_aarch64() { common_install qemu-user gcc-aarch64-linux-gnu install_musl aarch64-linux-musl aarch64 musl_linker aarch64-linux-musl- 48 >/aarch64-musl.sh chmod +x /aarch64-musl.sh compiler_rt_root install_android_ndk aarch64 } setup_riscv64() { common_install qemu-user gcc-riscv64-linux-gnu install_musl riscv64-linux-musl riscv64 compiler_rt_root } setup_powerpc() { common_install qemu-user gcc-powerpc-linux-gnu install_musl powerpc-linux-musl powerpc compiler_rt_root } setup_powerpc64() { common_install qemu-user gcc-powerpc64-linux-gnu \ gcc-powerpc64le-linux-gnu install_musl powerpc64-linux-musl powerpc64 install_musl powerpc64le-linux-musl powerpc64le compiler_rt_root } setup_mips() { common_install qemu-user gcc-mips-linux-gnu gcc-mipsel-linux-gnu install_musl mips-linux-musl mips install_musl mipsel-linux-musl mipsel compiler_rt_root } setup_mips64() { common_install qemu-user gcc-mips64-linux-gnuabi64 \ gcc-mips64el-linux-gnuabi64 install_musl mips64-linux-muslabi64 mips64 install_musl mips64el-linux-muslabi64 mips64el musl_linker mips64-linux-muslabi64- 65 >/mips64-musl.sh chmod +x /mips64-musl.sh musl_linker mips64el-linux-muslabi64- 65 >/mips64el-musl.sh chmod +x /mips64el-musl.sh compiler_rt_root } setup_s390x() { common_install qemu-user gcc-s390x-linux-gnu install_musl s390x-linux-musl s390x compiler_rt_root } # TODO: riscv32 "setup_${ARCH}"