Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust: release binary #353

Open
2 tasks done
hhstore opened this issue Jun 4, 2022 · 6 comments
Open
2 tasks done

Rust: release binary #353

hhstore opened this issue Jun 4, 2022 · 6 comments

Comments

@hhstore
Copy link
Owner

hhstore commented Jun 4, 2022

related:

@hhstore
Copy link
Owner Author

hhstore commented Jun 4, 2022

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
Copy link
Owner Author

hhstore commented Jun 4, 2022

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

安装命令:

  • This makes Cargo install it to ~/.cargo/bin.
  • 本地安装:
# cd:
cd project-root

# install local build: 
cargo install --path .

# 查看安装列表:
cargo install --list
  • 安装成功 log:

image

image

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

image

@hhstore
Copy link
Owner Author

hhstore commented Jun 4, 2022

Rust 发布二进制包:

  • 发布方式.

发布到 Github Release:

发布到 Crates.io:

发布流程:

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

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

其他方式:

使用 GoReleaser 发布 Rust:

install:

brew install goreleaser/tap/goreleaser

with Github Action:

使用思路:

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

image

@hhstore
Copy link
Owner Author

hhstore commented Jun 4, 2022

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

原因:

fix:

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

@hhstore
Copy link
Owner Author

hhstore commented Jun 4, 2022

Rust 交叉编译:

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

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
Copy link
Owner Author

hhstore commented Jun 6, 2022

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

交叉编译:

  • 基于 Rosetta 模式:
cargo build --target x86_64-apple-darwin

cargo run --target x86_64-apple-darwin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant