22
33extern crate cargo_metadata;
44
5- use std:: path :: { PathBuf , Path } ;
5+ use std:: fs :: { self , File } ;
66use std:: io:: { self , Write , BufRead } ;
7+ use std:: path:: { PathBuf , Path } ;
78use std:: process:: Command ;
8- use std:: fs:: { self , File } ;
99
1010const CARGO_MIRI_HELP : & str = r#"Interprets bin crates and tests in Miri
1111
@@ -55,15 +55,15 @@ fn show_error(msg: String) -> ! {
5555 std:: process:: exit ( 1 )
5656}
5757
58- // Determines whether a --flag is present
58+ // Determines whether a ` --flag` is present.
5959fn has_arg_flag ( name : & str ) -> bool {
6060 let mut args = std:: env:: args ( ) . take_while ( |val| val != "--" ) ;
6161 args. any ( |val| val == name)
6262}
6363
64- /// Gets the value of a --flag
64+ /// Gets the value of a ` --flag`.
6565fn get_arg_flag_value ( name : & str ) -> Option < String > {
66- // stop searching at `--`
66+ // Stop searching at `--`.
6767 let mut args = std:: env:: args ( ) . take_while ( |val| val != "--" ) ;
6868 loop {
6969 let arg = match args. next ( ) {
@@ -73,13 +73,15 @@ fn get_arg_flag_value(name: &str) -> Option<String> {
7373 if !arg. starts_with ( name) {
7474 continue ;
7575 }
76- let suffix = & arg[ name. len ( ) ..] ; // strip leading `name`
76+ // Strip leading `name`.
77+ let suffix = & arg[ name. len ( ) ..] ;
7778 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.
7980 return args. next ( ) ;
8081 } 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 ( ) ) ;
8385 }
8486 }
8587}
@@ -96,7 +98,7 @@ fn list_targets() -> impl Iterator<Item=cargo_metadata::Target> {
9698 {
9799 metadata
98100 } else {
99- show_error ( format ! ( "error: Could not obtain cargo metadata. " ) ) ;
101+ show_error ( format ! ( "Could not obtain Cargo metadata" ) ) ;
100102 } ;
101103
102104 let current_dir = std:: env:: current_dir ( ) ;
@@ -167,20 +169,22 @@ fn ask(question: &str) {
167169 io:: stdout ( ) . flush ( ) . unwrap ( ) ;
168170 io:: stdin ( ) . read_line ( & mut buf) . unwrap ( ) ;
169171 match buf. trim ( ) . to_lowercase ( ) . as_ref ( ) {
170- "" | "y" | "yes" => { } , // proceed
172+ // Proceed.
173+ "" | "y" | "yes" => { } ,
171174 "n" | "no" => show_error ( format ! ( "Aborting as per your request" ) ) ,
172175 a => show_error ( format ! ( "I do not understand `{}`" , a) )
173176 } ;
174177}
175178
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.
178182fn setup ( ask_user : bool ) {
179183 if std:: env:: var ( "MIRI_SYSROOT" ) . is_ok ( ) {
180184 return ;
181185 }
182186
183- // First, we need xargo
187+ // First, we need xargo.
184188 let xargo = xargo_version ( ) ;
185189 if xargo. map_or ( true , |v| v < ( 0 , 3 , 13 ) ) {
186190 if ask_user {
@@ -193,7 +197,7 @@ fn setup(ask_user: bool) {
193197 }
194198 }
195199
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.
197201 // Let's see if it is already installed.
198202 if std:: env:: var ( "XARGO_RUST_SRC" ) . is_err ( ) {
199203 let sysroot = Command :: new ( "rustc" ) . args ( & [ "--print" , "sysroot" ] ) . output ( ) . unwrap ( ) . stdout ;
@@ -229,7 +233,7 @@ features = ["panic_unwind"]
229233[dependencies.test]
230234stage = 1
231235 "# ) . unwrap ( ) ;
232- // The boring bits: A dummy project for xargo
236+ // The boring bits: a dummy project for xargo.
233237 File :: create ( dir. join ( "Cargo.toml" ) ) . unwrap ( )
234238 . write_all ( br#"
235239[package]
@@ -241,7 +245,7 @@ version = "0.0.0"
241245path = "lib.rs"
242246 "# ) . unwrap ( ) ;
243247 File :: create ( dir. join ( "lib.rs" ) ) . unwrap ( ) ;
244- // Run xargo
248+ // Run xargo.
245249 let target = get_arg_flag_value ( "--target" ) ;
246250 let mut command = Command :: new ( "xargo" ) ;
247251 command. arg ( "build" ) . arg ( "-q" )
@@ -256,7 +260,7 @@ path = "lib.rs"
256260 show_error ( format ! ( "Failed to run xargo" ) ) ;
257261 }
258262
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.
260264 // Unfortunately, it puts things into a different directory when the
261265 // architecture matches the host.
262266 let is_host = match target {
@@ -271,7 +275,7 @@ path = "lib.rs"
271275}
272276
273277fn 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`.
275279 if std:: env:: args ( ) . any ( |a| a == "--help" || a == "-h" ) {
276280 show_help ( ) ;
277281 return ;
@@ -282,17 +286,16 @@ fn main() {
282286 }
283287
284288 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.
289292 in_cargo_miri ( ) ;
290293 } 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` .
293296 inside_cargo_rustc ( ) ;
294297 } 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." ) )
296299 }
297300}
298301
@@ -301,17 +304,17 @@ fn in_cargo_miri() {
301304 Some ( "test" ) => ( MiriCommand :: Test , 3 ) ,
302305 Some ( "run" ) => ( MiriCommand :: Run , 3 ) ,
303306 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.
305308 Some ( s) if s. starts_with ( "-" ) => ( MiriCommand :: Run , 2 ) ,
306309 None => ( MiriCommand :: Run , 2 ) ,
307- // Unvalid command
310+ // Invalid command.
308311 Some ( s) => {
309312 show_error ( format ! ( "Unknown command `{}`" , s) )
310313 }
311314 } ;
312315 let verbose = has_arg_flag ( "-v" ) ;
313316
314- // We always setup
317+ // We always setup.
315318 let ask = subcommand != MiriCommand :: Setup ;
316319 setup ( ask) ;
317320 if subcommand == MiriCommand :: Setup {
@@ -326,41 +329,41 @@ fn in_cargo_miri() {
326329 "badly formatted cargo metadata: target::kind is an empty array" ,
327330 ) ;
328331 // 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
330333 // this target. The user gets to control what gets actually passed to Miri.
331334 // However, we need to add a flag to what gets passed to rustc for the finaly
332335 // 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` .
334337 let mut cmd = Command :: new ( "cargo" ) ;
335338 cmd. arg ( "rustc" ) ;
336339 match ( subcommand, & kind[ ..] ) {
337340 ( MiriCommand :: Run , "bin" ) => {
338- // FIXME: We just run all the binaries here.
341+ // FIXME: we just run all the binaries here.
339342 // We should instead support `cargo miri --bin foo`.
340343 cmd. arg ( "--bin" ) . arg ( target. name ) ;
341344 }
342345 ( MiriCommand :: Test , "test" ) => {
343346 cmd. arg ( "--test" ) . arg ( target. name ) ;
344347 }
345348 ( MiriCommand :: Test , "lib" ) => {
346- // There can be only one lib
349+ // There can be only one lib.
347350 cmd. arg ( "--lib" ) . arg ( "--profile" ) . arg ( "test" ) ;
348351 }
349352 ( MiriCommand :: Test , "bin" ) => {
350353 cmd. arg ( "--bin" ) . arg ( target. name ) . arg ( "--profile" ) . arg ( "test" ) ;
351354 }
352- // The remaining targets we do not even want to build
355+ // The remaining targets we do not even want to build.
353356 _ => continue ,
354357 }
355- // add user-defined args until first "--"
358+ // Add user-defined args until first `--`.
356359 while let Some ( arg) = args. next ( ) {
357360 if arg == "--" {
358361 break ;
359362 }
360363 cmd. arg ( arg) ;
361364 }
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.
364367 cmd
365368 . arg ( "--" )
366369 . arg ( "cargo-miri-marker-begin" )
@@ -403,11 +406,11 @@ fn inside_cargo_rustc() {
403406 . and_then ( |out| String :: from_utf8 ( out. stdout ) . ok ( ) )
404407 . map ( |s| s. trim ( ) . to_owned ( ) )
405408 } )
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" )
407410 } ;
408411
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.
411414 let rustc_args = std:: env:: args ( ) . skip ( 2 ) ;
412415 let mut args: Vec < String > = if std:: env:: args ( ) . any ( |s| s == "--sysroot" ) {
413416 rustc_args. collect ( )
@@ -419,25 +422,27 @@ fn inside_cargo_rustc() {
419422 } ;
420423 args. splice ( 0 ..0 , miri:: miri_default_args ( ) . iter ( ) . map ( ToString :: to_string) ) ;
421424
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).
425428 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.
428434 let mut user_args = args. drain ( begin..=end) ;
429435 assert_eq ! ( user_args. next( ) . unwrap( ) , "cargo-miri-marker-begin" ) ;
430436 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.
432438 let mut user_args = user_args. collect :: < Vec < String > > ( ) ;
433439 args. append ( & mut user_args) ;
434- // Run this in Miri
440+ // Run this in Miri.
435441 true
436442 } else {
437443 false
438444 } ;
439445
440-
441446 let mut command = if needs_miri {
442447 let mut path = std:: env:: current_exe ( ) . expect ( "current executable path invalid" ) ;
443448 path. set_file_name ( "miri" ) ;
0 commit comments