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