diff --git a/src/cargo/core/compiler/compilation.rs b/src/cargo/core/compiler/compilation.rs index 4c5fbbc4c95..2249c8a1b4b 100644 --- a/src/cargo/core/compiler/compilation.rs +++ b/src/cargo/core/compiler/compilation.rs @@ -144,6 +144,17 @@ impl<'gctx> Compilation<'gctx> { let primary_rustc_process = bcx.build_config.primary_unit_rustc.clone(); let rustc_workspace_wrapper_process = bcx.rustc().workspace_process(); let host = bcx.host_triple().to_string(); + + // When `target-applies-to-host=false`, and without `--target`, + // there will be only `CompileKind::Host` in requested_kinds. + // Need to insert target config explicitly for target-applies-to-host=false + // to find the correct configs. + let insert_explicit_host_runner = !bcx.gctx.target_applies_to_host()? + && bcx + .build_config + .requested_kinds + .iter() + .any(CompileKind::is_host); let mut runners = bcx .build_config .requested_kinds @@ -151,11 +162,7 @@ impl<'gctx> Compilation<'gctx> { .chain(Some(&CompileKind::Host)) .map(|kind| Ok((*kind, target_runner(bcx, *kind)?))) .collect::>>()?; - if !bcx.gctx.target_applies_to_host()? { - // When `target-applies-to-host=false`, and without `--target`, - // there will be only `CompileKind::Host` in requested_kinds. - // Need to insert target config explicitly for target-applies-to-host=false - // to find the correct configs. + if insert_explicit_host_runner { let kind = explicit_host_kind(&host); runners.insert(kind, target_runner(bcx, kind)?); } @@ -167,8 +174,7 @@ impl<'gctx> Compilation<'gctx> { .chain(Some(&CompileKind::Host)) .map(|kind| Ok((*kind, target_linker(bcx, *kind)?))) .collect::>>()?; - if !bcx.gctx.target_applies_to_host()? { - // See above reason in runner why we do this. + if insert_explicit_host_runner { let kind = explicit_host_kind(&host); linkers.insert(kind, target_linker(bcx, kind)?); } diff --git a/tests/testsuite/cross_compile.rs b/tests/testsuite/cross_compile.rs index 896ef26e11b..d6079e88352 100644 --- a/tests/testsuite/cross_compile.rs +++ b/tests/testsuite/cross_compile.rs @@ -1339,6 +1339,7 @@ fn host_linker_does_not_apply_to_binary_build() { // host.linker should not be applied but target.linker p.cargo("build -Z target-applies-to-host -Z host-config") .masquerade_as_nightly_cargo(&["target-applies-to-host", "host-config"]) + .env("CARGO_TARGET_APPLIES_TO_HOST", "false") .with_status(101) // Need to omit some MSVC-specific diagnostics // because rustc prints extra stuff when linker was not found. @@ -1352,3 +1353,20 @@ fn host_linker_does_not_apply_to_binary_build() { "#]]) .run(); } + +#[cargo_test] +fn cross_with_host_config() { + if cross_compile_disabled() { + return; + } + + let target = cross_compile::alternate(); + + let p = project().file("src/main.rs", "fn main() {}").build(); + + 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(0) + .run(); +}