Skip to content

Instantly share code, notes, and snippets.

@windoze
Created December 5, 2023 21:18
Show Gist options
  • Save windoze/0980595c06836108982f31bfec4f1232 to your computer and use it in GitHub Desktop.
Save windoze/0980595c06836108982f31bfec4f1232 to your computer and use it in GitHub Desktop.
Rust musl cross compile script
#!/bin/bash
# Usage:
# /path/to/musl.sh some-rust-target-name cargo subcommand --options ...
# E.g. musl.sh aarch64-linux-unknown-musl cargo build --release
#
# Note:
# Linking C++ libs doesn't work, as we don't have musl version of C++ std libs
TARGET=$1
shift
IFS='-' read -r -a array <<< "$TARGET"
ARCH="${array[0]}"
ABI="${array[@]: -1:1}"
IMAGE="messense/rust-musl-cross:${ARCH}-${ABI}"
TOOLCHAIN="${ARCH}-unknown-linux-${ABI}"
MUSL_ROOT="/usr/local/musl"
#echo "ARCH is ${ARCH}"
#echo "Using target ${TARGET}"
#echo "Image is ${IMAGE}"
docker run \
--env CC="${MUSL_ROOT}/bin/${TOOLCHAIN}-cc" \
--env CXX="${MUSL_ROOT}/bin/${TOOLCHAIN}-c++" \
--env AR="${MUSL_ROOT}/bin/${TOOLCHAIN}-ar" \
--env RUSTFLAGS="-C linker=${MUSL_ROOT}/bin/${TOOLCHAIN}-cc" \
--rm -it --user "$(id -u):$(id -g)" \
-v "$HOME/.cargo":/root/.cargo \
-v /root/.cargo/bin \
-v /root/.cargo/binstall \
-v "$(pwd)":/home/rust/src \
$IMAGE "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment