Skip to content

Commit

Permalink
Add feature gate for target-applies-to-host.
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshilliard committed Apr 27, 2021
1 parent 46f9541 commit e24bf92
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 19 deletions.
8 changes: 6 additions & 2 deletions src/cargo/core/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)?
Expand Down
2 changes: 2 additions & 0 deletions src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/util/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> {
target::get_target_applies_to_host(self)
}

Expand Down
22 changes: 16 additions & 6 deletions src/cargo/util/config/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,25 @@ pub(super) fn load_target_cfgs(config: &Config) -> CargoResult<Vec<(String, Targ
}

/// Returns true if the `[target]` table should be applied to host targets.
pub(super) fn get_target_applies_to_host(config: &Config) -> bool {
let target_applies_to_host = config.get::<bool>("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<bool> {
if config.cli_unstable().target_applies_to_host {
let target_applies_to_host = config.get::<bool>("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)
}
}
}
Expand Down
28 changes: 18 additions & 10 deletions tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e24bf92

Please sign in to comment.