From b4476d74f3644c79944b8bdf30dee5bdc94dbd19 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sat, 13 Jun 2020 21:19:22 -0700 Subject: [PATCH] Support linker with -Zdoctest-xcompile. --- src/cargo/core/compiler/compilation.rs | 2 ++ src/cargo/core/compiler/context/mod.rs | 1 + src/cargo/ops/cargo_test.rs | 6 ++++ tests/testsuite/cross_compile.rs | 49 ++++++++++++++++++++++++++ 4 files changed, 58 insertions(+) diff --git a/src/cargo/core/compiler/compilation.rs b/src/cargo/core/compiler/compilation.rs index b8fb670c299..53abd2212b6 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 af7c5983ffc..2428b3705a0 100644 --- a/src/cargo/ops/cargo_test.rs +++ b/src/cargo/ops/cargo_test.rs @@ -143,6 +143,7 @@ fn run_doc_tests( args, unstable_opts, unit, + linker, } = doctest_info; // Skip any `--target` tests unless `doctest-xcompile` is specified. @@ -170,6 +171,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 14dbd7f0cfd..b95fc825379 100644 --- a/tests/testsuite/cross_compile.rs +++ b/tests/testsuite/cross_compile.rs @@ -1066,3 +1066,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(); +}