forked from japaric/xargo
-
Notifications
You must be signed in to change notification settings - Fork 25
/
sysroot.rs
336 lines (285 loc) · 8.42 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
use std::collections::hash_map::DefaultHasher;
use std::env;
use std::fs;
use std::hash::{Hash, Hasher};
use std::io;
use std::io::Write;
use std::path::Path;
use std::process::Command;
use rustc_version::VersionMeta;
use tempfile::Builder;
use toml::{value::Table, Value};
use cargo;
use cargo::Rustflags;
use config::Config;
use errors::*;
use extensions::CommandExt;
use rustc::{Src, Sysroot, Target};
use util;
use xargo::Home;
use CompilationMode;
#[cfg(feature = "dev")]
fn profile() -> &'static str {
"debug"
}
#[cfg(not(feature = "dev"))]
fn profile() -> &'static str {
"release"
}
fn build(
cmode: &CompilationMode,
ctoml: &cargo::Toml,
home: &Home,
config: &Config,
src: &Src,
hash: u64,
verbose: bool,
) -> Result<()> {
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)?;
build_liballoc(cmode, &ctoml, src, &dst, config, verbose)?;
// Create hash file
util::write(&rustlib.parent().join(".hash"), &hash.to_string())?;
Ok(())
}
fn build_crate(
crate_name: &str,
lockfile: &Path,
mut stoml: String,
cmode: &CompilationMode,
ctoml: &cargo::Toml,
dst: &Path,
verbose: bool,
) -> Result<()> {
let td = Builder::new().prefix("cargo-xbuild").tempdir().chain_err(|| "couldn't create a temporary directory")?;
let td_path;
let td = if env::var_os("XBUILD_KEEP_TEMP").is_some() {
td_path = td.into_path();
println!("XBUILD_KEEP_TEMP: files at {:?}", td_path);
&td_path
} else {
td.path()
};
let target_dir = td.join("target");
if let Some(features) = ctoml.features() {
stoml.insert_str(0, &features.to_string())
}
if let Some(profile) = ctoml.profile() {
stoml.push_str(&profile.to_string())
}
util::write(&td.join("Cargo.toml"), &stoml)?;
fs::copy(lockfile, &td.join("Cargo.lock")).chain_err(||
format!("failed to copy Cargo.lock from `{}` to `{}`",
lockfile.display(), &td.join("Cargo.lock").display())
)?;
util::mkdir(&td.join("src"))?;
util::write(&td.join("src/lib.rs"), "")?;
let cargo = std::env::var("CARGO").unwrap_or("cargo".to_string());
let mut cmd = Command::new(cargo);
cmd.env("RUSTFLAGS", "-Cembed-bitcode=yes");
cmd.env("CARGO_TARGET_DIR", &target_dir);
cmd.env("__CARGO_DEFAULT_LIB_METADATA", "XARGO");
// As of rust-lang/cargo#4788 Cargo invokes rustc with a changed "current directory" so
// we can't assume that such directory will be the same as the directory from which
// Xargo was invoked. This is specially true when compiling the sysroot as the std
// source is provided as a workspace and Cargo will change the current directory to the
// root of the workspace when building one. To ensure rustc finds a target specification
// file stored in the current directory we'll set `RUST_TARGET_PATH` to the current
// directory.
if env::var_os("RUST_TARGET_PATH").is_none() {
if let CompilationMode::Cross(ref target) = *cmode {
if let Target::Custom { ref json, .. } = *target {
cmd.env("RUST_TARGET_PATH", json.parent().unwrap());
}
}
}
cmd.arg("rustc");
cmd.arg("-p").arg(crate_name);
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.orig_triple()]);
if verbose {
cmd.arg("-v");
}
cmd.arg("--");
cmd.arg("-Z");
cmd.arg("force-unstable-if-unmarked");
cmd.run(verbose)?;
// Copy artifacts to Xargo sysroot
util::cp_r(
&target_dir.join(cmode.triple()).join(profile()).join("deps"),
dst,
)?;
Ok(())
}
fn build_liballoc(
cmode: &CompilationMode,
ctoml: &cargo::Toml,
src: &Src,
dst: &Path,
config: &Config,
verbose: bool,
) -> Result<()> {
const TOML: &'static str = r#"
[package]
authors = ["The Rust Project Developers"]
name = "alloc"
version = "0.0.0"
edition = "2018"
[dependencies.compiler_builtins]
version = "0.1.0"
"#;
let mut stoml = TOML.to_owned();
if config.memcpy {
stoml.push_str("features = ['mem', 'core']\n");
} else {
stoml.push_str("features = ['core']\n");
}
stoml.push_str("[dependencies.core]\n");
stoml.push_str(&format!(
"path = '{}'\n",
src.path().join("libcore").display()
));
if config.panic_immediate_abort {
stoml.push_str("features = ['panic_immediate_abort']\n");
}
stoml.push_str("[patch.crates-io.rustc-std-workspace-core]\n");
stoml.push_str(&format!(
"path = '{}'\n",
src.path().join("tools/rustc-std-workspace-core").display()
));
let path = src.path().join("liballoc/lib.rs").display().to_string();
let mut map = Table::new();
let mut lib = Table::new();
lib.insert("name".to_owned(), Value::String("alloc".to_owned()));
lib.insert("path".to_owned(), Value::String(path));
map.insert("lib".to_owned(), Value::Table(lib));
stoml.push_str(&Value::Table(map).to_string());
let lockfile = src.path().join("..").join("Cargo.lock");
build_crate("alloc", &lockfile, stoml, cmode, ctoml, dst, verbose)
}
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
///
/// - RUSTFLAGS / build.rustflags / target.*.rustflags
/// - The target specification file, is any
/// - `[profile.release]` in `Cargo.toml`
/// - `rustc` commit hash
fn hash(
cmode: &CompilationMode,
rustflags: &Rustflags,
ctoml: &cargo::Toml,
meta: &VersionMeta,
config: &Config,
) -> Result<u64> {
let mut hasher = DefaultHasher::new();
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);
}
config.hash(&mut hasher);
Ok(hasher.finish())
}
pub fn update(
cmode: &CompilationMode,
home: &Home,
root: &Path,
config: &Config,
rustflags: &Rustflags,
meta: &VersionMeta,
src: &Src,
sysroot: &Sysroot,
verbose: bool,
) -> Result<()> {
let ctoml = cargo::toml(root)?;
let mut stderr = io::stderr();
let hash = hash(cmode, rustflags, &ctoml, meta, config)?;
if old_hash(cmode, home)? != Some(hash) {
build(cmode, &ctoml, home, config, src, 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)?;
match util::cp_r(
&sysroot
.path()
.join("lib")
.join("rustlib")
.join(&meta.host)
.join("lib"),
&dst,
) {
Ok(()) => {}
Err(e) => {
writeln!(
stderr,
"Unable to copy the directory 'lib' from sysroot: {}",
e
)
.ok();
}
};
let bin_dst = lock.parent().join("bin");
util::mkdir(&bin_dst)?;
match util::cp_r(
&sysroot
.path()
.join("lib")
.join("rustlib")
.join(&meta.host)
.join("bin"),
&bin_dst,
) {
Ok(()) => {}
Err(e) => {
writeln!(
stderr,
"Unable to copy the directory 'bin' from sysroot: {}",
e
)
.ok();
}
};
util::write(&hfile, hash)?;
Ok(())
}