1
1
use std:: ffi:: { OsStr , OsString } ;
2
+ use std:: fmt:: Write ;
2
3
use std:: ops:: Range ;
3
4
use std:: path:: { Path , PathBuf } ;
4
5
use std:: sync:: atomic:: { AtomicBool , AtomicU32 , Ordering } ;
@@ -7,7 +8,7 @@ use std::thread;
7
8
use anyhow:: { anyhow, Context , Result } ;
8
9
use dunce:: canonicalize;
9
10
use path_macro:: path;
10
- use xshell:: { cmd, Shell } ;
11
+ use xshell:: { cmd, Cmd , Shell } ;
11
12
12
13
pub fn miri_dir ( ) -> std:: io:: Result < PathBuf > {
13
14
const MIRI_SCRIPT_ROOT_DIR : & str = env ! ( "CARGO_MANIFEST_DIR" ) ;
@@ -28,13 +29,14 @@ pub fn flagsplit(flags: &str) -> Vec<String> {
28
29
}
29
30
30
31
/// Some extra state we track for building Miri, such as the right RUSTFLAGS.
32
+ #[ derive( Clone ) ]
31
33
pub struct MiriEnv {
32
34
/// miri_dir is the root of the miri repository checkout we are working in.
33
35
pub miri_dir : PathBuf ,
34
36
/// active_toolchain is passed as `+toolchain` argument to cargo/rustc invocations.
35
37
pub toolchain : String ,
36
38
/// Extra flags to pass to cargo.
37
- pub cargo_extra_flags : Vec < String > ,
39
+ cargo_extra_flags : Vec < String > ,
38
40
/// The rustc sysroot
39
41
pub sysroot : PathBuf ,
40
42
/// The shell we use.
@@ -59,10 +61,6 @@ impl MiriEnv {
59
61
println ! ( "Please report a bug at https://github.com/rust-lang/miri/issues." ) ;
60
62
std:: process:: exit ( 2 ) ;
61
63
}
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) ;
66
64
67
65
// We configure dev builds to not be unusably slow.
68
66
let devel_opt_level =
@@ -95,13 +93,37 @@ impl MiriEnv {
95
93
Ok ( MiriEnv { miri_dir, toolchain, sh, sysroot, cargo_extra_flags } )
96
94
}
97
95
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
+
98
119
pub fn install_to_sysroot (
99
120
& self ,
100
121
path : impl AsRef < OsStr > ,
101
122
args : impl IntoIterator < Item = impl AsRef < OsStr > > ,
102
123
) -> Result < ( ) > {
103
124
let MiriEnv { sysroot, toolchain, cargo_extra_flags, .. } = self ;
104
125
// 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`.)
105
127
cmd ! ( self . sh, "cargo +{toolchain} install {cargo_extra_flags...} --path {path} --force --root {sysroot} {args...}" ) . run ( ) ?;
106
128
Ok ( ( ) )
107
129
}
@@ -112,40 +134,31 @@ impl MiriEnv {
112
134
args : & [ String ] ,
113
135
quiet : bool ,
114
136
) -> Result < ( ) > {
115
- let MiriEnv { toolchain, cargo_extra_flags, .. } = self ;
116
137
let quiet_flag = if quiet { Some ( "--quiet" ) } else { None } ;
117
138
// We build the tests as well, (a) to avoid having rebuilds when building the tests later
118
139
// 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) ;
123
145
cmd. set_quiet ( quiet) ;
124
146
cmd. run ( ) ?;
125
147
Ok ( ( ) )
126
148
}
127
149
128
150
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 ( ) ?;
132
152
Ok ( ( ) )
133
153
}
134
154
135
155
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 ( ) ?;
139
157
Ok ( ( ) )
140
158
}
141
159
142
160
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 ( ) ?;
149
162
Ok ( ( ) )
150
163
}
151
164
@@ -155,7 +168,6 @@ impl MiriEnv {
155
168
pub fn format_files (
156
169
& self ,
157
170
files : impl Iterator < Item = Result < PathBuf , walkdir:: Error > > ,
158
- toolchain : & str ,
159
171
config_path : & Path ,
160
172
flags : & [ String ] ,
161
173
) -> anyhow:: Result < ( ) > {
@@ -166,6 +178,7 @@ impl MiriEnv {
166
178
// Format in batches as not all our files fit into Windows' command argument limit.
167
179
for batch in & files. chunks ( 256 ) {
168
180
// Build base command.
181
+ let toolchain = & self . toolchain ;
169
182
let mut cmd = cmd ! (
170
183
self . sh,
171
184
"rustfmt +{toolchain} --edition=2021 --config-path {config_path} --unstable-features --skip-children {flags...}"
@@ -197,7 +210,7 @@ impl MiriEnv {
197
210
pub fn run_many_times (
198
211
& self ,
199
212
range : Range < u32 > ,
200
- run : impl Fn ( & Shell , u32 ) -> Result < ( ) > + Sync ,
213
+ run : impl Fn ( & Self , u32 ) -> Result < ( ) > + Sync ,
201
214
) -> Result < ( ) > {
202
215
// `next` is atomic so threads can concurrently fetch their next value to run.
203
216
let next = AtomicU32 :: new ( range. start ) ;
@@ -207,10 +220,10 @@ impl MiriEnv {
207
220
let mut handles = Vec :: new ( ) ;
208
221
// Spawn one worker per core.
209
222
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 ( ) ;
212
225
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.
214
227
// Each worker thread keeps asking for numbers until we're all done.
215
228
loop {
216
229
let cur = next. fetch_add ( 1 , Ordering :: Relaxed ) ;
@@ -219,7 +232,7 @@ impl MiriEnv {
219
232
break ;
220
233
}
221
234
// Run the command with this seed.
222
- run ( & local_shell , cur) . inspect_err ( |_| {
235
+ run ( & local_miri , cur) . inspect_err ( |_| {
223
236
// If we failed, tell everyone about this.
224
237
failed. store ( true , Ordering :: Relaxed ) ;
225
238
} ) ?;
0 commit comments