Skip to content

Commit b17ae5b

Browse files
committed
Migrate x to using clap
1 parent 0a3b557 commit b17ae5b

File tree

10 files changed

+460
-723
lines changed

10 files changed

+460
-723
lines changed

src/bootstrap/Cargo.lock

+73-18
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ version = "0.0.0"
3838
dependencies = [
3939
"build_helper",
4040
"cc",
41+
"clap",
4142
"cmake",
4243
"fd-lock",
4344
"filetime",
44-
"getopts",
4545
"hex",
4646
"ignore",
4747
"libc",
@@ -88,6 +88,40 @@ version = "1.0.0"
8888
source = "registry+https://github.com/rust-lang/crates.io-index"
8989
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
9090

91+
[[package]]
92+
name = "clap"
93+
version = "4.1.4"
94+
source = "registry+https://github.com/rust-lang/crates.io-index"
95+
checksum = "f13b9c79b5d1dd500d20ef541215a6423c75829ef43117e1b4d17fd8af0b5d76"
96+
dependencies = [
97+
"bitflags",
98+
"clap_derive",
99+
"clap_lex",
100+
"once_cell",
101+
]
102+
103+
[[package]]
104+
name = "clap_derive"
105+
version = "4.1.0"
106+
source = "registry+https://github.com/rust-lang/crates.io-index"
107+
checksum = "684a277d672e91966334af371f1a7b5833f9aa00b07c84e92fbce95e00208ce8"
108+
dependencies = [
109+
"heck",
110+
"proc-macro-error",
111+
"proc-macro2",
112+
"quote",
113+
"syn",
114+
]
115+
116+
[[package]]
117+
name = "clap_lex"
118+
version = "0.3.1"
119+
source = "registry+https://github.com/rust-lang/crates.io-index"
120+
checksum = "783fe232adfca04f90f56201b26d79682d4cd2625e0bc7290b95123afe558ade"
121+
dependencies = [
122+
"os_str_bytes",
123+
]
124+
91125
[[package]]
92126
name = "cmake"
93127
version = "0.1.48"
@@ -257,15 +291,6 @@ dependencies = [
257291
"version_check",
258292
]
259293

260-
[[package]]
261-
name = "getopts"
262-
version = "0.2.21"
263-
source = "registry+https://github.com/rust-lang/crates.io-index"
264-
checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5"
265-
dependencies = [
266-
"unicode-width",
267-
]
268-
269294
[[package]]
270295
name = "globset"
271296
version = "0.4.8"
@@ -279,6 +304,12 @@ dependencies = [
279304
"regex",
280305
]
281306

307+
[[package]]
308+
name = "heck"
309+
version = "0.4.1"
310+
source = "registry+https://github.com/rust-lang/crates.io-index"
311+
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
312+
282313
[[package]]
283314
name = "hermit-abi"
284315
version = "0.1.19"
@@ -425,6 +456,12 @@ dependencies = [
425456
"winapi",
426457
]
427458

459+
[[package]]
460+
name = "os_str_bytes"
461+
version = "6.4.1"
462+
source = "registry+https://github.com/rust-lang/crates.io-index"
463+
checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee"
464+
428465
[[package]]
429466
name = "output_vt100"
430467
version = "0.1.3"
@@ -452,6 +489,30 @@ dependencies = [
452489
"yansi",
453490
]
454491

492+
[[package]]
493+
name = "proc-macro-error"
494+
version = "1.0.4"
495+
source = "registry+https://github.com/rust-lang/crates.io-index"
496+
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
497+
dependencies = [
498+
"proc-macro-error-attr",
499+
"proc-macro2",
500+
"quote",
501+
"syn",
502+
"version_check",
503+
]
504+
505+
[[package]]
506+
name = "proc-macro-error-attr"
507+
version = "1.0.4"
508+
source = "registry+https://github.com/rust-lang/crates.io-index"
509+
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
510+
dependencies = [
511+
"proc-macro2",
512+
"quote",
513+
"version_check",
514+
]
515+
455516
[[package]]
456517
name = "proc-macro2"
457518
version = "1.0.46"
@@ -527,9 +588,9 @@ checksum = "49b3de9ec5dc0a3417da371aab17d729997c15010e7fd24ff707773a33bddb64"
527588

528589
[[package]]
529590
name = "rustix"
530-
version = "0.36.3"
591+
version = "0.36.7"
531592
source = "registry+https://github.com/rust-lang/crates.io-index"
532-
checksum = "0b1fbb4dfc4eb1d390c02df47760bb19a84bb80b301ecc947ab5406394d8223e"
593+
checksum = "d4fdebc4b395b7fbb9ab11e462e20ed9051e7b16e42d24042c776eca0ac81b03"
533594
dependencies = [
534595
"bitflags",
535596
"errno",
@@ -666,12 +727,6 @@ version = "1.0.0"
666727
source = "registry+https://github.com/rust-lang/crates.io-index"
667728
checksum = "d22af068fba1eb5edcb4aea19d382b2a3deb4c8f9d475c589b6ada9e0fd493ee"
668729

669-
[[package]]
670-
name = "unicode-width"
671-
version = "0.1.9"
672-
source = "registry+https://github.com/rust-lang/crates.io-index"
673-
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
674-
675730
[[package]]
676731
name = "version_check"
677732
version = "0.9.4"

src/bootstrap/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ build_helper = { path = "../tools/build_helper" }
3434
cmake = "0.1.38"
3535
fd-lock = "3.0.8"
3636
filetime = "0.2"
37-
getopts = "0.2.19"
3837
cc = "1.0.69"
38+
clap = { version = "4.1.4", features = ["std", "usage", "help", "derive", "error-context"], default-features = false}
3939
libc = "0.2"
4040
hex = "0.4"
4141
object = { version = "0.29.0", default-features = false, features = ["archive", "coff", "read_core", "unaligned"] }

src/bootstrap/builder.rs

+18-12
Original file line numberDiff line numberDiff line change
@@ -546,19 +546,24 @@ impl<'a> ShouldRun<'a> {
546546
}
547547
}
548548

549-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
549+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, clap::ValueEnum)]
550550
pub enum Kind {
551+
#[clap(aliases = ["b"])]
551552
Build,
553+
#[clap(aliases = ["c"])]
552554
Check,
553555
Clippy,
554556
Fix,
555557
Format,
558+
#[clap(aliases = ["t"])]
556559
Test,
557560
Bench,
561+
#[clap(aliases = ["d"])]
558562
Doc,
559563
Clean,
560564
Dist,
561565
Install,
566+
#[clap(aliases = ["r"])]
562567
Run,
563568
Setup,
564569
}
@@ -846,18 +851,19 @@ impl<'a> Builder<'a> {
846851
}
847852

