在 Mac M1 上为目标 x86_64 linux 编译 Rust

Compling Rust on Mac M1 for target x86_64 linux

我正在尝试在我的 M1 Mac 上为 x86_64 目标编译我的 Rust 代码 linux。 我使用 Docker 来实现。

我的Docker文件:

FROM rust:latest AS builder

RUN rustup install stable-x86_64-unknown-linux-musl

RUN rustup target add x86_64-unknown-linux-musl
RUN apt -y update
RUN apt install -y musl-tools musl-dev
RUN apt-get install -y build-essential
RUN apt install -y gcc-x86-64-linux-gnu

ADD ./cargo ./cargo/
WORKDIR /cargo

ENV RUSTFLAGS='-C linker=x86_64-linux-gnu-gcc'
ENV CC='gcc'
ENV CC_x86_64_unknown_linux_musl=gcc-x86-64-linux-gnu
ENV CC_x86_64-unknown-linux-musl=gcc-x86-64-linux-gnu

RUN cargo build --target x86_64-unknown-linux-musl --release

但是我会报错

error occurred: Failed to find tool. Is gcc-x86-64-linux-gnu installed?

我认为应该安装它,因为我用 apt install -y gcc-x86-64-linux-gnu.

安装了它

有人知道为什么会这样吗?


完整输出:

docker build --no-cache -t myImage .

[+] Building 171.5s (14/14) FINISHED                                                                                                                                                          
 => [internal] load build definition from Dockerfile                                                                                                                                     0.0s
 => => transferring dockerfile: 1.91kB                                                                                                                                                   0.0s
 => [internal] load .dockerignore                                                                                                                                                        0.0s
 => => transferring context: 2B                                                                                                                                                          0.0s
 => [internal] load metadata for docker.io/library/rust:latest                                                                                                                           1.3s
 => CACHED [ 1/10] FROM docker.io/library/rust:latest@sha256:81af0a1fabc30ffce6ed3d569fc91a27e462c793560f09d10543fa8a040ac77b                                                            0.0s
 => [internal] load build context                                                                                                                                                        0.1s
 => => transferring context: 291.48kB                                                                                                                                                    0.1s
 => [ 2/10] RUN rustup install stable-x86_64-unknown-linux-musl                                                                                                                         32.7s
 => [ 3/10] RUN rustup target add x86_64-unknown-linux-musl                                                                                                                              7.8s 
 => [ 4/10] RUN apt -y update                                                                                                                                                            2.7s 
 => [ 5/10] RUN apt install -y musl-tools musl-dev                                                                                                                                       1.3s 
 => [ 6/10] RUN apt-get install -y build-essential                                                                                                                                       0.8s 
 => [ 7/10] RUN apt install -y gcc-x86-64-linux-gnu                                                                                                                                     24.9s 
 => [ 8/10] ADD ./cargo ./cargo/                                                                                                                                                         1.9s 
 => [ 9/10] WORKDIR /cargo                                                                                                                                                               0.0s
 => ERROR [10/10] RUN cargo build --target x86_64-unknown-linux-musl --release                                                                                                          97.8s
------
 > [10/10] RUN cargo build --target x86_64-unknown-linux-musl --release:
