Skip to content

Commit 738133b

Browse files
committed
complete setup routine: instal rust-src, build libstd, use it
1 parent c945e30 commit 738133b

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ required-features = ["rustc_tests"]
3535
[dependencies]
3636
byteorder = { version = "1.1", features = ["i128"]}
3737
cargo_metadata = { version = "0.6", optional = true }
38+
dirs = { version = "1.0.4", optional = true }
3839
env_logger = "0.5"
3940
log = "0.4"
4041

@@ -43,7 +44,7 @@ vergen = "3"
4344

4445
[features]
4546
default = ["cargo_miri"]
46-
cargo_miri = ["cargo_metadata"]
47+
cargo_miri = ["cargo_metadata", "dirs"]
4748
rustc_tests = []
4849

4950
[dev-dependencies]

src/bin/cargo-miri.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extern crate cargo_metadata;
55
use std::path::{PathBuf, Path};
66
use std::io::{self, Write};
77
use std::process::Command;
8-
8+
use std::fs::{self, File};
99

1010
const CARGO_MIRI_HELP: &str = r#"Interprets bin crates
1111
@@ -133,6 +133,59 @@ fn setup(ask_user: bool) {
133133
show_error(format!("Failed to install xargo"));
134134
}
135135
}
136+
137+
// Then, we also need rust-src. Let's see if it is already installed.
138+
let sysroot = Command::new("rustc").args(&["--print", "sysroot"]).output().unwrap().stdout;
139+
let sysroot = std::str::from_utf8(&sysroot[..]).unwrap();
140+
let src = Path::new(sysroot.trim_end_matches('\n')).join("lib/rustlib/src");
141+
if !src.exists() {
142+
println!("Could not find {:?}", src);
143+
if ask_user {
144+
ask("It seems you do not have the rust-src component installed. I will run `rustup component add rust-src`. Proceed?");
145+
}
146+
if !Command::new("rustup").args(&["component", "add", "rust-src"]).status().unwrap().success() {
147+
show_error(format!("Failed to install rust-src component"));
148+
}
149+
}
150+
151+
// Next, we need our own libstd. We will do this work in ~/.miri.
152+
let dir = dirs::home_dir().unwrap().join(".miri");
153+
if !dir.exists() {
154+
fs::create_dir(&dir).unwrap();
155+
}
156+
// The interesting bit: Xargo.toml
157+
File::create(dir.join("Xargo.toml")).unwrap()
158+
.write_all(br#"
159+
[dependencies.std]
160+
features = ["panic_unwind", "backtrace"]
161+
162+
[dependencies.test]
163+
stage = 1
164+
"#).unwrap();
165+
// The boring bits: A dummy project for xargo
166+
File::create(dir.join("Cargo.toml")).unwrap()
167+
.write_all(br#"
168+
[package]
169+
name = "miri-xargo"
170+
description = "A dummy project for building libstd with xargo."
171+
version = "0.0.0"
172+
173+
[lib]
174+
path = "lib.rs"
175+
"#).unwrap();
176+
File::create(dir.join("lib.rs")).unwrap();
177+
// Run xargo
178+
if !Command::new("xargo").arg("build")
179+
.current_dir(&dir)
180+
.env("RUSTFLAGS", miri::miri_default_args().join(" "))
181+
.env("XARGO_HOME", dir.to_str().unwrap())
182+
.status().unwrap().success()
183+
{
184+
show_error(format!("Failed to run xargo"));
185+
}
186+
187+
// That should be it!
188+
std::env::set_var("MIRI_SYSROOT", dir.join("HOST"));
136189
}
137190

138191
fn main() {

0 commit comments

Comments
 (0)