848853
pub fn new(build: &Build) -> Builder<'_> {
854+
let paths = &build.config.paths;
849855
let (kind, paths) = match build.config.cmd {
850-
Subcommand::Build { ref paths } => (Kind::Build, &paths[..]),
851-
Subcommand::Check { ref paths } => (Kind::Check, &paths[..]),
852-
Subcommand::Clippy { ref paths, .. } => (Kind::Clippy, &paths[..]),
853-
Subcommand::Fix { ref paths } => (Kind::Fix, &paths[..]),
854-
Subcommand::Doc { ref paths, .. } => (Kind::Doc, &paths[..]),
855-
Subcommand::Test { ref paths, .. } => (Kind::Test, &paths[..]),
856-
Subcommand::Bench { ref paths, .. } => (Kind::Bench, &paths[..]),
857-
Subcommand::Dist { ref paths } => (Kind::Dist, &paths[..]),
858-
Subcommand::Install { ref paths } => (Kind::Install, &paths[..]),
859-
Subcommand::Run { ref paths, .. } => (Kind::Run, &paths[..]),
860-
Subcommand::Clean { ref paths, .. } => (Kind::Clean, &paths[..]),
856+
Subcommand::Build => (Kind::Build, &paths[..]),
857+
Subcommand::Check { .. } => (Kind::Check, &paths[..]),
858+
Subcommand::Clippy { .. } => (Kind::Clippy, &paths[..]),
859+
Subcommand::Fix => (Kind::Fix, &paths[..]),
860+
Subcommand::Doc { .. } => (Kind::Doc, &paths[..]),
861+
Subcommand::Test { .. } => (Kind::Test, &paths[..]),
862+
Subcommand::Bench { .. } => (Kind::Bench, &paths[..]),
863+
Subcommand::Dist => (Kind::Dist, &paths[..]),
864+
Subcommand::Install => (Kind::Install, &paths[..]),
865+
Subcommand::Run { .. } => (Kind::Run, &paths[..]),
866+
Subcommand::Clean { .. } => (Kind::Clean, &paths[..]),
861867
Subcommand::Format { .. } => (Kind::Format, &[][..]),
862868
Subcommand::Setup { profile: ref path } => (
863869
Kind::Setup,

src/bootstrap/builder/tests.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ mod defaults {
236236
fn doc_default() {
237237
let mut config = configure("doc", &["A"], &["A"]);
238238
config.compiler_docs = true;
239-
config.cmd = Subcommand::Doc { paths: Vec::new(), open: false, json: false };
239+
config.cmd = Subcommand::Doc { open: false, json: false };
240240
let mut cache = run_build(&[], config);
241241
let a = TargetSelection::from_user("A");
242242

@@ -545,19 +545,21 @@ mod dist {
545545
fn test_with_no_doc_stage0() {
546546
let mut config = configure(&["A"], &["A"]);
547547
config.stage = 0;
548+
config.paths = vec!["library/std".into()];
548549
config.cmd = Subcommand::Test {
549-
paths: vec!["library/std".into()],
550550
test_args: vec![],
551551
rustc_args: vec![],
552-
fail_fast: true,
553-
doc_tests: DocTests::No,
552+
no_fail_fast: false,
553+
no_doc: true,
554+
doc: false,
554555
bless: false,
555556
force_rerun: false,
556557
compare_mode: None,
557558
rustfix_coverage: false,
558559
pass: None,
559560
run: None,
560561
only_modified: false,
562+
skip: vec![],
561563
};
562564

563565
let build = Build::new(config);
@@ -588,7 +590,7 @@ mod dist {
588590
fn doc_ci() {
589591
let mut config = configure(&["A"], &["A"]);
590592
config.compiler_docs = true;
591-
config.cmd = Subcommand::Doc { paths: Vec::new(), open: false, json: false };
593+
config.cmd = Subcommand::Doc { open: false, json: false };
592594
let build = Build::new(config);
593595
let mut builder = Builder::new(&build);
594596
builder.run_step_descriptions(&Builder::get_step_descriptions(Kind::Doc), &[]);
@@ -617,18 +619,19 @@ mod dist {
617619
// Behavior of `x.py test` doing various documentation tests.
618620
let mut config = configure(&["A"], &["A"]);
619621
config.cmd = Subcommand::Test {
620-
paths: vec![],
621622
test_args: vec![],
622623
rustc_args: vec![],
623-
fail_fast: true,
624-
doc_tests: DocTests::Yes,
624+
no_fail_fast: false,
625+
doc: true,
626+
no_doc: false,
625627
bless: false,
626628
force_rerun: false,
627629
compare_mode: None,
628630
rustfix_coverage: false,
629631
pass: None,
630632
run: None,
631633
only_modified: false,
634+
skip: vec![],
632635
};
633636
// Make sure rustfmt binary not being found isn't an error.
634637
config.channel = "beta".to_string();

src/bootstrap/check.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,7 @@ fn args(builder: &Builder<'_>) -> Vec<String> {
2020
arr.iter().copied().map(String::from)
2121
}
2222

23-
if let Subcommand::Clippy {
24-
fix,
25-
clippy_lint_allow,
26-
clippy_lint_deny,
27-
clippy_lint_warn,
28-
clippy_lint_forbid,
29-
..
30-
} = &builder.config.cmd
31-
{
23+
if let Subcommand::Clippy { fix, allow, deny, warn, forbid, .. } = &builder.config.cmd {
3224
// disable the most spammy clippy lints
3325
let ignored_lints = vec![
3426
"many_single_char_names", // there are a lot in stdarch
@@ -53,10 +45,10 @@ fn args(builder: &Builder<'_>) -> Vec<String> {
5345
args.extend(strings(&["--", "--cap-lints", "warn"]));
5446
args.extend(ignored_lints.iter().map(|lint| format!("-Aclippy::{}", lint)));
5547
let mut clippy_lint_levels: Vec<String> = Vec::new();
56-
clippy_lint_allow.iter().for_each(|v| clippy_lint_levels.push(format!("-A{}", v)));
57-
clippy_lint_deny.iter().for_each(|v| clippy_lint_levels.push(format!("-D{}", v)));
58-
clippy_lint_warn.iter().for_each(|v| clippy_lint_levels.push(format!("-W{}", v)));
59-
clippy_lint_forbid.iter().for_each(|v| clippy_lint_levels.push(format!("-F{}", v)));
48+
allow.iter().for_each(|v| clippy_lint_levels.push(format!("-A{}", v)));
49+
deny.iter().for_each(|v| clippy_lint_levels.push(format!("-D{}", v)));
50+
warn.iter().for_each(|v| clippy_lint_levels.push(format!("-W{}", v)));
51+
forbid.iter().for_each(|v| clippy_lint_levels.push(format!("-F{}", v)));
6052
args.extend(clippy_lint_levels);
6153
args.extend(builder.config.free_args.clone());
6254
args

src/bootstrap/config.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ pub struct Config {
232232
pub initial_rustfmt: RefCell<RustfmtState>,
233233
pub out: PathBuf,
234234
pub rust_info: channel::GitInfo,
235+
236+
pub paths: Vec<PathBuf>,
235237
}
236238

237239
#[derive(Default, Deserialize)]
@@ -367,6 +369,12 @@ pub struct TargetSelection {
367369
file: Option<Interned<String>>,
368370
}
369371

372+
impl<'a> From<&'a str> for TargetSelection {
373+
fn from(s: &'a str) -> Self {
374+
Self::from_user(s)
375+
}
376+
}
377+
370378
impl TargetSelection {
371379
pub fn from_user(selection: &str) -> Self {
372380
let path = Path::new(selection);
@@ -855,25 +863,26 @@ impl Config {
855863
}
856864

857865
fn parse_inner<'a>(args: &[String], get_toml: impl 'a + Fn(&Path) -> TomlConfig) -> Config {
858-
let flags = Flags::parse(&args);
866+
let mut flags = Flags::parse(&args);
859867
let mut config = Config::default_opts();
860868

861869
// Set flags.
870+
config.paths = std::mem::take(&mut flags.paths);
862871
config.exclude = flags.exclude.into_iter().map(|path| TaskPath::parse(path)).collect();
863872
config.include_default_paths = flags.include_default_paths;
864-
config.rustc_error_format = flags.rustc_error_format;
873+
config.rustc_error_format = flags.error_format;
865874
config.json_output = flags.json_output;
866875
config.on_fail = flags.on_fail;
867-
config.jobs = flags.jobs.map(threads_from_config);
876+
config.jobs = Some(threads_from_config(flags.jobs as u32));
868877
config.cmd = flags.cmd;
869878
config.incremental = flags.incremental;
870879
config.dry_run = if flags.dry_run { DryRun::UserSelected } else { DryRun::Disabled };
871880
config.keep_stage = flags.keep_stage;
872881
config.keep_stage_std = flags.keep_stage_std;
873882
config.color = flags.color;
874-
config.free_args = flags.free_args.clone().unwrap_or_default();
875-
if let Some(value) = flags.deny_warnings {
876-
config.deny_warnings = value;
883+
config.free_args = flags.free_args.clone();
884+
if matches!(flags.deny_warnings, crate::flags::Warnings::Deny) {
885+
config.deny_warnings = true;
877886
}
878887
config.llvm_profile_use = flags.llvm_profile_use;
879888
config.llvm_profile_generate = flags.llvm_profile_generate;
@@ -1046,7 +1055,7 @@ impl Config {
10461055
set(&mut config.print_step_rusage, build.print_step_rusage);
10471056
set(&mut config.patch_binaries_for_nix, build.patch_binaries_for_nix);
10481057

1049-
config.verbose = cmp::max(config.verbose, flags.verbose);
1058+
config.verbose = cmp::max(config.verbose, flags.verbose as usize);
10501059

10511060
if let Some(install) = toml.install {
10521061
config.prefix = install.prefix.map(PathBuf::from);
@@ -1119,7 +1128,6 @@ impl Config {
11191128
config.rustc_default_linker = rust.default_linker;
11201129
config.musl_root = rust.musl_root.map(PathBuf::from);
11211130
config.save_toolstates = rust.save_toolstates.map(PathBuf::from);
1122-
set(&mut config.deny_warnings, flags.deny_warnings.or(rust.deny_warnings));
11231131
set(&mut config.backtrace_on_ice, rust.backtrace_on_ice);
11241132
set(&mut config.rust_verify_llvm_ir, rust.verify_llvm_ir);
11251133
config.rust_thin_lto_import_instr_limit = rust.thin_lto_import_instr_limit;

0 commit comments

Comments
 (0)