Skip to content

Commit

Permalink
Add target-applies-to-host and default to true unless host-config is …
Browse files Browse the repository at this point in the history
…enabled.
  • Loading branch information
jameshilliard committed Apr 26, 2021
1 parent 0a603fd commit 46f9541
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/cargo/core/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions src/cargo/util/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,11 @@ impl Config {
.try_borrow_with(|| self.get::<RustdocExternMap>("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<TargetConfig> {
target::load_host_triple(self, target)
Expand Down
14 changes: 14 additions & 0 deletions src/cargo/util/config/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ pub(super) fn load_target_cfgs(config: &Config) -> CargoResult<Vec<(String, Targ
Ok(result)
}

/// 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()
} 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<TargetConfig> {
if config.cli_unstable().host_config {
Expand Down
83 changes: 83 additions & 0 deletions tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
"#,
Expand All @@ -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();
Expand Down

0 comments on commit 46f9541

Please sign in to comment.