Rust安装

版本:1.34.2
host:x86_64-unknown-linux-gnu

源码编译安装

环境准备

官方要求具备的环境:

  • g++ 5.1 or later or clang++ 3.5 or later
  • python 2.7 (but not 3.x)
  • GNU make 3.81 or later
  • cmake 3.4.3 or later
  • curl
  • git
  • ssl which comes in libssl-dev or openssl-devel
  • pkg-config if you are compiling on Linux and targeting Linux

先甭管都是用来干啥的,装上就是:

$ sudo apt install g++
$ sudo apt install python2.7
$ sudo apt install make
$ sudo apt install cmake
$ sudo apt install curl
$ sudo apt install git
$ sudo apt install libssl-dev
$ sudo apt install pkg-config

源码获取

本来可以直接git clone https://github.com/rust-lang/rust.git获取源码,但网速太慢,而且我们也不是要最新版,就自行从https://static.rust-lang.org/dist/rustc-1.34.2-src.tar.gz下载了。

放到~/下解压:

$ tar -zxvf rustc-1.34.2-src.tar.gz
$ mv rustc-1.34.2-src rust
$ cd rust

构建与安装

借用官方提供的config.toml

$ cp config.toml.example config.toml

鉴于/usr/local的权限问题,在config.toml中把安装目录配置到我们的工作目录:

[install]
# Instead of installing to /usr/local, install to this path instead.
prefix = "~/rust"

构建并安装:

$ ./x.py build && ./x.py install

build会在~\rust\build\x86_64-unknown-linux-gnu目录下生成stage0~stage2的工具。install会将rustc等程序装入~/rust/bin,将库文件装入~/rust/lib

cargo需要另行安装:

$ ./x.py install cargo

或者在一开始就修改config.toml配置,构建及安装所有工具:

# Enable a build of the extended rust tool set which is not only the compiler
# but also tools such as Cargo. This will also produce "combined installers"
# which are used to install Rust and Cargo together. This is disabled by
# default.
extended = true

遇到的坑

- 1 -

获取官方crates时可能太慢卡住了,像这样:

    Updating crates.io index
       Fetch [>                                                      ]   2.81%

改用镜像站,新建~/.cargo/config,贴入:

[source.crates-io]
replace-with = 'ustc'
[source.ustc]
registry = "https://mirrors.ustc.edu.cn/crates.io-index"

https可以换成git,还有一个阿里云的https://code.aliyun.com/rustcc/crates.io-index.git,但有点问题似乎暂时不能用。

- 2 -

下载crates期间遇到报错可能是网络不稳,重新运行会继续下。

- 3 -

编译Rust源码需要用到更早版本的Rust:

https://static.rust-lang.org/dist/2019-02-28/rust-std-1.33.0-x86_64-unknown-linux-gnu.tar.gz
https://static.rust-lang.org/dist/2019-02-28/rustc-1.33.0-x86_64-unknown-linux-gnu.tar.gz
https://static.rust-lang.org/dist/2019-02-28/cargo-0.34.0-x86_64-unknown-linux-gnu.tar.gz

网速太慢经常下载失败,可自行下载后放到~/rust/build/cache/2019-02-28

- 4 -

error: failed to run custom build command for `compiler_builtins v0.1.23`
process didn't exit successfully: `/home/yizhi/rust/build/x86_64-unknown-linux-gnu/stage0-std/release/build/compiler_builtins-b25c1064dfc3ea33/build-script-build` (exit code: 101)
--- stdout
cargo:rerun-if-changed=build.rs
cargo:compiler-rt=/home/yizhi/.cargo/registry/src/mirrors.ustc.edu.cn-12df342d903acd47/compiler_builtins-0.1.23/compiler-rt
cargo:rustc-cfg=feature="unstable"

--- stderr
thread 'main' panicked at 'RUST_COMPILER_RT_ROOT is not set', /home/yizhi/.cargo/registry/src/mirrors.ustc.edu.cn-12df342d903acd47/compiler_builtins-0.1.23/build.rs:423:21
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

添加环境变量

$ export RUST_COMPILER_RT_ROOT=~/rust/vendor/compiler_builtins/compiler-rt/

- 5 -

error[E0635]: unknown feature `compiler_builtins_lib`===============>  ] 35/36: std
   --> src/libstd/lib.rs:244:12
    |
