-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathsysroot.rs
382 lines (328 loc) · 10.4 KB
/
sysroot.rs
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
use std::collections::BTreeMap;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::path::PathBuf;
use std::process::Command;
use rustc_version::VersionMeta;
use tempdir::TempDir;
use toml::{Table, Value};
use CompilationMode;
use cargo::{Root, Rustflags};
use errors::*;
use extensions::CommandExt;
use rustc::{Src, Sysroot};
use util;
use xargo::Home;
use {cargo, xargo};
#[cfg(feature = "dev")]
fn profile() -> &'static str {
"debug"
}
#[cfg(not(feature = "dev"))]
fn profile() -> &'static str {
"release"
}
fn build(
cmode: &CompilationMode,
blueprint: Blueprint,
ctoml: &cargo::Toml,
home: &Home,
rustflags: &Rustflags,
hash: u64,
verbose: bool,
) -> Result<()> {
const TOML: &'static str = r#"
[package]
authors = ["The Rust Project Developers"]
name = "sysroot"
version = "0.0.0"
"#;
let rustlib = home.lock_rw(cmode.triple())?;
rustlib.remove_siblings().chain_err(|| {
format!("couldn't clear {}", rustlib.path().display())
})?;
let dst = rustlib.parent().join("lib");
util::mkdir(&dst)?;
for (_, stage) in blueprint.stages {
let td = TempDir::new("xargo").chain_err(
|| "couldn't create a temporary directory",
)?;
let td = td.path();
let mut stoml = TOML.to_owned();
let mut map = Table::new();
map.insert("dependencies".to_owned(), Value::Table(stage.toml));
stoml.push_str(&Value::Table(map).to_string());
if let Some(profile) = ctoml.profile() {
stoml.push_str(&profile.to_string())
}
util::write(&td.join("Cargo.toml"), &stoml)?;
util::mkdir(&td.join("src"))?;
util::write(&td.join("src/lib.rs"), "")?;
let cargo = || {
let mut cmd = Command::new("cargo");
let mut flags = rustflags.for_xargo(home);
flags.push_str(" -Z force-unstable-if-unmarked");
cmd.env("RUSTFLAGS", flags);
cmd.env_remove("CARGO_TARGET_DIR");
cmd.arg("build");
match () {
#[cfg(feature = "dev")]
() => {}
#[cfg(not(feature = "dev"))]
() => {
cmd.arg("--release");
}
}
cmd.arg("--manifest-path");
cmd.arg(td.join("Cargo.toml"));
cmd.args(&["--target", cmode.triple()]);
if verbose {
cmd.arg("-v");
}
cmd
};
for krate in stage.crates {
cargo().arg("-p").arg(krate).run(verbose)?;
}
// Copy artifacts to Xargo sysroot
util::cp_r(
&td.join("target")
.join(cmode.triple())
.join(profile())
.join("deps"),
&dst,
)?;
}
// Create hash file
util::write(&rustlib.parent().join(".hash"), &hash.to_string())?;
Ok(())
}
fn old_hash(cmode: &CompilationMode, home: &Home) -> Result<Option<u64>> {
// FIXME this should be `lock_ro`
let lock = home.lock_rw(cmode.triple())?;
let hfile = lock.parent().join(".hash");
if hfile.exists() {
Ok(util::read(&hfile)?.parse().ok())
} else {
Ok(None)
}
}
/// Computes the hash of the would-be target sysroot
///
/// This information is used to compute the hash
///
/// - Dependencies in `Xargo.toml` for a specific target
/// - RUSTFLAGS / build.rustflags / target.*.rustflags
/// - The target specification file, is any
/// - `[profile.release]` in `Cargo.toml`
/// - `rustc` commit hash
fn hash(
cmode: &CompilationMode,
blueprint: &Blueprint,
rustflags: &Rustflags,
ctoml: &cargo::Toml,
meta: &VersionMeta,
) -> Result<u64> {
let mut hasher = DefaultHasher::new();
blueprint.hash(&mut hasher);
rustflags.hash(&mut hasher);
cmode.hash(&mut hasher)?;
if let Some(profile) = ctoml.profile() {
profile.hash(&mut hasher);
}
if let Some(ref hash) = meta.commit_hash {
hash.hash(&mut hasher);
}
Ok(hasher.finish())
}
pub fn update(
cmode: &CompilationMode,
home: &Home,
root: &Root,
rustflags: &Rustflags,
meta: &VersionMeta,
src: &Src,
sysroot: &Sysroot,
verbose: bool,
) -> Result<()> {
let ctoml = cargo::toml(root)?;
let xtoml = xargo::toml(root)?;
let blueprint =
Blueprint::from(xtoml.as_ref(), cmode.triple(), root, &src)?;
let hash = hash(cmode, &blueprint, rustflags, &ctoml, meta)?;
if old_hash(cmode, home)? != Some(hash) {
build(cmode, blueprint, &ctoml, home, rustflags, hash, verbose)?;
}
// copy host artifacts into the sysroot, if necessary
if cmode.is_native() {
return Ok(());
}
let lock = home.lock_rw(&meta.host)?;
let hfile = lock.parent().join(".hash");
let hash = meta.commit_hash.as_ref().map(|s| &**s).unwrap_or("");
if hfile.exists() {
if util::read(&hfile)? == hash {
return Ok(());
}
}
lock.remove_siblings().chain_err(|| {
format!("couldn't clear {}", lock.path().display())
})?;
let dst = lock.parent().join("lib");
util::mkdir(&dst)?;
util::cp_r(
&sysroot.path().join("lib/rustlib").join(&meta.host).join(
"lib",
),
&dst,
)?;
util::write(&hfile, hash)?;
Ok(())
}
/// Per stage dependencies
#[derive(Debug)]
pub struct Stage {
crates: Vec<String>,
toml: Table,
}
/// A sysroot that will be built in "stages"
#[derive(Debug)]
pub struct Blueprint {
stages: BTreeMap<i64, Stage>,
}
impl Blueprint {
fn new() -> Self {
Blueprint { stages: BTreeMap::new() }
}
fn from(
toml: Option<&xargo::Toml>,
target: &str,
root: &Root,
src: &Src,
) -> Result<Self> {
let deps = match (
toml.and_then(|t| t.dependencies()),
toml.and_then(|t| t.target_dependencies(target)),
) {
(Some(value), Some(tvalue)) => {
let mut deps = value.as_table().cloned().ok_or_else(
|| format!("Xargo.toml: `dependencies` must be a table"),
)?;
let more_deps = tvalue.as_table().ok_or_else(|| {
format!(
"Xargo.toml: `target.{}.dependencies` must be \
a table",
target
)
})?;
for (k, v) in more_deps {
if deps.insert(k.to_owned(), v.clone()).is_some() {
Err(format!(
"found duplicate dependency name {}, \
but all dependencies must have a \
unique name",
k
))?
}
}
deps
}
(Some(value), None) |
(None, Some(value)) => {
if let Some(table) = value.as_table() {
table.clone()
} else {
Err(format!(
"Xargo.toml: target.{}.dependencies must be \
a table",
target
))?
}
}
(None, None) => {
// If no dependencies were listed, we assume `core` as the
// only dependency
let mut t = BTreeMap::new();
t.insert("core".to_owned(), Value::Table(BTreeMap::new()));
t
}
};
let mut blueprint = Blueprint::new();
for (k, v) in deps {
if let Value::Table(mut map) = v {
let stage = if let Some(value) = map.remove("stage") {
value.as_integer().ok_or_else(|| {
format!(
"dependencies.{}.stage must be an integer",
k
)
})?
} else {
0
};
if let Some(path) = map.get_mut("path") {
let p = PathBuf::from(
path.as_str().ok_or_else(|| {
format!(
"dependencies.{}.path must be a string",
k
)
})?,
);
if !p.is_absolute() {
*path = Value::String(
root.path()
.join(&p)
.canonicalize()
.chain_err(|| {
format!(
"couldn't canonicalize {}",
p.display()
)
})?
.display()
.to_string(),
);
}
}
if !map.contains_key("path") && !map.contains_key("git") {
let path = src.path()
.join(format!("lib{}", k))
.display()
.to_string();
map.insert("path".to_owned(), Value::String(path));
}
blueprint.push(stage, k, map);
} else {
Err(format!(
"Xargo.toml: target.{}.dependencies.{} must be \
a table",
target,
k
))?
}
}
Ok(blueprint)
}
fn push(&mut self, stage: i64, krate: String, toml: Table) {
let stage = self.stages.entry(stage).or_insert_with(|| {
Stage {
crates: vec![],
toml: Table::new(),
}
});
stage.toml.insert(krate.clone(), Value::Table(toml));
stage.crates.push(krate);
}
fn hash<H>(&self, hasher: &mut H)
where
H: Hasher,
{
for stage in self.stages.values() {
for (k, v) in stage.toml.iter() {
k.hash(hasher);
v.to_string().hash(hasher);
}
}
}
}