hhstore / blog

My Tech Blog: about Mojo / Rust / Golang / Python / Kotlin / Flutter / VueJS / Blockchain etc.
https://github.com/hhstore/blog/issues
294 stars 24 forks source link

Rust: release binary #353

Open hhstore opened 2 years ago

hhstore commented 2 years ago

related:

hhstore commented 2 years ago

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

Rust 构建:

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


[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.

构建命令:


cargo build --release

基于 Docker 构建:

# cd root:
cd project-root/

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

# run:
docker run mini-docker-rust .
hhstore commented 2 years ago

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

安装命令:


# cd:
cd project-root

# install local build: 
cargo install --path .

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

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

image

hhstore commented 2 years ago

Rust 发布二进制包:

发布到 Github Release:

发布到 Crates.io:

发布流程:

  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

```ruby

brew install goreleaser/tap/goreleaser

with Github Action:

使用思路:

image
hhstore commented 2 years ago

fix errors:

error 1:

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

原因:

fix:

hhstore commented 2 years ago

Rust 交叉编译:

1. 基于 Docker 编译:

基于 cross + docker:


$ 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


docker pull clux/muslrust:stable

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

Windows:

Linux:

2. 本机编译:


# 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 commented 2 years ago

Rust 跨平台:

-> % 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

交叉编译:


cargo build --target x86_64-apple-darwin

cargo run --target x86_64-apple-darwin