Skip to content

Commit e24bf92

Browse files
committed
Add feature gate for target-applies-to-host.
1 parent 46f9541 commit e24bf92

File tree

5 files changed

+43
-19
lines changed

5 files changed

+43
-19
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,10 +691,14 @@ impl<'cfg> RustcTargetData<'cfg> {
691691
// `--target` flag is not specified. Since the unit_dependency code
692692
// needs access to the target config data, create a copy so that it
693693
// can be found. See `rebuild_unit_graph_shared` for why this is done.
694+
let target_applies_to_host = config.target_applies_to_host();
695+
if target_applies_to_host.is_err() {
696+
return Err(target_applies_to_host.unwrap_err());
697+
}
694698
let host_config = if requested_kinds.iter().any(CompileKind::is_host) {
695699
let ct = CompileTarget::new(&rustc.host)?;
696700
target_info.insert(ct, host_info.clone());
697-
let target_host_config = if config.target_applies_to_host() {
701+
let target_host_config = if target_applies_to_host.unwrap() {
698702
let target_cfg_clone = config.target_cfg_triple(&rustc.host)?;
699703
target_config.insert(ct, target_cfg_clone.clone());
700704
target_cfg_clone
@@ -704,7 +708,7 @@ impl<'cfg> RustcTargetData<'cfg> {
704708
};
705709
target_host_config
706710
} else {
707-
if config.target_applies_to_host() {
711+
if target_applies_to_host.unwrap() {
708712
config.target_cfg_triple(&rustc.host)?
709713
} else {
710714
config.host_cfg_triple(&rustc.host)?

src/cargo/core/features.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ unstable_cli_options!(
601601
no_index_update: bool = ("Do not update the registry index even if the cache is outdated"),
602602
panic_abort_tests: bool = ("Enable support to run tests with -Cpanic=abort"),
603603
host_config: bool = ("Enable the [host] section in the .cargo/config.toml file"),
604+
target_applies_to_host: bool = ("Enable the `target-applies-to-host` key in the .cargo/config.toml file"),
604605
patch_in_config: bool = ("Allow `[patch]` sections in .cargo/config.toml files"),
605606
rustdoc_map: bool = ("Allow passing external documentation mappings to rustdoc"),
606607
separate_nightlies: bool = (HIDDEN),
@@ -789,6 +790,7 @@ impl CliUnstable {
789790
"jobserver-per-rustc" => self.jobserver_per_rustc = parse_empty(k, v)?,
790791
"configurable-env" => self.configurable_env = parse_empty(k, v)?,
791792
"host-config" => self.host_config = parse_empty(k, v)?,
793+
"target-applies-to-host" => self.target_applies_to_host = parse_empty(k, v)?,
792794
"patch-in-config" => self.patch_in_config = parse_empty(k, v)?,
793795
"features" => {
794796
// For now this is still allowed (there are still some

src/cargo/util/config/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1487,7 +1487,7 @@ impl Config {
14871487
}
14881488

14891489
/// Returns true if the `[target]` table should be applied to host targets.
1490-
pub fn target_applies_to_host(&self) -> bool {
1490+
pub fn target_applies_to_host(&self) -> CargoResult<bool> {
14911491
target::get_target_applies_to_host(self)
14921492
}
14931493

src/cargo/util/config/target.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,25 @@ pub(super) fn load_target_cfgs(config: &Config) -> CargoResult<Vec<(String, Targ
6565
}
6666

6767
/// 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()
68+
pub(super) fn get_target_applies_to_host(config: &Config) -> CargoResult<bool> {
69+
if config.cli_unstable().target_applies_to_host {
70+
let target_applies_to_host = config.get::<bool>("target-applies-to-host");
71+
if target_applies_to_host.is_ok() {
72+
Ok(target_applies_to_host.unwrap())
73+
} else {
74+
if config.cli_unstable().host_config {
75+
Ok(false)
76+
} else {
77+
Ok(true)
78+
}
79+
}
7280
} else {
7381
if config.cli_unstable().host_config {
74-
false
82+
anyhow::bail!(
83+
"the -Zhost-config flag requires the -Ztarget-applies-to-host flag to be set"
84+
);
7585
} else {
76-
true
86+
Ok(true)
7787
}
7888
}
7989
}

tests/testsuite/build_script.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,12 @@ fn custom_build_env_var_rustc_linker_host_target() {
234234

235235
// no crate type set => linker never called => build succeeds if and
236236
// only if build.rs succeeds, despite linker binary not existing.
237-
p.cargo("build --target").arg(&target).run();
237+
if cargo_test_support::is_nightly() {
238+
p.cargo("build -Z target-applies-to-host --target")
239+
.arg(&target)
240+
.masquerade_as_nightly_cargo()
241+
.run();
242+
}
238243
}
239244

240245
#[cargo_test]
@@ -266,10 +271,13 @@ fn custom_build_env_var_rustc_linker_host_target_env() {
266271

267272
// no crate type set => linker never called => build succeeds if and
268273
// 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();
274+
if cargo_test_support::is_nightly() {
275+
p.cargo("build -Z target-applies-to-host --target")
276+
.env("CARGO_TARGET_APPLIES_TO_HOST", "false")
277+
.arg(&target)
278+
.masquerade_as_nightly_cargo()
279+
.run();
280+
}
273281
}
274282

275283
#[cargo_test]
@@ -304,7 +312,7 @@ fn custom_build_env_var_rustc_linker_host_target_with_bad_host_config() {
304312

305313
// build.rs should fail due to bad target linker being set
306314
if cargo_test_support::is_nightly() {
307-
p.cargo("build -Z host-config --verbose --target")
315+
p.cargo("build -Z target-applies-to-host -Z host-config --verbose --target")
308316
.arg(&target)
309317
.masquerade_as_nightly_cargo()
310318
.with_status(101)
@@ -350,7 +358,7 @@ fn custom_build_env_var_rustc_linker_bad_host() {
350358

351359
// build.rs should fail due to bad host linker being set
352360
if cargo_test_support::is_nightly() {
353-
p.cargo("build -Z host-config --verbose --target")
361+
p.cargo("build -Z target-applies-to-host -Z host-config --verbose --target")
354362
.arg(&target)
355363
.masquerade_as_nightly_cargo()
356364
.with_status(101)
@@ -398,7 +406,7 @@ fn custom_build_env_var_rustc_linker_bad_host_with_arch() {
398406

399407
// build.rs should fail due to bad host linker being set
400408
if cargo_test_support::is_nightly() {
401-
p.cargo("build -Z host-config --verbose --target")
409+
p.cargo("build -Z target-applies-to-host -Z host-config --verbose --target")
402410
.arg(&target)
403411
.masquerade_as_nightly_cargo()
404412
.with_status(101)
@@ -445,7 +453,7 @@ fn custom_build_env_var_rustc_linker_cross_arch_host() {
445453

446454
// build.rs should fail due to bad host linker being set
447455
if cargo_test_support::is_nightly() {
448-
p.cargo("build -Z host-config --verbose --target")
456+
p.cargo("build -Z target-applies-to-host -Z host-config --verbose --target")
449457
.arg(&target)
450458
.masquerade_as_nightly_cargo()
451459
.run();
@@ -486,7 +494,7 @@ fn custom_build_env_var_rustc_linker_bad_cross_arch_host() {
486494

487495
// build.rs should fail due to bad host linker being set
488496
if cargo_test_support::is_nightly() {
489-
p.cargo("build -Z host-config --verbose --target")
497+
p.cargo("build -Z target-applies-to-host -Z host-config --verbose --target")
490498
.arg(&target)
491499
.masquerade_as_nightly_cargo()
492500
.with_status(101)

0 commit comments

Comments
 (0)