From 2fabe52ccdfaea40088ae99cddf383a0b0097fe5 Mon Sep 17 00:00:00 2001 From: chansuke Date: Thu, 16 Sep 2021 22:18:35 +0900 Subject: [PATCH] Add rustc-link-args to doctest build --- src/cargo/core/compiler/context/mod.rs | 14 ++++++--- .../testsuite/build_script_extra_link_arg.rs | 31 +++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/cargo/core/compiler/context/mod.rs b/src/cargo/core/compiler/context/mod.rs index 3d8967ee9ed..1f9f9902671 100644 --- a/src/cargo/core/compiler/context/mod.rs +++ b/src/cargo/core/compiler/context/mod.rs @@ -2,15 +2,14 @@ use std::collections::{BTreeSet, HashMap, HashSet}; use std::path::{Path, PathBuf}; use std::sync::{Arc, Mutex}; -use anyhow::{bail, Context as _}; -use filetime::FileTime; -use jobserver::Client; - use crate::core::compiler::compilation::{self, UnitOutput}; use crate::core::compiler::{self, Unit}; use crate::core::PackageId; use crate::util::errors::CargoResult; use crate::util::profile; +use anyhow::{bail, Context as _}; +use filetime::FileTime; +use jobserver::Client; use super::build_plan::BuildPlan; use super::custom_build::{self, BuildDeps, BuildScriptOutputs, BuildScripts}; @@ -241,6 +240,13 @@ impl<'a, 'cfg> Context<'a, 'cfg> { args.push("--cfg".into()); args.push(cfg.into()); } + + for (lt, arg) in &output.linker_args { + if lt.applies_to(&unit.target) { + args.push("-C".into()); + args.push(format!("link-arg={}", arg).into()); + } + } } } args.extend(self.bcx.rustdocflags_args(unit).iter().map(Into::into)); diff --git a/tests/testsuite/build_script_extra_link_arg.rs b/tests/testsuite/build_script_extra_link_arg.rs index 9ecfc6f26e9..116b094d770 100644 --- a/tests/testsuite/build_script_extra_link_arg.rs +++ b/tests/testsuite/build_script_extra_link_arg.rs @@ -279,3 +279,34 @@ fn link_arg_transitive_not_allowed() { .with_stderr_does_not_contain("--bogus") .run(); } + +#[cargo_test] +fn link_arg_with_doctest() { + let p = project() + .file( + "src/lib.rs", + r#" + //! ``` + //! let x = 5; + //! assert_eq!(x, 5); + //! ``` + "#, + ) + .file( + "build.rs", + r#" + fn main() { + println!("cargo:rustc-link-arg=--this-is-a-bogus-flag"); + } + "#, + ) + .build(); + + p.cargo("test --doc -v") + .masquerade_as_nightly_cargo() + .without_status() + .with_stderr_contains( + "[RUNNING] `rustdoc [..]--crate-name foo [..]-C link-arg=--this-is-a-bogus-flag[..]", + ) + .run(); +}