diff --git a/src/tools/run-make-support/src/external_deps/rustdoc.rs b/src/tools/run-make-support/src/external_deps/rustdoc.rs index 433a57cd9fa65..f5d3a7d835311 100644 --- a/src/tools/run-make-support/src/external_deps/rustdoc.rs +++ b/src/tools/run-make-support/src/external_deps/rustdoc.rs @@ -3,21 +3,35 @@ use std::path::Path; use crate::command::Command; use crate::env::env_var; +use crate::target; use crate::util::set_host_compiler_dylib_path; -/// Construct a new `rustdoc` invocation. +/// Construct a new `rustdoc` invocation with target automatically set to cross-compile target. Use +/// [`bare_rustdoc`] to avoid this. #[track_caller] pub fn rustdoc() -> Rustdoc { Rustdoc::new() } +/// Bare `rustdoc` invocation, no args set. +#[track_caller] +pub fn bare_rustdoc() -> Rustdoc { + Rustdoc::bare() +} + #[derive(Debug)] #[must_use] pub struct Rustdoc { cmd: Command, + target: Option, } -crate::macros::impl_common_helpers!(Rustdoc); +// Only fill in the target just before execution, so that it can be overridden. +crate::macros::impl_common_helpers!(Rustdoc, |rustdoc: &mut Rustdoc| { + if let Some(target) = &rustdoc.target { + rustdoc.cmd.arg(&format!("--target={target}")); + } +}); #[track_caller] fn setup_common() -> Command { @@ -28,11 +42,19 @@ fn setup_common() -> Command { } impl Rustdoc { - /// Construct a bare `rustdoc` invocation. + /// Construct a new `rustdoc` invocation with target automatically set to cross-compile target. + /// Use [`bare_rustdoc`] to avoid this. #[track_caller] pub fn new() -> Self { let cmd = setup_common(); - Self { cmd } + Self { cmd, target: Some(target()) } + } + + /// Bare `rustdoc` invocation, no args set. + #[track_caller] + pub fn bare() -> Self { + let cmd = setup_common(); + Self { cmd, target: None } } /// Specify where an external library is located. @@ -85,8 +107,9 @@ impl Rustdoc { /// Specify the target triple, or a path to a custom target json spec file. pub fn target>(&mut self, target: S) -> &mut Self { - let target = target.as_ref(); - self.cmd.arg(format!("--target={target}")); + // We store the target as a separate field, so that it can be specified multiple times. + // This is in particular useful to override the default target set in `Rustdoc::new()`. + self.target = Some(target.as_ref().to_string()); self } diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs index c75d500d2f06e..36fafa331769a 100644 --- a/src/tools/run-make-support/src/lib.rs +++ b/src/tools/run-make-support/src/lib.rs @@ -70,7 +70,7 @@ pub use llvm::{ }; pub use python::python_command; pub use rustc::{bare_rustc, rustc, rustc_path, Rustc}; -pub use rustdoc::{rustdoc, Rustdoc}; +pub use rustdoc::{bare_rustdoc, rustdoc, Rustdoc}; /// [`diff`][mod@diff] is implemented in terms of the [similar] library. /// diff --git a/tests/run-make/rustdoc-default-output/rmake.rs b/tests/run-make/rustdoc-default-output/rmake.rs index 5f9c501e5286b..1ed2e8d79bb02 100644 --- a/tests/run-make/rustdoc-default-output/rmake.rs +++ b/tests/run-make/rustdoc-default-output/rmake.rs @@ -3,10 +3,11 @@ // ensures the output of rustdoc's help menu is as expected. // See https://github.com/rust-lang/rust/issues/88756 -use run_make_support::{diff, rustdoc}; +use run_make_support::{bare_rustdoc, diff}; fn main() { - let out = rustdoc().run().stdout_utf8(); + // Use `bare_rustdoc` to ensure no `--target` is set. + let out = bare_rustdoc().run().stdout_utf8(); diff() .expected_file("output-default.stdout") .actual_text("actual", out)