解决交叉编译rust-std遇到的问题

之前,成功搞定Rust的交叉编译之后,其实又遇到过很多问题。这里记录一下。

关键的问题在于,交叉编译软件需要用到的rust-std,安装时经常出现问题。

最开始的一些版本,直到1.89之前,问题都还好,只需要简单的修改配置文件即可。

1.89和1.90两个版本,借助这个patch,还是能够成功。

但是在之后,理论上这个patch已经合并到主仓库了,安装rust-std时,编译没有问题,安装之后却没有任何实际内容。

所以后来无论rust怎么更新,我都只停留在了1.90的版本上。

直到今年7月,我突然尝试将这个问题抛给Claude,经过不断的补充信息,终于找到了问题所在:将编译好的target std部分拷贝到sysroot的函数,只在compiler.stage > 0时执行。

Claude能够直接获取Github上Rust对应tag的源代码,因此我不用把所有文件都传给Claude。

修改方法也很简单,在compiler.stage == 0的时候也调用一次add_to_sysroot即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
diff '--color=auto' -ur rustc-1.96.1-src.orig/src/bootstrap/src/core/build_steps/compile.rs rustc-1.96.1-src/src/bootstrap/src/core/build_steps/compile.rs
--- rustc-1.96.1-src.orig/src/bootstrap/src/core/build_steps/compile.rs 2026-07-09 13:18:43.419251751 +0800
+++ rustc-1.96.1-src/src/bootstrap/src/core/build_steps/compile.rs 2026-07-09 13:20:05.475173400 +0800
@@ -813,6 +813,21 @@
}

builder.cp_link_r(&builder.initial_sysroot.join("lib"), &sysroot.join("lib"));
+
+ // `initial_sysroot` only contains the *host*'s std (whatever the local
+ // system compiler was built for). When doing a local rebuild while
+ // cross-compiling (see the early-return check at the top of `Std::run`),
+ // std for `target` was actually built by cargo in this invocation, but
+ // is never picked up by the code above. Link it into the sysroot here,
+ // same as the normal (stage > 0) path does.
+ if builder.local_rebuild && target != compiler.host {
+ add_to_sysroot(
+ builder,
+ &libdir,
+ &hostdir,
+ &build_stamp::libstd_stamp(builder, compiler, target),
+ );
+ }
} else {
if builder.download_rustc() {
// Ensure there are no CI-rustc std artifacts.

目前这个patch,适用于1.96.1以来的所有版本。理论上前面几个版本应该也适用,不过没有测试过。

在最新的1.100.0版本中,有两点需要注意。

其一,正常来说,rust在升级到最新版本之后,eselect rust会自动变为最新的版本。但是目前它的排序有问题,1.100.0版本排在了最前面而不是最后面。

需要修改/usr/share/eselect/modules/rust.eselect里面的find_targets函数内部的排序命令。将

1
IFS=$'\n' LC_COLLATE=C providers_sorted=( $(sort <<<"${providers_unsorted[*]}") )

改为

1
IFS=$'\n' LC_COLLATE=C providers_sorted=( $(sort -V <<<"${providers_unsorted[*]}") )

即可。

其二,直接编译过不去,需要patch掉daa2432这个commit,rust-std才能正常编译。