从源码安装 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(注意是dev版)
  • 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交叉编译器。

  • 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日 无锡

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,402评论 6 499
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,377评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,483评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,165评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,176评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,146评论 1 297
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,032评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,896评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,311评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,536评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,696评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,413评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,008评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,659评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,815评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,698评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,592评论 2 353

推荐阅读更多精彩内容