开发者问题收集

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

2019-01-03
1774

我尝试使用 Docker 交叉编译 Rust 二进制文件,用于 ARM6 架构上的 raspberry-pi。我在构建之前指定了 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 中指定的原始目标?

这是 dockerhub 构建

2个回答
Cargo 未使用

TARGET 。 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

参见:

Stargateur
2019-01-04

我应该编译为与 Raspberry Pi 3 Model B 兼容的 armhv7。

这是一个为 armhv7 编译奇偶校验二进制文件的 Dockerfile:

FROM debian:latest

RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    ca-certificates \
    cmake \
    curl \
    git \
    pkg-config \
    gcc-arm-linux-gnueabihf \
    g++-arm-linux-gnueabihf \
    libc6-dev-armhf-cross \
    && rm -rf \
    /var/lib/apt/lists/*

RUN sed 's/^deb/deb-src/' /etc/apt/sources.list > \
        /etc/apt/sources.list.d/deb-src.list \
    && dpkg --add-architecture armhf \
    && apt-get update \
    && apt-get install -y \
        libssl-dev:armhf \
        libc6-dev:armhf \
    && rm -rf \
      /var/lib/apt/lists/*

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

ENV CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER=arm-linux-gnueabihf-gcc \
    CC_armv7_unknown_linux_gnueabihf="/usr/bin/arm-linux-gnueabihf-gcc" \
    CXX_armv7_unknown_linux_gnueabihf="/usr/bin/arm-linux-gnueabihf-g++" \
    CROSS_COMPILE="1" \
    OPENSSL_INCLUDE_DIR="/usr/include/arm-linux-gnueabihf" \
    OPENSSL_LIB_DIR="/usr/lib/arm-linux-gnueabihf"

RUN /root/.cargo/bin/rustup target add armv7-unknown-linux-gnueabihf

RUN dpkg --add-architecture armhf && \
    apt-get update && \
    apt-get install -y libudev-dev:armhf

RUN git clone https://github.com/paritytech/parity-ethereum.git && cd parity-ethereum && \
    cargo build --target=armv7-unknown-linux-gnueabihf --release


CMD ./parity-ethereum/target/armv7-unknown-linux-gnueabihf/release/parity

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