blog icon indicating copy to clipboard operation
blog copied to clipboard

Rust: release binary

Open hhstore opened this issue 2 years ago • 6 comments

related:

  • [x] #343
  • [x] #247

hhstore avatar Jun 04 '22 21:06 hhstore

Rust 构建+安装+发布 二进制程序:

  • 日常使用 rust 写一些 cli 命令行小工具. 非常方便.
  • 本篇示例项目: https://github.com/better-rs/learn-rs
    • 相关具体使用方式, 参考该项目 Taskfile 配置

Rust 构建:

优化 Rust 构建二进制文件大小:

  • 参考:
    • min-sized-rust # Cargo.toml
    • https://github.com/johnthagen/min-sized-rust
    • https://rustrepo.com/repo/johnthagen-min-sized-rust
    • https://lifthrasiir.github.io/rustlog/why-is-a-rust-executable-large.html
    • https://stackoverflow.com/questions/29008127/why-are-rust-executables-so-huge/54842093#54842093
  • Add the following to Cargo.toml:

[profile.dev]
# Must always use panic = "abort" to avoid needing to define the unstable eh_personality lang item.
panic = "abort"


[profile.release]
opt-level = "z"     # Optimize for size.
lto = true          # Enable Link Time Optimization
codegen-units = 1   # Reduce number of codegen units to increase optimizations.
panic = "abort"     # Abort on panic
strip = true        # Automatically strip symbols from the binary.


构建命令:

  • https://doc.rust-lang.org/book/ch14-01-release-profiles.html

cargo build --release

基于 Docker 构建:

  • https://github.com/kpcyrd/mini-docker-rust
# cd root:
cd project-root/

# build:
docker build -t mini-docker-rust .

# run:
docker run mini-docker-rust .

hhstore avatar Jun 04 '22 21:06 hhstore

Rust 安装本地构建二进制文件:

  • https://doc.rust-lang.org/cargo/commands/cargo-install.html#examples
  • https://stackoverflow.com/questions/70292749/rust-install-locally-built-executable-systemwide

安装命令:

  • This makes Cargo install it to ~/.cargo/bin.
  • 本地安装:

# cd:
cd project-root

# install local build: 
cargo install --path .

# 查看安装列表:
cargo install --list

  • 安装成功 log:
image image

命令行小工具安装成功示例图:

  • 下图, 对应相关项目: https://github.com/better-rs/learn-rs

image

hhstore avatar Jun 04 '22 21:06 hhstore

Rust 发布二进制包:

  • 发布方式.

发布到 Github Release:

  • 使用 Github Action, 自动发布
  • https://github.com/marketplace/actions/rust-release-binary
  • https://github.com/taiki-e/upload-rust-binary-action

发布到 Crates.io:

  • https://kaisery.github.io/trpl-zh-cn/ch14-02-publishing-to-crates-io.html

发布流程:

  1. 创建 Crates.io 账号
  2. 配置项目参数.
  3. 发布:
# 发布: 
cargo publish

# 撤回发布: (问题包, 禁止新项目引用)
cargo yank --vers 1.0.1

其他方式:

  • https://github.com/liuchong/docker-rustup

使用 GoReleaser 发布 Rust:

  • https://github.com/goreleaser/goreleaser
  • https://jondot.medium.com/shipping-rust-binaries-with-goreleaser-d5aa42a46be0
  • 一种 hack 用法. 非常巧妙. 无痛复用 goreleaser 的功能.

install:

  • https://goreleaser.com/install/
  • 使用方法:
    • https://goreleaser.com/quick-start/
    • https://www.jianshu.com/p/ac18956b0bc0

brew install goreleaser/tap/goreleaser

with Github Action:

  • https://goreleaser.com/ci/actions/
  • https://www.reddit.com/r/rust/comments/p67uiy/show_using_goreleaser_and_gh_actions_to_release/
  • https://github.com/LGUG2Z/komorebi/blob/master/.goreleaser.yml#L7

