gpt4 book ai didi

docker - 为什么在 Dockerfile 中编译 Rust 程序时会重置 TARGET 环境变量?

转载 作者:行者123 更新时间:2023-11-29 08:13:44 25 4
gpt4 key购买 nike

我正在尝试使用 Docker 为 ARM6 架构上的 raspberry-pi 交叉编译 Rust 二进制文件。我在构建之前为 Cargo 指定了环境参数,但它在特定库上出错,提示未找到“-m64”参数。

错误发生时的环境参数与我在Dockerfile中设置的不同;为什么要重置它们?

FROM ubuntu:latest

RUN apt-get update && \
apt-get install \
--yes \
binutils \
build-essential \
curl \
git \
wget \
libudev-dev \
zip

ARG RASPBERRY_PI_TOOLS_COMMIT_ID=5caa7046982f0539cf5380f94da04b31129ed521
ENV CC=arm-linux-gnueabihf-gcc
ENV TARGET=arm-unknown-linux-gnueabihf
ENV CARGO_CFG_TARGET_ARCH=arm
ENV CARGO_CFG_TARGET_ENDIAN=little
ENV CARGO_CFG_TARGET_ENV=gnu
ENV CARGO_CFG_TARGET_FAMILY=unix
ENV CARGO_CFG_TARGET_OS=linux
ENV CARGO_CFG_TARGET_POINTER_WIDTH=32
ENV CARGO_FEATURE_DEFAULT=1
ENV CARGO_FEATURE_DEV_URANDOM_FALLBACK=1
ENV CARGO_FEATURE_RSA_SIGNING=1
ENV CARGO_FEATURE_USE_HEAP=1
ENV LD=/usr/bin/arm-linux-gnueabihf-ld
ENV LD_LIBRARY_PATH=/src/parity/target/release/deps:/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib:/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib
ENV QEMU_LD_PREFIX=/usr/arm-linux-gnueabihf/libc
RUN wget https://github.com/raspberrypi/tools/archive/$RASPBERRY_PI_TOOLS_COMMIT_ID.zip -O /root/pi-tools.zip
RUN unzip /root/pi-tools.zip -d /root
RUN mv /root/tools-$RASPBERRY_PI_TOOLS_COMMIT_ID /root/pi-tools
ENV PATH=/root/pi-tools/arm-bcm2708/arm-linux-gnueabihf/bin:$PATH
ENV PATH=/root/pi-tools/arm-bcm2708/arm-linux-gnueabihf/libexec/gcc/arm-linux-gnueabihf/4.8.3:$PATH

# Install Rust.
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y --verbose
ENV PATH=/root/.cargo/bin:$PATH

# Install the arm target for Rust.
RUN rustup target add arm-unknown-linux-gnueabihf
# Configure the linker for the arm target.
ENV PKG_CONFIG_ALLOW_CROSS=1
RUN echo '[target.arm-unknown-linux-gnueabihf]\nlinker = "arm-linux-gnueabihf-gcc"' >> /root/.cargo/config

ENV USER=root
RUN cargo new /src
WORKDIR /src
RUN git clone https://github.com/paritytech/parity && cd parity && \
/root/.cargo/bin/rustup target add arm-unknown-linux-gnueabihf && \
/root/.cargo/bin/cargo build --target=arm-unknown-linux-gnueabihf --release --features final

# Verify that the output file is for armv6
RUN readelf --arch-specific ./target/arm-unknown-linux-gnueabihf/debug/src

RUN file /build/parity/target/armv7-unknown-linux-gnueabihf/release/parity

EXPOSE 8080 8545 8180
ENTRYPOINT ["/build/parity/target/armv7-unknown-linux-gnueabihf/release/parity"]

错误是:

