From e24bf92219b2296121c9306fde3e9f9436d01d61 Mon Sep 17 00:00:00 2001 From: James Hilliard Date: Mon, 26 Apr 2021 21:03:01 -0600 Subject: [PATCH] Add feature gate for target-applies-to-host. --- .../compiler/build_context/target_info.rs | 8 ++++-- src/cargo/core/features.rs | 2 ++ src/cargo/util/config/mod.rs | 2 +- src/cargo/util/config/target.rs | 22 +++++++++++---- tests/testsuite/build_script.rs | 28 ++++++++++++------- 5 files changed, 43 insertions(+), 19 deletions(-) diff --git a/src/cargo/core/compiler/build_context/target_info.rs b/src/cargo/core/compiler/build_context/target_info.rs index e663745b26b..08b6098cc4d 100644 --- a/src/cargo/core/compiler/build_context/target_info.rs +++ b/src/cargo/core/compiler/build_context/target_info.rs @@ -691,10 +691,14 @@ impl<'cfg> RustcTargetData<'cfg> { // `--target` flag is not specified. Since the unit_dependency code // needs access to the target config data, create a copy so that it // can be found. See `rebuild_unit_graph_shared` for why this is done. + let target_applies_to_host = config.target_applies_to_host(); + if target_applies_to_host.is_err() { + return Err(target_applies_to_host.unwrap_err()); + } let host_config = if requested_kinds.iter().any(CompileKind::is_host) { let ct = CompileTarget::new(&rustc.host)?; target_info.insert(ct, host_info.clone()); - let target_host_config = if config.target_applies_to_host() { + let target_host_config = if target_applies_to_host.unwrap() { let target_cfg_clone = config.target_cfg_triple(&rustc.host)?; target_config.insert(ct, target_cfg_clone.clone()); target_cfg_clone @@ -704,7 +708,7 @@ impl<'cfg> RustcTargetData<'cfg> { }; target_host_config } else { - if config.target_applies_to_host() { + if target_applies_to_host.unwrap() { config.target_cfg_triple(&rustc.host)? } else { config.host_cfg_triple(&rustc.host)? diff --git a/src/cargo/core/features.rs b/src/cargo/core/features.rs index b5298f19e6c..d78eed8ccd8 100644 --- a/src/cargo/core/features.rs +++ b/src/cargo/core/features.rs @@ -601,6 +601,7 @@ unstable_cli_options!( no_index_update: bool = ("Do not update the registry index even if the cache is outdated"), panic_abort_tests: bool = ("Enable support to run tests with -Cpanic=abort"), host_config: bool = ("Enable the [host] section in the .cargo/config.toml file"), + target_applies_to_host: bool = ("Enable the `target-applies-to-host` key in the .cargo/config.toml file"), patch_in_config: bool = ("Allow `[patch]` sections in .cargo/config.toml files"), rustdoc_map: bool = ("Allow passing external documentation mappings to rustdoc"), separate_nightlies: bool = (HIDDEN), @@ -789,6 +790,7 @@ impl CliUnstable { "jobserver-per-rustc" => self.jobserver_per_rustc = parse_empty(k, v)?, "configurable-env" => self.configurable_env = parse_empty(k, v)?, "host-config" => self.host_config = parse_empty(k, v)?, + "target-applies-to-host" => self.target_applies_to_host = parse_empty(k, v)?, "patch-in-config" => self.patch_in_config = parse_empty(k, v)?, "features" => { // For now this is still allowed (there are still some diff --git a/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index 9b1284fedde..71bbf78b456 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -1487,7 +1487,7 @@ impl Config { } /// Returns true if the `[target]` table should be applied to host targets. - pub fn target_applies_to_host(&self) -> bool { + pub fn target_applies_to_host(&self) -> CargoResult { target::get_target_applies_to_host(self) } diff --git a/src/cargo/util/config/target.rs b/src/cargo/util/config/target.rs index 0f9ea32af72..8fa7b6cc97f 100644 --- a/src/cargo/util/config/target.rs +++ b/src/cargo/util/config/target.rs @@ -65,15 +65,25 @@ pub(super) fn load_target_cfgs(config: &Config) -> CargoResult bool { - let target_applies_to_host = config.get::("target-applies-to-host"); - if target_applies_to_host.is_ok() { - target_applies_to_host.unwrap() +pub(super) fn get_target_applies_to_host(config: &Config) -> CargoResult { + if config.cli_unstable().target_applies_to_host { + let target_applies_to_host = config.get::("target-applies-to-host"); + if target_applies_to_host.is_ok() { + Ok(target_applies_to_host.unwrap()) + } else { + if config.cli_unstable().host_config { + Ok(false) + } else { + Ok(true) + } + } } else { if config.cli_unstable().host_config { - false + anyhow::bail!( + "the -Zhost-config flag requires the -Ztarget-applies-to-host flag to be set" + ); } else { - true + Ok(true) } } } diff --git a/tests/testsuite/build_script.rs b/tests/testsuite/build_script.rs index 7bc1d71598f..48112e56568 100644 --- a/tests/testsuite/build_script.rs +++ b/tests/testsuite/build_script.rs @@ -234,7 +234,12 @@ fn custom_build_env_var_rustc_linker_host_target() { // no crate type set => linker never called => build succeeds if and // only if build.rs succeeds, despite linker binary not existing. - p.cargo("build --target").arg(&target).run(); + if cargo_test_support::is_nightly() { + p.cargo("build -Z target-applies-to-host --target") + .arg(&target) + .masquerade_as_nightly_cargo() + .run(); + } } #[cargo_test] @@ -266,10 +271,13 @@ fn custom_build_env_var_rustc_linker_host_target_env() { // no crate type set => linker never called => build succeeds if and // only if build.rs succeeds, despite linker binary not existing. - p.cargo("build --target") - .env("CARGO_TARGET_APPLIES_TO_HOST", "false") - .arg(&target) - .run(); + if cargo_test_support::is_nightly() { + p.cargo("build -Z target-applies-to-host --target") + .env("CARGO_TARGET_APPLIES_TO_HOST", "false") + .arg(&target) + .masquerade_as_nightly_cargo() + .run(); + } } #[cargo_test] @@ -304,7 +312,7 @@ fn custom_build_env_var_rustc_linker_host_target_with_bad_host_config() { // build.rs should fail due to bad target linker being set if cargo_test_support::is_nightly() { - p.cargo("build -Z host-config --verbose --target") + p.cargo("build -Z target-applies-to-host -Z host-config --verbose --target") .arg(&target) .masquerade_as_nightly_cargo() .with_status(101) @@ -350,7 +358,7 @@ fn custom_build_env_var_rustc_linker_bad_host() { // build.rs should fail due to bad host linker being set if cargo_test_support::is_nightly() { - p.cargo("build -Z host-config --verbose --target") + p.cargo("build -Z target-applies-to-host -Z host-config --verbose --target") .arg(&target) .masquerade_as_nightly_cargo() .with_status(101) @@ -398,7 +406,7 @@ fn custom_build_env_var_rustc_linker_bad_host_with_arch() { // build.rs should fail due to bad host linker being set if cargo_test_support::is_nightly() { - p.cargo("build -Z host-config --verbose --target") + p.cargo("build -Z target-applies-to-host -Z host-config --verbose --target") .arg(&target) .masquerade_as_nightly_cargo() .with_status(101) @@ -445,7 +453,7 @@ fn custom_build_env_var_rustc_linker_cross_arch_host() { // build.rs should fail due to bad host linker being set if cargo_test_support::is_nightly() { - p.cargo("build -Z host-config --verbose --target") + p.cargo("build -Z target-applies-to-host -Z host-config --verbose --target") .arg(&target) .masquerade_as_nightly_cargo() .run(); @@ -486,7 +494,7 @@ fn custom_build_env_var_rustc_linker_bad_cross_arch_host() { // build.rs should fail due to bad host linker being set if cargo_test_support::is_nightly() { - p.cargo("build -Z host-config --verbose --target") + p.cargo("build -Z target-applies-to-host -Z host-config --verbose --target") .arg(&target) .masquerade_as_nightly_cargo() .with_status(101)