diff --git a/src/cargo/core/compiler/build_context/target_info.rs b/src/cargo/core/compiler/build_context/target_info.rs index f36fc173bcc0..5d34b8aacba2 100644 --- a/src/cargo/core/compiler/build_context/target_info.rs +++ b/src/cargo/core/compiler/build_context/target_info.rs @@ -953,6 +953,16 @@ impl<'gctx> RustcTargetData<'gctx> { res.merge_compile_kind(kind)?; } + // TODO: Although `-Zsandbox need this target platform to exist, + // if there is no build script involved in the entire dependency tree, + // there is no need for doing this target pre-fetch. + if ws.gctx().cli_unstable().sandbox { + if let Some(target) = gctx.sandbox_config()?.target.as_ref() { + let kind = CompileKind::Target(CompileTarget::new(target)?); + res.merge_compile_kind(kind)?; + } + } + Ok(res) } diff --git a/src/cargo/core/compiler/compilation.rs b/src/cargo/core/compiler/compilation.rs index 5ecf77d4c6a0..8f28355aa020 100644 --- a/src/cargo/core/compiler/compilation.rs +++ b/src/cargo/core/compiler/compilation.rs @@ -227,13 +227,23 @@ impl<'gctx> Compilation<'gctx> { cmd: T, pkg: &Package, ) -> CargoResult { - self.fill_env( - ProcessBuilder::new(cmd), - pkg, - None, - CompileKind::Host, - ToolKind::HostProcess, - ) + let builder = if self.gctx.cli_unstable().sandbox { + let sandbox = self.gctx.sandbox_config()?; + match &sandbox.runner { + Some(runner) => { + let program = runner.path.resolve_program(self.gctx); + let mut builder = ProcessBuilder::new(program); + builder.args(&runner.args); + builder.arg(cmd); + builder + } + None => ProcessBuilder::new(cmd), + } + } else { + ProcessBuilder::new(cmd) + }; + + self.fill_env(builder, pkg, None, CompileKind::Host, ToolKind::HostProcess) } pub fn target_runner(&self, kind: CompileKind) -> Option<&(PathBuf, Vec)> { diff --git a/src/cargo/core/compiler/custom_build.rs b/src/cargo/core/compiler/custom_build.rs index f36e741c65b6..b0224410901c 100644 --- a/src/cargo/core/compiler/custom_build.rs +++ b/src/cargo/core/compiler/custom_build.rs @@ -32,10 +32,10 @@ //! [instructions]: https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script use super::{fingerprint, BuildRunner, Job, Unit, Work}; -use crate::core::compiler::artifact; use crate::core::compiler::build_runner::Metadata; use crate::core::compiler::fingerprint::DirtyReason; use crate::core::compiler::job_queue::JobState; +use crate::core::compiler::{artifact, FileFlavor}; use crate::core::{profiles::ProfileRoot, PackageId, Target}; use crate::util::errors::CargoResult; use crate::util::internal; @@ -265,7 +265,19 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul } // Building the command to execute - let to_exec = script_dir.join(unit.target.name()); + let to_exec = if build_runner.bcx.gctx.cli_unstable().sandbox { + // XXX: is the `expect` statement here really the truth? + let outputs = build_runner.outputs(build_script_unit)?; + let outputs = outputs + .iter() + .filter(|output| output.flavor == FileFlavor::Normal); + let (Some(output), None) = (outputs.next(), outputs.next()) else { + panic!("a build script to produce the one and only executable"); + }; + output.bin_dst().as_os_str().to_os_string() + } else { + script_dir.join(unit.target.name()).into_os_string() + }; // Start preparing the process to execute, starting out with some // environment variables. Note that the profile-related environment @@ -274,7 +286,6 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul // NOTE: if you add any profile flags, be sure to update // `Profiles::get_profile_run_custom_build` so that those flags get // carried over. - let to_exec = to_exec.into_os_string(); let mut cmd = build_runner.compilation.host_process(to_exec, &unit.pkg)?; let debug = unit.profile.debuginfo.is_turned_on(); cmd.env("OUT_DIR", &script_out_dir) diff --git a/src/cargo/core/compiler/unit_dependencies.rs b/src/cargo/core/compiler/unit_dependencies.rs index 59e356fc6828..54d0aa017ac6 100644 --- a/src/cargo/core/compiler/unit_dependencies.rs +++ b/src/cargo/core/compiler/unit_dependencies.rs @@ -481,8 +481,16 @@ fn compute_deps_custom_build( &unit.pkg, &unit.target, script_unit_for, - // Build scripts always compiled for the host. - CompileKind::Host, + // Build scripts always compiled for the host, + // unless `sandbox.target` is set. + if state.gctx.cli_unstable().sandbox { + match state.gctx.sandbox_config()?.target.as_ref() { + Some(target) => CompileKind::Target(super::CompileTarget::new(target)?), + None => CompileKind::Host, + } + } else { + CompileKind::Host + }, CompileMode::Build, IS_NO_ARTIFACT_DEP, )?;