From 083cc9e8d0cc369658963399fe517492ca8fafb7 Mon Sep 17 00:00:00 2001 From: James Hilliard Date: Thu, 1 Apr 2021 21:06:31 -0600 Subject: [PATCH 01/12] Configure hosts separately from targets when --target is specified. This prevents target configs from accidentally being picked up when cross compiling from hosts that have the same architecture as their targets. closes #3349 --- .../compiler/build_context/target_info.rs | 20 +- src/cargo/util/config/mod.rs | 5 + src/cargo/util/config/target.rs | 31 ++- tests/testsuite/build_script.rs | 240 ++++++++++++++++++ 4 files changed, 292 insertions(+), 4 deletions(-) diff --git a/src/cargo/core/compiler/build_context/target_info.rs b/src/cargo/core/compiler/build_context/target_info.rs index 325d4168bc3..0692e0c1ca6 100644 --- a/src/cargo/core/compiler/build_context/target_info.rs +++ b/src/cargo/core/compiler/build_context/target_info.rs @@ -682,7 +682,6 @@ impl<'cfg> RustcTargetData<'cfg> { ) -> CargoResult> { let config = ws.config(); let rustc = config.load_global_rustc(Some(ws))?; - let host_config = config.target_cfg_triple(&rustc.host)?; let host_info = TargetInfo::new(config, requested_kinds, &rustc, CompileKind::Host)?; let mut target_config = HashMap::new(); let mut target_info = HashMap::new(); @@ -692,10 +691,25 @@ 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. - if requested_kinds.iter().any(CompileKind::is_host) { + let host_config = if requested_kinds.iter().any(CompileKind::is_host) { let ct = CompileTarget::new(&rustc.host)?; target_info.insert(ct, host_info.clone()); - target_config.insert(ct, host_config.clone()); + let target_host_config = config.target_cfg_triple(&rustc.host)?; + target_config.insert(ct, target_host_config.clone()); + target_host_config + } else { + config.host_cfg_triple(&rustc.host)? + }; + + for kind in requested_kinds { + if let CompileKind::Target(target) = *kind { + let tcfg = config.target_cfg_triple(target.short_name())?; + target_config.insert(target, tcfg); + target_info.insert( + target, + TargetInfo::new(config, requested_kinds, &rustc, *kind)?, + ); + } } let mut res = RustcTargetData { diff --git a/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index b9921dfee26..c5b9d7c62da 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -1486,6 +1486,11 @@ impl Config { .try_borrow_with(|| self.get::("doc.extern-map")) } + /// Returns the `[host]` table definition for the given target triple. + pub fn host_cfg_triple(&self, target: &str) -> CargoResult { + target::load_host_triple(self, target) + } + /// Returns the `[target]` table definition for the given target triple. pub fn target_cfg_triple(&self, target: &str) -> CargoResult { target::load_target_triple(self, target) diff --git a/src/cargo/util/config/target.rs b/src/cargo/util/config/target.rs index e22cab92ee5..11f6085c7d5 100644 --- a/src/cargo/util/config/target.rs +++ b/src/cargo/util/config/target.rs @@ -19,7 +19,7 @@ pub struct TargetCfgConfig { pub other: BTreeMap, } -/// Config definition of a `[target]` table. +/// Config definition of a `[target]` table or `[host]`. #[derive(Debug, Clone)] pub struct TargetConfig { /// Process to run as a wrapper for `cargo run`, `test`, and `bench` commands. @@ -64,6 +64,35 @@ pub(super) fn load_target_cfgs(config: &Config) -> CargoResult CargoResult { + // This needs to get each field individually because it cannot fetch the + // struct all at once due to `links_overrides`. Can't use `serde(flatten)` + // because it causes serde to use `deserialize_map` which means the config + // deserializer does not know which keys to deserialize, which means + // environment variables would not work. + let host_triple_key = ConfigKey::from_str(&format!("host.{}", triple)); + let host_prefix = match config.get_cv(&host_triple_key)? { + Some(_) => format!("host.{}", triple), + None => "host".to_string(), + }; + let runner: OptValue = config.get(&format!("{}.runner", host_prefix))?; + let rustflags: OptValue = config.get(&format!("{}.rustflags", host_prefix))?; + let linker: OptValue = config.get(&format!("{}.linker", host_prefix))?; + // Links do not support environment variables. + let target_key = ConfigKey::from_str(&host_prefix); + let links_overrides = match config.get_table(&target_key)? { + Some(links) => parse_links_overrides(&target_key, links.val, config)?, + None => BTreeMap::new(), + }; + Ok(TargetConfig { + runner, + rustflags, + linker, + links_overrides, + }) +} + /// Loads a single `[target]` table for the given triple. pub(super) fn load_target_triple(config: &Config, triple: &str) -> CargoResult { // This needs to get each field individually because it cannot fetch the diff --git a/tests/testsuite/build_script.rs b/tests/testsuite/build_script.rs index a43856548b0..d3126e1cad5 100644 --- a/tests/testsuite/build_script.rs +++ b/tests/testsuite/build_script.rs @@ -164,6 +164,246 @@ fn custom_build_env_var_rustc_linker() { p.cargo("build --target").arg(&target).run(); } +#[cargo_test] +fn custom_build_env_var_rustc_linker_bad_host_target() { + let target = rustc_host(); + let p = project() + .file( + ".cargo/config", + &format!( + r#" + [target.{}] + linker = "/path/to/linker" + "#, + target + ), + ) + .file( + "build.rs", + r#" + use std::env; + + fn main() { + assert!(env::var("RUSTC_LINKER").unwrap().ends_with("/path/to/linker")); + } + "#, + ) + .file("src/lib.rs", "") + .build(); + + // build.rs should fail since host == target when no target is set + p.cargo("build --verbose") + .with_status(101) + .with_stderr_contains( + "\ +[COMPILING] foo v0.0.1 ([CWD]) +[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin [..]-C linker=[..]/path/to/linker [..]` +[ERROR] linker `[..]/path/to/linker` not found +" + ) + .run(); +} + +#[cargo_test] +fn custom_build_env_var_rustc_linker_host_target() { + let target = rustc_host(); + let p = project() + .file( + ".cargo/config", + &format!( + r#" + [target.{}] + linker = "/path/to/linker" + "#, + target + ), + ) + .file( + "build.rs", + r#" + use std::env; + + fn main() { + assert!(env::var("RUSTC_LINKER").unwrap().ends_with("/path/to/linker")); + } + "#, + ) + .file("src/lib.rs", "") + .build(); + + // 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(); +} + +#[cargo_test] +fn custom_build_env_var_rustc_linker_bad_host() { + let target = rustc_host(); + let p = project() + .file( + ".cargo/config", + &format!( + r#" + [host] + linker = "/path/to/host/linker" + [target.{}] + linker = "/path/to/target/linker" + "#, + target + ), + ) + .file( + "build.rs", + r#" + use std::env; + + fn main() { + assert!(env::var("RUSTC_LINKER").unwrap().ends_with("/path/to/target/linker")); + } + "#, + ) + .file("src/lib.rs", "") + .build(); + + // build.rs should fail due to bad host linker being set + p.cargo("build --verbose --target") + .arg(&target) + .with_status(101) + .with_stderr_contains( + "\ +[COMPILING] foo v0.0.1 ([CWD]) +[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin [..]-C linker=[..]/path/to/host/linker [..]` +[ERROR] linker `[..]/path/to/host/linker` not found +" + ) + .run(); +} + +#[cargo_test] +fn custom_build_env_var_rustc_linker_bad_host_with_arch() { + let target = rustc_host(); + let p = project() + .file( + ".cargo/config", + &format!( + r#" + [host] + linker = "/path/to/host/linker" + [host.{}] + linker = "/path/to/host/arch/linker" + [target.{}] + linker = "/path/to/target/linker" + "#, + target, target + ), + ) + .file( + "build.rs", + r#" + use std::env; + + fn main() { + assert!(env::var("RUSTC_LINKER").unwrap().ends_with("/path/to/target/linker")); + } + "#, + ) + .file("src/lib.rs", "") + .build(); + + // build.rs should fail due to bad host linker being set + p.cargo("build --verbose --target") + .arg(&target) + .with_status(101) + .with_stderr_contains( + "\ +[COMPILING] foo v0.0.1 ([CWD]) +[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin [..]-C linker=[..]/path/to/host/arch/linker [..]` +[ERROR] linker `[..]/path/to/host/arch/linker` not found +" + ) + .run(); +} + +#[cargo_test] +fn custom_build_env_var_rustc_linker_cross_arch_host() { + let target = rustc_host(); + let cross_target = cross_compile::alternate(); + let p = project() + .file( + ".cargo/config", + &format!( + r#" + [host.{}] + linker = "/path/to/host/arch/linker" + [target.{}] + linker = "/path/to/target/linker" + "#, + cross_target, target + ), + ) + .file( + "build.rs", + r#" + use std::env; + + fn main() { + assert!(env::var("RUSTC_LINKER").unwrap().ends_with("/path/to/target/linker")); + } + "#, + ) + .file("src/lib.rs", "") + .build(); + + // build.rs should fail due to bad host linker being set + p.cargo("build --verbose --target").arg(&target).run(); +} + +#[cargo_test] +fn custom_build_env_var_rustc_linker_bad_cross_arch_host() { + let target = rustc_host(); + let cross_target = cross_compile::alternate(); + let p = project() + .file( + ".cargo/config", + &format!( + r#" + [host] + linker = "/path/to/host/linker" + [host.{}] + linker = "/path/to/host/arch/linker" + [target.{}] + linker = "/path/to/target/linker" + "#, + cross_target, target + ), + ) + .file( + "build.rs", + r#" + use std::env; + + fn main() { + assert!(env::var("RUSTC_LINKER").unwrap().ends_with("/path/to/target/linker")); + } + "#, + ) + .file("src/lib.rs", "") + .build(); + + // build.rs should fail due to bad host linker being set + p.cargo("build --verbose --target") + .arg(&target) + .with_status(101) + .with_stderr_contains( + "\ +[COMPILING] foo v0.0.1 ([CWD]) +[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin [..]-C linker=[..]/path/to/host/linker [..]` +[ERROR] linker `[..]/path/to/host/linker` not found +" + ) + .run(); +} + #[cargo_test] fn custom_build_script_wrong_rustc_flags() { let p = project() From e51d1511a661a3e15bff8b152c515ca514bf708b Mon Sep 17 00:00:00 2001 From: James Hilliard Date: Tue, 20 Apr 2021 09:44:02 -0600 Subject: [PATCH 02/12] Refactor host and target config table loads to deduplicate logic. --- src/cargo/util/config/target.rs | 34 ++++++++++----------------------- 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/src/cargo/util/config/target.rs b/src/cargo/util/config/target.rs index 11f6085c7d5..59bf7c1866a 100644 --- a/src/cargo/util/config/target.rs +++ b/src/cargo/util/config/target.rs @@ -66,45 +66,31 @@ pub(super) fn load_target_cfgs(config: &Config) -> CargoResult CargoResult { - // This needs to get each field individually because it cannot fetch the - // struct all at once due to `links_overrides`. Can't use `serde(flatten)` - // because it causes serde to use `deserialize_map` which means the config - // deserializer does not know which keys to deserialize, which means - // environment variables would not work. let host_triple_key = ConfigKey::from_str(&format!("host.{}", triple)); let host_prefix = match config.get_cv(&host_triple_key)? { Some(_) => format!("host.{}", triple), None => "host".to_string(), }; - let runner: OptValue = config.get(&format!("{}.runner", host_prefix))?; - let rustflags: OptValue = config.get(&format!("{}.rustflags", host_prefix))?; - let linker: OptValue = config.get(&format!("{}.linker", host_prefix))?; - // Links do not support environment variables. - let target_key = ConfigKey::from_str(&host_prefix); - let links_overrides = match config.get_table(&target_key)? { - Some(links) => parse_links_overrides(&target_key, links.val, config)?, - None => BTreeMap::new(), - }; - Ok(TargetConfig { - runner, - rustflags, - linker, - links_overrides, - }) + load_config_table(config, &host_prefix) } /// Loads a single `[target]` table for the given triple. pub(super) fn load_target_triple(config: &Config, triple: &str) -> CargoResult { + load_config_table(config, &format!("target.{}", triple)) +} + +/// Loads a single table for the given prefix. +fn load_config_table(config: &Config, prefix: &str) -> CargoResult { // This needs to get each field individually because it cannot fetch the // struct all at once due to `links_overrides`. Can't use `serde(flatten)` // because it causes serde to use `deserialize_map` which means the config // deserializer does not know which keys to deserialize, which means // environment variables would not work. - let runner: OptValue = config.get(&format!("target.{}.runner", triple))?; - let rustflags: OptValue = config.get(&format!("target.{}.rustflags", triple))?; - let linker: OptValue = config.get(&format!("target.{}.linker", triple))?; + let runner: OptValue = config.get(&format!("{}.runner", prefix))?; + let rustflags: OptValue = config.get(&format!("{}.rustflags", prefix))?; + let linker: OptValue = config.get(&format!("{}.linker", prefix))?; // Links do not support environment variables. - let target_key = ConfigKey::from_str(&format!("target.{}", triple)); + let target_key = ConfigKey::from_str(prefix); let links_overrides = match config.get_table(&target_key)? { Some(links) => parse_links_overrides(&target_key, links.val, config)?, None => BTreeMap::new(), From 0a603fdf6eddf7ed21874fc846af14ac7120d4e9 Mon Sep 17 00:00:00 2001 From: James Hilliard Date: Wed, 21 Apr 2021 00:18:30 -0600 Subject: [PATCH 03/12] Document and feature-gate host-config tables. --- src/cargo/core/features.rs | 2 ++ src/cargo/util/config/target.rs | 21 +++++++---- src/doc/src/reference/unstable.md | 33 ++++++++++++++++++ tests/testsuite/build_script.rs | 58 +++++++++++++++++++------------ 4 files changed, 86 insertions(+), 28 deletions(-) diff --git a/src/cargo/core/features.rs b/src/cargo/core/features.rs index 56321cdde80..b5298f19e6c 100644 --- a/src/cargo/core/features.rs +++ b/src/cargo/core/features.rs @@ -600,6 +600,7 @@ unstable_cli_options!( namespaced_features: bool = ("Allow features with `dep:` prefix"), 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"), 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), @@ -787,6 +788,7 @@ impl CliUnstable { "panic-abort-tests" => self.panic_abort_tests = parse_empty(k, v)?, "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)?, "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/target.rs b/src/cargo/util/config/target.rs index 59bf7c1866a..1af36c12ac1 100644 --- a/src/cargo/util/config/target.rs +++ b/src/cargo/util/config/target.rs @@ -66,12 +66,21 @@ pub(super) fn load_target_cfgs(config: &Config) -> CargoResult CargoResult { - let host_triple_key = ConfigKey::from_str(&format!("host.{}", triple)); - let host_prefix = match config.get_cv(&host_triple_key)? { - Some(_) => format!("host.{}", triple), - None => "host".to_string(), - }; - load_config_table(config, &host_prefix) + if config.cli_unstable().host_config { + let host_triple_key = ConfigKey::from_str(&format!("host.{}", triple)); + let host_prefix = match config.get_cv(&host_triple_key)? { + Some(_) => format!("host.{}", triple), + None => "host".to_string(), + }; + load_config_table(config, &host_prefix) + } else { + Ok(TargetConfig { + runner: None, + rustflags: None, + linker: None, + links_overrides: BTreeMap::new(), + }) + } } /// Loads a single `[target]` table for the given triple. diff --git a/src/doc/src/reference/unstable.md b/src/doc/src/reference/unstable.md index 8e7927f8308..f5b671d4f7d 100644 --- a/src/doc/src/reference/unstable.md +++ b/src/doc/src/reference/unstable.md @@ -641,6 +641,39 @@ cargo +nightly -Zunstable-options -Zconfig-include --config somefile.toml build CLI paths are relative to the current working directory. +### host-config +* Original Pull Request: [#9322](https://github.com/rust-lang/cargo/pull/9322) +* Tracking Issue: [#3349](https://github.com/rust-lang/cargo/issues/3349) + +The `host` key in a config file can be used pass flags to host build targets +such as build scripts that must run on the host system instead of the target +system when cross compiling. It supports both generic and host arch specific +tables. Matching host arch tables take precedence over generic host tables. + +It requires the `-Zhost-config` command-line option. + +```toml +# .cargo/config +cargo-features = ["host-config"] + +[host] +linker = "/path/to/host/linker" +[host.x86_64-unknown-linux-gnu] +linker = "/path/to/host/arch/linker" +[target.x86_64-unknown-linux-gnu] +linker = "/path/to/target/linker" +``` + +The generic `host` table above will be entirely ignored when building on a +`x86_64-unknown-linux-gnu` host as the `host.x86_64-unknown-linux-gnu` table +takes precedence. + +This feature requires a `--target` to be specified. + +```console +cargo +nightly -Zunstable-options -Zhost-config --config somefile.toml build --target x86_64-unknown-linux-gnu +``` + ### unit-graph * Tracking Issue: [#8002](https://github.com/rust-lang/cargo/issues/8002) diff --git a/tests/testsuite/build_script.rs b/tests/testsuite/build_script.rs index d3126e1cad5..d383004f084 100644 --- a/tests/testsuite/build_script.rs +++ b/tests/testsuite/build_script.rs @@ -266,17 +266,20 @@ fn custom_build_env_var_rustc_linker_bad_host() { .build(); // build.rs should fail due to bad host linker being set - p.cargo("build --verbose --target") - .arg(&target) - .with_status(101) - .with_stderr_contains( - "\ + if cargo_test_support::is_nightly() { + p.cargo("build -Z host-config --verbose --target") + .arg(&target) + .masquerade_as_nightly_cargo() + .with_status(101) + .with_stderr_contains( + "\ [COMPILING] foo v0.0.1 ([CWD]) [RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin [..]-C linker=[..]/path/to/host/linker [..]` [ERROR] linker `[..]/path/to/host/linker` not found " - ) - .run(); + ) + .run(); + } } #[cargo_test] @@ -311,17 +314,20 @@ fn custom_build_env_var_rustc_linker_bad_host_with_arch() { .build(); // build.rs should fail due to bad host linker being set - p.cargo("build --verbose --target") - .arg(&target) - .with_status(101) - .with_stderr_contains( - "\ + if cargo_test_support::is_nightly() { + p.cargo("build -Z host-config --verbose --target") + .arg(&target) + .masquerade_as_nightly_cargo() + .with_status(101) + .with_stderr_contains( + "\ [COMPILING] foo v0.0.1 ([CWD]) [RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin [..]-C linker=[..]/path/to/host/arch/linker [..]` [ERROR] linker `[..]/path/to/host/arch/linker` not found " - ) - .run(); + ) + .run(); + } } #[cargo_test] @@ -355,7 +361,12 @@ fn custom_build_env_var_rustc_linker_cross_arch_host() { .build(); // build.rs should fail due to bad host linker being set - p.cargo("build --verbose --target").arg(&target).run(); + if cargo_test_support::is_nightly() { + p.cargo("build -Z host-config --verbose --target") + .arg(&target) + .masquerade_as_nightly_cargo() + .run(); + } } #[cargo_test] @@ -391,17 +402,20 @@ fn custom_build_env_var_rustc_linker_bad_cross_arch_host() { .build(); // build.rs should fail due to bad host linker being set - p.cargo("build --verbose --target") - .arg(&target) - .with_status(101) - .with_stderr_contains( - "\ + if cargo_test_support::is_nightly() { + p.cargo("build -Z host-config --verbose --target") + .arg(&target) + .masquerade_as_nightly_cargo() + .with_status(101) + .with_stderr_contains( + "\ [COMPILING] foo v0.0.1 ([CWD]) [RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin [..]-C linker=[..]/path/to/host/linker [..]` [ERROR] linker `[..]/path/to/host/linker` not found " - ) - .run(); + ) + .run(); + } } #[cargo_test] From 46f9541740201ca3b095de016f7a4226ce24e717 Mon Sep 17 00:00:00 2001 From: James Hilliard Date: Fri, 23 Apr 2021 07:23:13 -0600 Subject: [PATCH 04/12] Add target-applies-to-host and default to true unless host-config is enabled. --- .../compiler/build_context/target_info.rs | 16 +++- src/cargo/util/config/mod.rs | 5 ++ src/cargo/util/config/target.rs | 14 ++++ tests/testsuite/build_script.rs | 83 +++++++++++++++++++ 4 files changed, 115 insertions(+), 3 deletions(-) diff --git a/src/cargo/core/compiler/build_context/target_info.rs b/src/cargo/core/compiler/build_context/target_info.rs index 0692e0c1ca6..e663745b26b 100644 --- a/src/cargo/core/compiler/build_context/target_info.rs +++ b/src/cargo/core/compiler/build_context/target_info.rs @@ -694,11 +694,21 @@ impl<'cfg> RustcTargetData<'cfg> { 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 = config.target_cfg_triple(&rustc.host)?; - target_config.insert(ct, target_host_config.clone()); + let target_host_config = if config.target_applies_to_host() { + let target_cfg_clone = config.target_cfg_triple(&rustc.host)?; + target_config.insert(ct, target_cfg_clone.clone()); + target_cfg_clone + } else { + target_config.insert(ct, config.target_cfg_triple(&rustc.host)?); + config.host_cfg_triple(&rustc.host)? + }; target_host_config } else { - config.host_cfg_triple(&rustc.host)? + if config.target_applies_to_host() { + config.target_cfg_triple(&rustc.host)? + } else { + config.host_cfg_triple(&rustc.host)? + } }; for kind in requested_kinds { diff --git a/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index c5b9d7c62da..9b1284fedde 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -1486,6 +1486,11 @@ impl Config { .try_borrow_with(|| self.get::("doc.extern-map")) } + /// Returns true if the `[target]` table should be applied to host targets. + pub fn target_applies_to_host(&self) -> bool { + target::get_target_applies_to_host(self) + } + /// Returns the `[host]` table definition for the given target triple. pub fn host_cfg_triple(&self, target: &str) -> CargoResult { target::load_host_triple(self, target) diff --git a/src/cargo/util/config/target.rs b/src/cargo/util/config/target.rs index 1af36c12ac1..0f9ea32af72 100644 --- a/src/cargo/util/config/target.rs +++ b/src/cargo/util/config/target.rs @@ -64,6 +64,20 @@ 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() + } else { + if config.cli_unstable().host_config { + false + } else { + true + } + } +} + /// Loads a single `[host]` table for the given triple. pub(super) fn load_host_triple(config: &Config, triple: &str) -> CargoResult { if config.cli_unstable().host_config { diff --git a/tests/testsuite/build_script.rs b/tests/testsuite/build_script.rs index d383004f084..7bc1d71598f 100644 --- a/tests/testsuite/build_script.rs +++ b/tests/testsuite/build_script.rs @@ -212,6 +212,7 @@ fn custom_build_env_var_rustc_linker_host_target() { ".cargo/config", &format!( r#" + target-applies-to-host = false [target.{}] linker = "/path/to/linker" "#, @@ -236,6 +237,88 @@ fn custom_build_env_var_rustc_linker_host_target() { p.cargo("build --target").arg(&target).run(); } +#[cargo_test] +fn custom_build_env_var_rustc_linker_host_target_env() { + let target = rustc_host(); + let p = project() + .file( + ".cargo/config", + &format!( + r#" + [target.{}] + linker = "/path/to/linker" + "#, + target + ), + ) + .file( + "build.rs", + r#" + use std::env; + + fn main() { + assert!(env::var("RUSTC_LINKER").unwrap().ends_with("/path/to/linker")); + } + "#, + ) + .file("src/lib.rs", "") + .build(); + + // 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(); +} + +#[cargo_test] +fn custom_build_env_var_rustc_linker_host_target_with_bad_host_config() { + let target = rustc_host(); + let p = project() + .file( + ".cargo/config", + &format!( + r#" + target-applies-to-host = true + [host] + linker = "/path/to/host/linker" + [target.{}] + linker = "/path/to/target/linker" + "#, + target + ), + ) + .file( + "build.rs", + r#" + use std::env; + + fn main() { + assert!(env::var("RUSTC_LINKER").unwrap().ends_with("/path/to/target/linker")); + } + "#, + ) + .file("src/lib.rs", "") + .build(); + + // 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") + .arg(&target) + .masquerade_as_nightly_cargo() + .with_status(101) + .with_stderr_contains( + "\ +[COMPILING] foo v0.0.1 ([CWD]) +[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin [..]-C linker=[..]/path/to/target/linker [..]` +[ERROR] linker `[..]/path/to/target/linker` not found +" + ) + .run(); + } +} + #[cargo_test] fn custom_build_env_var_rustc_linker_bad_host() { let target = rustc_host(); From e24bf92219b2296121c9306fde3e9f9436d01d61 Mon Sep 17 00:00:00 2001 From: James Hilliard Date: Mon, 26 Apr 2021 21:03:01 -0600 Subject: [PATCH 05/12] 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) From e79752629afe7200edf7537c4fe16bfb2c27939b Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Thu, 29 Apr 2021 19:28:07 -0700 Subject: [PATCH 06/12] Code cleanups for the split-host work --- .../core/compiler/build_context/target_info.rs | 9 +++------ src/cargo/util/config/target.rs | 16 ++++++---------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/cargo/core/compiler/build_context/target_info.rs b/src/cargo/core/compiler/build_context/target_info.rs index 08b6098cc4d..2413cb48f36 100644 --- a/src/cargo/core/compiler/build_context/target_info.rs +++ b/src/cargo/core/compiler/build_context/target_info.rs @@ -691,14 +691,11 @@ 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 target_applies_to_host = config.target_applies_to_host()?; 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 target_applies_to_host.unwrap() { + let target_host_config = if target_applies_to_host { let target_cfg_clone = config.target_cfg_triple(&rustc.host)?; target_config.insert(ct, target_cfg_clone.clone()); target_cfg_clone @@ -708,7 +705,7 @@ impl<'cfg> RustcTargetData<'cfg> { }; target_host_config } else { - if target_applies_to_host.unwrap() { + if target_applies_to_host { config.target_cfg_triple(&rustc.host)? } else { config.host_cfg_triple(&rustc.host)? diff --git a/src/cargo/util/config/target.rs b/src/cargo/util/config/target.rs index 8fa7b6cc97f..4fe59eaa417 100644 --- a/src/cargo/util/config/target.rs +++ b/src/cargo/util/config/target.rs @@ -67,15 +67,10 @@ pub(super) fn load_target_cfgs(config: &Config) -> CargoResult 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()) + if let Ok(target_applies_to_host) = config.get::("target-applies-to-host") { + Ok(target_applies_to_host) } else { - if config.cli_unstable().host_config { - Ok(false) - } else { - Ok(true) - } + Ok(!config.cli_unstable().host_config) } } else { if config.cli_unstable().host_config { @@ -91,9 +86,10 @@ pub(super) fn get_target_applies_to_host(config: &Config) -> CargoResult { /// Loads a single `[host]` table for the given triple. pub(super) fn load_host_triple(config: &Config, triple: &str) -> CargoResult { if config.cli_unstable().host_config { - let host_triple_key = ConfigKey::from_str(&format!("host.{}", triple)); + let host_triple_prefix = format!("host.{}", triple); + let host_triple_key = ConfigKey::from_str(&host_triple_prefix); let host_prefix = match config.get_cv(&host_triple_key)? { - Some(_) => format!("host.{}", triple), + Some(_) => host_triple_prefix, None => "host".to_string(), }; load_config_table(config, &host_prefix) From 62e2fd433f38c512e62f9433b8f00ad3892b9471 Mon Sep 17 00:00:00 2001 From: James Hilliard Date: Sat, 1 May 2021 11:29:33 -0600 Subject: [PATCH 07/12] Fix #9030 rebase. --- src/cargo/core/compiler/build_context/target_info.rs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/cargo/core/compiler/build_context/target_info.rs b/src/cargo/core/compiler/build_context/target_info.rs index 2413cb48f36..6dbcf19a1af 100644 --- a/src/cargo/core/compiler/build_context/target_info.rs +++ b/src/cargo/core/compiler/build_context/target_info.rs @@ -712,17 +712,6 @@ impl<'cfg> RustcTargetData<'cfg> { } }; - for kind in requested_kinds { - if let CompileKind::Target(target) = *kind { - let tcfg = config.target_cfg_triple(target.short_name())?; - target_config.insert(target, tcfg); - target_info.insert( - target, - TargetInfo::new(config, requested_kinds, &rustc, *kind)?, - ); - } - } - let mut res = RustcTargetData { rustc, config, From 6dcfe51bdb88363befb1a7b31cbaf2182ee81bc4 Mon Sep 17 00:00:00 2001 From: James Hilliard Date: Sat, 1 May 2021 12:25:57 -0600 Subject: [PATCH 08/12] Test that setting -Zhost-config without -Ztarget-applies-to-host fails. --- tests/testsuite/build_script.rs | 42 +++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/testsuite/build_script.rs b/tests/testsuite/build_script.rs index 48112e56568..428b7b9e9c2 100644 --- a/tests/testsuite/build_script.rs +++ b/tests/testsuite/build_script.rs @@ -280,6 +280,48 @@ fn custom_build_env_var_rustc_linker_host_target_env() { } } +#[cargo_test] +fn custom_build_invalid_host_config_feature_flag() { + let target = rustc_host(); + let p = project() + .file( + ".cargo/config", + &format!( + r#" + [target.{}] + linker = "/path/to/linker" + "#, + target + ), + ) + .file( + "build.rs", + r#" + use std::env; + + fn main() { + assert!(env::var("RUSTC_LINKER").unwrap().ends_with("/path/to/linker")); + } + "#, + ) + .file("src/lib.rs", "") + .build(); + + // build.rs should fail due to -Zhost-config being set without -Ztarget-applies-to-host + if cargo_test_support::is_nightly() { + p.cargo("build -Z host-config --target") + .arg(&target) + .masquerade_as_nightly_cargo() + .with_status(101) + .with_stderr_contains( + "\ +error: the -Zhost-config flag requires the -Ztarget-applies-to-host flag to be set +", + ) + .run(); + } +} + #[cargo_test] fn custom_build_env_var_rustc_linker_host_target_with_bad_host_config() { let target = rustc_host(); From 222f6ea2e4ce0643efbc5fb63322082c0ee78e24 Mon Sep 17 00:00:00 2001 From: James Hilliard Date: Sat, 1 May 2021 13:00:42 -0600 Subject: [PATCH 09/12] Move host_info outside of requested_kinds is_host if statement. --- .../compiler/build_context/target_info.rs | 25 ++++++++----------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/cargo/core/compiler/build_context/target_info.rs b/src/cargo/core/compiler/build_context/target_info.rs index 6dbcf19a1af..1acf3982f66 100644 --- a/src/cargo/core/compiler/build_context/target_info.rs +++ b/src/cargo/core/compiler/build_context/target_info.rs @@ -682,34 +682,29 @@ impl<'cfg> RustcTargetData<'cfg> { ) -> CargoResult> { let config = ws.config(); let rustc = config.load_global_rustc(Some(ws))?; - let host_info = TargetInfo::new(config, requested_kinds, &rustc, CompileKind::Host)?; let mut target_config = HashMap::new(); let mut target_info = HashMap::new(); + let target_applies_to_host = config.target_applies_to_host()?; + let host_info = TargetInfo::new(config, requested_kinds, &rustc, CompileKind::Host)?; + let host_config = if target_applies_to_host { + config.target_cfg_triple(&rustc.host)? + } else { + config.host_cfg_triple(&rustc.host)? + }; // This is a hack. The unit_dependency graph builder "pretends" that // `CompileKind::Host` is `CompileKind::Target(host)` if the // `--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()?; - let host_config = if requested_kinds.iter().any(CompileKind::is_host) { + 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 target_applies_to_host { - let target_cfg_clone = config.target_cfg_triple(&rustc.host)?; - target_config.insert(ct, target_cfg_clone.clone()); - target_cfg_clone + if target_applies_to_host { + target_config.insert(ct, host_config.clone()); } else { target_config.insert(ct, config.target_cfg_triple(&rustc.host)?); - config.host_cfg_triple(&rustc.host)? }; - target_host_config - } else { - if target_applies_to_host { - config.target_cfg_triple(&rustc.host)? - } else { - config.host_cfg_triple(&rustc.host)? - } }; let mut res = RustcTargetData { From 4994b5f13773bb8048ffd4145763c388774ca3ad Mon Sep 17 00:00:00 2001 From: James Hilliard Date: Sat, 1 May 2021 15:24:10 -0600 Subject: [PATCH 10/12] Document target-applies-to-host and fixup host-config docs. --- src/doc/src/reference/unstable.md | 33 +++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/doc/src/reference/unstable.md b/src/doc/src/reference/unstable.md index f5b671d4f7d..9e38b8b3725 100644 --- a/src/doc/src/reference/unstable.md +++ b/src/doc/src/reference/unstable.md @@ -641,6 +641,27 @@ cargo +nightly -Zunstable-options -Zconfig-include --config somefile.toml build CLI paths are relative to the current working directory. +### target-applies-to-host +* Original Pull Request: [#9322](https://github.com/rust-lang/cargo/pull/9322) + +The `target-applies-to-host` key in a config file can be used set the desired +behavior for passing target config flags to build scripts. + +It requires the `-Ztarget-applies-to-host` command-line option. + +The current default for `target-applies-to-host` is `true`, which will be +changed to `false` in the future, if `-Zhost-config` is used the new `false` +default will be set for `target-applies-to-host`. + +```toml +# config.toml +target-applies-to-host = false +``` + +```console +cargo +nightly -Ztarget-applies-to-host build --target x86_64-unknown-linux-gnu +``` + ### host-config * Original Pull Request: [#9322](https://github.com/rust-lang/cargo/pull/9322) * Tracking Issue: [#3349](https://github.com/rust-lang/cargo/issues/3349) @@ -650,12 +671,11 @@ such as build scripts that must run on the host system instead of the target system when cross compiling. It supports both generic and host arch specific tables. Matching host arch tables take precedence over generic host tables. -It requires the `-Zhost-config` command-line option. +It requires the `-Zhost-config` and `-Ztarget-applies-to-host` command-line +options to be set. ```toml -# .cargo/config -cargo-features = ["host-config"] - +# config.toml [host] linker = "/path/to/host/linker" [host.x86_64-unknown-linux-gnu] @@ -668,10 +688,11 @@ The generic `host` table above will be entirely ignored when building on a `x86_64-unknown-linux-gnu` host as the `host.x86_64-unknown-linux-gnu` table takes precedence. -This feature requires a `--target` to be specified. +Setting `-Zhost-config` changes the default for `target-applies-to-host` to +`false` from `true`. ```console -cargo +nightly -Zunstable-options -Zhost-config --config somefile.toml build --target x86_64-unknown-linux-gnu +cargo +nightly -Ztarget-applies-to-host -Zhost-config build --target x86_64-unknown-linux-gnu ``` ### unit-graph From b8be3af7a4c654d20c502d27c9d981714761b176 Mon Sep 17 00:00:00 2001 From: James Hilliard Date: Mon, 3 May 2021 13:10:30 -0600 Subject: [PATCH 11/12] Add dedicated tracking issues for host-config/target-applies-to-host features. --- src/doc/src/reference/unstable.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/doc/src/reference/unstable.md b/src/doc/src/reference/unstable.md index 9e38b8b3725..007c482c830 100644 --- a/src/doc/src/reference/unstable.md +++ b/src/doc/src/reference/unstable.md @@ -643,6 +643,7 @@ CLI paths are relative to the current working directory. ### target-applies-to-host * Original Pull Request: [#9322](https://github.com/rust-lang/cargo/pull/9322) +* Tracking Issue: [#9453](https://github.com/rust-lang/cargo/issues/9453) The `target-applies-to-host` key in a config file can be used set the desired behavior for passing target config flags to build scripts. @@ -664,7 +665,7 @@ cargo +nightly -Ztarget-applies-to-host build --target x86_64-unknown-linux-gnu ### host-config * Original Pull Request: [#9322](https://github.com/rust-lang/cargo/pull/9322) -* Tracking Issue: [#3349](https://github.com/rust-lang/cargo/issues/3349) +* Tracking Issue: [#9452](https://github.com/rust-lang/cargo/issues/9452) The `host` key in a config file can be used pass flags to host build targets such as build scripts that must run on the host system instead of the target From 5338bb2af1365a8f50f67768754034fecfbbfc4f Mon Sep 17 00:00:00 2001 From: James Hilliard Date: Fri, 7 May 2021 08:50:31 -0600 Subject: [PATCH 12/12] Simplify target_config insert. --- src/cargo/core/compiler/build_context/target_info.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/cargo/core/compiler/build_context/target_info.rs b/src/cargo/core/compiler/build_context/target_info.rs index 1acf3982f66..8aff294bb7a 100644 --- a/src/cargo/core/compiler/build_context/target_info.rs +++ b/src/cargo/core/compiler/build_context/target_info.rs @@ -700,11 +700,7 @@ impl<'cfg> RustcTargetData<'cfg> { if requested_kinds.iter().any(CompileKind::is_host) { let ct = CompileTarget::new(&rustc.host)?; target_info.insert(ct, host_info.clone()); - if target_applies_to_host { - target_config.insert(ct, host_config.clone()); - } else { - target_config.insert(ct, config.target_cfg_triple(&rustc.host)?); - }; + target_config.insert(ct, config.target_cfg_triple(&rustc.host)?); }; let mut res = RustcTargetData {