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

ih99xse1  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(146)

我正在学习Rust并编写一些基本的CLI工具作为练习。我将我的应用程序源代码存储在Github中,使用Github操作生成二进制文件并通过Github发布发布这些二进制文件。
问题是;我不确定如何针对各种目标架构和操作系统交叉编译Rust应用程序。
(为比较道歉)以前使用Go时,我可以在build命令中指定目标CPU架构和目标操作系统,如:

env GOARCH=arm64 GOOS=darwin go build

当我想看看Rust中是否有等价的东西时,我看到了一些说明,告诉我使用虚拟化和其他各种技术来交叉编译。
我怀疑我可能只是不擅长研究,有没有一种等价的简单方法来交叉编译Rust应用程序?
如果没有,为什么会这样,你能给我指一些资源来帮助我学习如何做到这一点吗?

qv7cva1a

qv7cva1a1#

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

在一个my project上。([编辑:] Improved version with autopublishing)
我还特别指出

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

在我的Cargo.toml使发布的文件更好一点。
可能值得注意的是,Rust crates使得在C/C++库中构建和链接比Go更容易,可能会调用CMake或更糟。交叉编译这样的crate可能要困难得多,具体如何做取决于具体的crate。

7bsow1i6

7bsow1i62#

对于一次性测试,或者在https://godbolt.org/上运行,可以使用--target=,类似于clang -target
例如,Godbolt使用**-O --target=aarch64-apple-darwin而不是默认的x86_64-unknown-linux-gnu。(我猜x86_64有第四个组件,就像x86一样,对于Windows可以是pc,对于非Windows可以是unknown
Godbolt的“compile to binary”选项在“output”中没有给出输出,可能是因为它仍然使用x86-64 objdump而不是llvm-objdump,后者可以自动检测目标文件的体系结构。
如果你想测试不同架构的内联asm如何编译,这甚至可以用在use std::arch::asm;上。
要查看可用的目标,请执行
--print target-list**。(你可以在Godbolt上这样做,只要看看编译器的输出窗格。

$ rustc  --print target-list
aarch64-apple-darwin
aarch64-apple-ios
...
riscv64gc-unknown-linux-gnu
riscv64gc-unknown-linux-musl
...
x86_64-pc-windows-gnu
x86_64-pc-windows-gnullvm
x86_64-pc-windows-msvc
...
x86_64-unknown-linux-gnu
...
x86_64h-apple-darwin

相关问题