Dav*_*lsh 3 cross-compiling rust
我正在学习 Rust 并编写一些基本的 CLI 工具作为练习。我将应用程序源存储在 Github 中,使用 Github 操作生成二进制文件并通过 Github 版本发布这些二进制文件。
问题是;我不确定如何针对各种目标架构和操作系统交叉编译 Rust 应用程序。
(抱歉进行比较)以前使用 Go 时,我可以在构建命令中指定目标 CPU 架构和目标操作系统,如下所示:
env GOARCH=arm64 GOOS=darwin go build
Run Code Online (Sandbox Code Playgroud)
当我查看 Rust 中是否有等效项时,我看到指示告诉我使用虚拟化和各种其他技术进行交叉编译。
我怀疑我可能不擅长研究,是否有一种等效的简单方法来交叉编译 Rust 应用程序?
如果不是,那是为什么?您能否为我提供一些资源来帮助我学习如何做到这一点?
cross
使这变得非常容易,特别是因为它受actions-rs/cargo
.
我正在使用类似的东西
name: 'Release'
on:
push:
tags:
- 'v*'
env:
CARGO_INCREMENTAL: 0
jobs:
build:
name: Binary
strategy:
fail-fast: false
matrix:
job:
- { target: x86_64-unknown-linux-musl, exe: amd64-linux, os: ubuntu-latest }
- { target: aarch64-unknown-linux-musl, exe: aarch64-linux, os: ubuntu-latest }
- { target: armv7-unknown-linux-musleabi, exe: armv7-linux, os: ubuntu-latest }
- { target: wasm32-wasi, exe: wasi.wasm, os: ubuntu-latest }
- { target: x86_64-apple-darwin, exe: macos, os: macos-latest }
- { target: x86_64-pc-windows-msvc, exe: windows.exe, os: windows-2019 }
runs-on: ${{ matrix.job.os }}
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: 1.62.0
override: true
target: ${{ matrix.job.target }}
components: rust-src # necessary for wasi, because there isn't a cross image for it
- uses: actions-rs/cargo@v1
with:
use-cross: true
args: --release --target=${{ matrix.job.target }} --locked
command: build
- name: Rename result
run: |
rm target/${{ matrix.job.target }}/release/name-of-binary.d
cp target/${{ matrix.job.target }}/release/name-of-binary* name-of-binary-${{ matrix.job.exe }}
- name: Archive production artifacts
uses: actions/upload-artifact@v2
with:
name: arty
path: name-of-binary-${{ matrix.job.exe }}
# Release artifacts in a separate step to make sure all are successfully produced and no partial release is created
Run Code Online (Sandbox Code Playgroud)
我还指定
[profile.release]
lto = "fat"
strip = "debuginfo"
Run Code Online (Sandbox Code Playgroud)
在我的Cargo.toml
使发布的文件更好一点。
可能值得注意的是,Rust 包使得在 C/C++ 库中构建和链接比 Go 更容易,可能调用 CMake 或更糟。交叉编译此类 crate 可能要困难得多,具体如何执行取决于特定的 crate。