process didn't exit successfully: `/src/parity/target/release/build/backtrace-sys-3c9a41016b6512eb/build-script-build` (exit code: 101)
--- stdout
TARGET = Some("x86_64-unknown-linux-gnu")
OPT_LEVEL = Some("3")
HOST = Some("x86_64-unknown-linux-gnu")
CC_x86_64-unknown-linux-gnu = None
CC_x86_64_unknown_linux_gnu = None
HOST_CC = None
CC = Some("arm-linux-gnueabihf-gcc")
CFLAGS_x86_64-unknown-linux-gnu = None
CFLAGS_x86_64_unknown_linux_gnu = None
HOST_CFLAGS = None
CFLAGS = None
DEBUG = Some("false")
running: "arm-linux-gnueabihf-gcc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-m64" "-I" "src/libbacktrace" "-I" "/src/parity/target/release/build/backtrace-sys-5fc2d3da7fb5f8d4/out" "-fvisibility=hidden" "-DBACKTRACE_ELF_SIZE=64" "-DBACKTRACE_SUPPORTED=1" "-DBACKTRACE_USES_MALLOC=1" "-DBACKTRACE_SUPPORTS_THREADS=0" "-DBACKTRACE_SUPPORTS_DATA=0" "-DHAVE_DL_ITERATE_PHDR=1" "-D_GNU_SOURCE=1" "-D_LARGE_FILES=1" "-Dbacktrace_full=__rbt_backtrace_full" "-Dbacktrace_dwarf_add=__rbt_backtrace_dwarf_add" "-Dbacktrace_initialize=__rbt_backtrace_initialize" "-Dbacktrace_pcinfo=__rbt_backtrace_pcinfo" "-Dbacktrace_syminfo=__rbt_backtrace_syminfo" "-Dbacktrace_get_view=__rbt_backtrace_get_view" "-Dbacktrace_release_view=__rbt_backtrace_release_view" "-Dbacktrace_alloc=__rbt_backtrace_alloc" "-Dbacktrace_free=__rbt_backtrace_free" "-Dbacktrace_vector_finish=__rbt_backtrace_vector_finish" "-Dbacktrace_vector_grow=__rbt_backtrace_vector_grow" "-Dbacktrace_vector_release=__rbt_backtrace_vector_release" "-Dbacktrace_close=__rbt_backtrace_close" "-Dbacktrace_open=__rbt_backtrace_open" "-Dbacktrace_print=__rbt_backtrace_print" "-Dbacktrace_simple=__rbt_backtrace_simple" "-Dbacktrace_qsort=__rbt_backtrace_qsort" "-Dbacktrace_create_state=__rbt_backtrace_create_state" "-Dbacktrace_uncompress_zdebug=__rbt_backtrace_uncompress_zdebug" "-o" "/src/parity/target/release/build/backtrace-sys-5fc2d3da7fb5f8d4/out/src/libbacktrace/alloc.o" "-c" "src/libbacktrace/alloc.c"
cargo:warning=arm-linux-gnueabihf-gcc: error: unrecognized command line option '-m64'
exit code: 1
--- stderr
thread 'main

我已经在 Dockerfile 中设置了我的 ENV TARGET=arm-unknown-linux-gnueabihf,但是当错误发生并且它提示不理解什么 -m64是,它正在针对 TARGET = Some("x86_64-unknown-linux-gnu") 进行编译,这不是我指定的目标。

如何使用我在 Dockerfile 中指定的原始目标进行编译?

This is the dockerhub build

最佳答案

TARGET不被 Cargo 使用。 Cargo 可以通过环境变量配置如下:

Cargo can also be configured through environment variables in addition to the TOML syntax above. For each configuration key above of the form foo.bar the environment variable CARGO_FOO_BAR can also be used to define the value. For example the build.jobs key can also be defined by CARGO_BUILD_JOBS.

Source

您对 Cargo 为您的构建脚本设置的环境变量感到困惑:

Cargo sets several environment variables when build scripts are run. Because these variables are not yet set when the build script is compiled, the above example using env! won't work and instead you'll need to retrieve the values when the build script is run:

TARGET - the target triple that is being compiled for. Native code should be compiled for this triple. Some more information about target triples can be found in clang’s own documentation.

Source

要修复您的 docker 脚本,请执行以下操作:

ENV CARGO_BUILD_TARGET=arm-unknown-linux-gnueabihf

参见:

关于docker - 为什么在 Dockerfile 中编译 Rust 程序时会重置 TARGET 环境变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54029117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com