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: 0 additions & 1 deletion src/cargo/core/compiler/build_context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ 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
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ fn rustflags_from_build(gctx: &GlobalContext, flag: Flags) -> CargoResult<Option
}

/// Whether a host artifact must take its configuration solely from `[host]` and ignore `[target]`.
pub(crate) fn host_artifact_uses_only_host_config(
fn host_artifact_uses_only_host_config(
gctx: &GlobalContext,
requested_kinds: &[CompileKind],
kind: CompileKind,
Expand Down
13 changes: 8 additions & 5 deletions src/cargo/core/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ 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 @@ -509,8 +508,10 @@ 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)? {
// With `target-applies-to-host = true`,
// host artifacts must fall through to pick up from [target]
// since this is the stable behavior
if kind.is_host() && !bcx.gctx.target_applies_to_host()? {
return Ok(None);
}

Expand Down Expand Up @@ -555,8 +556,10 @@ 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)? {
// With `target-applies-to-host = true`,
// host artifacts must fall through to pick up from [target]
// since this is the stable behavior
if kind.is_host() && !bcx.gctx.target_applies_to_host()? {
return Ok(None);
}

Expand Down
202 changes: 202 additions & 0 deletions tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,208 @@ fn target_runner_does_not_apply_to_build_script() {
.run();
}

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

p.cargo("check -v --target")
.arg(&target)
.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 [ELAPSED]s

"#]])
.run();
}

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

p.cargo("check -v -Ztarget-applies-to-host -Zhost-config --target")
.arg(&target)
.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 [ELAPSED]s

"#]])
.run();
}

#[cargo_test]
fn host_runner_build_script_without_target() {
let p = project()
.file(
".cargo/config.toml",
r#"
[host]
runner = "nonexistent-host-runner"
"#,
)
.file("build.rs", "fn main() {}")
.file("src/lib.rs", "")
.build();

p.cargo("check -Ztarget-applies-to-host -Zhost-config")
.masquerade_as_nightly_cargo(&["target-applies-to-host", "host-config"])
.with_status(101)
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[ERROR] failed to run custom build command for `foo v0.0.1 ([ROOT]/foo)`

Caused by:
could not execute process `nonexistent-host-runner [ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build` (never executed)

Caused by:
[NOT_FOUND]

"#]])
.run();
}

#[cargo_test]
fn target_cfg_linker_build_script() {
let p = project()
.file(
".cargo/config.toml",
r#"
[target.'cfg(all())']
linker = "/path/to/cfg/linker"
"#,
)
.file("build.rs", "fn main() {}")
.file("src/lib.rs", "")
.build();

p.cargo("build -v")
.with_status(101)
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[RUNNING] `rustc --crate-name build_script_build [..]--crate-type bin [..]-C linker=[..]/path/to/cfg/linker [..]`
[ERROR] linker `[..]/path/to/cfg/linker` not found
...
"#]])
.run();
}

#[cargo_test]
fn target_cfg_linker_build_script_with_target() {
let target = rustc_host();
let p = project()
.file(
".cargo/config.toml",
r#"
[target.'cfg(all())']
linker = "/path/to/cfg/linker"
"#,
)
.file("build.rs", "fn main() {}")
.file("src/lib.rs", "")
.build();

p.cargo("build -v --target")
.arg(&target)
.with_status(101)
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[RUNNING] `rustc --crate-name build_script_build [..]--crate-type bin [..]-C linker=[..]/path/to/cfg/linker [..]`
[ERROR] linker `[..]/path/to/cfg/linker` not found
...
"#]])
.run();
}

#[cargo_test]
fn target_linker_build_script_with_target() {
let target = rustc_host();
let p = project()
.file(
".cargo/config.toml",
&format!(
r#"
[target.{target}]
linker = "/path/to/target/linker"
"#,
),
)
.file("build.rs", "fn main() {}")
.file("src/lib.rs", "")
.build();

p.cargo("build -v --target")
.arg(&target)
.with_status(101)
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[RUNNING] `rustc --crate-name build_script_build [..]--crate-type bin [..]-C linker=[..]/path/to/target/linker [..]`
[ERROR] linker `[..]/path/to/target/linker` not found
...
"#]])
.run();
}

#[cargo_test]
fn target_cfg_linker_build_script_with_host_config_and_target() {
let target = rustc_host();
let p = project()
.file(
".cargo/config.toml",
r#"
[target.'cfg(all())']
linker = "/path/to/cfg/linker"
"#,
)
.file("build.rs", "fn main() {}")
.file("src/lib.rs", "")
.build();

p.cargo("build -v -Ztarget-applies-to-host -Zhost-config --target")
.arg(&target)
.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 [..]--crate-type bin [..]`
[RUNNING] `[ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build`
[RUNNING] `rustc --crate-name foo [..]-C linker=[..]/path/to/cfg/linker [..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]])
.run();
}

#[cargo_test]
fn custom_build_script_wrong_rustc_flags() {
let p = project()
Expand Down
Loading