diff --git a/src/cargo/core/compiler/compilation.rs b/src/cargo/core/compiler/compilation.rs index 2738baa2503..32b51aa5833 100644 --- a/src/cargo/core/compiler/compilation.rs +++ b/src/cargo/core/compiler/compilation.rs @@ -20,6 +20,8 @@ pub struct Doctest { pub args: Vec, /// Whether or not -Zunstable-options is needed. pub unstable_opts: bool, + /// The -Clinker value to use. + pub linker: Option, } /// A structure returning the result of a compilation. diff --git a/src/cargo/core/compiler/context/mod.rs b/src/cargo/core/compiler/context/mod.rs index 602aba2cda8..c2139c43a4d 100644 --- a/src/cargo/core/compiler/context/mod.rs +++ b/src/cargo/core/compiler/context/mod.rs @@ -209,6 +209,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> { unit: unit.clone(), args, unstable_opts, + linker: self.bcx.linker(unit.kind), }); } diff --git a/src/cargo/ops/cargo_test.rs b/src/cargo/ops/cargo_test.rs index e82827d09a9..de1a46585ee 100644 --- a/src/cargo/ops/cargo_test.rs +++ b/src/cargo/ops/cargo_test.rs @@ -144,6 +144,7 @@ fn run_doc_tests( args, unstable_opts, unit, + linker, } = doctest_info; if !doctest_xcompile { @@ -178,6 +179,11 @@ fn run_doc_tests( p.arg("--runtool-arg").arg(arg); } } + if let Some(linker) = linker { + let mut joined = OsString::from("linker="); + joined.push(linker); + p.arg("-C").arg(joined); + } } for &rust_dep in &[ diff --git a/tests/testsuite/cross_compile.rs b/tests/testsuite/cross_compile.rs index a781729a7b0..fe573a88270 100644 --- a/tests/testsuite/cross_compile.rs +++ b/tests/testsuite/cross_compile.rs @@ -1067,3 +1067,52 @@ fn cross_test_dylib() { .with_stdout_contains_n("test foo ... ok", 2) .run(); } + +#[cargo_test] +fn doctest_xcompile_linker() { + if cross_compile::disabled() { + return; + } + if !is_nightly() { + // -Zdoctest-xcompile is unstable + return; + } + + let target = cross_compile::alternate(); + let p = project() + .file( + ".cargo/config", + &format!( + r#" + [target.{}] + linker = "my-linker-tool" + "#, + target + ), + ) + .file("Cargo.toml", &basic_manifest("foo", "0.1.0")) + .file( + "src/lib.rs", + r#" + /// ``` + /// assert_eq!(1, 1); + /// ``` + pub fn foo() {} + "#, + ) + .build(); + + // Fails because `my-linker-tool` doesn't actually exist. + p.cargo("test --doc -v -Zdoctest-xcompile --target") + .arg(&target) + .with_status(101) + .masquerade_as_nightly_cargo() + .with_stderr_contains(&format!( + "\ +[RUNNING] `rustdoc --crate-type lib --test [..]\ + --target {target} [..] -C linker=my-linker-tool[..] +", + target = target, + )) + .run(); +}