Open hhstore opened 2 years ago
优化 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 .
安装命令:
~/.cargo/bin
.
# cd:
cd project-root
# install local build:
cargo install --path .
# 查看安装列表:
cargo install --list
命令行小工具安装成功示例图:
发布流程:
# 发布:
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:
使用思路:
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:
cargo.toml
中的编译配置段, 移动到 workspace 下的 cargo.toml
内.基于 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
# 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
-> % 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
related: