Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

docker - Trouble compiling rust crates that include C libraries for aarch64

I am trying to compile a rust app in an aarch64 linux docker container to run on raspberry pi 4. I have things working except when a package is not pure rust and utilizes c libraries. The ring crate is an example of this, when I try to build the ring library without setting ENV TARGET_CC=something it fails with the following error:

#18 32.35    Compiling ring v0.16.19
#18 38.13 error: failed to run custom build command for `ring v0.16.19`
#18 38.13 
#18 38.13 Caused by:
#18 38.13   process didn't exit successfully: `/usr/src/content-manager/target/release/build/ring-902dd3bf18c6ec17/build-script-build` (exit code: 101)
#18 38.13   --- stdout
#18 38.13   OPT_LEVEL = Some("3")
#18 38.13   TARGET = Some("aarch64-unknown-linux-musl")
#18 38.13   HOST = Some("aarch64-unknown-linux-gnu")
#18 38.13   CC_aarch64-unknown-linux-musl = None
#18 38.13   CC_aarch64_unknown_linux_musl = None
#18 38.13   TARGET_CC = Some("aarch64-linux-musl-gcc")
#18 38.13   CFLAGS_aarch64-unknown-linux-musl = None
#18 38.13   CFLAGS_aarch64_unknown_linux_musl = None
#18 38.13   TARGET_CFLAGS = None
#18 38.13   CFLAGS = None
#18 38.13   CRATE_CC_NO_DEFAULTS = None
#18 38.13   DEBUG = Some("false")
#18 38.13   CARGO_CFG_TARGET_FEATURE = None
#18 38.13 
#18 38.13   --- stderr
#18 38.13   running "aarch64-linux-musl-gcc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-I" "include" "-Wall" "-Wextra" "-pedantic" "-pedantic-errors" "-Wall" "-Wextra" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wenum-compare" "-Wfloat-equal" "-Wformat=2" "-Winline" "-Winvalid-pch" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wundef" "-Wuninitialized" "-Wwrite-strings" "-fno-strict-aliasing" "-fvisibility=hidden" "-fstack-protector" "-g3" "-U_FORTIFY_SOURCE" "-DNDEBUG" "-c" "-o/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/aesv8-armx-linux64.o" "/usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.19/pregenerated/aesv8-armx-linux64.S"
#18 38.13   thread 'main' panicked at 'execution failed', /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.19/build.rs:673:9
#18 38.13   note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

In the ring build docs it mentions the need to set TARGET_CC and TARGET_AR, when I set TARGET_CC to aarch64-Linux-gnu-gcc I get the following error