244 | #![feature(compiler_builtins_lib)]
    |            ^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0635`.
error: Could not compile `std`.

src/libstd/lib.rs中的#![feature(compiler_builtins_lib)]注释掉。

- 6 -

error: cannot satisfy dependencies so `unicode_width` only shows up once31/149: rustc
  |
  = help: having upstream crates all available in one format will likely make this go away

error: aborting due to previous error

error: Could not compile `rustc`.

这个问题实在无解,更新了一下系统就过了,而且连前面的4和5都没遇到了。

- 7 -

安装cargo期间会遇到系统目录无权限的问题:

cp: cannot create regular file '/etc/bash_completion.d/cargo': Permission denied
chmod: cannot access '/etc/bash_completion.d/cargo': No such file or directory
install: error: file creation failed. see logs at '/home/yizhi/rust/lib/rustlib/install.log'

那是因为config.toml[install]下的几个子路径并不都是默认相对路径,有些个尝试装到系统目录下了,改成相对路径即可。

Rustup安装

Rustup是Rust的官方版本管理工具,是安装Rust的首选,一行命令搞定:

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

如果网速慢,设置镜像站:

$ export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
$ export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup

如果想安装指定的版本,使用toolchain子命令:

$ rustup toolchain install stable-x86_64-unknown-linux-gnu stable-2019-05-14 1.34.2

会逐一下载各工具,其中体积比较大的经常会下载失败,又需要手动下载,比如:

https://static.rust-lang.org/dist/2019-05-14/rust-docs-1.34.2-x86_64-unknown-linux-gnu.tar.xz
https://static.rust-lang.org/dist/2019-05-14/rust-std-1.34.2-x86_64-unknown-linux-gnu.tar.xz
https://static.rust-lang.org/dist/2019-05-14/rustc-1.34.2-x86_64-unknown-linux-gnu.tar.xz
https://static.rust-lang.org/dist/2020-03-12/rust-std-1.42.0-x86_64-unknown-linux-gnu.tar.xz
https://static.rust-lang.org/dist/2020-03-12/rustc-1.42.0-x86_64-unknown-linux-gnu.tar.xz

然后放到~/.rustup/downloads/下,重命名成对应的SHA256值(用sha256sum命令查看)。


2020年3月23日 无锡


离线安装(没有网络怎么办?)

事先下载好:

  • cmake等环境工具
  • rustc-1.34.2-src.tar.gz
  • rust-std-1.33.0-x86_64-unknown-linux-gnu.tar.gz
  • rustc-1.33.0-x86_64-unknown-linux-gnu.tar.gz
  • cargo-0.34.0-x86_64-unknown-linux-gnu.tar.gz

按上文步骤操作。

由于无法连接crates站点,利用源码vendor目录中现成的crates,在config.toml中做如下配置:

[build]
# Indicate whether the vendored sources are used for Rust dependencies or not
vendor = true



2020年3月27日 无锡


从源码构建Rust交叉编译器

在前文的离线安装基础上,构建Rust交叉编译器。

  • host:x86_64-unknown-linux-gnu
  • target:aarch64-unknown-linux-gnu

gcc交叉编译环境

首先,我们需要gcc交叉编译工具aarch64-linux-gnu-gcc。在这里下载如下几个包:

  • aarch64-linux-gnu-gcc-9.3.0-1-x86_64.pkg.tar.zst
  • aarch64-linux-gnu-glibc-2.31-1-any.pkg.tar.zst
  • aarch64-linux-gnu-binutils-2.34-1-x86_64.pkg.tar.zst
  • aarch64-linux-gnu-linux-api-headers-5.5-1-any.pkg.tar.zst

当然还有它们所依赖的mpfrglibc等基础包,根据自己的环境按需下载。

aarch64-linux-gnu-gcc-9.3.0-1-x86_64.pkg.tar.zst为例,把上述包逐一安顿到一个文件夹里:

$ mkdir aarch64-linux-gnu-gcc
$ zstd -d aarch64-linux-gnu-gcc-9.3.0-1-x86_64.pkg.tar.zst
$ tar -xvf aarch64-linux-gnu-gcc-9.3.0-1-x86_64.pkg.tar --directory=aarch64-linux-gnu-gcc

将该文件夹添加至环境变量:

$ export PATH=$HOME/aarch64-linux-gnu-gcc/usr/bin:$PATH

构建Rust交叉编译器

生成交叉编译的config.tomlMakefile

$ cd rust
$ ./configure --target=aarch64-unknown-linux-gnu

构建:

$ make -j$(nproc)

此举会在build/x86_64-unknown-linux-gnu/stage2/lib/rustlib目录下生成target库,将其拷贝至lib/rustlib即可:

$ cp -r build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/aarch64-unknown-linux-gnu lib/rustlib

使用

让我们写一个简单的测试程序hello.rs,对其交叉编译:

$ ~/rust/bin/rustc --target=aarch64-unknown-linux-gnu -C linker=aarch64-linux-gnu-gcc hello.rs

也可以对Rust源码中自带的测试集进行交叉编译测试:

$ cd ~/rust
$ ./x.py test --target=aarch64-unknown-linux-gnu

不过程序在本地也跑不起来,这么用的意义似乎也不大,目前主要靠自己写脚本批量编译后scp到目标平台。



2020年4月10日 无锡

推荐阅读更多精彩内容