Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cargo/core/compiler/build_context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub use self::target_info::FileFlavor;
pub use self::target_info::FileType;
pub use self::target_info::RustcTargetData;
pub use self::target_info::TargetInfo;
pub(crate) use self::target_info::host_artifact_uses_only_host_config;

/// The build context, containing complete information needed for a build task
/// before it gets started.
Expand Down
49 changes: 31 additions & 18 deletions src/cargo/core/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,24 +783,8 @@ fn extra_args(
kind: CompileKind,
flags: Flags,
) -> CargoResult<Vec<String>> {
let target_applies_to_host = gctx.target_applies_to_host()?;

// Host artifacts should not generally pick up rustflags from anywhere except [host].
//
// The one exception to this is if `target-applies-to-host = true`, which opts into a
// particular (inconsistent) past Cargo behavior where host artifacts _do_ pick up rustflags
// set elsewhere when `--target` isn't passed.
if kind.is_host() {
if target_applies_to_host && requested_kinds == [CompileKind::Host] {
// This is the past Cargo behavior where we fall back to the same logic as for other
// artifacts without --target.
} else {
// In all other cases, host artifacts just get flags from [host], regardless of
// --target. Or, phrased differently, no `--target` behaves the same as `--target
// <host>`, and host artifacts are always "special" (they don't pick up `RUSTFLAGS` for
// example).
return Ok(rustflags_from_host(gctx, flags, host_triple)?.unwrap_or_else(Vec::new));
}
if host_artifact_uses_only_host_config(gctx, requested_kinds, kind)? {
return Ok(rustflags_from_host(gctx, flags, host_triple)?.unwrap_or_else(Vec::new));
}

// All other artifacts pick up the RUSTFLAGS, [target.*], and [build], in that order.
Expand Down Expand Up @@ -923,6 +907,35 @@ fn rustflags_from_build(gctx: &GlobalContext, flag: Flags) -> CargoResult<Option
Ok(list.as_ref().map(|l| l.as_slice().to_vec()))
}

/// Whether a host artifact must take its configuration solely from `[host]` and ignore `[target]`.
pub(crate) fn host_artifact_uses_only_host_config(
gctx: &GlobalContext,
requested_kinds: &[CompileKind],
kind: CompileKind,
) -> CargoResult<bool> {
let target_applies_to_host = gctx.target_applies_to_host()?;

// Host artifacts should not generally pick up rustflags from anywhere except [host].
//
// The one exception to this is if `target-applies-to-host = true`, which opts into a
// particular (inconsistent) past Cargo behavior where host artifacts _do_ pick up rustflags
// set elsewhere when `--target` isn't passed.
if kind.is_host() {
if target_applies_to_host && requested_kinds == [CompileKind::Host] {
// This is the past Cargo behavior where we fall back to the same logic as for other
// artifacts without --target.
} else {
// In all other cases, host artifacts just get flags from [host], regardless of
// --target. Or, phrased differently, no `--target` behaves the same as `--target
// <host>`, and host artifacts are always "special" (they don't pick up `RUSTFLAGS` for
// example).
return Ok(true);
}
}

Ok(false)
}

/// Collection of information about `rustc` and the host and target.
pub struct RustcTargetData<'gctx> {
/// Information about `rustc` itself.
Expand Down
11 changes: 11 additions & 0 deletions src/cargo/core/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::core::compiler::BuildContext;
use crate::core::compiler::CompileTarget;
use crate::core::compiler::RustdocFingerprint;
use crate::core::compiler::apply_env_config;
use crate::core::compiler::build_context::host_artifact_uses_only_host_config;
use crate::core::compiler::{CompileKind, Unit, UnitHash};
use crate::util::{CargoResult, GlobalContext};

Expand Down Expand Up @@ -507,6 +508,11 @@ fn target_runner(
return Ok(Some((path, runner.val.args.clone())));
}

// Host artifacts should not pick up a runner from `[target.'cfg(...)']`.
if host_artifact_uses_only_host_config(bcx.gctx, &bcx.build_config.requested_kinds, kind)? {
return Ok(None);
}

// try target.'cfg(...)'.runner
let target_cfg = bcx.target_data.info(kind).cfg();
let mut cfgs = bcx
Expand Down Expand Up @@ -548,6 +554,11 @@ fn target_linker(bcx: &BuildContext<'_, '_>, kind: CompileKind) -> CargoResult<O
return Ok(Some(path));
}

// Host artifacts should not pick up a linker from `[target.'cfg(...)']`.
if host_artifact_uses_only_host_config(bcx.gctx, &bcx.build_config.requested_kinds, kind)? {
return Ok(None);
}

// Try target.'cfg(...)'.linker.
let target_cfg = bcx.target_data.info(kind).cfg();
let mut cfgs = bcx
Expand Down
58 changes: 58 additions & 0 deletions tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6812,3 +6812,61 @@ fn linker_search_path_preference() {
...
"#]]).run();
}

#[cargo_test]
fn target_runner_does_not_apply_to_build_script_with_host_config() {
let p = project()
.file(
".cargo/config.toml",
&format!(
r#"
[target.'cfg(r#true)']
runner = "nonexistent-target-runner"
"#,
),
)
.file("build.rs", "fn main() {}")
.file("src/lib.rs", "")
.build();

p.cargo("build -Z target-applies-to-host --verbose -Z host-config")
.masquerade_as_nightly_cargo(&["target-applies-to-host", "host-config"])
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[RUNNING] `rustc --crate-name build_script_build [..]`
[RUNNING] `[ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build`
[RUNNING] `rustc --crate-name foo [..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..]

"#]])
.run();
}

#[cargo_test]
fn target_linker_does_not_apply_to_build_script_with_host_config() {
let p = project()
.file(
".cargo/config.toml",
&format!(
r#"
[target.'cfg(r#true)']
linker = "nonexistent-host-linker"
"#,
),
)
.file("build.rs", "fn main() {}")
.file("src/lib.rs", "")
.build();

p.cargo("build -Z target-applies-to-host --verbose -Z host-config")
.masquerade_as_nightly_cargo(&["target-applies-to-host", "host-config"])
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[RUNNING] `rustc --crate-name build_script_build [..]`
[RUNNING] `[ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build`
[RUNNING] `rustc --crate-name foo [..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..]

"#]])
.run();
}