Skip to content

Commit 77a9b2d

Browse files
committed
Auto merge of #12607 - epage:target, r=weihanglo
fix(cli): Help users know possible `--target` values ### What does this PR try to resolve? I was needing to do some more cross-compilation and forgot what the target triple was that I needed to run. I realized i had to re-remember the command yet again. Especially with #12585 in memory, I realized that `--target` isn't working like `--package` and other arguments that can report supported values. In working on it, I realized I probably didn't want to report supported values yet out of concern for how big the list is (see also #12585), so I decided to just list the relevant commands for now. We *might* be able to parse the rustup output to report those targets but I didn't receive a glowing endorsement from the rustup team about parsing the list (more of "yes, hyrums law and at least its interactive rather than CI"). Before: ``` error: a value is required for '--target <TRIPLE>' but none was supplied For more information, try '--help'. ``` After: ``` error: "--target" takes a target architecture as an argument. Run `rustup target list` to see possible targets. ``` (quotes were used because the other "list available" use them, we should probably work to be uniform in how we quote) ### How should we test and review this PR? First PR adds a test showing the existing output and then through the rest you can see how the output changed
2 parents 11966a4 + 802cb38 commit 77a9b2d

File tree

24 files changed

+77
-42
lines changed

24 files changed

+77
-42
lines changed

src/bin/cargo/cli.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::fmt::Write;
1212
use super::commands;
1313
use super::list_commands;
1414
use crate::command_prelude::*;
15+
use crate::util::is_rustup;
1516
use cargo::core::features::HIDDEN;
1617

