]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/flags.rs
Rollup merge of #94831 - yaahc:lib-tracking-issue-template-update, r=JohnTitor
[rust.git] / src / bootstrap / flags.rs
1 //! Command-line interface of the rustbuild build system.
2 //!
3 //! This module implements the command-line parsing of the build system which
4 //! has various flags to configure how it's run.
5
6 use std::path::PathBuf;
7 use std::process;
8
9 use getopts::Options;
10
11 use crate::builder::{Builder, Kind};
12 use crate::config::{Config, TargetSelection};
13 use crate::setup::Profile;
14 use crate::util::t;
15 use crate::{Build, DocTests};
16
17 #[derive(Copy, Clone)]
18 pub enum Color {
19     Always,
20     Never,
21     Auto,
22 }
23
24 impl Default for Color {
25     fn default() -> Self {
26         Self::Auto
27     }
28 }
29
30 impl std::str::FromStr for Color {
31     type Err = ();
32
33     fn from_str(s: &str) -> Result<Self, Self::Err> {
34         match s.to_lowercase().as_str() {
35             "always" => Ok(Self::Always),
36             "never" => Ok(Self::Never),
37             "auto" => Ok(Self::Auto),
38             _ => Err(()),
39         }
40     }
41 }
42
43 /// Deserialized version of all flags for this compile.
44 pub struct Flags {
45     pub verbose: usize, // number of -v args; each extra -v after the first is passed to Cargo
46     pub on_fail: Option<String>,
47     pub stage: Option<u32>,
48     pub keep_stage: Vec<u32>,
49     pub keep_stage_std: Vec<u32>,
50
51     pub host: Option<Vec<TargetSelection>>,
52     pub target: Option<Vec<TargetSelection>>,
53     pub config: Option<PathBuf>,
54     pub build_dir: Option<PathBuf>,
55     pub jobs: Option<u32>,
56     pub cmd: Subcommand,
57     pub incremental: bool,
58     pub exclude: Vec<PathBuf>,
59     pub include_default_paths: bool,
60     pub rustc_error_format: Option<String>,
61     pub json_output: bool,
62     pub dry_run: bool,
63     pub color: Color,
64
65     // This overrides the deny-warnings configuration option,
66     // which passes -Dwarnings to the compiler invocations.
67     //
68     // true => deny, false => warn
69     pub deny_warnings: Option<bool>,
70
71     pub llvm_skip_rebuild: Option<bool>,
72
73     pub rust_profile_use: Option<String>,
74     pub rust_profile_generate: Option<String>,
75
76     pub llvm_profile_use: Option<String>,
77     // LLVM doesn't support a custom location for generating profile
78     // information.
79     //
80     // llvm_out/build/profiles/ is the location this writes to.
81     pub llvm_profile_generate: bool,
82 }
83
84 #[cfg_attr(test, derive(Clone))]
85 pub enum Subcommand {
86     Build {
87         paths: Vec<PathBuf>,
88     },
89     Check {
90         paths: Vec<PathBuf>,
91     },
92     Clippy {
93         fix: bool,
94         paths: Vec<PathBuf>,
95     },
96     Fix {
97         paths: Vec<PathBuf>,
98     },
99     Format {
100         paths: Vec<PathBuf>,
101         check: bool,
102     },
103     Doc {
104         paths: Vec<PathBuf>,
105         open: bool,
106     },
107     Test {
108         paths: Vec<PathBuf>,
109         /// Whether to automatically update stderr/stdout files
110         bless: bool,
111         force_rerun: bool,
112         compare_mode: Option<String>,
113         pass: Option<String>,
114         run: Option<String>,
115         skip: Vec<String>,
116         test_args: Vec<String>,
117         rustc_args: Vec<String>,
118         fail_fast: bool,
119         doc_tests: DocTests,
120         rustfix_coverage: bool,
121     },
122     Bench {
123         paths: Vec<PathBuf>,
124         test_args: Vec<String>,
125     },
126     Clean {
127         all: bool,
128     },
129     Dist {
130         paths: Vec<PathBuf>,
131     },
132     Install {
133         paths: Vec<PathBuf>,
134     },
135     Run {
136         paths: Vec<PathBuf>,
137     },
138     Setup {
139         profile: Profile,
140     },
141 }
142
143 impl Default for Subcommand {
144     fn default() -> Subcommand {
145         Subcommand::Build { paths: vec![PathBuf::from("nowhere")] }
146     }
147 }
148
149 impl Flags {
150     pub fn parse(args: &[String]) -> Flags {
151         let mut subcommand_help = String::from(
152             "\
153 Usage: x.py <subcommand> [options] [<paths>...]
154
155 Subcommands:
156     build, b    Compile either the compiler or libraries
157     check, c    Compile either the compiler or libraries, using cargo check
158     clippy      Run clippy (uses rustup/cargo-installed clippy binary)
159     fix         Run cargo fix
160     fmt         Run rustfmt
161     test, t     Build and run some test suites
162     bench       Build and run some benchmarks
163     doc, d      Build documentation
164     clean       Clean out build directories
165     dist        Build distribution artifacts
166     install     Install distribution artifacts
167     run, r      Run tools contained in this repository
168     setup       Create a config.toml (making it easier to use `x.py` itself)
169
170 To learn more about a subcommand, run `./x.py <subcommand> -h`",
171         );
172
173         let mut opts = Options::new();
174         // Options common to all subcommands
175         opts.optflagmulti("v", "verbose", "use verbose output (-vv for very verbose)");
176         opts.optflag("i", "incremental", "use incremental compilation");
177         opts.optopt("", "config", "TOML configuration file for build", "FILE");
178         opts.optopt(
179             "",
180             "build-dir",
181             "Build directory, overrides `build.build-dir` in `config.toml`",
182             "DIR",
183         );
184         opts.optopt("", "build", "build target of the stage0 compiler", "BUILD");
185         opts.optmulti("", "host", "host targets to build", "HOST");
186         opts.optmulti("", "target", "target targets to build", "TARGET");
187         opts.optmulti("", "exclude", "build paths to exclude", "PATH");
188         opts.optflag(
189             "",
190             "include-default-paths",
191             "include default paths in addition to the provided ones",
192         );
193         opts.optopt("", "on-fail", "command to run on failure", "CMD");
194         opts.optflag("", "dry-run", "dry run; don't build anything");
195         opts.optopt(
196             "",
197             "stage",
198             "stage to build (indicates compiler to use/test, e.g., stage 0 uses the \
199              bootstrap compiler, stage 1 the stage 0 rustc artifacts, etc.)",
200             "N",
201         );
202         opts.optmulti(
203             "",
204             "keep-stage",
205             "stage(s) to keep without recompiling \
206             (pass multiple times to keep e.g., both stages 0 and 1)",
207             "N",
208         );
209         opts.optmulti(
210             "",
211             "keep-stage-std",
212             "stage(s) of the standard library to keep without recompiling \
213             (pass multiple times to keep e.g., both stages 0 and 1)",
214             "N",
215         );
216         opts.optopt("", "src", "path to the root of the rust checkout", "DIR");
217         let j_msg = format!(
218             "number of jobs to run in parallel; \
219              defaults to {} (this host's logical CPU count)",
220             num_cpus::get()
221         );
222         opts.optopt("j", "jobs", &j_msg, "JOBS");
223         opts.optflag("h", "help", "print this help message");
224         opts.optopt(
225             "",
226             "warnings",
227             "if value is deny, will deny warnings, otherwise use default",
228             "VALUE",
229         );
230         opts.optopt("", "error-format", "rustc error format", "FORMAT");
231         opts.optflag("", "json-output", "use message-format=json");
232         opts.optopt("", "color", "whether to use color in cargo and rustc output", "STYLE");
233         opts.optopt(
234             "",
235             "llvm-skip-rebuild",
236             "whether rebuilding llvm should be skipped \
237              a VALUE of TRUE indicates that llvm will not be rebuilt \
238              VALUE overrides the skip-rebuild option in config.toml.",
239             "VALUE",
240         );
241         opts.optopt(
242             "",
243             "rust-profile-generate",
244             "generate PGO profile with rustc build",
245             "PROFILE",
246         );
247         opts.optopt("", "rust-profile-use", "use PGO profile for rustc build", "PROFILE");
248         opts.optflag("", "llvm-profile-generate", "generate PGO profile with llvm built for rustc");
249         opts.optopt("", "llvm-profile-use", "use PGO profile for llvm build", "PROFILE");
250
251         // We can't use getopt to parse the options until we have completed specifying which
252         // options are valid, but under the current implementation, some options are conditional on
253         // the subcommand. Therefore we must manually identify the subcommand first, so that we can
254         // complete the definition of the options.  Then we can use the getopt::Matches object from
255         // there on out.
256         let subcommand = match args.iter().find_map(|s| Kind::parse(&s)) {
257             Some(s) => s,
258             None => {
259                 // No or an invalid subcommand -- show the general usage and subcommand help
260                 // An exit code will be 0 when no subcommand is given, and 1 in case of an invalid
261                 // subcommand.
262                 println!("{}\n", subcommand_help);
263                 let exit_code = if args.is_empty() { 0 } else { 1 };
264                 process::exit(exit_code);
265             }
266         };
267
268         // Some subcommands get extra options
269         match subcommand {
270             Kind::Test => {
271                 opts.optflag("", "no-fail-fast", "Run all tests regardless of failure");
272                 opts.optmulti("", "skip", "skips tests matching SUBSTRING, if supported by test tool. May be passed multiple times", "SUBSTRING");
273                 opts.optmulti(
274                     "",
275                     "test-args",
276                     "extra arguments to be passed for the test tool being used \
277                         (e.g. libtest, compiletest or rustdoc)",
278                     "ARGS",
279                 );
280                 opts.optmulti(
281                     "",
282                     "rustc-args",
283                     "extra options to pass the compiler when running tests",
284                     "ARGS",
285                 );
286                 opts.optflag("", "no-doc", "do not run doc tests");
287                 opts.optflag("", "doc", "only run doc tests");
288                 opts.optflag("", "bless", "update all stderr/stdout files of failing ui tests");
289                 opts.optflag("", "force-rerun", "rerun tests even if the inputs are unchanged");
290                 opts.optopt(
291                     "",
292                     "compare-mode",
293                     "mode describing what file the actual ui output will be compared to",
294                     "COMPARE MODE",
295                 );
296                 opts.optopt(
297                     "",
298                     "pass",
299                     "force {check,build,run}-pass tests to this mode.",
300                     "check | build | run",
301                 );
302                 opts.optopt("", "run", "whether to execute run-* tests", "auto | always | never");
303                 opts.optflag(
304                     "",
305                     "rustfix-coverage",
306                     "enable this to generate a Rustfix coverage file, which is saved in \
307                         `/<build_base>/rustfix_missing_coverage.txt`",
308                 );
309             }
310             Kind::Check => {
311                 opts.optflag("", "all-targets", "Check all targets");
312             }
313             Kind::Bench => {
314                 opts.optmulti("", "test-args", "extra arguments", "ARGS");
315             }
316             Kind::Clippy => {
317                 opts.optflag("", "fix", "automatically apply lint suggestions");
318             }
319             Kind::Doc => {
320                 opts.optflag("", "open", "open the docs in a browser");
321             }
322             Kind::Clean => {
323                 opts.optflag("", "all", "clean all build artifacts");
324             }
325             Kind::Format => {
326                 opts.optflag("", "check", "check formatting instead of applying.");
327             }
328             _ => {}
329         };
330
331         // fn usage()
332         let usage = |exit_code: i32, opts: &Options, verbose: bool, subcommand_help: &str| -> ! {
333             let config = Config::parse(&["build".to_string()]);
334             let build = Build::new(config);
335             let paths = Builder::get_help(&build, subcommand);
336
337             println!("{}", opts.usage(subcommand_help));
338             if let Some(s) = paths {
339                 if verbose {
340                     println!("{}", s);
341                 } else {
342                     println!(
343                         "Run `./x.py {} -h -v` to see a list of available paths.",
344                         subcommand.as_str()
345                     );
346                 }
347             } else if verbose {
348                 panic!("No paths available for subcommand `{}`", subcommand.as_str());
349             }
350             process::exit(exit_code);
351         };
352
353         // Done specifying what options are possible, so do the getopts parsing
354         let matches = opts.parse(args).unwrap_or_else(|e| {
355             // Invalid argument/option format
356             println!("\n{}\n", e);
357             usage(1, &opts, false, &subcommand_help);
358         });
359
360         // Extra sanity check to make sure we didn't hit this crazy corner case:
361         //
362         //     ./x.py --frobulate clean build
363         //            ^-- option  ^     ^- actual subcommand
364         //                        \_ arg to option could be mistaken as subcommand
365         let mut pass_sanity_check = true;
366         match matches.free.get(0).and_then(|s| Kind::parse(&s)) {
367             Some(check_subcommand) => {
368                 if check_subcommand != subcommand {
369                     pass_sanity_check = false;
370                 }
371             }
372             None => {
373                 pass_sanity_check = false;
374             }
375         }
376         if !pass_sanity_check {
377             eprintln!("{}\n", subcommand_help);
378             eprintln!(
379                 "Sorry, I couldn't figure out which subcommand you were trying to specify.\n\
380                  You may need to move some options to after the subcommand.\n"
381             );
382             process::exit(1);
383         }
384         // Extra help text for some commands
385         match subcommand {
386             Kind::Build => {
387                 subcommand_help.push_str(
388                     "\n
389 Arguments:
390     This subcommand accepts a number of paths to directories to the crates
391     and/or artifacts to compile. For example, for a quick build of a usable
392     compiler:
393
394         ./x.py build --stage 1 library/std
395
396     This will build a compiler and standard library from the local source code.
397     Once this is done, build/$ARCH/stage1 contains a usable compiler.
398
399     If no arguments are passed then the default artifacts for that stage are
400     compiled. For example:
401
402         ./x.py build --stage 0
403         ./x.py build ",
404                 );
405             }
406             Kind::Check => {
407                 subcommand_help.push_str(
408                     "\n
409 Arguments:
410     This subcommand accepts a number of paths to directories to the crates
411     and/or artifacts to compile. For example:
412
413         ./x.py check library/std
414
415     If no arguments are passed then many artifacts are checked.",
416                 );
417             }
418             Kind::Clippy => {
419                 subcommand_help.push_str(
420                     "\n
421 Arguments:
422     This subcommand accepts a number of paths to directories to the crates
423     and/or artifacts to run clippy against. For example:
424
425         ./x.py clippy library/core
426         ./x.py clippy library/core library/proc_macro",
427                 );
428             }
429             Kind::Fix => {
430                 subcommand_help.push_str(
431                     "\n
432 Arguments:
433     This subcommand accepts a number of paths to directories to the crates
434     and/or artifacts to run `cargo fix` against. For example:
435
436         ./x.py fix library/core
437         ./x.py fix library/core library/proc_macro",
438                 );
439             }
440             Kind::Format => {
441                 subcommand_help.push_str(
442                     "\n
443 Arguments:
444     This subcommand optionally accepts a `--check` flag which succeeds if formatting is correct and
445     fails if it is not. For example:
446
447         ./x.py fmt
448         ./x.py fmt --check",
449                 );
450             }
451             Kind::Test => {
452                 subcommand_help.push_str(
453                     "\n
454 Arguments:
455     This subcommand accepts a number of paths to test directories that
456     should be compiled and run. For example:
457
458         ./x.py test src/test/ui
459         ./x.py test library/std --test-args hash_map
460         ./x.py test library/std --stage 0 --no-doc
461         ./x.py test src/test/ui --bless
462         ./x.py test src/test/ui --compare-mode chalk
463
464     Note that `test src/test/* --stage N` does NOT depend on `build compiler/rustc --stage N`;
465     just like `build library/std --stage N` it tests the compiler produced by the previous
466     stage.
467
468     Execute tool tests with a tool name argument:
469
470         ./x.py test tidy
471
472     If no arguments are passed then the complete artifacts for that stage are
473     compiled and tested.
474
475         ./x.py test
476         ./x.py test --stage 1",
477                 );
478             }
479             Kind::Doc => {
480                 subcommand_help.push_str(
481                     "\n
482 Arguments:
483     This subcommand accepts a number of paths to directories of documentation
484     to build. For example:
485
486         ./x.py doc src/doc/book
487         ./x.py doc src/doc/nomicon
488         ./x.py doc src/doc/book library/std
489         ./x.py doc library/std --open
490
491     If no arguments are passed then everything is documented:
492
493         ./x.py doc
494         ./x.py doc --stage 1",
495                 );
496             }
497             Kind::Run => {
498                 subcommand_help.push_str(
499                     "\n
500 Arguments:
501     This subcommand accepts a number of paths to tools to build and run. For
502     example:
503
504         ./x.py run src/tools/expand-yaml-anchors
505
506     At least a tool needs to be called.",
507                 );
508             }
509             Kind::Setup => {
510                 subcommand_help.push_str(&format!(
511                     "\n
512 x.py setup creates a `config.toml` which changes the defaults for x.py itself.
513
514 Arguments:
515     This subcommand accepts a 'profile' to use for builds. For example:
516
517         ./x.py setup library
518
519     The profile is optional and you will be prompted interactively if it is not given.
520     The following profiles are available:
521
522 {}",
523                     Profile::all_for_help("        ").trim_end()
524                 ));
525             }
526             Kind::Bench | Kind::Clean | Kind::Dist | Kind::Install => {}
527         };
528         // Get any optional paths which occur after the subcommand
529         let mut paths = matches.free[1..].iter().map(|p| p.into()).collect::<Vec<PathBuf>>();
530
531         let verbose = matches.opt_present("verbose");
532
533         // User passed in -h/--help?
534         if matches.opt_present("help") {
535             usage(0, &opts, verbose, &subcommand_help);
536         }
537
538         let cmd = match subcommand {
539             Kind::Build => Subcommand::Build { paths },
540             Kind::Check => {
541                 if matches.opt_present("all-targets") {
542                     println!(
543                         "Warning: --all-targets is now on by default and does not need to be passed explicitly."
544                     );
545                 }
546                 Subcommand::Check { paths }
547             }
548             Kind::Clippy => Subcommand::Clippy { paths, fix: matches.opt_present("fix") },
549             Kind::Fix => Subcommand::Fix { paths },
550             Kind::Test => Subcommand::Test {
551                 paths,
552                 bless: matches.opt_present("bless"),
553                 force_rerun: matches.opt_present("force-rerun"),
554                 compare_mode: matches.opt_str("compare-mode"),
555                 pass: matches.opt_str("pass"),
556                 run: matches.opt_str("run"),
557                 skip: matches.opt_strs("skip"),
558                 test_args: matches.opt_strs("test-args"),
559                 rustc_args: matches.opt_strs("rustc-args"),
560                 fail_fast: !matches.opt_present("no-fail-fast"),
561                 rustfix_coverage: matches.opt_present("rustfix-coverage"),
562                 doc_tests: if matches.opt_present("doc") {
563                     DocTests::Only
564                 } else if matches.opt_present("no-doc") {
565                     DocTests::No
566                 } else {
567                     DocTests::Yes
568                 },
569             },
570             Kind::Bench => Subcommand::Bench { paths, test_args: matches.opt_strs("test-args") },
571             Kind::Doc => Subcommand::Doc { paths, open: matches.opt_present("open") },
572             Kind::Clean => {
573                 if !paths.is_empty() {
574                     println!("\nclean does not take a path argument\n");
575                     usage(1, &opts, verbose, &subcommand_help);
576                 }
577
578                 Subcommand::Clean { all: matches.opt_present("all") }
579             }
580             Kind::Format => Subcommand::Format { check: matches.opt_present("check"), paths },
581             Kind::Dist => Subcommand::Dist { paths },
582             Kind::Install => Subcommand::Install { paths },
583             Kind::Run => {
584                 if paths.is_empty() {
585                     println!("\nrun requires at least a path!\n");
586                     usage(1, &opts, verbose, &subcommand_help);
587                 }
588                 Subcommand::Run { paths }
589             }
590             Kind::Setup => {
591                 let profile = if paths.len() > 1 {
592                     println!("\nat most one profile can be passed to setup\n");
593                     usage(1, &opts, verbose, &subcommand_help)
594                 } else if let Some(path) = paths.pop() {
595                     let profile_string = t!(path.into_os_string().into_string().map_err(
596                         |path| format!("{} is not a valid UTF8 string", path.to_string_lossy())
597                     ));
598
599                     profile_string.parse().unwrap_or_else(|err| {
600                         eprintln!("error: {}", err);
601                         eprintln!("help: the available profiles are:");
602                         eprint!("{}", Profile::all_for_help("- "));
603                         std::process::exit(1);
604                     })
605                 } else {
606                     t!(crate::setup::interactive_path())
607                 };
608                 Subcommand::Setup { profile }
609             }
610         };
611
612         if let Subcommand::Check { .. } = &cmd {
613             if matches.opt_str("keep-stage").is_some()
614                 || matches.opt_str("keep-stage-std").is_some()
615             {
616                 eprintln!("--keep-stage not yet supported for x.py check");
617                 process::exit(1);
618             }
619         }
620
621         Flags {
622             verbose: matches.opt_count("verbose"),
623             stage: matches.opt_str("stage").map(|j| j.parse().expect("`stage` should be a number")),
624             dry_run: matches.opt_present("dry-run"),
625             on_fail: matches.opt_str("on-fail"),
626             rustc_error_format: matches.opt_str("error-format"),
627             json_output: matches.opt_present("json-output"),
628             keep_stage: matches
629                 .opt_strs("keep-stage")
630                 .into_iter()
631                 .map(|j| j.parse().expect("`keep-stage` should be a number"))
632                 .collect(),
633             keep_stage_std: matches
634                 .opt_strs("keep-stage-std")
635                 .into_iter()
636                 .map(|j| j.parse().expect("`keep-stage-std` should be a number"))
637                 .collect(),
638             host: if matches.opt_present("host") {
639                 Some(
640                     split(&matches.opt_strs("host"))
641                         .into_iter()
642                         .map(|x| TargetSelection::from_user(&x))
643                         .collect::<Vec<_>>(),
644                 )
645             } else {
646                 None
647             },
648             target: if matches.opt_present("target") {
649                 Some(
650                     split(&matches.opt_strs("target"))
651                         .into_iter()
652                         .map(|x| TargetSelection::from_user(&x))
653                         .collect::<Vec<_>>(),
654                 )
655             } else {
656                 None
657             },
658             config: matches.opt_str("config").map(PathBuf::from),
659             build_dir: matches.opt_str("build-dir").map(PathBuf::from),
660             jobs: matches.opt_str("jobs").map(|j| j.parse().expect("`jobs` should be a number")),
661             cmd,
662             incremental: matches.opt_present("incremental"),
663             exclude: split(&matches.opt_strs("exclude"))
664                 .into_iter()
665                 .map(|p| p.into())
666                 .collect::<Vec<_>>(),
667             include_default_paths: matches.opt_present("include-default-paths"),
668             deny_warnings: parse_deny_warnings(&matches),
669             llvm_skip_rebuild: matches.opt_str("llvm-skip-rebuild").map(|s| s.to_lowercase()).map(
670                 |s| s.parse::<bool>().expect("`llvm-skip-rebuild` should be either true or false"),
671             ),
672             color: matches
673                 .opt_get_default("color", Color::Auto)
674                 .expect("`color` should be `always`, `never`, or `auto`"),
675             rust_profile_use: matches.opt_str("rust-profile-use"),
676             rust_profile_generate: matches.opt_str("rust-profile-generate"),
677             llvm_profile_use: matches.opt_str("llvm-profile-use"),
678             llvm_profile_generate: matches.opt_present("llvm-profile-generate"),
679         }
680     }
681 }
682
683 impl Subcommand {
684     pub fn kind(&self) -> Kind {
685         match self {
686             Subcommand::Bench { .. } => Kind::Bench,
687             Subcommand::Build { .. } => Kind::Build,
688             Subcommand::Check { .. } => Kind::Check,
689             Subcommand::Clippy { .. } => Kind::Clippy,
690             Subcommand::Doc { .. } => Kind::Doc,
691             Subcommand::Fix { .. } => Kind::Fix,
692             Subcommand::Format { .. } => Kind::Format,
693             Subcommand::Test { .. } => Kind::Test,
694             Subcommand::Clean { .. } => Kind::Clean,
695             Subcommand::Dist { .. } => Kind::Dist,
696             Subcommand::Install { .. } => Kind::Install,
697             Subcommand::Run { .. } => Kind::Run,
698             Subcommand::Setup { .. } => Kind::Setup,
699         }
700     }
701
702     pub fn test_args(&self) -> Vec<&str> {
703         let mut args = vec![];
704
705         match *self {
706             Subcommand::Test { ref skip, .. } => {
707                 for s in skip {
708                     args.push("--skip");
709                     args.push(s.as_str());
710                 }
711             }
712             _ => (),
713         };
714
715         match *self {
716             Subcommand::Test { ref test_args, .. } | Subcommand::Bench { ref test_args, .. } => {
717                 args.extend(test_args.iter().flat_map(|s| s.split_whitespace()))
718             }
719             _ => (),
720         }
721
722         args
723     }
724
725     pub fn rustc_args(&self) -> Vec<&str> {
726         match *self {
727             Subcommand::Test { ref rustc_args, .. } => {
728                 rustc_args.iter().flat_map(|s| s.split_whitespace()).collect()
729             }
730             _ => Vec::new(),
731         }
732     }
733
734     pub fn fail_fast(&self) -> bool {
735         match *self {
736             Subcommand::Test { fail_fast, .. } => fail_fast,
737             _ => false,
738         }
739     }
740
741     pub fn doc_tests(&self) -> DocTests {
742         match *self {
743             Subcommand::Test { doc_tests, .. } => doc_tests,
744             _ => DocTests::Yes,
745         }
746     }
747
748     pub fn bless(&self) -> bool {
749         match *self {
750             Subcommand::Test { bless, .. } => bless,
751             _ => false,
752         }
753     }
754
755     pub fn force_rerun(&self) -> bool {
756         match *self {
757             Subcommand::Test { force_rerun, .. } => force_rerun,
758             _ => false,
759         }
760     }
761
762     pub fn rustfix_coverage(&self) -> bool {
763         match *self {
764             Subcommand::Test { rustfix_coverage, .. } => rustfix_coverage,
765             _ => false,
766         }
767     }
768
769     pub fn compare_mode(&self) -> Option<&str> {
770         match *self {
771             Subcommand::Test { ref compare_mode, .. } => compare_mode.as_ref().map(|s| &s[..]),
772             _ => None,
773         }
774     }
775
776     pub fn pass(&self) -> Option<&str> {
777         match *self {
778             Subcommand::Test { ref pass, .. } => pass.as_ref().map(|s| &s[..]),
779             _ => None,
780         }
781     }
782
783     pub fn run(&self) -> Option<&str> {
784         match *self {
785             Subcommand::Test { ref run, .. } => run.as_ref().map(|s| &s[..]),
786             _ => None,
787         }
788     }
789
790     pub fn open(&self) -> bool {
791         match *self {
792             Subcommand::Doc { open, .. } => open,
793             _ => false,
794         }
795     }
796 }
797
798 fn split(s: &[String]) -> Vec<String> {
799     s.iter().flat_map(|s| s.split(',')).filter(|s| !s.is_empty()).map(|s| s.to_string()).collect()
800 }
801
802 fn parse_deny_warnings(matches: &getopts::Matches) -> Option<bool> {
803     match matches.opt_str("warnings").as_deref() {
804         Some("deny") => Some(true),
805         Some("warn") => Some(false),
806         Some(value) => {
807             eprintln!(r#"invalid value for --warnings: {:?}, expected "warn" or "deny""#, value,);
808             process::exit(1);
809         }
810         None => None,
811     }
812 }