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
31 changes: 15 additions & 16 deletions src/cargo/core/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::core::compiler::BuildContext;
use crate::core::compiler::RustdocFingerprint;
use crate::core::compiler::apply_env_config;
use crate::core::compiler::{CompileKind, Unit, UnitHash};
use crate::util::{CargoResult, GlobalContext, context};
use crate::util::{CargoResult, GlobalContext};

/// Represents the kind of process we are creating.
#[derive(Debug)]
Expand Down Expand Up @@ -235,13 +235,15 @@ impl<'gctx> Compilation<'gctx> {
cmd: T,
pkg: &Package,
) -> CargoResult<ProcessBuilder> {
self.fill_env(
ProcessBuilder::new(cmd),
pkg,
None,
CompileKind::Host,
ToolKind::HostProcess,
)
let builder = if let Some((runner, args)) = self.target_runner(CompileKind::Host) {
let mut builder = ProcessBuilder::new(runner);
builder.args(args);
builder.arg(cmd);
builder
} else {
ProcessBuilder::new(cmd)
};
self.fill_env(builder, pkg, None, CompileKind::Host, ToolKind::HostProcess)
}

pub fn target_runner(&self, kind: CompileKind) -> Option<&(PathBuf, Vec<String>)> {
Expand Down Expand Up @@ -440,14 +442,11 @@ fn target_runner(
bcx: &BuildContext<'_, '_>,
kind: CompileKind,
) -> CargoResult<Option<(PathBuf, Vec<String>)>> {
let target = bcx.target_data.short_name(&kind);

// try target.{}.runner
let key = format!("target.{}.runner", target);

if let Some(v) = bcx.gctx.get::<Option<context::PathAndArgs>>(&key)? {
let path = v.path.resolve_program(bcx.gctx);
return Ok(Some((path, v.args)));
// Try host.runner / target.{}.runner via target_config, which routes
// through host_config for CompileKind::Host when -Zhost-config is enabled.
if let Some(runner) = bcx.target_data.target_config(kind).runner.as_ref() {
let path = runner.val.path.clone().resolve_program(bcx.gctx);
return Ok(Some((path, runner.val.args.clone())));
}

// try target.'cfg(...)'.runner
Expand Down
5 changes: 5 additions & 0 deletions src/doc/src/reference/unstable.md
Original file line number Diff line number Diff line change
Expand Up @@ -708,13 +708,18 @@ false` is set in the Cargo configuration file.
# config.toml
[host]
linker = "/path/to/host/linker"
runner = "host-runner"
[host.x86_64-unknown-linux-gnu]
linker = "/path/to/host/arch/linker"
runner = "host-arch-runner"
rustflags = ["-Clink-arg=--verbose"]
[target.x86_64-unknown-linux-gnu]
linker = "/path/to/target/linker"
```

The `host.runner` setting wraps execution of host build targets such as build
scripts, similar to how `target.<triple>.runner` wraps `cargo run`/`test`/`bench`.

The generic `host` table above will be entirely ignored when building on an
`x86_64-unknown-linux-gnu` host as the `host.x86_64-unknown-linux-gnu` table
takes precedence.
Expand Down
153 changes: 153 additions & 0 deletions tests/testsuite/build_script.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the the failure already show that it is working, could we have a successful case using the echo_wrapper? Such as having this WRAPPER CALLED: snapshot

cargo/tests/testsuite/build.rs

Lines 4464 to 4474 in b9f0d83

let p = project().file("src/lib.rs", "").build();
let wrapper = tools::echo_wrapper();
p.cargo("build -v")
.env("RUSTC_WRAPPER", &wrapper)
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[RUNNING] `[..]/rustc-echo-wrapper[EXE] rustc --crate-name foo [..]`
WRAPPER CALLED: rustc --crate-name foo [..]
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a commit to handle this.

Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,159 @@ fn custom_build_linker_bad_cross_arch_host() {
.run();
}

#[cargo_test]
fn host_runner_wraps_build_script() {
let target = rustc_host();
let wrapper = tools::echo_wrapper();
let p = project()
.file("build.rs", "fn main() {}")
.file("src/lib.rs", "")
.build();

// Build should succeed with the host runner wrapping the build script
p.cargo("build -Z target-applies-to-host -Z host-config -v --target")
.arg(&target)
.env("CARGO_HOST_RUNNER", &wrapper)
.masquerade_as_nightly_cargo(&["target-applies-to-host", "host-config"])
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[RUNNING] `rustc [..]build.rs [..]`
[RUNNING] `[..]/rustc-echo-wrapper[EXE] [ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build`
[RUNNING] `rustc [..]--crate-name foo [..]`
[FINISHED] [..]

"#]])
.run();
}

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

// build.rs execution should fail due to the host runner, not the target runner
p.cargo("build -Z target-applies-to-host -Z host-config --target")
.arg(&target)
.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-runner [ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build` (never executed)

Caused by:
[NOT_FOUND]

"#]])
.run();
}

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

// host.<triple>.runner should take precedence over host.runner
p.cargo("build -Z target-applies-to-host -Z host-config --target")
.arg(&target)
.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-arch-runner [ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build` (never executed)

Caused by:
[NOT_FOUND]

"#]])
.run();
}

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

// Without -Zhost-config, host.runner should be ignored and build succeeds
p.cargo("build").run();
}

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

// Runner args should be passed correctly before the build script path
p.cargo("build -Z target-applies-to-host -Z host-config --target")
.arg(&target)
.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-runner --flag arg1 [ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build` (never executed)

Caused by:
[NOT_FOUND]

"#]])
.run();
}

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