@@ -7,7 +7,7 @@ use std::thread;
7
7
use anyhow:: { anyhow, Context , Result } ;
8
8
use dunce:: canonicalize;
9
9
use path_macro:: path;
10
- use xshell:: { cmd, Shell } ;
10
+ use xshell:: { cmd, Cmd , Shell } ;
11
11
12
12
pub fn miri_dir ( ) -> std:: io:: Result < PathBuf > {
13
13
const MIRI_SCRIPT_ROOT_DIR : & str = env ! ( "CARGO_MANIFEST_DIR" ) ;
@@ -28,13 +28,14 @@ pub fn flagsplit(flags: &str) -> Vec<String> {
28
28
}
29
29
30
30
/// Some extra state we track for building Miri, such as the right RUSTFLAGS.
31
+ #[ derive( Clone ) ]
31
32
pub struct MiriEnv {
32
33
/// miri_dir is the root of the miri repository checkout we are working in.
33
34
pub miri_dir : PathBuf ,
34
35
/// active_toolchain is passed as `+toolchain` argument to cargo/rustc invocations.
35
36
pub toolchain : String ,
36
37
/// Extra flags to pass to cargo.
37
- pub cargo_extra_flags : Vec < String > ,
38
+ cargo_extra_flags : Vec < String > ,
38
39
/// The rustc sysroot
39
40
pub sysroot : PathBuf ,
40
41
/// The shell we use.
@@ -54,15 +55,14 @@ impl MiriEnv {
54
55
55
56
// Determine some toolchain properties
56
57
if !libdir. exists ( ) {
57
- println ! ( "Something went wrong determining the library dir." ) ;
58
- println ! ( "I got {} but that does not exist." , libdir. display( ) ) ;
59
- println ! ( "Please report a bug at https://github.com/rust-lang/miri/issues." ) ;
58
+ eprintln ! ( "Something went wrong determining the library dir." ) ;
59
+ eprintln ! ( "I got {} but that does not exist." , libdir. display( ) ) ;
60
+ eprintln ! ( "Please report a bug at https://github.com/rust-lang/miri/issues." ) ;
60
61
std:: process:: exit ( 2 ) ;
61
62
}
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) ;
63
+
64
+ // Hard-code the target dir, since we rely on all binaries ending up in the same spot.
65
+ sh. set_var ( "CARGO_TARGET_DIR" , path ! ( miri_dir / "target" ) ) ;
66
66
67
67
// We configure dev builds to not be unusably slow.
68
68
let devel_opt_level =
@@ -91,17 +91,34 @@ impl MiriEnv {
91
91
// Get extra flags for cargo.
92
92
let cargo_extra_flags = std:: env:: var ( "CARGO_EXTRA_FLAGS" ) . unwrap_or_default ( ) ;
93
93
let cargo_extra_flags = flagsplit ( & cargo_extra_flags) ;
94
+ if cargo_extra_flags. iter ( ) . any ( |a| a == "--release" || a. starts_with ( "--profile" ) ) {
95
+ // This makes binaries end up in different paths, let's not do that.
96
+ eprintln ! (
97
+ "Passing `--release` or `--profile` in `CARGO_EXTRA_FLAGS` will totally confuse miri-script, please don't do that."
98
+ ) ;
99
+ std:: process:: exit ( 1 ) ;
100
+ }
94
101
95
102
Ok ( MiriEnv { miri_dir, toolchain, sh, sysroot, cargo_extra_flags } )
96
103
}
97
104
105
+ pub fn cargo_cmd ( & self , manifest_path : impl AsRef < OsStr > , cmd : & str ) -> Cmd < ' _ > {
106
+ let MiriEnv { toolchain, cargo_extra_flags, .. } = self ;
107
+ let manifest_path = Path :: new ( manifest_path. as_ref ( ) ) ;
108
+ cmd ! (
109
+ self . sh,
110
+ "cargo +{toolchain} {cmd} {cargo_extra_flags...} --manifest-path {manifest_path}"
111
+ )
112
+ }
113
+
98
114
pub fn install_to_sysroot (
99
115
& self ,
100
116
path : impl AsRef < OsStr > ,
101
117
args : impl IntoIterator < Item = impl AsRef < OsStr > > ,
102
118
) -> Result < ( ) > {
103
119
let MiriEnv { sysroot, toolchain, cargo_extra_flags, .. } = self ;
104
120
// Install binaries to the miri toolchain's `sysroot` so they do not interact with other toolchains.
121
+ // (Not using `cargo_cmd` as `install` is special and doesn't use `--manifest-path`.)
105
122
cmd ! ( self . sh, "cargo +{toolchain} install {cargo_extra_flags...} --path {path} --force --root {sysroot} {args...}" ) . run ( ) ?;
106
123
Ok ( ( ) )
107
124
}
@@ -112,40 +129,34 @@ impl MiriEnv {
112
129
args : & [ String ] ,
113
130
quiet : bool ,
114
131
) -> Result < ( ) > {
115
- let MiriEnv { toolchain, cargo_extra_flags, .. } = self ;
116
132
let quiet_flag = if quiet { Some ( "--quiet" ) } else { None } ;
117
133
// We build the tests as well, (a) to avoid having rebuilds when building the tests later
118
134
// 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
- ) ;
135
+ // This means `./miri run` without `--dep` will build Miri twice (for the sysroot with
136
+ // dev-dependencies, and then for running without dev-dependencies), but the way more common
137
+ // `./miri test` will avoid building Miri twice.
138
+ let mut cmd = self
139
+ . cargo_cmd ( manifest_path, "build" )
140
+ . args ( & [ "--bins" , "--tests" ] )
141
+ . args ( quiet_flag)
142
+ . args ( args) ;
123
143
cmd. set_quiet ( quiet) ;
124
144
cmd. run ( ) ?;
125
145
Ok ( ( ) )
126
146
}
127
147
128
148
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 ( ) ?;
149
+ self . cargo_cmd ( manifest_path, "check" ) . arg ( "--all-targets" ) . args ( args) . run ( ) ?;
132
150
Ok ( ( ) )
133
151
}
134
152
135
153
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 ( ) ?;
154
+ self . cargo_cmd ( manifest_path, "clippy" ) . arg ( "--all-targets" ) . args ( args) . run ( ) ?;
139
155
Ok ( ( ) )
140
156
}
141
157
142
158
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 ( ) ?;
159
+ self . cargo_cmd ( manifest_path, "test" ) . args ( args) . run ( ) ?;
149
160
Ok ( ( ) )
150
161
}
151
162
@@ -155,7 +166,6 @@ impl MiriEnv {
155
166
pub fn format_files (
156
167
& self ,
157
168
files : impl Iterator < Item = Result < PathBuf , walkdir:: Error > > ,
158
- toolchain : & str ,
159
169
config_path : & Path ,
160
170
flags : & [ String ] ,
161
171
) -> anyhow:: Result < ( ) > {
@@ -166,6 +176,7 @@ impl MiriEnv {
166
176
// Format in batches as not all our files fit into Windows' command argument limit.
167
177
for batch in & files. chunks ( 256 ) {
168
178
// Build base command.
179
+ let toolchain = & self . toolchain ;
169
180
let mut cmd = cmd ! (
170
181
self . sh,
171
182
"rustfmt +{toolchain} --edition=2021 --config-path {config_path} --unstable-features --skip-children {flags...}"
@@ -197,7 +208,7 @@ impl MiriEnv {
197
208
pub fn run_many_times (
198
209
& self ,
199
210
range : Range < u32 > ,
200
- run : impl Fn ( & Shell , u32 ) -> Result < ( ) > + Sync ,
211
+ run : impl Fn ( & Self , u32 ) -> Result < ( ) > + Sync ,
201
212
) -> Result < ( ) > {
202
213
// `next` is atomic so threads can concurrently fetch their next value to run.
203
214
let next = AtomicU32 :: new ( range. start ) ;
@@ -207,10 +218,10 @@ impl MiriEnv {
207
218
let mut handles = Vec :: new ( ) ;
208
219
// Spawn one worker per core.
209
220
for _ in 0 ..thread:: available_parallelism ( ) ?. get ( ) {
210
- // Create a copy of the shell for this thread.
211
- let local_shell = self . sh . clone ( ) ;
221
+ // Create a copy of the environment for this thread.
222
+ let local_miri = self . clone ( ) ;
212
223
let handle = s. spawn ( || -> Result < ( ) > {
213
- let local_shell = local_shell ; // move the copy into this thread.
224
+ let local_miri = local_miri ; // move the copy into this thread.
214
225
// Each worker thread keeps asking for numbers until we're all done.
215
226
loop {
216
227
let cur = next. fetch_add ( 1 , Ordering :: Relaxed ) ;
@@ -219,7 +230,7 @@ impl MiriEnv {
219
230
break ;
220
231
}
221
232
// Run the command with this seed.
222
- run ( & local_shell , cur) . inspect_err ( |_| {
233
+ run ( & local_miri , cur) . inspect_err ( |_| {
223
234
// If we failed, tell everyone about this.
224
235
failed. store ( true , Ordering :: Relaxed ) ;
225
236
} ) ?;
0 commit comments