1use std::path::{Path, PathBuf};
7
8use clap::{CommandFactory, Parser, ValueEnum};
9use clap_complete::Generator;
10#[cfg(feature = "tracing")]
11use tracing::instrument;
12
13use crate::core::build_steps::perf::PerfArgs;
14use crate::core::build_steps::setup::Profile;
15use crate::core::builder::{Builder, Kind};
16use crate::core::config::Config;
17use crate::core::config::target_selection::{TargetSelectionList, target_selection_list};
18use crate::{Build, CodegenBackendKind, DocTests};
19
20#[derive(Copy, Clone, Default, Debug, ValueEnum)]
21pub enum Color {
22 Always,
23 Never,
24 #[default]
25 Auto,
26}
27
28#[derive(Copy, Clone, Default, Debug, ValueEnum)]
30pub enum Warnings {
31 Deny,
32 Warn,
33 #[default]
34 Default,
35}
36
37#[derive(Debug, Parser)]
39#[command(
40 override_usage = "x.py <subcommand> [options] [<paths>...]",
41 disable_help_subcommand(true),
42 about = "",
43 next_line_help(false)
44)]
45pub struct Flags {
46 #[command(subcommand)]
47 pub cmd: Subcommand,
48
49 #[arg(global = true, short, long, action = clap::ArgAction::Count)]
50 pub verbose: u8, #[arg(global = true, short, long)]
53 pub incremental: bool,
55 #[arg(global = true, long, value_hint = clap::ValueHint::FilePath, value_name = "FILE")]
56 pub config: Option<PathBuf>,
58 #[arg(global = true, long, value_hint = clap::ValueHint::DirPath, value_name = "DIR")]
59 pub build_dir: Option<PathBuf>,
61
62 #[arg(global = true, long, value_hint = clap::ValueHint::Other, value_name = "BUILD")]
63 pub build: Option<String>,
65
66 #[arg(global = true, long, value_hint = clap::ValueHint::Other, value_name = "HOST", value_parser = target_selection_list)]
67 pub host: Option<TargetSelectionList>,
69
70 #[arg(global = true, long, value_hint = clap::ValueHint::Other, value_name = "TARGET", value_parser = target_selection_list)]
71 pub target: Option<TargetSelectionList>,
73
74 #[arg(global = true, long, value_name = "PATH")]
75 pub exclude: Vec<PathBuf>, #[arg(global = true, long, value_name = "PATH")]
78 pub skip: Vec<PathBuf>,
80 #[arg(global = true, long)]
81 pub include_default_paths: bool,
83
84 #[arg(global = true, value_hint = clap::ValueHint::Other, long)]
86 pub rustc_error_format: Option<String>,
87
88 #[arg(global = true, long, value_hint = clap::ValueHint::CommandString, value_name = "CMD")]
89 pub on_fail: Option<String>,
91 #[arg(global = true, long)]
92 pub dry_run: bool,
94 #[arg(global = true, long)]
96 pub dump_bootstrap_shims: bool,
97 #[arg(global = true, value_hint = clap::ValueHint::Other, long, value_name = "N")]
98 pub stage: Option<u32>,
101
102 #[arg(global = true, value_hint = clap::ValueHint::Other, long, value_name = "N")]
103 pub keep_stage: Vec<u32>,
106 #[arg(global = true, value_hint = clap::ValueHint::Other, long, value_name = "N")]
107 pub keep_stage_std: Vec<u32>,
110 #[arg(global = true, long, value_hint = clap::ValueHint::DirPath, value_name = "DIR")]
111 pub src: Option<PathBuf>,
113
114 #[arg(
115 global = true,
116 short,
117 long,
118 value_hint = clap::ValueHint::Other,
119 value_name = "JOBS"
120 )]
121 pub jobs: Option<u32>,
123 #[arg(global = true, long)]
126 #[arg(value_enum, default_value_t=Warnings::Default, value_name = "deny|warn")]
127 pub warnings: Warnings,
131
132 #[arg(global = true, long)]
133 pub json_output: bool,
135 #[arg(global = true, long)]
136 pub compile_time_deps: bool,
138
139 #[arg(global = true, long, value_name = "STYLE")]
140 #[arg(value_enum, default_value_t = Color::Auto)]
141 pub color: Color,
143
144 #[arg(global = true, long)]
145 pub bypass_bootstrap_lock: bool,
150
151 #[arg(global = true, value_hint = clap::ValueHint::FilePath, long, value_name = "PROFILE")]
153 pub rust_profile_generate: Option<String>,
154 #[arg(global = true, value_hint = clap::ValueHint::FilePath, long, value_name = "PROFILE")]
156 pub rust_profile_use: Option<String>,
157 #[arg(global = true, value_hint = clap::ValueHint::FilePath, long, value_name = "PROFILE")]
159 pub llvm_profile_use: Option<String>,
160 #[arg(global = true, long)]
166 pub llvm_profile_generate: bool,
167 #[arg(global = true, long)]
169 pub enable_bolt_settings: bool,
170 #[arg(global = true, long)]
172 pub skip_stage0_validation: bool,
173 #[arg(global = true, long)]
175 pub reproducible_artifact: Vec<String>,
176 #[arg(global = true)]
177 pub paths: Vec<PathBuf>,
179 #[arg(global = true, value_hint = clap::ValueHint::Other, long, value_name = "section.option=value")]
181 pub set: Vec<String>,
182 #[arg(global = true, last(true), value_name = "ARGS")]
184 pub free_args: Vec<String>,
185 #[arg(global = true, long, value_name = "bool")]
187 pub ci: Option<bool>,
188 #[arg(global = true, long)]
192 pub skip_std_check_if_no_download_rustc: bool,
193}
194
195impl Flags {
196 pub fn try_parse_verbose_help(args: &[String]) -> bool {
199 #[derive(Parser)]
201 #[command(disable_help_flag(true))]
202 struct HelpVerboseOnly {
203 #[arg(short, long)]
204 help: bool,
205 #[arg(global = true, short, long, action = clap::ArgAction::Count)]
206 pub verbose: u8,
207 #[arg(value_enum)]
208 cmd: Kind,
209 }
210 if let Ok(HelpVerboseOnly { help: true, verbose: 1.., cmd: subcommand }) =
211 HelpVerboseOnly::try_parse_from(normalize_args(args))
212 {
213 println!("NOTE: updating submodules before printing available paths");
214 let flags = Self::parse(&[String::from("build")]);
215 let config = Config::parse(flags);
216 let build = Build::new(config);
217 let paths = Builder::get_help(&build, subcommand);
218 if let Some(s) = paths {
219 println!("{s}");
220 } else {
221 panic!("No paths available for subcommand `{}`", subcommand.as_str());
222 }
223 true
224 } else {
225 false
226 }
227 }
228
229 #[cfg_attr(
230 feature = "tracing",
231 instrument(level = "trace", name = "Flags::parse", skip_all, fields(args = ?args))
232 )]
233 pub fn parse(args: &[String]) -> Self {
234 Flags::parse_from(normalize_args(args))
235 }
236}
237
238fn normalize_args(args: &[String]) -> Vec<String> {
239 let first = String::from("x.py");
240 let it = std::iter::once(first).chain(args.iter().cloned());
241 it.collect()
242}
243
244#[derive(Debug, Clone, clap::Subcommand)]
245pub enum Subcommand {
246 #[command(aliases = ["b"], long_about = "\n
247 Arguments:
248 This subcommand accepts a number of paths to directories to the crates
249 and/or artifacts to compile. For example, for a quick build of a usable
250 compiler:
251 ./x.py build --stage 1 library/std
252 This will build a compiler and standard library from the local source code.
253 Once this is done, build/$ARCH/stage1 contains a usable compiler.
254 If no arguments are passed then the default artifacts for that stage are
255 compiled. For example:
256 ./x.py build --stage 0
257 ./x.py build ")]
258 Build {
260 #[arg(long)]
261 timings: bool,
263 },
264 #[command(aliases = ["c"], long_about = "\n
265 Arguments:
266 This subcommand accepts a number of paths to directories to the crates
267 and/or artifacts to compile. For example:
268 ./x.py check library/std
269 If no arguments are passed then many artifacts are checked.")]
270 Check {
272 #[arg(long)]
273 all_targets: bool,
275 #[arg(long)]
276 timings: bool,
278 },
279 #[command(long_about = "\n
281 Arguments:
282 This subcommand accepts a number of paths to directories to the crates
283 and/or artifacts to run clippy against. For example:
284 ./x.py clippy library/core
285 ./x.py clippy library/core library/proc_macro")]
286 Clippy {
287 #[arg(long)]
288 fix: bool,
289 #[arg(long, requires = "fix")]
290 allow_dirty: bool,
291 #[arg(long, requires = "fix")]
292 allow_staged: bool,
293 #[arg(global = true, short = 'A', action = clap::ArgAction::Append, value_name = "LINT")]
295 allow: Vec<String>,
296 #[arg(global = true, short = 'D', action = clap::ArgAction::Append, value_name = "LINT")]
298 deny: Vec<String>,
299 #[arg(global = true, short = 'W', action = clap::ArgAction::Append, value_name = "LINT")]
301 warn: Vec<String>,
302 #[arg(global = true, short = 'F', action = clap::ArgAction::Append, value_name = "LINT")]
304 forbid: Vec<String>,
305 },
306 #[command(long_about = "\n
308 Arguments:
309 This subcommand accepts a number of paths to directories to the crates
310 and/or artifacts to run `cargo fix` against. For example:
311 ./x.py fix library/core
312 ./x.py fix library/core library/proc_macro")]
313 Fix,
314 #[command(
315 name = "fmt",
316 long_about = "\n
317 Arguments:
318 This subcommand optionally accepts a `--check` flag which succeeds if
319 formatting is correct and fails if it is not. For example:
320 ./x.py fmt
321 ./x.py fmt --check"
322 )]
323 Format {
325 #[arg(long)]
327 check: bool,
328
329 #[arg(long)]
331 all: bool,
332 },
333 #[command(aliases = ["d"], long_about = "\n
334 Arguments:
335 This subcommand accepts a number of paths to directories of documentation
336 to build. For example:
337 ./x.py doc src/doc/book
338 ./x.py doc src/doc/nomicon
339 ./x.py doc src/doc/book library/std
340 ./x.py doc library/std --json
341 ./x.py doc library/std --open
342 If no arguments are passed then everything is documented:
343 ./x.py doc
344 ./x.py doc --stage 1")]
345 Doc {
347 #[arg(long)]
348 open: bool,
350 #[arg(long)]
351 json: bool,
353 },
354 #[command(aliases = ["t"], long_about = "\n
355 Arguments:
356 This subcommand accepts a number of paths to test directories that
357 should be compiled and run. For example:
358 ./x.py test tests/ui
359 ./x.py test library/std --test-args hash_map
360 ./x.py test library/std --stage 0 --no-doc
361 ./x.py test tests/ui --bless
362 ./x.py test tests/ui --compare-mode next-solver
363 Note that `test tests/* --stage N` does NOT depend on `build compiler/rustc --stage N`;
364 just like `build library/std --stage N` it tests the compiler produced by the previous
365 stage.
366 Execute tool tests with a tool name argument:
367 ./x.py test tidy
368 If no arguments are passed then the complete artifacts for that stage are
369 compiled and tested.
370 ./x.py test
371 ./x.py test --stage 1")]
372 Test {
374 #[arg(long)]
375 no_fail_fast: bool,
377 #[arg(long, value_name = "ARGS", allow_hyphen_values(true))]
378 test_args: Vec<String>,
381 #[arg(long, value_name = "ARGS", allow_hyphen_values(true))]
383 compiletest_rustc_args: Vec<String>,
384 #[arg(long)]
385 no_doc: bool,
387 #[arg(long)]
388 doc: bool,
390 #[arg(long)]
391 bless: bool,
393 #[arg(long)]
394 extra_checks: Option<String>,
400 #[arg(long)]
401 force_rerun: bool,
403 #[arg(long)]
404 only_modified: bool,
406 #[arg(long, value_name = "COMPARE MODE")]
407 compare_mode: Option<String>,
409 #[arg(long, value_name = "check | build | run")]
410 pass: Option<String>,
412 #[arg(long, value_name = "auto | always | never")]
413 run: Option<String>,
415 #[arg(long)]
416 rustfix_coverage: bool,
419 #[arg(long)]
420 no_capture: bool,
422 #[arg(long)]
423 test_codegen_backend: Option<CodegenBackendKind>,
425 #[arg(long)]
426 bypass_ignore_backends: bool,
428 },
429 Miri {
431 #[arg(long)]
432 no_fail_fast: bool,
434 #[arg(long, value_name = "ARGS", allow_hyphen_values(true))]
435 test_args: Vec<String>,
438 #[arg(long)]
439 no_doc: bool,
441 #[arg(long)]
442 doc: bool,
444 },
445 Bench {
447 #[arg(long, allow_hyphen_values(true))]
448 test_args: Vec<String>,
449 },
450 Clean {
452 #[arg(long)]
453 all: bool,
455 #[arg(long, value_name = "N")]
456 stage: Option<u32>,
458 },
459 Dist,
461 Install,
463 #[command(aliases = ["r"], long_about = "\n
464 Arguments:
465 This subcommand accepts a number of paths to tools to build and run. For
466 example:
467 ./x.py run src/tools/bump-stage0
468 At least a tool needs to be called.")]
469 Run {
471 #[arg(long, allow_hyphen_values(true))]
473 args: Vec<String>,
474 },
475 #[command(long_about = format!(
477 "\n
478x.py setup creates a `bootstrap.toml` which changes the defaults for x.py itself,
479as well as setting up a git pre-push hook, VS Code config and toolchain link.
480Arguments:
481 This subcommand accepts a 'profile' to use for builds. For example:
482 ./x.py setup library
483 The profile is optional and you will be prompted interactively if it is not given.
484 The following profiles are available:
485{}
486 To only set up the git hook, editor config or toolchain link, you may use
487 ./x.py setup hook
488 ./x.py setup editor
489 ./x.py setup link", Profile::all_for_help(" ").trim_end()))]
490 Setup {
491 #[arg(value_name = "<PROFILE>|hook|editor|link")]
494 profile: Option<PathBuf>,
495 },
496 Vendor {
498 #[arg(long)]
500 sync: Vec<PathBuf>,
501 #[arg(long)]
503 versioned_dirs: bool,
504 },
505 Perf(PerfArgs),
507}
508
509impl Default for Subcommand {
510 fn default() -> Self {
511 Subcommand::Build { timings: false }
512 }
513}
514
515impl Subcommand {
516 pub fn kind(&self) -> Kind {
517 match self {
518 Subcommand::Bench { .. } => Kind::Bench,
519 Subcommand::Build { .. } => Kind::Build,
520 Subcommand::Check { .. } => Kind::Check,
521 Subcommand::Clippy { .. } => Kind::Clippy,
522 Subcommand::Doc { .. } => Kind::Doc,
523 Subcommand::Fix => Kind::Fix,
524 Subcommand::Format { .. } => Kind::Format,
525 Subcommand::Test { .. } => Kind::Test,
526 Subcommand::Miri { .. } => Kind::Miri,
527 Subcommand::Clean { .. } => Kind::Clean,
528 Subcommand::Dist => Kind::Dist,
529 Subcommand::Install => Kind::Install,
530 Subcommand::Run { .. } => Kind::Run,
531 Subcommand::Setup { .. } => Kind::Setup,
532 Subcommand::Vendor { .. } => Kind::Vendor,
533 Subcommand::Perf { .. } => Kind::Perf,
534 }
535 }
536
537 pub fn compiletest_rustc_args(&self) -> Vec<&str> {
538 match *self {
539 Subcommand::Test { ref compiletest_rustc_args, .. } => {
540 compiletest_rustc_args.iter().flat_map(|s| s.split_whitespace()).collect()
541 }
542 _ => vec![],
543 }
544 }
545
546 pub fn fail_fast(&self) -> bool {
547 match *self {
548 Subcommand::Test { no_fail_fast, .. } | Subcommand::Miri { no_fail_fast, .. } => {
549 !no_fail_fast
550 }
551 _ => false,
552 }
553 }
554
555 pub fn doc_tests(&self) -> DocTests {
556 match *self {
557 Subcommand::Test { doc, no_doc, .. } | Subcommand::Miri { no_doc, doc, .. } => {
558 if doc {
559 DocTests::Only
560 } else if no_doc {
561 DocTests::No
562 } else {
563 DocTests::Yes
564 }
565 }
566 _ => DocTests::Yes,
567 }
568 }
569
570 pub fn bless(&self) -> bool {
571 match *self {
572 Subcommand::Test { bless, .. } => bless,
573 _ => false,
574 }
575 }
576
577 pub fn extra_checks(&self) -> Option<&str> {
578 match *self {
579 Subcommand::Test { ref extra_checks, .. } => extra_checks.as_ref().map(String::as_str),
580 _ => None,
581 }
582 }
583
584 pub fn only_modified(&self) -> bool {
585 match *self {
586 Subcommand::Test { only_modified, .. } => only_modified,
587 _ => false,
588 }
589 }
590
591 pub fn force_rerun(&self) -> bool {
592 match *self {
593 Subcommand::Test { force_rerun, .. } => force_rerun,
594 _ => false,
595 }
596 }
597
598 pub fn no_capture(&self) -> bool {
599 match *self {
600 Subcommand::Test { no_capture, .. } => no_capture,
601 _ => false,
602 }
603 }
604
605 pub fn rustfix_coverage(&self) -> bool {
606 match *self {
607 Subcommand::Test { rustfix_coverage, .. } => rustfix_coverage,
608 _ => false,
609 }
610 }
611
612 pub fn compare_mode(&self) -> Option<&str> {
613 match *self {
614 Subcommand::Test { ref compare_mode, .. } => compare_mode.as_ref().map(|s| &s[..]),
615 _ => None,
616 }
617 }
618
619 pub fn pass(&self) -> Option<&str> {
620 match *self {
621 Subcommand::Test { ref pass, .. } => pass.as_ref().map(|s| &s[..]),
622 _ => None,
623 }
624 }
625
626 pub fn run(&self) -> Option<&str> {
627 match *self {
628 Subcommand::Test { ref run, .. } => run.as_ref().map(|s| &s[..]),
629 _ => None,
630 }
631 }
632
633 pub fn open(&self) -> bool {
634 match *self {
635 Subcommand::Doc { open, .. } => open,
636 _ => false,
637 }
638 }
639
640 pub fn json(&self) -> bool {
641 match *self {
642 Subcommand::Doc { json, .. } => json,
643 _ => false,
644 }
645 }
646
647 pub fn timings(&self) -> bool {
648 match *self {
649 Subcommand::Build { timings, .. } | Subcommand::Check { timings, .. } => timings,
650 _ => false,
651 }
652 }
653
654 pub fn vendor_versioned_dirs(&self) -> bool {
655 match *self {
656 Subcommand::Vendor { versioned_dirs, .. } => versioned_dirs,
657 _ => false,
658 }
659 }
660
661 pub fn vendor_sync_args(&self) -> Vec<PathBuf> {
662 match self {
663 Subcommand::Vendor { sync, .. } => sync.clone(),
664 _ => vec![],
665 }
666 }
667
668 pub fn test_codegen_backend(&self) -> Option<&CodegenBackendKind> {
669 match self {
670 Subcommand::Test { test_codegen_backend, .. } => test_codegen_backend.as_ref(),
671 _ => None,
672 }
673 }
674
675 pub fn bypass_ignore_backends(&self) -> bool {
676 match self {
677 Subcommand::Test { bypass_ignore_backends, .. } => *bypass_ignore_backends,
678 _ => false,
679 }
680 }
681}
682
683pub fn get_completion(shell: &dyn Generator, path: &Path) -> Option<String> {
686 let mut cmd = Flags::command();
687 let current = if !path.exists() {
688 String::new()
689 } else {
690 std::fs::read_to_string(path).unwrap_or_else(|_| {
691 eprintln!("couldn't read {}", path.display());
692 crate::exit!(1)
693 })
694 };
695 let mut buf = Vec::new();
696 let (bin_name, _) = path
697 .file_name()
698 .expect("path should be a regular file")
699 .to_str()
700 .expect("file name should be UTF-8")
701 .rsplit_once('.')
702 .expect("file name should have an extension");
703
704 cmd.set_bin_name(bin_name);
707 cmd.build();
708 shell.generate(&cmd, &mut buf);
709 if buf == current.as_bytes() {
710 return None;
711 }
712 Some(String::from_utf8(buf).expect("completion script should be UTF-8"))
713}
714
715pub fn top_level_help() -> String {
717 let mut cmd = Flags::command();
718 cmd.render_help().to_string()
719}