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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
use std::collections::hash_map::DefaultHasher;
use std::ffi::{OsStr, OsString};
use std::fs::{self, File};
use std::hash::{Hash, Hasher};
use std::io::{Read, Write};
use std::ops::Not;
use std::path::{Path, PathBuf};
use std::process::Command;
use anyhow::{bail, Context, Result};
use rustc_version::VersionMeta;
use tempdir::TempDir;
pub fn rustc_sysroot_src(mut rustc: Command) -> Result<PathBuf> {
let output = rustc
.args(["--print", "sysroot"])
.output()
.context("failed to determine sysroot")?;
if !output.status.success() {
bail!(
"failed to determine sysroot; rustc said:\n{}",
String::from_utf8_lossy(&output.stderr).trim_end()
);
}
let sysroot =
std::str::from_utf8(&output.stdout).context("sysroot folder is not valid UTF-8")?;
let sysroot = Path::new(sysroot.trim_end_matches('\n'));
Ok(sysroot
.join("lib")
.join("rustlib")
.join("src")
.join("rust")
.join("library"))
}
pub fn encode_rustflags(flags: &[OsString]) -> OsString {
let mut res = OsString::new();
for flag in flags {
if !res.is_empty() {
res.push(OsStr::new("\x1f"));
}
res.push(flag);
}
res
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum BuildMode {
Build,
Check,
}
impl BuildMode {
pub fn as_str(&self) -> &str {
use BuildMode::*;
match self {
Build => "build",
Check => "check",
}
}
}
#[derive(Copy, Clone, Debug)]
pub enum SysrootConfig<'a> {
NoStd,
WithStd {
std_features: &'a [&'a str],
},
}
pub struct Sysroot {
sysroot_dir: PathBuf,
target: String,
}
const HASH_FILE_NAME: &str = ".rustc-build-sysroot-hash";
impl Sysroot {
pub fn new(sysroot_dir: &Path, target: &str) -> Self {
Sysroot {
sysroot_dir: sysroot_dir.to_owned(),
target: target.to_owned(),
}
}
fn target_dir(&self) -> PathBuf {
self.sysroot_dir
.join("lib")
.join("rustlib")
.join(&self.target)
}
fn sysroot_compute_hash(&self, src_dir: &Path, rustc_version: &VersionMeta) -> u64 {
let mut hasher = DefaultHasher::new();
src_dir.hash(&mut hasher);
rustc_version.commit_hash.hash(&mut hasher);
hasher.finish()
}
fn sysroot_read_hash(&self) -> Option<u64> {
let hash_file = self.target_dir().join("lib").join(HASH_FILE_NAME);
let mut hash = String::new();
File::open(&hash_file)
.ok()?
.read_to_string(&mut hash)
.ok()?;
hash.parse().ok()
}
pub fn build_from_source(
&self,
src_dir: &Path,
mode: BuildMode,
config: SysrootConfig<'_>,
rustc_version: &VersionMeta,
cargo_cmd: impl Fn() -> (Command, Vec<OsString>),
) -> Result<()> {
if !src_dir.join("std").join("Cargo.toml").exists() {
bail!(
"{:?} does not seem to be a rust library source folder: `src/Cargo.toml` not found",
src_dir
);
}
let cur_hash = self.sysroot_compute_hash(src_dir, rustc_version);
if self.sysroot_read_hash() == Some(cur_hash) {
return Ok(());
}
let build_dir = TempDir::new("rustc-build-sysroot").context("failed to create tempdir")?;
let lock_file = build_dir.path().join("Cargo.lock");
let manifest_file = build_dir.path().join("Cargo.toml");
let lib_file = build_dir.path().join("lib.rs");
fs::copy(
src_dir
.parent()
.expect("src_dir must have a parent")
.join("Cargo.lock"),
&lock_file,
)
.context("failed to copy lockfile from sysroot source")?;
let crates = match config {
SysrootConfig::NoStd => format!(
r#"
[dependencies.core]
path = {src_dir_core:?}
[dependencies.compiler_builtins]
features = ["rustc-dep-of-std", "mem"]
version = "*"
"#,
src_dir_core = src_dir.join("core"),
),
SysrootConfig::WithStd { std_features } => format!(
r#"
[dependencies.std]
features = {std_features:?}
path = {src_dir_std:?}
[dependencies.test]
path = {src_dir_test:?}
"#,
std_features = std_features,
src_dir_std = src_dir.join("std"),
src_dir_test = src_dir.join("test"),
),
};
let manifest = format!(
r#"
[package]
authors = ["rustc-build-sysroot"]
name = "sysroot"
version = "0.0.0"
[lib]
# empty dummy, just so that things are being built
path = "lib.rs"
{crates}
[patch.crates-io.rustc-std-workspace-core]
path = {src_dir_workspace_core:?}
[patch.crates-io.rustc-std-workspace-alloc]
path = {src_dir_workspace_alloc:?}
[patch.crates-io.rustc-std-workspace-std]
path = {src_dir_workspace_std:?}
"#,
crates = crates,
src_dir_workspace_core = src_dir.join("rustc-std-workspace-core"),
src_dir_workspace_alloc = src_dir.join("rustc-std-workspace-alloc"),
src_dir_workspace_std = src_dir.join("rustc-std-workspace-std"),
);
File::create(&manifest_file)
.context("failed to create manifest file")?
.write_all(manifest.as_bytes())
.context("failed to write manifest file")?;
let lib = match config {
SysrootConfig::NoStd => r#"#![no_std]"#,
SysrootConfig::WithStd { .. } => "",
};
File::create(&lib_file)
.context("failed to create lib file")?
.write_all(lib.as_bytes())
.context("failed to write lib file")?;
let (mut cmd, mut flags) = cargo_cmd();
cmd.arg(mode.as_str());
cmd.arg("--release");
cmd.arg("--manifest-path");
cmd.arg(&manifest_file);
cmd.arg("--target");
cmd.arg(&self.target);
flags.push("-Zforce-unstable-if-unmarked".into());
cmd.env("CARGO_ENCODED_RUSTFLAGS", encode_rustflags(&flags));
let build_target_dir = build_dir.path().join("target");
cmd.env("CARGO_TARGET_DIR", &build_target_dir);
cmd.env("__CARGO_DEFAULT_LIB_METADATA", "rustc-build-sysroot");
if cmd
.status()
.unwrap_or_else(|_| panic!("failed to execute cargo for sysroot build"))
.success()
.not()
{
bail!("sysroot build failed");
}
fs::create_dir_all(&self.sysroot_dir).context("failed to create sysroot dir")?; let staging_dir = TempDir::new_in(&self.sysroot_dir, "rustc-build-sysroot")
.context("failed to create staging dir")?;
let out_dir = build_target_dir
.join(&self.target)
.join("release")
.join("deps");
for entry in fs::read_dir(&out_dir).context("failed to read cargo out dir")? {
let entry = entry.context("failed to read cargo out dir entry")?;
assert!(
entry.file_type().unwrap().is_file(),
"cargo out dir must not contain directories"
);
let entry = entry.path();
fs::copy(&entry, staging_dir.path().join(entry.file_name().unwrap()))
.context("failed to copy cargo out file")?;
}
File::create(staging_dir.path().join(HASH_FILE_NAME))
.context("failed to create hash file")?
.write_all(cur_hash.to_string().as_bytes())
.context("failed to write hash file")?;
let target_lib_dir = self.target_dir().join("lib");
if target_lib_dir.exists() {
fs::remove_dir_all(&target_lib_dir).context("failed to clean sysroot target dir")?;
}
fs::create_dir_all(
target_lib_dir
.parent()
.expect("target/lib dir must have a parent"),
)
.context("failed to create target directory")?;
fs::rename(staging_dir.path(), target_lib_dir).context("failed installing sysroot")?;
Ok(())
}
}