Skip to content

Commit 58dee7d

Browse files
committed
rustdoc: add --test-builder-wrapper argument
Instead of executing the test builder directly, the test builder wrapper will be called with test builder as the first argument and subsequent arguments. This is similar to cargo's RUSTC_WRAPPER argument. The `--test-builder-wrapper` argument can be passed multiple times to allow "nesting" of wrappers.
1 parent ee03c28 commit 58dee7d

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/librustdoc/config.rs

+7
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ pub(crate) struct Options {
130130
/// default to loading from `$sysroot/bin/rustc`.
131131
pub(crate) test_builder: Option<PathBuf>,
132132

133+
/// Run these wrapper instead of rustc directly
134+
pub(crate) test_builder_wrappers: Vec<PathBuf>,
135+
133136
// Options that affect the documentation process
134137
/// Whether to run the `calculate-doc-coverage` pass, which counts the number of public items
135138
/// with and without documentation.
@@ -204,6 +207,7 @@ impl fmt::Debug for Options {
204207
.field("enable-per-target-ignores", &self.enable_per_target_ignores)
205208
.field("run_check", &self.run_check)
206209
.field("no_run", &self.no_run)
210+
.field("test_builder_wrappers", &self.test_builder_wrappers)
207211
.field("nocapture", &self.nocapture)
208212
.field("scrape_examples_options", &self.scrape_examples_options)
209213
.field("unstable_features", &self.unstable_features)
@@ -521,6 +525,8 @@ impl Options {
521525
dcx.fatal("the `--test` flag must be passed to enable `--no-run`");
522526
}
523527

528+
let test_builder_wrappers =
529+
matches.opt_strs("test-builder-wrapper").iter().map(PathBuf::from).collect();
524530
let out_dir = matches.opt_str("out-dir").map(|s| PathBuf::from(&s));
525531
let output = matches.opt_str("output").map(|s| PathBuf::from(&s));
526532
let output = match (out_dir, output) {
@@ -727,6 +733,7 @@ impl Options {
727733
test_builder,
728734
run_check,
729735
no_run,
736+
test_builder_wrappers,
730737
nocapture,
731738
crate_name,
732739
output_format,

src/librustdoc/doctest.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use tempfile::Builder as TempFileBuilder;
2525
use std::env;
2626
use std::io::{self, Write};
2727
use std::panic;
28-
use std::path::PathBuf;
28+
use std::path::{Path, PathBuf};
2929
use std::process::{self, Command, Stdio};
3030
use std::str;
3131
use std::sync::atomic::{AtomicUsize, Ordering};
@@ -306,6 +306,16 @@ fn add_exe_suffix(input: String, target: &TargetTriple) -> String {
306306
input + &exe_suffix
307307
}
308308

309+
fn wrapped_rustc_command(rustc_wrappers: &[PathBuf], rustc_binary: &Path) -> Command {
310+
let args: Vec<&Path> =
311+
rustc_wrappers.iter().map(PathBuf::as_path).chain([rustc_binary].into_iter()).collect();
312+
let (exe, args) = args.split_first().expect("unable to create rustc command");
313+
314+
let mut command = Command::new(exe);
315+
command.args(args);
316+
command
317+
}
318+
309319
fn run_test(
310320
test: &str,
311321
crate_name: &str,
@@ -334,7 +344,7 @@ fn run_test(
334344
.test_builder
335345
.as_deref()
336346
.unwrap_or_else(|| rustc_interface::util::rustc_path().expect("found rustc"));
337-
let mut compiler = Command::new(&rustc_binary);
347+
let mut compiler = wrapped_rustc_command(&rustdoc_options.test_builder_wrappers, rustc_binary);
338348
compiler.arg("--crate-type").arg("bin");
339349
for cfg in &rustdoc_options.cfgs {
340350
compiler.arg("--cfg").arg(&cfg);

src/librustdoc/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,14 @@ fn opts() -> Vec<RustcOptGroup> {
530530
unstable("test-builder", |o| {
531531
o.optopt("", "test-builder", "The rustc-like binary to use as the test builder", "PATH")
532532
}),
533+
unstable("test-builder-wrapper", |o| {
534+
o.optmulti(
535+
"",
536+
"test-builder-wrapper",
537+
"The wrapper program for running rustc",
538+
"WRAPPER",
539+
)
540+
}),
533541
unstable("check", |o| o.optflagmulti("", "check", "Run rustdoc checks")),
534542
unstable("generate-redirect-map", |o| {
535543
o.optflagmulti(

0 commit comments

Comments
 (0)