grpc-rs
grpc-rs copied to clipboard
How to compile with x86_64-unknown-linux-musl
Here is my Dockerfile:
FROM rust:latest as builder RUN rustup update RUN rustup target add x86_64-unknown-linux-musl RUN apt-get update RUN apt-get install cmake musl-tools clang libc++-dev build-essential autoconf libtool pkg-config libgflags-dev libgtest-dev -y WORKDIR /workspace COPY Cargo.toml . COPY Cargo.lock . RUN mkdir src RUN echo "// placeholder" > src/lib.rs RUN cargo fetch RUN CC=clang CXX=clang++ cargo build --release --target=x86_64-unknown-linux-musl RUN rm src/lib.rs COPY ./ /workspace/ RUN CC=clang CXX=clang++ cargo test RUN CC=clang CXX=clang++ cargo build --release --target=x86_64-unknown-linux-musl FROM alpine COPY --from=builder /workspace/target/x86_64-unknown-linux-musl/release/app / ENTRYPOINT ["/app"] EXPOSE 9090
But compile fail with error:
undefined reference to symbol 'secure_getenv@@GLIBC_2.17'
Reference: https://stackoverflow.com/q/41856536/7083401
I managed to build it on musl at https://github.com/pingcap/grpc-rs/issues/233
I had some issues with my toolchain starting in the upgrade from 0.3.1 -> 0.4.0, and TBH I'm not entirely sure why. I ended up writing two scripts to handle it, build_musl.sh. I was trying to build a static binary with all deps included.
My steps:
- build+install the musl toolchain from https://github.com/GregorR/musl-cross
- setup the scripts
build_musl.shandcustom_link.shsourced below. - run
build_musl.sh
my build.sh
export CMP_ROOT=$PWD
export RUST_BACKTRACE=full
export TOOLCHAIN_ROOT="${TOOLCHAIN_ROOT:-/opt/cross/x86_64-linux-musl}"
echo "TOOLCHAIN_ROOT: $TOOLCHAIN_ROOT"
export CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER=$CMP_ROOT/custom_link.sh
export TARGET_CC=$TOOLCHAIN_ROOT/bin/musl-gcc
export TARGET_AR=$TOOLCHAIN_ROOT/bin/x86_64-linux-musl-ar
export HOST_CC=cc
export PKG_CONFIG_ALLOW_CROSS=1
export RUSTFLAGS="-C link-arg=-v -C linker=$CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER -C ar=$TARGET_AR"
export PATH=/opt/cross/x86_64-linux-musl/bin:$PATH
cargo build --release --target=x86_64-unknown-linux-musl
my custom_link.sh (from this suggestion: https://github.com/rust-lang/rust/issues/36710#issuecomment-364623950)
#!/bin/bash
export GCC_ROOT="$TOOLCHAIN_ROOT/lib/gcc/x86_64-linux-musl/5.3.0"
args=()
for arg in "$@"; do
if [[ $arg = *"Bdynamic"* ]]; then
args+=() # we do not want this arg
elif [[ $arg = *"crti.o"* ]]; then
args+=("$arg" "$GCC_ROOT/crtbeginT.o" "-Bstatic")
elif [[ $arg = *"crtn.o"* ]]; then
args+=("-lgcc" "-lgcc_eh" "-lc" "$GCC_ROOT/crtend.o" "$arg")
else
args+=("$arg")
fi
done
echo "RUNNING WITH ARGS: ${args[@]}"
musl-gcc "${args[@]}"
Oh scripts wizard :dizzy_face: Thank you for your information!
Have you been able to build statically some other way?
No, haven't tried to build statically yet. I will give it a try when I have some free time.