@@ -5,7 +5,7 @@ extern crate cargo_metadata;
55use std:: path:: { PathBuf , Path } ;
66use std:: io:: { self , Write } ;
77use std:: process:: Command ;
8-
8+ use std :: fs :: { self , File } ;
99
1010const 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
138191fn main ( ) {
0 commit comments