11use std:: ffi:: { OsStr , OsString } ;
2+ use std:: fmt:: Write ;
23use std:: ops:: Range ;
34use std:: path:: { Path , PathBuf } ;
45use std:: sync:: atomic:: { AtomicBool , AtomicU32 , Ordering } ;
@@ -7,7 +8,7 @@ use std::thread;
78use anyhow:: { anyhow, Context , Result } ;
89use dunce:: canonicalize;
910use path_macro:: path;
10- use xshell:: { cmd, Shell } ;
11+ use xshell:: { cmd, Cmd , Shell } ;
1112
1213pub fn miri_dir ( ) -> std:: io:: Result < PathBuf > {
1314 const MIRI_SCRIPT_ROOT_DIR : & str = env ! ( "CARGO_MANIFEST_DIR" ) ;
@@ -28,13 +29,14 @@ pub fn flagsplit(flags: &str) -> Vec<String> {
2829}
2930
3031/// Some extra state we track for building Miri, such as the right RUSTFLAGS.
32+ #[ derive( Clone ) ]
3133pub struct MiriEnv {
3234 /// miri_dir is the root of the miri repository checkout we are working in.
3335 pub miri_dir : PathBuf ,
3436 /// active_toolchain is passed as `+toolchain` argument to cargo/rustc invocations.
3537 pub toolchain : String ,
3638 /// Extra flags to pass to cargo.
37- pub cargo_extra_flags : Vec < String > ,
39+ cargo_extra_flags : Vec < String > ,
3840 /// The rustc sysroot
3941 pub sysroot : PathBuf ,
4042 /// The shell we use.
@@ -59,10 +61,6 @@ impl MiriEnv {
5961 println ! ( "Please report a bug at https://github.com/rust-lang/miri/issues." ) ;
6062 std:: process:: exit ( 2 ) ;
6163 }
62- // Share target dir between `miri` and `cargo-miri`.
63- let target_dir = std:: env:: var_os ( "CARGO_TARGET_DIR" )
64- . unwrap_or_else ( || path ! ( miri_dir / "target" ) . into ( ) ) ;
65- sh. set_var ( "CARGO_TARGET_DIR" , target_dir) ;
6664
6765 // We configure dev builds to not be unusably slow.
6866 let devel_opt_level =
@@ -95,13 +93,37 @@ impl MiriEnv {
9593 Ok ( MiriEnv { miri_dir, toolchain, sh, sysroot, cargo_extra_flags } )
9694 }
9795
96+ pub fn cargo_cmd ( & self , manifest_path : impl AsRef < OsStr > , cmd : & str ) -> Cmd < ' _ > {
97+ let MiriEnv { toolchain, cargo_extra_flags, .. } = self ;
98+ let manifest_path = Path :: new ( manifest_path. as_ref ( ) ) ;
99+ let cmd = cmd ! (
100+ self . sh,
101+ "cargo +{toolchain} {cmd} {cargo_extra_flags...} --manifest-path {manifest_path}"
102+ ) ;
103+ // Hard-code the target dir, since we rely on knowing where the binaries end up.
104+ let manifest_dir = manifest_path. parent ( ) . unwrap ( ) ;
105+ let cmd = cmd. env ( "CARGO_TARGET_DIR" , path ! ( manifest_dir / "target" ) ) ;
106+ // Apply path remapping to have errors printed relative to `miri_dir`.
107+ let cmd = if let Ok ( relative_to_miri) = manifest_dir. strip_prefix ( & self . miri_dir ) {
108+ // Add `--remap-path-prefix` to RUSTFLAGS.
109+ let mut rustflags = self . sh . var ( "RUSTFLAGS" ) . unwrap ( ) ;
110+ write ! ( rustflags, " --remap-path-prefix ={}" , relative_to_miri. display( ) ) . unwrap ( ) ;
111+ cmd. env ( "RUSTFLAGS" , rustflags)
112+ } else {
113+ cmd
114+ } ;
115+ // All set up!
116+ cmd
117+ }
118+
98119 pub fn install_to_sysroot (
99120 & self ,
100121 path : impl AsRef < OsStr > ,
101122 args : impl IntoIterator < Item = impl AsRef < OsStr > > ,
102123 ) -> Result < ( ) > {
103124 let MiriEnv { sysroot, toolchain, cargo_extra_flags, .. } = self ;
104125 // Install binaries to the miri toolchain's `sysroot` so they do not interact with other toolchains.
126+ // (Not using `cargo_cmd` as `install` is special and doesn't use `--manifest-path`.)
105127 cmd ! ( self . sh, "cargo +{toolchain} install {cargo_extra_flags...} --path {path} --force --root {sysroot} {args...}" ) . run ( ) ?;
106128 Ok ( ( ) )
107129 }
@@ -112,40 +134,31 @@ impl MiriEnv {
112134 args : & [ String ] ,
113135 quiet : bool ,
114136 ) -> Result < ( ) > {
115- let MiriEnv { toolchain, cargo_extra_flags, .. } = self ;
116137 let quiet_flag = if quiet { Some ( "--quiet" ) } else { None } ;
117138 // We build the tests as well, (a) to avoid having rebuilds when building the tests later
118139 // and (b) to have more parallelism during the build of Miri and its tests.
119- let mut cmd = cmd ! (
120- self . sh,
121- "cargo +{toolchain} build --bins --tests {cargo_extra_flags...} --manifest-path {manifest_path} {quiet_flag...} {args...}"
122- ) ;
140+ let mut cmd = self
141+ . cargo_cmd ( manifest_path, "build" )
142+ . args ( & [ "--bins" , "--tests" ] )
143+ . args ( quiet_flag)
144+ . args ( args) ;
123145 cmd. set_quiet ( quiet) ;
124146 cmd. run ( ) ?;
125147 Ok ( ( ) )
126148 }
127149
128150 pub fn check ( & self , manifest_path : impl AsRef < OsStr > , args : & [ String ] ) -> Result < ( ) > {
129- let MiriEnv { toolchain, cargo_extra_flags, .. } = self ;
130- cmd ! ( self . sh, "cargo +{toolchain} check {cargo_extra_flags...} --manifest-path {manifest_path} --all-targets {args...}" )
131- . run ( ) ?;
151+ self . cargo_cmd ( manifest_path, "check" ) . arg ( "--all-targets" ) . args ( args) . run ( ) ?;
132152 Ok ( ( ) )
133153 }
134154
135155 pub fn clippy ( & self , manifest_path : impl AsRef < OsStr > , args : & [ String ] ) -> Result < ( ) > {
136- let MiriEnv { toolchain, cargo_extra_flags, .. } = self ;
137- cmd ! ( self . sh, "cargo +{toolchain} clippy {cargo_extra_flags...} --manifest-path {manifest_path} --all-targets {args...}" )
138- . run ( ) ?;
156+ self . cargo_cmd ( manifest_path, "clippy" ) . arg ( "--all-targets" ) . args ( args) . run ( ) ?;
139157 Ok ( ( ) )
140158 }
141159
142160 pub fn test ( & self , manifest_path : impl AsRef < OsStr > , args : & [ String ] ) -> Result < ( ) > {
143- let MiriEnv { toolchain, cargo_extra_flags, .. } = self ;
144- cmd ! (
145- self . sh,
146- "cargo +{toolchain} test {cargo_extra_flags...} --manifest-path {manifest_path} {args...}"
147- )
148- . run ( ) ?;
161+ self . cargo_cmd ( manifest_path, "test" ) . args ( args) . run ( ) ?;
149162 Ok ( ( ) )
150163 }
151164
@@ -155,7 +168,6 @@ impl MiriEnv {
155168 pub fn format_files (
156169 & self ,
157170 files : impl Iterator < Item = Result < PathBuf , walkdir:: Error > > ,
158- toolchain : & str ,
159171 config_path : & Path ,
160172 flags : & [ String ] ,
161173 ) -> anyhow:: Result < ( ) > {
@@ -166,6 +178,7 @@ impl MiriEnv {
166178 // Format in batches as not all our files fit into Windows' command argument limit.
167179 for batch in & files. chunks ( 256 ) {
168180 // Build base command.
181+ let toolchain = & self . toolchain ;
169182 let mut cmd = cmd ! (
170183 self . sh,
171184 "rustfmt +{toolchain} --edition=2021 --config-path {config_path} --unstable-features --skip-children {flags...}"
@@ -197,7 +210,7 @@ impl MiriEnv {
197210 pub fn run_many_times (
198211 & self ,
199212 range : Range < u32 > ,
200- run : impl Fn ( & Shell , u32 ) -> Result < ( ) > + Sync ,
213+ run : impl Fn ( & Self , u32 ) -> Result < ( ) > + Sync ,
201214 ) -> Result < ( ) > {
202215 // `next` is atomic so threads can concurrently fetch their next value to run.
203216 let next = AtomicU32 :: new ( range. start ) ;
@@ -207,10 +220,10 @@ impl MiriEnv {
207220 let mut handles = Vec :: new ( ) ;
208221 // Spawn one worker per core.
209222 for _ in 0 ..thread:: available_parallelism ( ) ?. get ( ) {
210- // Create a copy of the shell for this thread.
211- let local_shell = self . sh . clone ( ) ;
223+ // Create a copy of the environment for this thread.
224+ let local_miri = self . clone ( ) ;
212225 let handle = s. spawn ( || -> Result < ( ) > {
213- let local_shell = local_shell ; // move the copy into this thread.
226+ let local_miri = local_miri ; // move the copy into this thread.
214227 // Each worker thread keeps asking for numbers until we're all done.
215228 loop {
216229 let cur = next. fetch_add ( 1 , Ordering :: Relaxed ) ;
@@ -219,7 +232,7 @@ impl MiriEnv {
219232 break ;
220233 }
221234 // Run the command with this seed.
222- run ( & local_shell , cur) . inspect_err ( |_| {
235+ run ( & local_miri , cur) . inspect_err ( |_| {
223236 // If we failed, tell everyone about this.
224237 failed. store ( true , Ordering :: Relaxed ) ;
225238 } ) ?;
0 commit comments