使用思路:

  • 假装编译 go, 利用 hook, 上传本地已经编译好的 rust binary 文件到 github.
  • 先创建临时 go 文件, 执行 go 编译, 然后触发 hook, 执行后续上传流程.
image

hhstore avatar Jun 04 '22 21:06 hhstore

fix errors:

error 1:

  • build error:
warning: profiles for the non root package will be ignored, specify profiles at the workspace root:
package:   ~better-rs/learn-rs/crates/rs-scripts/Cargo.toml
workspace: ~/better-rs/learn-rs/Cargo.toml

原因:

  • cargo workspace 模式, 只能在 root cargo.toml 文件添加编译配置.
    • 参考: https://doc.rust-lang.org/cargo/reference/profiles.html#overrides
  • https://github.com/rust-lang/cargo/issues/8264

fix:

  • 把 workspace 内子项目 cargo.toml 中的编译配置段, 移动到 workspace 下的 cargo.toml 内.
  • 然后, cd 到子项目中, 执行 cargo build 即可.

hhstore avatar Jun 04 '22 21:06 hhstore

Rust 交叉编译:

  • 推荐基于 Docker 的方式, 进行交叉编译.
  • 简单高效, 且无需关注复杂的环境配置.

1. 基于 Docker 编译:

  • https://www.natarajmb.com/2021/02/cross-compiling-rust-using-docker/
  • https://jakewharton.com/cross-compiling-static-rust-binaries-in-docker-for-raspberry-pi/
  • https://kerkour.com/rust-reproducible-cross-compilation-with-docker

基于 cross + docker:

  • https://github.com/cross-rs/cross

$ docker run -v /var/run/docker.sock:/var/run/docker.sock -v .:/project \
  -w /project my/development-image:tag cross build --target mips64-unknown-linux-gnuabi64



building musl based static linux rust binaries

  • https://github.com/clux/muslrust

docker pull clux/muslrust:stable

# build: 
docker run -v $PWD:/volume --rm -t clux/muslrust:stable cargo build --release

Windows:

  • https://github.com/cross-rs/cross/blob/main/docker/Dockerfile.i686-pc-windows-gnu

Linux:

  • https://github.com/cross-rs/cross/blob/main/docker/Dockerfile.aarch64-unknown-linux-musl

2. 本机编译:

  • https://github.com/cross-rs/cross
  • https://github.com/richfelker/musl-cross-make
  • https://kerkour.com/rust-reproducible-cross-compilation-with-docker
  • https://zhuanlan.zhihu.com/p/343551786
  • https://saekiraku.github.io/article/18577/
  • https://www.jianshu.com/p/6690476d4c3b
  • https://blog.hcl.moe/archives/2630

# install:
cargo install -f cross


# build:
$ cross build --target aarch64-unknown-linux-gnu
$ cross build --target x86_64-pc-windows-gnu
$ cross build --target armv7-unknown-linux-gnueabihf

构建:


# install:
brew install FiloSottile/musl-cross/musl-cross
brew install mingw-w64


rustup toolchain add x86_64-unknown-linux-musl

rustup target add x86_64-unknown-linux-musl
rustup target list

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

cargo build --release --target=x86_64-pc-windows-msvc


hhstore avatar Jun 04 '22 21:06 hhstore

Rust 跨平台:

  • https://doc.rust-lang.org/nightly/rustc/platform-support.html
  • M1 Mac 安装:
-> % rustup target list
aarch64-apple-darwin (installed)
aarch64-apple-ios
aarch64-apple-ios-sim
aarch64-fuchsia
aarch64-linux-android
aarch64-pc-windows-msvc
aarch64-unknown-linux-gnu
aarch64-unknown-linux-musl (installed)
aarch64-unknown-none
aarch64-unknown-none-softfloat


# 添加: 
rustup target add aarch64-unknown-linux-gnu
rustup target add aarch64-unknown-linux-musl

# 模拟 x86_64: 
rustup target add x86_64-apple-darwin


交叉编译:

  • 基于 Rosetta 模式:

cargo build --target x86_64-apple-darwin

cargo run --target x86_64-apple-darwin

hhstore avatar Jun 06 '22 20:06 hhstore