Skip to content

Commit d9bc16c

Browse files
authored
Rollup merge of #82261 - ojeda:rustdoc-argfile, r=jyn514
rustdoc: Support argument files Factors out the `rustc_driver` logic that handles argument files so that rustdoc supports them as well, e.g.: rustdoc `@argfile` This is needed to be able to generate docs for projects that already use argument files when compiling them, e.g. projects that pass a huge number of `--cfg` arguments. The feature was stabilized for `rustc` in #66172.
2 parents cc01bbe + 755b3fc commit d9bc16c

11 files changed

+77
-13
lines changed

compiler/rustc_driver/src/args.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt;
33
use std::fs;
44
use std::io;
55

6-
pub fn arg_expand(arg: String) -> Result<Vec<String>, Error> {
6+
fn arg_expand(arg: String) -> Result<Vec<String>, Error> {
77
if let Some(path) = arg.strip_prefix('@') {
88
let file = match fs::read_to_string(path) {
99
Ok(file) => file,
@@ -18,6 +18,20 @@ pub fn arg_expand(arg: String) -> Result<Vec<String>, Error> {
1818
}
1919
}
2020

21+
pub fn arg_expand_all(at_args: &[String]) -> Vec<String> {
22+
let mut args = Vec::new();
23+
for arg in at_args {
24+
match arg_expand(arg.clone()) {
25+
Ok(arg) => args.extend(arg),
26+
Err(err) => rustc_session::early_error(
27+
rustc_session::config::ErrorOutputType::default(),
28+
&format!("Failed to load argument file: {}", err),
29+
),
30+
}
31+
}
32+
args
33+
}
34+
2135
#[derive(Debug)]
2236
pub enum Error {
2337
Utf8Error(Option<String>),

compiler/rustc_driver/src/lib.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ use std::process::{self, Command, Stdio};
5555
use std::str;
5656
use std::time::Instant;
5757

58-
mod args;
58+
pub mod args;
5959
pub mod pretty;
6060

6161
/// Exit status code used for successful compilation and help output.
@@ -188,16 +188,8 @@ fn run_compiler(
188188
Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>,
189189
>,
190190
) -> interface::Result<()> {
191-
let mut args = Vec::new();
192-
for arg in at_args {
193-
match args::arg_expand(arg.clone()) {
194-
Ok(arg) => args.extend(arg),
195-
Err(err) => early_error(
196-
ErrorOutputType::default(),
197-
&format!("Failed to load argument file: {}", err),
198-
),
199-
}
200-
}
191+
let args = args::arg_expand_all(at_args);
192+
201193
let diagnostic_output = emitter.map_or(DiagnosticOutput::Default, DiagnosticOutput::Raw);
202194
let matches = match handle_options(&args) {
203195
Some(matches) => matches,

src/doc/rustdoc/src/command-line-arguments.md

+7
Original file line numberDiff line numberDiff line change
@@ -422,3 +422,10 @@ $ rustdoc src/lib.rs --crate-version 1.3.37
422422
When `rustdoc` receives this flag, it will print an extra "Version (version)" into the sidebar of
423423
the crate root's docs. You can use this flag to differentiate between different versions of your
424424
library's documentation.
425+
426+
## `@path`: load command-line flags from a path
427+
428+
If you specify `@path` on the command-line, then it will open `path` and read
429+
command line options from it. These options are one per line; a blank line indicates
430+
an empty option. The file can use Unix or Windows style line endings, and must be
431+
encoded as UTF-8.

src/librustdoc/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -432,13 +432,16 @@ fn usage(argv0: &str) {
432432
(option.apply)(&mut options);
433433
}
434434
println!("{}", options.usage(&format!("{} [options] <input>", argv0)));
435+
println!(" @path Read newline separated options from `path`\n");
435436
println!("More information available at https://doc.rust-lang.org/rustdoc/what-is-rustdoc.html")
436437
}
437438

438439
/// A result type used by several functions under `main()`.
439440
type MainResult = Result<(), ErrorReported>;
440441

441-
fn main_args(args: &[String]) -> MainResult {
442+
fn main_args(at_args: &[String]) -> MainResult {
443+
let args = rustc_driver::args::arg_expand_all(at_args);
444+
442445
let mut options = getopts::Options::new();
443446
for option in opts() {
444447
(option.apply)(&mut options);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--cfg
2+
unbroken�
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Check to see if we can get parameters from an @argsfile file
2+
//
3+
// compile-flags: --cfg cmdline_set @{{src-base}}/commandline-argfile-badutf8.args
4+
5+
#[cfg(not(cmdline_set))]
6+
compile_error!("cmdline_set not set");
7+
8+
#[cfg(not(unbroken))]
9+
compile_error!("unbroken not set");
10+
11+
fn main() {
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: Failed to load argument file: Utf8 error in $DIR/commandline-argfile-badutf8.args
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Check to see if we can get parameters from an @argsfile file
2+
//
3+
// ignore-tidy-linelength
4+
// normalize-stderr-test: "os error \d+" -> "os error $$ERR"
5+
// normalize-stderr-test: "commandline-argfile-missing.args:[^(]*" -> "commandline-argfile-missing.args: $$FILE_MISSING "
6+
// compile-flags: --cfg cmdline_set @{{src-base}}/commandline-argfile-missing.args
7+
8+
#[cfg(not(cmdline_set))]
9+
compile_error!("cmdline_set not set");
10+
11+
#[cfg(not(unbroken))]
12+
compile_error!("unbroken not set");
13+
14+
fn main() {
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: Failed to load argument file: IO Error: $DIR/commandline-argfile-missing.args: $FILE_MISSING (os error $ERR)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--cfg
2+
unbroken
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Check to see if we can get parameters from an @argsfile file
2+
//
3+
// check-pass
4+
// compile-flags: --cfg cmdline_set @{{src-base}}/commandline-argfile.args
5+
6+
#[cfg(not(cmdline_set))]
7+
compile_error!("cmdline_set not set");
8+
9+
#[cfg(not(unbroken))]
10+
compile_error!("unbroken not set");
11+
12+
fn main() {
13+
}

0 commit comments

Comments
 (0)