Skip to content

Commit 46f9541

Browse files
committed
Add target-applies-to-host and default to true unless host-config is enabled.
1 parent 0a603fd commit 46f9541

File tree

4 files changed

+115
-3
lines changed

4 files changed

+115
-3
lines changed

src/cargo/core/compiler/build_context/target_info.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -694,11 +694,21 @@ impl<'cfg> RustcTargetData<'cfg> {
694694
let host_config = if requested_kinds.iter().any(CompileKind::is_host) {
695695
let ct = CompileTarget::new(&rustc.host)?;
696696
target_info.insert(ct, host_info.clone());
697-
let target_host_config = config.target_cfg_triple(&rustc.host)?;
698-
target_config.insert(ct, target_host_config.clone());
697+
let target_host_config = if config.target_applies_to_host() {
698+
let target_cfg_clone = config.target_cfg_triple(&rustc.host)?;
699+
target_config.insert(ct, target_cfg_clone.clone());
700+
target_cfg_clone
701+
} else {
702+
target_config.insert(ct, config.target_cfg_triple(&rustc.host)?);
703+
config.host_cfg_triple(&rustc.host)?
704+
};
699705
target_host_config
700706
} else {
701-
config.host_cfg_triple(&rustc.host)?
707+
if config.target_applies_to_host() {
708+
config.target_cfg_triple(&rustc.host)?
709+
} else {
710+
config.host_cfg_triple(&rustc.host)?
711+
}
702712
};
703713

704714
for kind in requested_kinds {

src/cargo/util/config/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,11 @@ impl Config {
14861486
.try_borrow_with(|| self.get::<RustdocExternMap>("doc.extern-map"))
14871487
}
14881488

1489+
/// Returns true if the `[target]` table should be applied to host targets.
1490+
pub fn target_applies_to_host(&self) -> bool {
1491+
target::get_target_applies_to_host(self)
1492+
}
1493+
14891494
/// Returns the `[host]` table definition for the given target triple.
14901495
pub fn host_cfg_triple(&self, target: &str) -> CargoResult<TargetConfig> {
14911496
target::load_host_triple(self, target)

src/cargo/util/config/target.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ pub(super) fn load_target_cfgs(config: &Config) -> CargoResult<Vec<(String, Targ
6464
Ok(result)
6565
}
6666

67+
/// Returns true if the `[target]` table should be applied to host targets.
68+
pub(super) fn get_target_applies_to_host(config: &Config) -> bool {
69+
let target_applies_to_host = config.get::<bool>("target-applies-to-host");
70+
if target_applies_to_host.is_ok() {
71+
target_applies_to_host.unwrap()
72+
} else {
73+
if config.cli_unstable().host_config {
74+
false
75+
} else {
76+
true
77+
}
78+
}
79+
}
80+
6781
/// Loads a single `[host]` table for the given triple.
6882
pub(super) fn load_host_triple(config: &Config, triple: &str) -> CargoResult<TargetConfig> {
6983
if config.cli_unstable().host_config {

tests/testsuite/build_script.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ fn custom_build_env_var_rustc_linker_host_target() {
212212
".cargo/config",
213213
&format!(
214214
r#"
215+
target-applies-to-host = false
215216
[target.{}]
216217
linker = "/path/to/linker"
217218
"#,
@@ -236,6 +237,88 @@ fn custom_build_env_var_rustc_linker_host_target() {
236237
p.cargo("build --target").arg(&target).run();
237238
}
238239

240+
#[cargo_test]
241+
fn custom_build_env_var_rustc_linker_host_target_env() {
242+
let target = rustc_host();
243+
let p = project()
244+
.file(
245+
".cargo/config",
246+
&format!(
247+
r#"
248+
[target.{}]
249+
linker = "/path/to/linker"
250+
"#,
251+
target
252+
),
253+
)
254+
.file(
255+
"build.rs",
256+
r#"
257+
use std::env;
258+
259+
fn main() {
260+
assert!(env::var("RUSTC_LINKER").unwrap().ends_with("/path/to/linker"));
261+
}
262+
"#,
263+
)
264+
.file("src/lib.rs", "")
265+
.build();
266+
267+
// no crate type set => linker never called => build succeeds if and
268+
// only if build.rs succeeds, despite linker binary not existing.
269+
p.cargo("build --target")
270+
.env("CARGO_TARGET_APPLIES_TO_HOST", "false")
271+
.arg(&target)
272+
.run();
273+
}
274+
275+
#[cargo_test]
276+
fn custom_build_env_var_rustc_linker_host_target_with_bad_host_config() {
277+
let target = rustc_host();
278+
let p = project()
279+
.file(
280+
".cargo/config",
281+
&format!(
282+
r#"
283+
target-applies-to-host = true
284+
[host]
285+
linker = "/path/to/host/linker"
286+
[target.{}]
287+
linker = "/path/to/target/linker"
288+
"#,
289+
target
290+
),
291+
)
292+
.file(
293+
"build.rs",
294+
r#"
295+
use std::env;
296+
297+
fn main() {
298+
assert!(env::var("RUSTC_LINKER").unwrap().ends_with("/path/to/target/linker"));
299+
}
300+
"#,
301+
)
302+
.file("src/lib.rs", "")
303+
.build();
304+
305+
// build.rs should fail due to bad target linker being set
306+
if cargo_test_support::is_nightly() {
307+
p.cargo("build -Z host-config --verbose --target")
308+
.arg(&target)
309+
.masquerade_as_nightly_cargo()
310+
.with_status(101)
311+
.with_stderr_contains(
312+
"\
313+
[COMPILING] foo v0.0.1 ([CWD])
314+
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin [..]-C linker=[..]/path/to/target/linker [..]`
315+
[ERROR] linker `[..]/path/to/target/linker` not found
316+
"
317+
)
318+
.run();
319+
}
320+
}
321+
239322
#[cargo_test]
240323
fn custom_build_env_var_rustc_linker_bad_host() {
241324
let target = rustc_host();

0 commit comments

Comments
 (0)