#18 62.62   process didn't exit successfully: `/usr/src/content-manager/target/release/build/ring-902dd3bf18c6ec17/build-script-build` (exit code: 1)
#18 62.62   --- stdout
#18 62.62   OPT_LEVEL = Some("3")
#18 62.62   TARGET = Some("aarch64-unknown-linux-musl")
#18 62.62   HOST = Some("aarch64-unknown-linux-gnu")
#18 62.62   CC_aarch64-unknown-linux-musl = None
#18 62.62   CC_aarch64_unknown_linux_musl = None
...
#18 62.62   CRATE_CC_NO_DEFAULTS = None
#18 62.62   DEBUG = Some("false")
#18 62.62   CARGO_CFG_TARGET_FEATURE = None
#18 62.62   running: "aarch64-linux-musl-ar" "cq" "/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/libring-core.a" "/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/aesv8-armx-linux64.o" "/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/ghashv8-armx-linux64.o" "/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/vpaes-armv8-linux64.o" "/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/armv8-mont-linux64.o" "/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/chacha-armv8-linux64.o" "/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/ecp_nistz256-armv8-linux64.o" "/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/ghash-neon-armv8-linux64.o" "/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/sha512-armv8-linux64.o" "/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/sha256-armv8-linux64.o" "/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/aes_nohw.o" "/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/montgomery.o" "/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/montgomery_inv.o" "/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/limbs.o" "/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/mem.o" "/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/poly1305.o" "/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/crypto.o" "/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/curve25519.o" "/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/ecp_nistz.o" "/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/ecp_nistz256.o" "/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/gfp_p256.o" "/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/gfp_p384.o"
#18 62.62   exit code: 127
#18 62.62 
#18 62.62   --- stderr
#18 62.62   running "aarch64-linux-gnu-gcc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-I" "include" "-Wall" "-Wextra" "-pedantic" "-pedantic-errors" "-Wall" "-Wextra" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wenum-compare" "-Wfloat-equal" "-Wformat=2" "-Winline" "-Winvalid-pch" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wundef" "-Wuninitialized" "-Wwrite-strings" "-fno-strict-aliasing" "-fvisibility=hidden" "-fstack-protector" "-g3" "-U_FORTIFY_SOURCE" "-DNDEBUG" "-c" "-o/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/aesv8-armx-linux64.o" "/usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.19/pregenerated/aesv8-armx-linux64.S"
#18 62.62   running "aarch64-linux-gnu-gcc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-I" "include" "-Wall" "-Wextra" "-pedantic" "-pedantic-errors" "-Wall" "-Wextra" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wenum-compare" "-Wfloat-equal" "-Wformat=2" "-Winline" "-Winvalid-pch" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wundef" "-Wuninitialized" "-Wwrite-strings" "-fno-strict-aliasing" "-fvisibility=hidden" "-fstack-protector" "-g3" "-U_FORTIFY_SOURCE" "-DNDEBUG" "-c" "-o/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/ghashv8-armx-linux64.o" "/usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.19/pregenerated/ghashv8-armx-linux64.S"
#18 62.62   running "aarch64-linux-gnu-gcc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-I" "include" "-Wall" "-Wextra" "-pedantic" "-pedantic-errors" "-Wall" "-Wextra" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wenum-compare" "-Wfloat-equal" "-Wformat=2" "-Winline" "-Winvalid-pch" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wundef" "-Wuninitialized" "-Wwrite-strings" "-fno-strict-aliasing" "-fvisibility=hidden" "-fstack-protector" "-g3" "-U_FORTIFY_SOURCE" "-DNDEBUG" "-c" "-o/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/vpaes-armv8-linux64.o" "/usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.19/pregenerated/vpaes-armv8-linux64.S"
#18 62.62   running "aarch64-linux-gnu-gcc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-I" "include" "-Wall" "-Wextra" "-pedantic" "-pedantic-errors" "-Wall" "-Wextra" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wenum-compare" "-Wfloat-equal" "-Wformat=2" "-Winline" "-Winvalid-pch" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wundef" "-Wuninitialized" "-Wwrite-strings" "-fno-strict-aliasing" "-fvisibility=hidden" "-fstack-protector" "-g3" "-U_FORTIFY_SOURCE" "-DNDEBUG" "-c" "-o/usr/src/content-manager/target/aarch64-unknown-linux-musl/release/build/ring-ce2ec03868462ce3/out/armv8-mont-linux64.o" "/usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.19/pregenerated/armv8-mont-linux64.S"
#18 62.62   running "aarch64-linux-gnu-gcc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-I" "include" "-Wall" "-Wextra" "-pedantic" "-pedantic-errors" "-Wall" "-Wextra" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wenum-compare" "-Wfloat-equal" "-Wformat=2" "-Winline" "-Winvalid-pch" "-

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I was able to get it working after copying the env vars in this file mentioned here

My updated Dockerfile looks like this

FROM rust as builder
ARG APP_NAME="app"
ARG TARGET="aarch64-unknown-linux-musl"
ARG GITHUB_SSH_KEY=""
RUN apt-get update
RUN apt-get install clang llvm -y
RUN rustup target add $TARGET
RUN mkdir /usr/src/$APP_NAME
WORKDIR /usr/src/$APP_NAME

ENV CARGO_NET_GIT_FETCH_WITH_CLI=true

ENV CC_aarch64_unknown_linux_musl=clang
ENV AR_aarch64_unknown_linux_musl=llvm-ar
ENV CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_RUSTFLAGS="-Clink-self-contained=yes -Clinker=rust-lld"
ENV CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_RUNNER="qemu-aarch64 -L /usr/aarch64-linux-gnu"

COPY Cargo.toml Cargo.lock ./
COPY ./src ./src

RUN mkdir /root/.ssh/
RUN echo "$GITHUB_SSH_KEY" > /root/.ssh/id_rsa;
RUN chmod 400 /root/.ssh/id_rsa
RUN ssh-keyscan -H github.com >> /etc/ssh/ssh_known_hosts

RUN cargo build --release --target=$TARGET
RUN groupadd -g 10001 -r $APP_NAME
RUN useradd -r -g $APP_NAME -u 10001 $APP_NAME

# ------------------------------------------------------------------------------

FROM scratch
ARG APP_NAME="app"
ARG TARGET="aarch64-unknown-linux-musl"
WORKDIR /user/local/bin/
COPY --from=0 /etc/passwd /etc/passwd
COPY --from=builder /usr/src/$APP_NAME/target/$TARGET/release/$APP_NAME ./app
USER $APP_NAME

CMD ["./app"]

With this set up the binary compiles and runs from the scratch container, thanks @HHK for pointing me to the open issues on this topic


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...