1 //! Command-line interface of the rustbuild build system.
3 //! This module implements the command-line parsing of the build system which
4 //! has various flags to configure how it's run.
6 use std::path::PathBuf;
11 use crate::builder::{Builder, Kind};
12 use crate::config::{Config, TargetSelection};
13 use crate::setup::Profile;
15 use crate::{Build, DocTests};
17 #[derive(Copy, Clone)]
24 impl Default for Color {
25 fn default() -> Self {
30 impl std::str::FromStr for Color {
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),
43 /// Deserialized version of all flags for this compile.
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>,
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>,
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,
65 // This overrides the deny-warnings configuration option,
66 // which passes -Dwarnings to the compiler invocations.
68 // true => deny, false => warn
69 pub deny_warnings: Option<bool>,
71 pub llvm_skip_rebuild: Option<bool>,
73 pub rust_profile_use: Option<String>,
74 pub rust_profile_generate: Option<String>,
76 pub llvm_profile_use: Option<String>,
77 // LLVM doesn't support a custom location for generating profile
80 // llvm_out/build/profiles/ is the location this writes to.
81 pub llvm_profile_generate: bool,
84 #[cfg_attr(test, derive(Clone))]
109 /// Whether to automatically update stderr/stdout files
112 compare_mode: Option<String>,
113 pass: Option<String>,
116 test_args: Vec<String>,
117 rustc_args: Vec<String>,
120 rustfix_coverage: bool,
124 test_args: Vec<String>,
143 impl Default for Subcommand {
144 fn default() -> Subcommand {
145 Subcommand::Build { paths: vec![PathBuf::from("nowhere")] }
150 pub fn parse(args: &[String]) -> Flags {
151 let mut subcommand_help = String::from(
153 Usage: x.py <subcommand> [options] [<paths>...]
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)
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)
170 To learn more about a subcommand, run `./x.py <subcommand> -h`",
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");
181 "Build directory, overrides `build.build-dir` in `config.toml`",
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");
190 "include-default-paths",
191 "include default paths in addition to the provided ones",
193 opts.optopt("", "on-fail", "command to run on failure", "CMD");
194 opts.optflag("", "dry-run", "dry run; don't build anything");
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.)",
205 "stage(s) to keep without recompiling \
206 (pass multiple times to keep e.g., both stages 0 and 1)",
212 "stage(s) of the standard library to keep without recompiling \
213 (pass multiple times to keep e.g., both stages 0 and 1)",
216 opts.optopt("", "src", "path to the root of the rust checkout", "DIR");
218 "number of jobs to run in parallel; \
219 defaults to {} (this host's logical CPU count)",
222 opts.optopt("j", "jobs", &j_msg, "JOBS");
223 opts.optflag("h", "help", "print this help message");
227 "if value is deny, will deny warnings, otherwise use default",
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");
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.",
243 "rust-profile-generate",
244 "generate PGO profile with rustc build",
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");
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
256 let subcommand = match args.iter().find_map(|s| Kind::parse(&s)) {
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
262 println!("{}\n", subcommand_help);
263 let exit_code = if args.is_empty() { 0 } else { 1 };
264 process::exit(exit_code);
268 // Some subcommands get extra options
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");
276 "extra arguments to be passed for the test tool being used \
277 (e.g. libtest, compiletest or rustdoc)",
283 "extra options to pass the compiler when running tests",
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");
293 "mode describing what file the actual ui output will be compared to",
299 "force {check,build,run}-pass tests to this mode.",
300 "check | build | run",
302 opts.optopt("", "run", "whether to execute run-* tests", "auto | always | never");
306 "enable this to generate a Rustfix coverage file, which is saved in \
307 `/<build_base>/rustfix_missing_coverage.txt`",
311 opts.optflag("", "all-targets", "Check all targets");
314 opts.optmulti("", "test-args", "extra arguments", "ARGS");
317 opts.optflag("", "fix", "automatically apply lint suggestions");
320 opts.optflag("", "open", "open the docs in a browser");
323 opts.optflag("", "all", "clean all build artifacts");
326 opts.optflag("", "check", "check formatting instead of applying.");
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);
337 println!("{}", opts.usage(subcommand_help));
338 if let Some(s) = paths {
343 "Run `./x.py {} -h -v` to see a list of available paths.",
348 panic!("No paths available for subcommand `{}`", subcommand.as_str());
350 process::exit(exit_code);
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);
360 // Extra sanity check to make sure we didn't hit this crazy corner case:
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;
373 pass_sanity_check = false;
376 if !pass_sanity_check {
377 eprintln!("{}\n", subcommand_help);
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"
384 // Extra help text for some commands
387 subcommand_help.push_str(
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
394 ./x.py build --stage 1 library/std
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.
399 If no arguments are passed then the default artifacts for that stage are
400 compiled. For example:
402 ./x.py build --stage 0
407 subcommand_help.push_str(
410 This subcommand accepts a number of paths to directories to the crates
411 and/or artifacts to compile. For example:
413 ./x.py check library/std
415 If no arguments are passed then many artifacts are checked.",
419 subcommand_help.push_str(
422 This subcommand accepts a number of paths to directories to the crates
423 and/or artifacts to run clippy against. For example:
425 ./x.py clippy library/core
426 ./x.py clippy library/core library/proc_macro",
430 subcommand_help.push_str(
433 This subcommand accepts a number of paths to directories to the crates
434 and/or artifacts to run `cargo fix` against. For example:
436 ./x.py fix library/core
437 ./x.py fix library/core library/proc_macro",
441 subcommand_help.push_str(
444 This subcommand optionally accepts a `--check` flag which succeeds if formatting is correct and
445 fails if it is not. For example:
452 subcommand_help.push_str(
455 This subcommand accepts a number of paths to test directories that
456 should be compiled and run. For example:
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
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
468 Execute tool tests with a tool name argument:
472 If no arguments are passed then the complete artifacts for that stage are
476 ./x.py test --stage 1",
480 subcommand_help.push_str(
483 This subcommand accepts a number of paths to directories of documentation
484 to build. For example:
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
491 If no arguments are passed then everything is documented:
494 ./x.py doc --stage 1",
498 subcommand_help.push_str(
501 This subcommand accepts a number of paths to tools to build and run. For
504 ./x.py run src/tools/expand-yaml-anchors
506 At least a tool needs to be called.",
510 subcommand_help.push_str(&format!(
512 x.py setup creates a `config.toml` which changes the defaults for x.py itself.
515 This subcommand accepts a 'profile' to use for builds. For example:
519 The profile is optional and you will be prompted interactively if it is not given.
520 The following profiles are available:
523 Profile::all_for_help(" ").trim_end()
526 Kind::Bench | Kind::Clean | Kind::Dist | Kind::Install => {}
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>>();
531 let verbose = matches.opt_present("verbose");
533 // User passed in -h/--help?
534 if matches.opt_present("help") {
535 usage(0, &opts, verbose, &subcommand_help);
538 let cmd = match subcommand {
539 Kind::Build => Subcommand::Build { paths },
541 if matches.opt_present("all-targets") {
543 "Warning: --all-targets is now on by default and does not need to be passed explicitly."
546 Subcommand::Check { paths }
548 Kind::Clippy => Subcommand::Clippy { paths, fix: matches.opt_present("fix") },
549 Kind::Fix => Subcommand::Fix { paths },
550 Kind::Test => Subcommand::Test {
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") {
564 } else if matches.opt_present("no-doc") {
570 Kind::Bench => Subcommand::Bench { paths, test_args: matches.opt_strs("test-args") },
571 Kind::Doc => Subcommand::Doc { paths, open: matches.opt_present("open") },
573 if !paths.is_empty() {
574 println!("\nclean does not take a path argument\n");
575 usage(1, &opts, verbose, &subcommand_help);
578 Subcommand::Clean { all: matches.opt_present("all") }
580 Kind::Format => Subcommand::Format { check: matches.opt_present("check"), paths },
581 Kind::Dist => Subcommand::Dist { paths },
582 Kind::Install => Subcommand::Install { paths },
584 if paths.is_empty() {
585 println!("\nrun requires at least a path!\n");
586 usage(1, &opts, verbose, &subcommand_help);
588 Subcommand::Run { paths }
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())
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);
606 t!(crate::setup::interactive_path())
608 Subcommand::Setup { profile }
612 if let Subcommand::Check { .. } = &cmd {
613 if matches.opt_str("keep-stage").is_some()
614 || matches.opt_str("keep-stage-std").is_some()
616 eprintln!("--keep-stage not yet supported for x.py check");
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"),
629 .opt_strs("keep-stage")
631 .map(|j| j.parse().expect("`keep-stage` should be a number"))
633 keep_stage_std: matches
634 .opt_strs("keep-stage-std")
636 .map(|j| j.parse().expect("`keep-stage-std` should be a number"))
638 host: if matches.opt_present("host") {
640 split(&matches.opt_strs("host"))
642 .map(|x| TargetSelection::from_user(&x))
643 .collect::<Vec<_>>(),
648 target: if matches.opt_present("target") {
650 split(&matches.opt_strs("target"))
652 .map(|x| TargetSelection::from_user(&x))
653 .collect::<Vec<_>>(),
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")),
662 incremental: matches.opt_present("incremental"),
663 exclude: split(&matches.opt_strs("exclude"))
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"),
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"),
684 pub fn kind(&self) -> Kind {
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,
702 pub fn test_args(&self) -> Vec<&str> {
703 let mut args = vec![];
706 Subcommand::Test { ref skip, .. } => {
709 args.push(s.as_str());
716 Subcommand::Test { ref test_args, .. } | Subcommand::Bench { ref test_args, .. } => {
717 args.extend(test_args.iter().flat_map(|s| s.split_whitespace()))
725 pub fn rustc_args(&self) -> Vec<&str> {
727 Subcommand::Test { ref rustc_args, .. } => {
728 rustc_args.iter().flat_map(|s| s.split_whitespace()).collect()
734 pub fn fail_fast(&self) -> bool {
736 Subcommand::Test { fail_fast, .. } => fail_fast,
741 pub fn doc_tests(&self) -> DocTests {
743 Subcommand::Test { doc_tests, .. } => doc_tests,
748 pub fn bless(&self) -> bool {
750 Subcommand::Test { bless, .. } => bless,
755 pub fn force_rerun(&self) -> bool {
757 Subcommand::Test { force_rerun, .. } => force_rerun,
762 pub fn rustfix_coverage(&self) -> bool {
764 Subcommand::Test { rustfix_coverage, .. } => rustfix_coverage,
769 pub fn compare_mode(&self) -> Option<&str> {
771 Subcommand::Test { ref compare_mode, .. } => compare_mode.as_ref().map(|s| &s[..]),
776 pub fn pass(&self) -> Option<&str> {
778 Subcommand::Test { ref pass, .. } => pass.as_ref().map(|s| &s[..]),
783 pub fn run(&self) -> Option<&str> {
785 Subcommand::Test { ref run, .. } => run.as_ref().map(|s| &s[..]),
790 pub fn open(&self) -> bool {
792 Subcommand::Doc { open, .. } => open,
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()
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),
807 eprintln!(r#"invalid value for --warnings: {:?}, expected "warn" or "deny""#, value,);