2
2
3
3
extern crate cargo_metadata;
4
4
5
- use std:: path :: { PathBuf , Path } ;
5
+ use std:: fs :: { self , File } ;
6
6
use std:: io:: { self , Write , BufRead } ;
7
+ use std:: path:: { PathBuf , Path } ;
7
8
use std:: process:: Command ;
8
- use std:: fs:: { self , File } ;
9
9
10
10
const CARGO_MIRI_HELP : & str = r#"Interprets bin crates and tests in Miri
11
11
@@ -55,15 +55,15 @@ fn show_error(msg: String) -> ! {
55
55
std:: process:: exit ( 1 )
56
56
}
57
57
58
- // Determines whether a --flag is present
58
+ // Determines whether a ` --flag` is present.
59
59
fn has_arg_flag ( name : & str ) -> bool {
60
60
let mut args = std:: env:: args ( ) . take_while ( |val| val != "--" ) ;
61
61
args. any ( |val| val == name)
62
62
}
63
63
64
- /// Gets the value of a --flag
64
+ /// Gets the value of a ` --flag`.
65
65
fn get_arg_flag_value ( name : & str ) -> Option < String > {
66
- // stop searching at `--`
66
+ // Stop searching at `--`.
67
67
let mut args = std:: env:: args ( ) . take_while ( |val| val != "--" ) ;
68
68
loop {
69
69
let arg = match args. next ( ) {
@@ -73,13 +73,15 @@ fn get_arg_flag_value(name: &str) -> Option<String> {
73
73
if !arg. starts_with ( name) {
74
74
continue ;
75
75
}
76
- let suffix = & arg[ name. len ( ) ..] ; // strip leading `name`
76
+ // Strip leading `name`.
77
+ let suffix = & arg[ name. len ( ) ..] ;
77
78
if suffix. is_empty ( ) {
78
- // This argument is exactly `name`, the next one is the value
79
+ // This argument is exactly `name`; the next one is the value.
79
80
return args. next ( ) ;
80
81
} else if suffix. starts_with ( '=' ) {
81
- // This argument is `name=value`, get the value
82
- return Some ( suffix[ 1 ..] . to_owned ( ) ) ; // strip leading `=`
82
+ // This argument is `name=value`; get the value.
83
+ // Strip leading `=`.
84
+ return Some ( suffix[ 1 ..] . to_owned ( ) ) ;
83
85
}
84
86
}
85
87
}
@@ -96,7 +98,7 @@ fn list_targets() -> impl Iterator<Item=cargo_metadata::Target> {
96
98
{
97
99
metadata
98
100
} else {
99
- show_error ( format ! ( "error: Could not obtain cargo metadata. " ) ) ;
101
+ show_error ( format ! ( "Could not obtain Cargo metadata" ) ) ;
100
102
} ;
101
103
102
104
let current_dir = std:: env:: current_dir ( ) ;
@@ -167,20 +169,22 @@ fn ask(question: &str) {
167
169
io:: stdout ( ) . flush ( ) . unwrap ( ) ;
168
170
io:: stdin ( ) . read_line ( & mut buf) . unwrap ( ) ;
169
171
match buf. trim ( ) . to_lowercase ( ) . as_ref ( ) {
170
- "" | "y" | "yes" => { } , // proceed
172
+ // Proceed.
173
+ "" | "y" | "yes" => { } ,
171
174
"n" | "no" => show_error ( format ! ( "Aborting as per your request" ) ) ,
172
175
a => show_error ( format ! ( "I do not understand `{}`" , a) )
173
176
} ;
174
177
}
175
178
176
- /// Perform the setup requires to make `cargo miri` work: Getting a custom-built libstd. Then sets MIRI_SYSROOT.
177
- /// Skipped if MIRI_SYSROOT is already set, in that case we expect the user has done all this already.
179
+ /// Performs the setup required to make `cargo miri` work: Getting a custom-built libstd. Then sets
180
+ /// `MIRI_SYSROOT`. Skipped if `MIRI_SYSROOT` is already set, in which case we expect the user has
181
+ /// done all this already.
178
182
fn setup ( ask_user : bool ) {
179
183
if std:: env:: var ( "MIRI_SYSROOT" ) . is_ok ( ) {
180
184
return ;
181
185
}
182
186
183
- // First, we need xargo
187
+ // First, we need xargo.
184
188
let xargo = xargo_version ( ) ;
185
189
if xargo. map_or ( true , |v| v < ( 0 , 3 , 13 ) ) {
186
190
if ask_user {
@@ -193,7 +197,7 @@ fn setup(ask_user: bool) {
193
197
}
194
198
}
195
199
196
- // Then, unless XARGO_RUST_SRC is set, we also need rust-src.
200
+ // Then, unless ` XARGO_RUST_SRC` is set, we also need rust-src.
197
201
// Let's see if it is already installed.
198
202
if std:: env:: var ( "XARGO_RUST_SRC" ) . is_err ( ) {
199
203
let sysroot = Command :: new ( "rustc" ) . args ( & [ "--print" , "sysroot" ] ) . output ( ) . unwrap ( ) . stdout ;
@@ -229,7 +233,7 @@ features = ["panic_unwind"]
229
233
[dependencies.test]
230
234
stage = 1
231
235
"# ) . unwrap ( ) ;
232
- // The boring bits: A dummy project for xargo
236
+ // The boring bits: a dummy project for xargo.
233
237
File :: create ( dir. join ( "Cargo.toml" ) ) . unwrap ( )
234
238
. write_all ( br#"
235
239
[package]
@@ -241,7 +245,7 @@ version = "0.0.0"
241
245
path = "lib.rs"
242
246
"# ) . unwrap ( ) ;
243
247
File :: create ( dir. join ( "lib.rs" ) ) . unwrap ( ) ;
244
- // Run xargo
248
+ // Run xargo.
245
249
let target = get_arg_flag_value ( "--target" ) ;
246
250
let mut command = Command :: new ( "xargo" ) ;
247
251
command. arg ( "build" ) . arg ( "-q" )
@@ -256,7 +260,7 @@ path = "lib.rs"
256
260
show_error ( format ! ( "Failed to run xargo" ) ) ;
257
261
}
258
262
259
- // That should be it! But we need to figure out where xargo built stuff.
263
+ // That should be it! But we need to figure out where xargo built stuff.
260
264
// Unfortunately, it puts things into a different directory when the
261
265
// architecture matches the host.
262
266
let is_host = match target {
@@ -271,7 +275,7 @@ path = "lib.rs"
271
275
}
272
276
273
277
fn main ( ) {
274
- // Check for version and help flags even when invoked as ' cargo-miri'
278
+ // Check for version and help flags even when invoked as ` cargo-miri`.
275
279
if std:: env:: args ( ) . any ( |a| a == "--help" || a == "-h" ) {
276
280
show_help ( ) ;
277
281
return ;
@@ -282,17 +286,16 @@ fn main() {
282
286
}
283
287
284
288
if let Some ( "miri" ) = std:: env:: args ( ) . nth ( 1 ) . as_ref ( ) . map ( AsRef :: as_ref) {
285
- // this arm is when `cargo miri` is called. We call `cargo rustc` for
286
- // each applicable target, but with the RUSTC env var set to the `cargo-miri`
287
- // binary so that we come back in the other branch, and dispatch
288
- // the invocations to rustc and miri, respectively.
289
+ // This arm is for when `cargo miri` is called. We call `cargo rustc` for each applicable target,
290
+ // but with the `RUSTC` env var set to the `cargo-miri` binary so that we come back in the other branch,
291
+ // and dispatch the invocations to `rustc` and `miri`, respectively.
289
292
in_cargo_miri ( ) ;
290
293
} else if let Some ( "rustc" ) = std:: env:: args ( ) . nth ( 1 ) . as_ref ( ) . map ( AsRef :: as_ref) {
291
- // This arm is executed when cargo-miri runs `cargo rustc` with the `RUSTC_WRAPPER` env var set to itself:
292
- // Dependencies get dispatched to rustc, the final test/binary to miri.
294
+ // This arm is executed when ` cargo-miri` runs `cargo rustc` with the `RUSTC_WRAPPER` env var set to itself:
295
+ // dependencies get dispatched to ` rustc` , the final test/binary to ` miri` .
293
296
inside_cargo_rustc ( ) ;
294
297
} else {
295
- show_error ( format ! ( "Must be called with either `miri` or `rustc` as first argument." ) )
298
+ show_error ( format ! ( "must be called with either `miri` or `rustc` as first argument." ) )
296
299
}
297
300
}
298
301
@@ -301,17 +304,17 @@ fn in_cargo_miri() {
301
304
Some ( "test" ) => ( MiriCommand :: Test , 3 ) ,
302
305
Some ( "run" ) => ( MiriCommand :: Run , 3 ) ,
303
306
Some ( "setup" ) => ( MiriCommand :: Setup , 3 ) ,
304
- // Default command, if there is an option or nothing
307
+ // Default command, if there is an option or nothing.
305
308
Some ( s) if s. starts_with ( "-" ) => ( MiriCommand :: Run , 2 ) ,
306
309
None => ( MiriCommand :: Run , 2 ) ,
307
- // Unvalid command
310
+ // Invalid command.
308
311
Some ( s) => {
309
312
show_error ( format ! ( "Unknown command `{}`" , s) )
310
313
}
311
314
} ;
312
315
let verbose = has_arg_flag ( "-v" ) ;
313
316
314
- // We always setup
317
+ // We always setup.
315
318
let ask = subcommand != MiriCommand :: Setup ;
316
319
setup ( ask) ;
317
320
if subcommand == MiriCommand :: Setup {
@@ -326,41 +329,41 @@ fn in_cargo_miri() {
326
329
"badly formatted cargo metadata: target::kind is an empty array" ,
327
330
) ;
328
331
// Now we run `cargo rustc $FLAGS $ARGS`, giving the user the
329
- // change to add additional flags. " FLAGS" is set to identify
332
+ // change to add additional flags. ` FLAGS` is set to identify
330
333
// this target. The user gets to control what gets actually passed to Miri.
331
334
// However, we need to add a flag to what gets passed to rustc for the finaly
332
335
// binary, so that we know to interpret that with Miri.
333
- // So after the first "--" , we add " -Zcargo-miri-marker" .
336
+ // So after the first `--` , we add ` -Zcargo-miri-marker` .
334
337
let mut cmd = Command :: new ( "cargo" ) ;
335
338
cmd. arg ( "rustc" ) ;
336
339
match ( subcommand, & kind[ ..] ) {
337
340
( MiriCommand :: Run , "bin" ) => {
338
- // FIXME: We just run all the binaries here.
341
+ // FIXME: we just run all the binaries here.
339
342
// We should instead support `cargo miri --bin foo`.
340
343
cmd. arg ( "--bin" ) . arg ( target. name ) ;
341
344
}
342
345
( MiriCommand :: Test , "test" ) => {
343
346
cmd. arg ( "--test" ) . arg ( target. name ) ;
344
347
}
345
348
( MiriCommand :: Test , "lib" ) => {
346
- // There can be only one lib
349
+ // There can be only one lib.
347
350
cmd. arg ( "--lib" ) . arg ( "--profile" ) . arg ( "test" ) ;
348
351
}
349
352
( MiriCommand :: Test , "bin" ) => {
350
353
cmd. arg ( "--bin" ) . arg ( target. name ) . arg ( "--profile" ) . arg ( "test" ) ;
351
354
}
352
- // The remaining targets we do not even want to build
355
+ // The remaining targets we do not even want to build.
353
356
_ => continue ,
354
357
}
355
- // add user-defined args until first "--"
358
+ // Add user-defined args until first `--`.
356
359
while let Some ( arg) = args. next ( ) {
357
360
if arg == "--" {
358
361
break ;
359
362
}
360
363
cmd. arg ( arg) ;
361
364
}
362
- // Add "--" (to end the cargo flags), and then the user flags. We add markers around the user flags
363
- // to be able to identify them later.
365
+ // Add `--` (to end the ` cargo` flags), and then the user flags. We add markers around the
366
+ // user flags to be able to identify them later.
364
367
cmd
365
368
. arg ( "--" )
366
369
. arg ( "cargo-miri-marker-begin" )
@@ -403,11 +406,11 @@ fn inside_cargo_rustc() {
403
406
. and_then ( |out| String :: from_utf8 ( out. stdout ) . ok ( ) )
404
407
. map ( |s| s. trim ( ) . to_owned ( ) )
405
408
} )
406
- . expect ( "need to specify RUST_SYSROOT env var during miri compilation, or use rustup or multirust" )
409
+ . expect ( "need to specify ` RUST_SYSROOT` env var during miri compilation, or use rustup or multirust" )
407
410
} ;
408
411
409
- // this conditional check for the --sysroot flag is there so users can call `cargo-miri` directly
410
- // without having to pass --sysroot or anything
412
+ // This conditional check for the ` --sysroot` flag is there so that users can call `cargo-miri`
413
+ // directly without having to pass ` --sysroot` or anything.
411
414
let rustc_args = std:: env:: args ( ) . skip ( 2 ) ;
412
415
let mut args: Vec < String > = if std:: env:: args ( ) . any ( |s| s == "--sysroot" ) {
413
416
rustc_args. collect ( )
@@ -419,25 +422,27 @@ fn inside_cargo_rustc() {
419
422
} ;
420
423
args. splice ( 0 ..0 , miri:: miri_default_args ( ) . iter ( ) . map ( ToString :: to_string) ) ;
421
424
422
- // See if we can find the cargo-miri markers. Those only get added to the binary we want to
423
- // run. They also serve to mark the user-defined arguments, which we have to move all the way to the
424
- // end (they get added somewhere in the middle).
425
+ // See if we can find the ` cargo-miri` markers. Those only get added to the binary we want to
426
+ // run. They also serve to mark the user-defined arguments, which we have to move all the way
427
+ // to the end (they get added somewhere in the middle).
425
428
let needs_miri = if let Some ( begin) = args. iter ( ) . position ( |arg| arg == "cargo-miri-marker-begin" ) {
426
- let end = args. iter ( ) . position ( |arg| arg == "cargo-miri-marker-end" ) . expect ( "Cannot find end marker" ) ;
427
- // These mark the user arguments. We remove the first and last as they are the markers.
429
+ let end = args
430
+ . iter ( )
431
+ . position ( |arg| arg == "cargo-miri-marker-end" )
432
+ . expect ( "cannot find end marker" ) ;
433
+ // These mark the user arguments. We remove the first and last as they are the markers.
428
434
let mut user_args = args. drain ( begin..=end) ;
429
435
assert_eq ! ( user_args. next( ) . unwrap( ) , "cargo-miri-marker-begin" ) ;
430
436
assert_eq ! ( user_args. next_back( ) . unwrap( ) , "cargo-miri-marker-end" ) ;
431
- // Collect the rest and add it back at the end
437
+ // Collect the rest and add it back at the end.
432
438
let mut user_args = user_args. collect :: < Vec < String > > ( ) ;
433
439
args. append ( & mut user_args) ;
434
- // Run this in Miri
440
+ // Run this in Miri.
435
441
true
436
442
} else {
437
443
false
438
444
} ;
439
445
440
-
441
446
let mut command = if needs_miri {
442
447
let mut path = std:: env:: current_exe ( ) . expect ( "current executable path invalid" ) ;
443
448
path. set_file_name ( "miri" ) ;
0 commit comments