如何跨操作系统和CPU架构交叉编译Rust



我正在学习Rust并编写一些基本的CLI工具作为练习。我将我的应用程序源代码存储在Github中,使用Github操作生成二进制文件并通过Github发布这些二进制文件。

问题是;我不确定如何为各种目标体系结构和操作系统交叉编译我的Rust应用程序。

(为比较道歉(以前使用Go时,我可以在构建命令中指定目标CPU架构和目标操作系统,如:

env GOARCH=arm64 GOOS=darwin go build

当我想看看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

在我的一个项目上。([编辑:]具有自动发布功能的改进版(

我还指定

[profile.release]
lto = "fat"
strip = "debuginfo"

在我的Cargo.toml中,以使发布的文件更好一点。

值得注意的是,Rust crates使C/C++库中的构建和链接比Go更容易,可能会调用CMake或更糟。交叉编译这样的板条箱可能会困难得多,如何准确地完成这项工作取决于具体的板条箱。

最新更新