diff --git a/src/bin/cargo/cli.rs b/src/bin/cargo/cli.rs index ce6112817ec..d39dce754e5 100644 --- a/src/bin/cargo/cli.rs +++ b/src/bin/cargo/cli.rs @@ -12,6 +12,7 @@ use std::fmt::Write; use super::commands; use super::list_commands; use crate::command_prelude::*; +use crate::util::is_rustup; use cargo::core::features::HIDDEN; pub fn main(config: &mut LazyConfig) -> CliResult { @@ -511,11 +512,7 @@ impl GlobalArgs { } pub fn cli() -> Command { - // ALLOWED: `RUSTUP_HOME` should only be read from process env, otherwise - // other tools may point to executables from incompatible distributions. - #[allow(clippy::disallowed_methods)] - let is_rustup = std::env::var_os("RUSTUP_HOME").is_some(); - let usage = if is_rustup { + let usage = if is_rustup() { "cargo [+toolchain] [OPTIONS] [COMMAND]\n cargo [+toolchain] [OPTIONS] -Zscript [ARGS]..." } else { "cargo [OPTIONS] [COMMAND]\n cargo [OPTIONS] -Zscript [ARGS]..." diff --git a/src/bin/cargo/commands/clean.rs b/src/bin/cargo/commands/clean.rs index 9fa3c852700..c47e1670203 100644 --- a/src/bin/cargo/commands/clean.rs +++ b/src/bin/cargo/commands/clean.rs @@ -27,7 +27,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { let opts = CleanOptions { config, spec: values(args, "package"), - targets: args.targets(), + targets: args.targets()?, requested_profile: args.get_profile_name(config, "dev", ProfileChecking::Custom)?, profile_specified: args.contains_id("profile") || args.flag("release"), doc: args.flag("doc"), diff --git a/src/bin/cargo/commands/fetch.rs b/src/bin/cargo/commands/fetch.rs index 4b1fcb40f25..ba9e8d82b9e 100644 --- a/src/bin/cargo/commands/fetch.rs +++ b/src/bin/cargo/commands/fetch.rs @@ -17,7 +17,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { let opts = FetchOptions { config, - targets: args.targets(), + targets: args.targets()?, }; let _ = ops::fetch(&ws, &opts)?; Ok(()) diff --git a/src/bin/cargo/commands/package.rs b/src/bin/cargo/commands/package.rs index e87fb3ef8a7..7fa993f1dd3 100644 --- a/src/bin/cargo/commands/package.rs +++ b/src/bin/cargo/commands/package.rs @@ -58,7 +58,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { check_metadata: !args.flag("no-metadata"), allow_dirty: args.flag("allow-dirty"), to_package: specs, - targets: args.targets(), + targets: args.targets()?, jobs: args.jobs()?, keep_going: args.keep_going(), cli_features: args.cli_features()?, diff --git a/src/bin/cargo/commands/publish.rs b/src/bin/cargo/commands/publish.rs index f30633e3de4..8a9175f6cd8 100644 --- a/src/bin/cargo/commands/publish.rs +++ b/src/bin/cargo/commands/publish.rs @@ -50,7 +50,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { verify: !args.flag("no-verify"), allow_dirty: args.flag("allow-dirty"), to_publish: args.packages_from_flags()?, - targets: args.targets(), + targets: args.targets()?, jobs: args.jobs()?, keep_going: args.keep_going(), dry_run: args.dry_run(), diff --git a/src/bin/cargo/commands/tree.rs b/src/bin/cargo/commands/tree.rs index 4472765a92d..4657eafff96 100644 --- a/src/bin/cargo/commands/tree.rs +++ b/src/bin/cargo/commands/tree.rs @@ -136,7 +136,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { .warn("the --all-targets flag has been changed to --target=all")?; vec!["all".to_string()] } else { - args._values_of("target") + args.targets()? }; let target = tree::Target::from_cli(targets); diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index 2186b9f2900..f55e44f2c68 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -4,6 +4,7 @@ use crate::core::{Edition, Workspace}; use crate::ops::{CompileFilter, CompileOptions, NewOptions, Packages, VersionControl}; use crate::util::important_paths::find_root_manifest_for_wd; use crate::util::interning::InternedString; +use crate::util::is_rustup; use crate::util::restricted_names::is_glob_pattern; use crate::util::toml::{StringOrVec, TomlProfile}; use crate::util::validate_package_name; @@ -218,7 +219,10 @@ pub trait CommandExt: Sized { } fn arg_target_triple(self, target: &'static str) -> Self { - self._arg(multi_opt("target", "TRIPLE", target).help_heading(heading::COMPILATION_OPTIONS)) + self._arg( + optional_multi_opt("target", "TRIPLE", target) + .help_heading(heading::COMPILATION_OPTIONS), + ) } fn arg_target_dir(self) -> Self { @@ -440,8 +444,20 @@ pub trait ArgMatchesExt { self.maybe_flag("keep-going") } - fn targets(&self) -> Vec { - self._values_of("target") + fn targets(&self) -> CargoResult> { + if self.is_present_with_zero_values("target") { + let cmd = if is_rustup() { + "rustup target list" + } else { + "rustc --print target-list" + }; + bail!( + "\"--target\" takes a target architecture as an argument. + +Run `{cmd}` to see possible targets." + ); + } + Ok(self._values_of("target")) } fn get_profile_name( @@ -590,7 +606,7 @@ pub trait ArgMatchesExt { config, self.jobs()?, self.keep_going(), - &self.targets(), + &self.targets()?, mode, )?; build_config.message_format = message_format.unwrap_or(MessageFormat::Human); diff --git a/src/cargo/util/mod.rs b/src/cargo/util/mod.rs index af7dd368781..2c3e5a80274 100644 --- a/src/cargo/util/mod.rs +++ b/src/cargo/util/mod.rs @@ -66,6 +66,13 @@ pub mod toml_mut; mod vcs; mod workspace; +pub fn is_rustup() -> bool { + // ALLOWED: `RUSTUP_HOME` should only be read from process env, otherwise + // other tools may point to executables from incompatible distributions. + #[allow(clippy::disallowed_methods)] + std::env::var_os("RUSTUP_HOME").is_some() +} + pub fn elapsed(duration: Duration) -> String { let secs = duration.as_secs(); diff --git a/tests/testsuite/cargo_bench/help/stdout.log b/tests/testsuite/cargo_bench/help/stdout.log index 5d9484df9ee..430d8be426f 100644 --- a/tests/testsuite/cargo_bench/help/stdout.log +++ b/tests/testsuite/cargo_bench/help/stdout.log @@ -45,7 +45,7 @@ Feature Selection: Compilation Options: -j, --jobs Number of parallel jobs, defaults to # of CPUs. --profile Build artifacts with the specified profile - --target Build for the target triple + --target [] Build for the target triple --target-dir Directory for all generated artifacts --unit-graph Output build graph in JSON (unstable) --timings[=] Timing output formats (unstable) (comma separated): html, json diff --git a/tests/testsuite/cargo_build/help/stdout.log b/tests/testsuite/cargo_build/help/stdout.log index e51a3b177dc..56b934cd168 100644 --- a/tests/testsuite/cargo_build/help/stdout.log +++ b/tests/testsuite/cargo_build/help/stdout.log @@ -42,7 +42,7 @@ Compilation Options: --profile Build artifacts with the specified profile -j, --jobs Number of parallel jobs, defaults to # of CPUs. --keep-going Do not abort the build as soon as there is an error - --target Build for the target triple + --target [] Build for the target triple --target-dir Directory for all generated artifacts --out-dir Copy final artifacts to this directory (unstable) --build-plan Output the build plan in JSON (unstable) diff --git a/tests/testsuite/cargo_check/help/stdout.log b/tests/testsuite/cargo_check/help/stdout.log index 8aee0bcce86..92d44a6ded2 100644 --- a/tests/testsuite/cargo_check/help/stdout.log +++ b/tests/testsuite/cargo_check/help/stdout.log @@ -42,7 +42,7 @@ Compilation Options: --keep-going Do not abort the build as soon as there is an error -r, --release Check artifacts in release mode, with optimizations --profile Check artifacts with the specified profile - --target Check for the target triple + --target [] Check for the target triple --target-dir Directory for all generated artifacts --unit-graph Output build graph in JSON (unstable) --timings[=] Timing output formats (unstable) (comma separated): html, json diff --git a/tests/testsuite/cargo_clean/help/stdout.log b/tests/testsuite/cargo_clean/help/stdout.log index fd3c8855c50..2074d9633a3 100644 --- a/tests/testsuite/cargo_clean/help/stdout.log +++ b/tests/testsuite/cargo_clean/help/stdout.log @@ -17,7 +17,7 @@ Package Selection: Compilation Options: -r, --release Whether or not to clean release artifacts --profile Clean artifacts of the specified profile - --target Target triple to clean output for + --target [] Target triple to clean output for --target-dir Directory for all generated artifacts Manifest Options: diff --git a/tests/testsuite/cargo_doc/help/stdout.log b/tests/testsuite/cargo_doc/help/stdout.log index d910c960a35..8ff5f9b72a7 100644 --- a/tests/testsuite/cargo_doc/help/stdout.log +++ b/tests/testsuite/cargo_doc/help/stdout.log @@ -39,7 +39,7 @@ Compilation Options: --keep-going Do not abort the build as soon as there is an error -r, --release Build artifacts in release mode, with optimizations --profile Build artifacts with the specified profile - --target Build for the target triple + --target [] Build for the target triple --target-dir Directory for all generated artifacts --unit-graph Output build graph in JSON (unstable) --timings[=] Timing output formats (unstable) (comma separated): html, json diff --git a/tests/testsuite/cargo_fetch/help/stdout.log b/tests/testsuite/cargo_fetch/help/stdout.log index b9bd6c35bb1..32f29f1b39a 100644 --- a/tests/testsuite/cargo_fetch/help/stdout.log +++ b/tests/testsuite/cargo_fetch/help/stdout.log @@ -11,7 +11,7 @@ Options: -h, --help Print help Compilation Options: - --target Fetch dependencies for the target triple + --target [] Fetch dependencies for the target triple Manifest Options: --manifest-path Path to Cargo.toml diff --git a/tests/testsuite/cargo_fix/help/stdout.log b/tests/testsuite/cargo_fix/help/stdout.log index cfd76a4ff07..dbbd11b7752 100644 --- a/tests/testsuite/cargo_fix/help/stdout.log +++ b/tests/testsuite/cargo_fix/help/stdout.log @@ -47,7 +47,7 @@ Compilation Options: --keep-going Do not abort the build as soon as there is an error -r, --release Fix artifacts in release mode, with optimizations --profile Build artifacts with the specified profile - --target Fix for the target triple + --target [] Fix for the target triple --target-dir Directory for all generated artifacts --timings[=] Timing output formats (unstable) (comma separated): html, json diff --git a/tests/testsuite/cargo_install/help/stdout.log b/tests/testsuite/cargo_install/help/stdout.log index de23100f42e..2267c5f6b01 100644 --- a/tests/testsuite/cargo_install/help/stdout.log +++ b/tests/testsuite/cargo_install/help/stdout.log @@ -44,7 +44,7 @@ Compilation Options: -j, --jobs Number of parallel jobs, defaults to # of CPUs. --keep-going Do not abort the build as soon as there is an error --profile Install artifacts with the specified profile - --target Build for the target triple + --target [] Build for the target triple --target-dir Directory for all generated artifacts --timings[=] Timing output formats (unstable) (comma separated): html, json diff --git a/tests/testsuite/cargo_package/help/stdout.log b/tests/testsuite/cargo_package/help/stdout.log index 568cc5796c8..5079c2a6fa3 100644 --- a/tests/testsuite/cargo_package/help/stdout.log +++ b/tests/testsuite/cargo_package/help/stdout.log @@ -25,7 +25,7 @@ Feature Selection: --no-default-features Do not activate the `default` feature Compilation Options: - --target Build for the target triple + --target [] Build for the target triple --target-dir Directory for all generated artifacts -j, --jobs Number of parallel jobs, defaults to # of CPUs. --keep-going Do not abort the build as soon as there is an error diff --git a/tests/testsuite/cargo_publish/help/stdout.log b/tests/testsuite/cargo_publish/help/stdout.log index 7f37ee56c09..313b937f91a 100644 --- a/tests/testsuite/cargo_publish/help/stdout.log +++ b/tests/testsuite/cargo_publish/help/stdout.log @@ -27,7 +27,7 @@ Feature Selection: Compilation Options: -j, --jobs Number of parallel jobs, defaults to # of CPUs. --keep-going Do not abort the build as soon as there is an error - --target Build for the target triple + --target [] Build for the target triple --target-dir Directory for all generated artifacts Manifest Options: diff --git a/tests/testsuite/cargo_run/help/stdout.log b/tests/testsuite/cargo_run/help/stdout.log index c8777eaef56..4b39f30b3cf 100644 --- a/tests/testsuite/cargo_run/help/stdout.log +++ b/tests/testsuite/cargo_run/help/stdout.log @@ -33,7 +33,7 @@ Compilation Options: --keep-going Do not abort the build as soon as there is an error -r, --release Build artifacts in release mode, with optimizations --profile Build artifacts with the specified profile - --target Build for the target triple + --target [] Build for the target triple --target-dir Directory for all generated artifacts --unit-graph Output build graph in JSON (unstable) --timings[=] Timing output formats (unstable) (comma separated): html, json diff --git a/tests/testsuite/cargo_rustc/help/stdout.log b/tests/testsuite/cargo_rustc/help/stdout.log index d6394effeab..9d43841fe2e 100644 --- a/tests/testsuite/cargo_rustc/help/stdout.log +++ b/tests/testsuite/cargo_rustc/help/stdout.log @@ -44,7 +44,7 @@ Compilation Options: --keep-going Do not abort the build as soon as there is an error -r, --release Build artifacts in release mode, with optimizations --profile Build artifacts with the specified profile - --target Target triple which compiles will be for + --target [] Target triple which compiles will be for --target-dir Directory for all generated artifacts --unit-graph Output build graph in JSON (unstable) --timings[=] Timing output formats (unstable) (comma separated): html, json diff --git a/tests/testsuite/cargo_rustdoc/help/stdout.log b/tests/testsuite/cargo_rustdoc/help/stdout.log index 93c3ba85199..706072f24de 100644 --- a/tests/testsuite/cargo_rustdoc/help/stdout.log +++ b/tests/testsuite/cargo_rustdoc/help/stdout.log @@ -42,7 +42,7 @@ Compilation Options: --keep-going Do not abort the build as soon as there is an error -r, --release Build artifacts in release mode, with optimizations --profile Build artifacts with the specified profile - --target Build for the target triple + --target [] Build for the target triple --target-dir Directory for all generated artifacts --unit-graph Output build graph in JSON (unstable) --timings[=] Timing output formats (unstable) (comma separated): html, json diff --git a/tests/testsuite/cargo_test/help/stdout.log b/tests/testsuite/cargo_test/help/stdout.log index d693dc3c92d..5df62d6bb17 100644 --- a/tests/testsuite/cargo_test/help/stdout.log +++ b/tests/testsuite/cargo_test/help/stdout.log @@ -48,7 +48,7 @@ Compilation Options: -j, --jobs Number of parallel jobs, defaults to # of CPUs. -r, --release Build artifacts in release mode, with optimizations --profile Build artifacts with the specified profile - --target Build for the target triple + --target [] Build for the target triple --target-dir Directory for all generated artifacts --unit-graph Output build graph in JSON (unstable) --timings[=] Timing output formats (unstable) (comma separated): html, json diff --git a/tests/testsuite/cargo_tree/help/stdout.log b/tests/testsuite/cargo_tree/help/stdout.log index 268b6b2ada6..4170583a854 100644 --- a/tests/testsuite/cargo_tree/help/stdout.log +++ b/tests/testsuite/cargo_tree/help/stdout.log @@ -33,8 +33,8 @@ Feature Selection: --no-default-features Do not activate the `default` feature Compilation Options: - --target Filter dependencies matching the given target-triple (default host - platform). Pass `all` to include all targets. + --target [] Filter dependencies matching the given target-triple (default host + platform). Pass `all` to include all targets. Manifest Options: --manifest-path Path to Cargo.toml diff --git a/tests/testsuite/list_availables.rs b/tests/testsuite/list_availables.rs index 6bbbeb160d5..fe635a19bd5 100644 --- a/tests/testsuite/list_availables.rs +++ b/tests/testsuite/list_availables.rs @@ -8,6 +8,7 @@ const BIN: u8 = 1 << 1; const TEST: u8 = 1 << 2; const BENCH: u8 = 1 << 3; const PACKAGE: u8 = 1 << 4; +const TARGET: u8 = 1 << 5; fn list_availables_test(command: &str, targets: u8) { let full_project = project() @@ -154,6 +155,20 @@ No benches available. error: \"--test\" takes one argument. No tests available. +", + ) + .with_status(101) + .run(); + } + + if targets & TARGET != 0 { + empty_project + .cargo(&format!("{} --target", command)) + .with_stderr( + "\ +error: \"--target\" takes a target architecture as an argument. + +Run `[..]` to see possible targets. ", ) .with_status(101) @@ -163,52 +178,52 @@ No tests available. #[cargo_test] fn build_list_availables() { - list_availables_test("build", EXAMPLE | BIN | TEST | BENCH | PACKAGE); + list_availables_test("build", EXAMPLE | BIN | TEST | BENCH | PACKAGE | TARGET); } #[cargo_test] fn check_list_availables() { - list_availables_test("check", EXAMPLE | BIN | TEST | BENCH | PACKAGE); + list_availables_test("check", EXAMPLE | BIN | TEST | BENCH | PACKAGE | TARGET); } #[cargo_test] fn doc_list_availables() { - list_availables_test("doc", BIN | PACKAGE); + list_availables_test("doc", BIN | PACKAGE | TARGET); } #[cargo_test] fn fix_list_availables() { - list_availables_test("fix", EXAMPLE | BIN | TEST | BENCH | PACKAGE); + list_availables_test("fix", EXAMPLE | BIN | TEST | BENCH | PACKAGE | TARGET); } #[cargo_test] fn run_list_availables() { - list_availables_test("run", EXAMPLE | BIN | PACKAGE); + list_availables_test("run", EXAMPLE | BIN | PACKAGE | TARGET); } #[cargo_test] fn test_list_availables() { - list_availables_test("test", EXAMPLE | BIN | TEST | BENCH | PACKAGE); + list_availables_test("test", EXAMPLE | BIN | TEST | BENCH | PACKAGE | TARGET); } #[cargo_test] fn bench_list_availables() { - list_availables_test("bench", EXAMPLE | BIN | TEST | BENCH | PACKAGE); + list_availables_test("bench", EXAMPLE | BIN | TEST | BENCH | PACKAGE | TARGET); } #[cargo_test] fn install_list_availables() { - list_availables_test("install", EXAMPLE | BIN); + list_availables_test("install", EXAMPLE | BIN | TARGET); } #[cargo_test] fn rustdoc_list_availables() { - list_availables_test("rustdoc", EXAMPLE | BIN | TEST | BENCH | PACKAGE); + list_availables_test("rustdoc", EXAMPLE | BIN | TEST | BENCH | PACKAGE | TARGET); } #[cargo_test] fn rustc_list_availables() { - list_availables_test("rustc", EXAMPLE | BIN | TEST | BENCH | PACKAGE); + list_availables_test("rustc", EXAMPLE | BIN | TEST | BENCH | PACKAGE | TARGET); } #[cargo_test] @@ -218,12 +233,12 @@ fn pkgid_list_availables() { #[cargo_test] fn tree_list_availables() { - list_availables_test("tree", PACKAGE); + list_availables_test("tree", PACKAGE | TARGET); } #[cargo_test] fn clean_list_availables() { - list_availables_test("clean", PACKAGE); + list_availables_test("clean", PACKAGE | TARGET); } #[cargo_test]