grpc-rs icon indicating copy to clipboard operation
grpc-rs copied to clipboard

How to compile with x86_64-unknown-linux-musl

Open haozileung opened this issue 7 years ago • 6 comments

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'

haozileung avatar Oct 20 '18 15:10 haozileung

Reference: https://stackoverflow.com/q/41856536/7083401

ice1000 avatar Oct 20 '18 18:10 ice1000

I managed to build it on musl at https://github.com/pingcap/grpc-rs/issues/233

overvenus avatar Oct 22 '18 04:10 overvenus

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:

  1. build+install the musl toolchain from https://github.com/GregorR/musl-cross
  2. setup the scripts build_musl.sh and custom_link.sh sourced below.
  3. 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[@]}"

dwerner avatar Oct 23 '18 22:10 dwerner

Oh scripts wizard :dizzy_face: Thank you for your information!

overvenus avatar Oct 25 '18 03:10 overvenus

Have you been able to build statically some other way?

dwerner avatar Oct 25 '18 14:10 dwerner

No, haven't tried to build statically yet. I will give it a try when I have some free time.

overvenus avatar Oct 25 '18 15:10 overvenus