1718
pub fn main(config: &mut LazyConfig) -> CliResult {
@@ -511,11 +512,7 @@ impl GlobalArgs {
511512
}
512513

513514
pub fn cli() -> Command {
514-
// ALLOWED: `RUSTUP_HOME` should only be read from process env, otherwise
515-
// other tools may point to executables from incompatible distributions.
516-
#[allow(clippy::disallowed_methods)]
517-
let is_rustup = std::env::var_os("RUSTUP_HOME").is_some();
518-
let usage = if is_rustup {
515+
let usage = if is_rustup() {
519516
"cargo [+toolchain] [OPTIONS] [COMMAND]\n cargo [+toolchain] [OPTIONS] -Zscript <MANIFEST_RS> [ARGS]..."
520517
} else {
521518
"cargo [OPTIONS] [COMMAND]\n cargo [OPTIONS] -Zscript <MANIFEST_RS> [ARGS]..."

src/bin/cargo/commands/clean.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
2727
let opts = CleanOptions {
2828
config,
2929
spec: values(args, "package"),
30-
targets: args.targets(),
30+
targets: args.targets()?,
3131
requested_profile: args.get_profile_name(config, "dev", ProfileChecking::Custom)?,
3232
profile_specified: args.contains_id("profile") || args.flag("release"),
3333
doc: args.flag("doc"),

src/bin/cargo/commands/fetch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
1717

1818
let opts = FetchOptions {
1919
config,
20-
targets: args.targets(),
20+
targets: args.targets()?,
2121
};
2222
let _ = ops::fetch(&ws, &opts)?;
2323
Ok(())

src/bin/cargo/commands/package.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
5858
check_metadata: !args.flag("no-metadata"),
5959
allow_dirty: args.flag("allow-dirty"),
6060
to_package: specs,
61-
targets: args.targets(),
61+
targets: args.targets()?,
6262
jobs: args.jobs()?,
6363
keep_going: args.keep_going(),
6464
cli_features: args.cli_features()?,

src/bin/cargo/commands/publish.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
5050
verify: !args.flag("no-verify"),
5151
allow_dirty: args.flag("allow-dirty"),
5252
to_publish: args.packages_from_flags()?,
53-
targets: args.targets(),
53+
targets: args.targets()?,
5454
jobs: args.jobs()?,
5555
keep_going: args.keep_going(),
5656
dry_run: args.dry_run(),

src/bin/cargo/commands/tree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
136136
.warn("the --all-targets flag has been changed to --target=all")?;
137137
vec!["all".to_string()]
138138
} else {
139-
args._values_of("target")
139+
args.targets()?
140140
};
141141
let target = tree::Target::from_cli(targets);
142142

src/cargo/util/command_prelude.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::core::{Edition, Workspace};
44
use crate::ops::{CompileFilter, CompileOptions, NewOptions, Packages, VersionControl};
55
use crate::util::important_paths::find_root_manifest_for_wd;
66
use crate::util::interning::InternedString;
7+
use crate::util::is_rustup;
78
use crate::util::restricted_names::is_glob_pattern;
89
use crate::util::toml::{StringOrVec, TomlProfile};
910
use crate::util::validate_package_name;
@@ -218,7 +219,10 @@ pub trait CommandExt: Sized {
218219
}
219220

220221
fn arg_target_triple(self, target: &'static str) -> Self {
221-
self._arg(multi_opt("target", "TRIPLE", target).help_heading(heading::COMPILATION_OPTIONS))
222+
self._arg(
223+
optional_multi_opt("target", "TRIPLE", target)
224+
.help_heading(heading::COMPILATION_OPTIONS),
225+
)
222226
}
223227

224228
fn arg_target_dir(self) -> Self {
@@ -440,8 +444,20 @@ pub trait ArgMatchesExt {
440444
self.maybe_flag("keep-going")
441445
}
442446

443-
fn targets(&self) -> Vec<String> {
444-
self._values_of("target")
447+
fn targets(&self) -> CargoResult<Vec<String>> {
448+
if self.is_present_with_zero_values("target") {
449+
let cmd = if is_rustup() {
450+
"rustup target list"
451+
} else {
452+
"rustc --print target-list"
453+
};
454+
bail!(
455+
"\"--target\" takes a target architecture as an argument.
456+
457+
Run `{cmd}` to see possible targets."
458+
);
459+
}
460+
Ok(self._values_of("target"))
445461
}
446462

447463
fn get_profile_name(
@@ -590,7 +606,7 @@ pub trait ArgMatchesExt {
590606
config,
591607
self.jobs()?,
592608
self.keep_going(),
593-
&self.targets(),
609+
&self.targets()?,
594610
mode,
595611
)?;
596612
build_config.message_format = message_format.unwrap_or(MessageFormat::Human);

src/cargo/util/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ pub mod toml_mut;
6666
mod vcs;
6767
mod workspace;
6868

69+
pub fn is_rustup() -> bool {
70+
// ALLOWED: `RUSTUP_HOME` should only be read from process env, otherwise
71+
// other tools may point to executables from incompatible distributions.
72+
#[allow(clippy::disallowed_methods)]
73+
std::env::var_os("RUSTUP_HOME").is_some()
74+
}
75+
6976
pub fn elapsed(duration: Duration) -> String {
7077
let secs = duration.as_secs();
7178

tests/testsuite/cargo_bench/help/stdout.log

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Feature Selection:
4545
Compilation Options:
4646
-j, --jobs <N> Number of parallel jobs, defaults to # of CPUs.
4747
--profile <PROFILE-NAME> Build artifacts with the specified profile
48-
--target <TRIPLE> Build for the target triple
48+
--target [<TRIPLE>] Build for the target triple
4949
--target-dir <DIRECTORY> Directory for all generated artifacts
5050
--unit-graph Output build graph in JSON (unstable)
5151
--timings[=<FMTS>] Timing output formats (unstable) (comma separated): html, json

tests/testsuite/cargo_build/help/stdout.log

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Compilation Options:
4242
--profile <PROFILE-NAME> Build artifacts with the specified profile
4343
-j, --jobs <N> Number of parallel jobs, defaults to # of CPUs.
4444
--keep-going Do not abort the build as soon as there is an error
45-
--target <TRIPLE> Build for the target triple
45+
--target [<TRIPLE>] Build for the target triple
4646
--target-dir <DIRECTORY> Directory for all generated artifacts
4747
--out-dir <PATH> Copy final artifacts to this directory (unstable)
4848
--build-plan Output the build plan in JSON (unstable)

0 commit comments

Comments
 (0)