#14 0.646     Updating crates.io index
#14 30.52     Updating git repository `https://github.com/snipsco/snips-nlu-rs`
#14 35.18     Updating git repository `https://github.com/snipsco/crfsuite-rs`
#14 35.76     Updating git repository `https://github.com/snipsco/snips-nlu-ontology`
#14 36.37     Updating git repository `https://github.com/snipsco/snips-nlu-parsers`
#14 36.90     Updating git repository `https://github.com/snipsco/snips-nlu-utils`
#14 37.44     Updating git repository `https://github.com/snipsco/gazetteer-entity-parser`
#14 38.04     Updating git repository `https://github.com/snipsco/rustling-ontology`
#14 39.26     Updating git repository `https://github.com/snipsco/rustling`
#14 39.87  Downloading crates ...
#14 40.35   Downloaded peeking_take_while v0.1.2
#14 40.36   Downloaded quick-error v1.2.3
#14 40.38   Downloaded num-traits v0.2.14
#14 40.39   Downloaded remove_dir_all v0.5.3
#14 40.39   Downloaded unicode-xid v0.2.2
#14 40.40   Downloaded version_check v0.9.3
#14 40.41   Downloaded adler v1.0.2
#14 40.41   Downloaded atty v0.2.14
#14 40.41   Downloaded autocfg v1.0.1
#14 40.41   Downloaded cfg-if v1.0.0
#14 40.42   Downloaded either v1.6.1
#14 40.42   Downloaded fnv v1.0.7
#14 40.43   Downloaded proc-macro2 v1.0.28
#14 40.43   Downloaded rawpointer v0.1.0
#14 40.45   Downloaded regex-automata v0.1.10
#14 40.45   Downloaded shlex v0.1.1
#14 40.46   Downloaded string-interner v0.7.1
#14 40.46   Downloaded termcolor v1.1.2
#14 40.47   Downloaded which v3.1.1
#14 40.49   Downloaded backtrace v0.3.61
#14 40.50   Downloaded base64 v0.10.1
#14 40.53   Downloaded bstr v0.2.16
#14 40.54   Downloaded cc v1.0.69
#14 40.54   Downloaded cexpr v0.3.6
#14 40.55   Downloaded clang-sys v0.28.1
#14 40.56   Downloaded crc32fast v1.2.1
#14 40.61   Downloaded dinghy-build v0.4.62
#14 40.61   Downloaded derive-new v0.5.9
#14 40.61   Downloaded env_logger v0.7.1
#14 40.62   Downloaded failure_derive v0.1.8
#14 40.62   Downloaded error-chain v0.12.4
#14 40.62   Downloaded flate2 v1.0.20
#14 40.63   Downloaded rand_core v0.6.3
#14 40.63   Downloaded quote v1.0.9
#14 40.64   Downloaded bindgen v0.52.0
#14 40.67   Downloaded regex-syntax v0.6.25
#14 40.68   Downloaded bitflags v1.3.2
#14 40.69   Downloaded aho-corasick v0.7.18
#14 40.69   Downloaded getrandom v0.2.3
#14 40.70   Downloaded cfg-if v0.1.10
#14 40.70   Downloaded byteorder v1.4.3
#14 40.72   Downloaded clap v2.33.3
#14 40.73   Downloaded lazy_static v1.4.0
#14 40.73   Downloaded itoa v0.4.8
#14 40.73   Downloaded lru-cache v0.1.2
#14 40.74   Downloaded num-integer v0.1.44
#14 40.75   Downloaded tinyvec_macros v0.1.0
#14 40.80   Downloaded syn v1.0.75
#14 40.81   Downloaded addr2line v0.16.0
#14 40.81   Downloaded thiserror-impl v1.0.26
#14 40.82   Downloaded chrono v0.4.8
#14 40.83   Downloaded enum_primitive v0.1.1
#14 40.83   Downloaded csv-core v0.1.10
#14 40.83   Downloaded failure v0.1.8
#14 40.83   Downloaded glob v0.3.0
#14 40.84   Downloaded humantime v1.3.0
#14 40.85   Downloaded itertools v0.7.11
#14 40.86   Downloaded itertools v0.8.2
#14 40.90   Downloaded libloading v0.5.2
#14 40.90   Downloaded matrixmultiply v0.1.15
#14 40.90   Downloaded maybe-uninit v2.0.0
#14 40.93   Downloaded ndarray v0.12.1
#14 40.95   Downloaded nom v4.2.3
#14 40.95   Downloaded num-complex v0.2.4
#14 40.95   Downloaded rmp-serde v0.13.7
#14 40.95   Downloaded rmp-serde v0.14.4
#14 40.96   Downloaded rustc-hash v1.1.0
#14 40.96   Downloaded rustc-demangle v0.1.20
#14 40.97   Downloaded serde v1.0.129
#14 40.98   Downloaded serde_derive v1.0.129
#14 41.00   Downloaded serde_json v1.0.66
#14 41.01   Downloaded time v0.1.44
#14 41.01   Downloaded thiserror v1.0.26
#14 41.02   Downloaded tinyvec v1.3.1
#14 41.02   Downloaded version_check v0.1.5
#14 41.02   Downloaded smallvec v0.6.14
#14 41.02   Downloaded zip v0.5.13
#14 41.03   Downloaded log v0.4.14
#14 41.03   Downloaded linked-hash-map v0.5.4
#14 41.03   Downloaded memchr v2.4.1
#14 41.04   Downloaded tempfile v3.2.0
#14 41.04   Downloaded rand_chacha v0.3.1
#14 41.04   Downloaded miniz_oxide v0.4.4
#14 41.04   Downloaded rand v0.8.4
#14 41.05   Downloaded ryu v1.0.5
#14 41.06   Downloaded unicode-normalization v0.1.19
#14 41.08   Downloaded regex v1.5.4
#14 41.09   Downloaded vec_map v0.8.2
#14 41.09   Downloaded textwrap v0.11.0
#14 41.09   Downloaded strsim v0.8.0
#14 41.12   Downloaded num-traits v0.1.43
#14 41.13   Downloaded lazycell v1.3.0
#14 41.16   Downloaded object v0.26.1
#14 41.17   Downloaded unicode-width v0.1.8
#14 41.17   Downloaded synstructure v0.12.5
#14 41.17   Downloaded ppv-lite86 v0.2.10
#14 41.17   Downloaded rmp v0.8.10
#14 41.17   Downloaded ansi_term v0.11.0
#14 41.32   Downloaded libc v0.2.100
#14 41.35   Downloaded gimli v0.25.0
#14 41.36   Downloaded csv v1.1.6
#14 41.46    Compiling autocfg v1.0.1
#14 41.46    Compiling libc v0.2.100
#14 41.46    Compiling proc-macro2 v1.0.28
#14 41.46    Compiling unicode-xid v0.2.2
#14 41.46    Compiling memchr v2.4.1
#14 41.69    Compiling syn v1.0.75
#14 41.97    Compiling cc v1.0.69
#14 41.97    Compiling serde_derive v1.0.129
#14 41.98    Compiling serde v1.0.129
#14 41.98    Compiling failure_derive v0.1.8
#14 42.10    Compiling log v0.4.14
#14 42.36    Compiling maybe-uninit v2.0.0
#14 42.37    Compiling cfg-if v1.0.0
#14 42.39    Compiling adler v1.0.2
#14 42.49    Compiling regex-syntax v0.6.25
#14 42.52    Compiling gimli v0.25.0
#14 42.79    Compiling rustc-demangle v0.1.20
#14 43.33    Compiling vec_map v0.8.2
#14 44.65    Compiling fnv v1.0.7
#14 45.23    Compiling version_check v0.1.5
#14 45.70    Compiling glob v0.3.0
#14 46.45    Compiling byteorder v1.4.3
#14 46.57    Compiling version_check v0.9.3
#14 46.73    Compiling quick-error v1.2.3
#14 46.90    Compiling unicode-width v0.1.8
#14 46.98    Compiling ryu v1.0.5
#14 47.09    Compiling strsim v0.8.0
#14 47.24    Compiling bitflags v1.3.2
#14 47.40    Compiling ansi_term v0.11.0
#14 47.62    Compiling termcolor v1.1.2
#14 47.84    Compiling bindgen v0.52.0
#14 47.93    Compiling lazycell v1.3.0
#14 48.01    Compiling lazy_static v1.4.0
#14 48.12    Compiling cfg-if v0.1.10
#14 48.20    Compiling itoa v0.4.8
#14 48.24    Compiling peeking_take_while v0.1.2
#14 48.27    Compiling either v1.6.1
#14 48.40    Compiling rustc-hash v1.1.0
#14 48.40    Compiling serde_json v1.0.66
#14 48.46    Compiling shlex v0.1.1
#14 48.59    Compiling crc32fast v1.2.1
#14 48.69    Compiling tinyvec_macros v0.1.0
#14 48.77    Compiling matrixmultiply v0.1.15
#14 48.83    Compiling ppv-lite86 v0.2.10
#14 48.99    Compiling rawpointer v0.1.0
#14 49.12    Compiling ndarray v0.12.1
#14 49.18    Compiling regex-automata v0.1.10
#14 49.24    Compiling remove_dir_all v0.5.3
#14 49.38    Compiling linked-hash-map v0.5.4
#14 49.47    Compiling rmv v0.1.0 (/cargo)
#14 49.71    Compiling miniz_oxide v0.4.4
#14 49.77    Compiling num-traits v0.2.14
#14 49.81    Compiling num-integer v0.1.44
#14 49.82    Compiling num-complex v0.2.4
#14 50.34    Compiling backtrace v0.3.61
#14 50.38    Compiling libloading v0.5.2
#14 50.42    Compiling addr2line v0.16.0
#14 50.43    Compiling nom v4.2.3
#14 50.86    Compiling clang-sys v0.28.1
#14 50.88    Compiling base64 v0.10.1
#14 51.16    Compiling humantime v1.3.0
#14 51.31    Compiling textwrap v0.11.0
#14 51.41    Compiling error-chain v0.12.4
#14 51.72    Compiling itertools v0.8.2
#14 51.78    Compiling itertools v0.7.11
#14 51.80    Compiling tinyvec v1.3.1
#14 52.65    Compiling lru-cache v0.1.2
#14 55.95    Compiling time v0.1.44
#14 56.34    Compiling atty v0.2.14
#14 56.40    Compiling which v3.1.1
#14 56.51    Compiling aho-corasick v0.7.18
#14 56.71    Compiling object v0.26.1
#14 56.79    Compiling unicode-normalization v0.1.19
#14 58.64    Compiling csv-core v0.1.10
#14 60.94    Compiling quote v1.0.9
#14 61.57    Compiling getrandom v0.2.3
#14 63.18    Compiling smallvec v0.6.14
#14 64.21    Compiling clap v2.33.3
#14 64.28    Compiling regex v1.5.4
#14 64.45    Compiling flate2 v1.0.20
#14 67.89    Compiling rand_core v0.6.3
#14 70.23    Compiling num-traits v0.1.43
#14 70.46    Compiling rmp v0.8.10
#14 72.36    Compiling dinghy-build v0.4.62
#14 73.89    Compiling cexpr v0.3.6
#14 74.08    Compiling env_logger v0.7.1
#14 74.86    Compiling rand_chacha v0.3.1
#14 75.67    Compiling synstructure v0.12.5
#14 76.51    Compiling enum_primitive v0.1.1
#14 76.67    Compiling chrono v0.4.8
#14 78.40    Compiling derive-new v0.5.9
#14 79.85    Compiling thiserror-impl v1.0.26
#14 81.11    Compiling rand v0.8.4
#14 82.99    Compiling thiserror v1.0.26
#14 83.21    Compiling tempfile v3.2.0
#14 83.56    Compiling zip v0.5.13
#14 86.17    Compiling failure v0.1.8
#14 87.47    Compiling rustling-ontology-moment v0.19.3 (https://github.com/snipsco/rustling-ontology?tag=0.19.3#3bb1313d)
#14 88.77    Compiling snips-nlu-utils v0.9.1 (https://github.com/snipsco/snips-nlu-utils?tag=0.9.1#b6329344)
#14 89.66    Compiling string-interner v0.7.1
#14 89.75    Compiling rustling-ml v0.9.1 (https://github.com/snipsco/rustling?tag=0.9.1#758074a2)
#14 90.01    Compiling rmp-serde v0.13.7
#14 90.17    Compiling rmp-serde v0.14.4
#14 90.26    Compiling bstr v0.2.16
#14 91.55    Compiling crfsuite-sys v0.3.3 (https://github.com/snipsco/crfsuite-rs?tag=0.3.3#df9bfefe)
#14 91.91    Compiling rustling-core v0.9.1 (https://github.com/snipsco/rustling?tag=0.9.1#758074a2)
#14 92.20    Compiling csv v1.1.6
#14 93.53    Compiling gazetteer-entity-parser v0.8.0 (https://github.com/snipsco/gazetteer-entity-parser?tag=0.8.0#0e117818)
#14 93.55    Compiling snips-nlu-ontology v0.67.1 (https://github.com/snipsco/snips-nlu-ontology?tag=0.67.1#194f99d8)
#14 93.90    Compiling rustling v0.9.1 (https://github.com/snipsco/rustling?tag=0.9.1#758074a2)
#14 94.43 error: failed to run custom build command for `crfsuite-sys v0.3.3 (https://github.com/snipsco/crfsuite-rs?tag=0.3.3#df9bfefe)`
#14 94.44 
#14 94.44 Caused by:
#14 94.44   process didn't exit successfully: `/cargo/target/release/build/crfsuite-sys-79c89f1db9b53beb/build-script-build` (exit status: 1)
#14 94.44   --- stdout
#14 94.44   TARGET = Some("x86_64-unknown-linux-musl")
#14 94.44   OPT_LEVEL = Some("3")
#14 94.44   HOST = Some("aarch64-unknown-linux-gnu")
#14 94.44   CC_x86_64-unknown-linux-musl = None
#14 94.44   CC_x86_64_unknown_linux_musl = Some("gcc-x86-64-linux-gnu")
#14 94.44   CFLAGS_x86_64-unknown-linux-musl = None
#14 94.44   CFLAGS_x86_64_unknown_linux_musl = None
#14 94.44   TARGET_CFLAGS = None
#14 94.44   CFLAGS = None
#14 94.44   CRATE_CC_NO_DEFAULTS = None
#14 94.44   DEBUG = Some("false")
#14 94.44   CARGO_CFG_TARGET_FEATURE = Some("fxsr,sse,sse2")
#14 94.44   CC_x86_64-unknown-linux-musl = None
#14 94.44   CC_x86_64_unknown_linux_musl = Some("gcc-x86-64-linux-gnu")
#14 94.44   CFLAGS_x86_64-unknown-linux-musl = None
#14 94.44   CFLAGS_x86_64_unknown_linux_musl = None
#14 94.44   TARGET_CFLAGS = None
#14 94.44   CFLAGS = None
#14 94.44   CRATE_CC_NO_DEFAULTS = None
#14 94.44   CARGO_CFG_TARGET_FEATURE = Some("fxsr,sse,sse2")
#14 94.44   running: "gcc-x86-64-linux-gnu" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-m64" "-I" "c/include" "-Wall" "-Wextra" "-o" "/cargo/target/x86_64-unknown-linux-musl/release/build/crfsuite-sys-ac90263cdd6e14ed/out/c/lbfgs/lbfgs.o" "-c" "c/lbfgs/lbfgs.c"
#14 94.44 
#14 94.44   --- stderr
#14 94.44 
#14 94.44 
#14 94.44   error occurred: Failed to find tool. Is `gcc-x86-64-linux-gnu` installed?
#14 94.44 
#14 94.44 
#14 94.44 warning: build failed, waiting for other jobs to finish...
#14 97.65 error: build failed 

看起来可执行文件实际上被命名为 x86_64-linux-gnu-gcc,参见 https://packages.debian.org/bullseye/arm64/gcc-x86-64-linux-gnu/filelist

如果你想从 macOS M1 交叉编译到任何 OS 使用这个。

https://github.com/messense/homebrew-macos-cross-toolchains

这解决了我所有的交叉编译问题。

现在我的 Dockerfile 看起来像:

FROM scratch
COPY target/x86_64-unknown-linux-musl/release/myapp /myapp
ENTRYPOINT ["/myapp"]