2020-12-24 (一)Rust 入坑安装、修改国内源、使用musl、优化二进制文件等

为以往项目开发 http api ,准备使用rust开发尝个鲜,看是否有应用到其他项目中的可能性,本系列内容均为一步一步入坑的实况,未来有时间可能整理的更为系统性,目前仅作记录。

目标

  • 仅需提供 http api
  • 需运行于各个版本 linux 系统上,有余力可支持windows(以往项目使用java开发,客户服务器版本复杂)
  • 数据库采用mysql(项目要和以往项目共同使用数据库)
  • 支持原有加解密、摘要、编码等方式(原因同上)

1、安装 Rust

curl https://sh.rustup.rs -sSf | sh

安装成功

2、rustup、cargo 换国内源加速

echo "export RUSTUP_DIST_SERVER=https://mirrors.sjtug.sjtu.edu.cn/rust-static" >> ~/.bashrc
echo "export RUSTUP_UPDATE_ROOT=https://mirrors.sjtug.sjtu.edu.cn/rust-static/rustup" >> ~/.bashrc
source ~/.bashrc

cat $HOME/.cargo/config
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
# 指定镜像
replace-with = '镜像源名' # 如:tuna、sjtu、ustc,或者 rustcc

# 注:以下源配置一个即可,无需全部
# 中国科学技术大学
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"

# 上海交通大学
[source.sjtu]
registry = "https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index"

# 清华大学
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"

# rustcc社区
[source.rustcc]
registry = "https://code.aliyun.com/rustcc/crates.io-index.git"

3、安装编译依赖、musl 交叉编译工具链、musl 编译器

由于需要支持各个发行版 linux,目前的想法是采用 musl 平台编译100%静态链接的可执行文件,尽量单文件发布。

sudo apt install build-essential
sudo apt install musl-tools

rustup target add x86_64-unknown-linux-musl

4、创建工作空间目录

mkdir rust_projects
cd rust_projects
cargo new workspace

5、修改工作空间配置

cd workspace
cat Cargo.toml
[workspace]
members = [
"demo"
]

mkdir .cargo
cat .cargo/config
[build]
target = "x86_64-unknown-linux-musl"

7、创建 demo 项目

cargo new --bin demo

8、测试执行

workspace# cargo run --bin demo
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/demo`
Hello, world!

9、构建

workspace# cargo build
Compiling demo v0.1.0 (/opt/rust_projects/workspace/demo)
Finished dev [unoptimized + debuginfo] target(s) in 1.39s

10、查看二进制文件属性

workspace# file target/x86_64-unknown-linux-musl/debug/demo
target/x86_64-unknown-linux-musl/debug/demo: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, BuildID[sha1]=b52d8266daa7e4f17dfd089455bbbb88fa811d77, with debug_info, not stripped

workspace# ls -l target/x86_64-unknown-linux-musl/debug/demo
-rwxr-xr-x 2 root root 3687136 Dec 24 12:37 target/x86_64-unknown-linux-musl/debug/demo

文件为静态链接,未优化,包含调试信息,未经 strip,大小 3687136

11、(选修)优化二进制文件

(1)增加工作空间配置

cat .cargo/config
[build]
target = "x86_64-unknown-linux-musl"

[profile.release]
panic = 'abort'
opt-level = 'z'
codegen-units = 1
lto = true

优化配置完全参考 https://www.aloxaf.com/2018/09/reduce_rust_size/ 此文章,在此表示感谢。

(2)构建发布版本

workspace# cargo build --release
Compiling demo v0.1.0 (/opt/rust_projects/workspace/demo)
Finished release [optimized] target(s) in 4.76s

(3)查看发布版本二进制文件属性

workspace# file target/x86_64-unknown-linux-musl/release/demo
target/x86_64-unknown-linux-musl/release/demo: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, BuildID[sha1]=c8ca1905fccc06b7d7a3803238aafe43569189fa, with debug_info, not stripped

workspace# ls -l target/x86_64-unknown-linux-musl/release/demo
-rwxr-xr-x 2 root root 1731352 Dec 24 12:42 target/x86_64-unknown-linux-musl/release/demo

文件为静态链接,包含调试信息,未经 strip,大小 1731352

(4)strip处理

strip -s target/x86_64-unknown-linux-musl/release/demo

(5)查看stripped发布版本二进制文件属性

workspace# file target/x86_64-unknown-linux-musl/release/demo
target/x86_64-unknown-linux-musl/release/demo: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, BuildID[sha1]=c8ca1905fccc06b7d7a3803238aafe43569189fa, stripped

workspace# ls -l target/x86_64-unknown-linux-musl/release/demo
-rwxr-xr-x 2 root root 292264 Dec 24 12:44 target/x86_64-unknown-linux-musl/release/demo

文件为静态链接,不包含调试信息,stripped,大小 292264

Rust 安装、验证完毕。

你可能感兴趣的:(2020-12-24 (一)Rust 入坑安装、修改国内源、使用musl、优化二进制文件等)