Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustc: implement argsfiles for command line #63175

Merged
merged 3 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/doc/rustc/src/command-line-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,10 @@ to customize the output:

Note that it is invalid to combine the `--json` argument with the `--color`
argument, and it is required to combine `--json` with `--error-format=json`.

## `@path`: load command-line flags from a path

If you specify `@path` on the command-line, then it will open `path` and read
command line options from it. These options are one per line; a blank line indicates
an empty option. The file can use Unix or Windows style line endings, and must be
encoded as UTF-8.
53 changes: 53 additions & 0 deletions src/librustc_driver/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::error;
use std::fmt;
use std::fs;
use std::io;
use std::str;
use std::sync::atomic::{AtomicBool, Ordering};

static USED_ARGSFILE_FEATURE: AtomicBool = AtomicBool::new(false);

pub fn used_unstable_argsfile() -> bool {
USED_ARGSFILE_FEATURE.load(Ordering::Relaxed)
}

pub fn arg_expand(arg: String) -> Result<Vec<String>, Error> {
if arg.starts_with("@") {
let path = &arg[1..];
let file = match fs::read_to_string(path) {
Ok(file) => {
USED_ARGSFILE_FEATURE.store(true, Ordering::Relaxed);
file
}
Err(ref err) if err.kind() == io::ErrorKind::InvalidData => {
return Err(Error::Utf8Error(Some(path.to_string())));
}
Err(err) => return Err(Error::IOError(path.to_string(), err)),
};
Ok(file.lines().map(ToString::to_string).collect())
} else {
Ok(vec![arg])
}
}

#[derive(Debug)]
pub enum Error {
Utf8Error(Option<String>),
IOError(String, io::Error),
}

impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Utf8Error(None) => write!(fmt, "Utf8 error"),
Error::Utf8Error(Some(path)) => write!(fmt, "Utf8 error in {}", path),
Error::IOError(path, err) => write!(fmt, "IO Error: {}: {}", path, err),
}
}
}

impl error::Error for Error {
fn description(&self) -> &'static str {
"argument error"
}
}
37 changes: 29 additions & 8 deletions src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ use syntax::symbol::sym;
use syntax_pos::{DUMMY_SP, MultiSpan, FileName};

pub mod pretty;
mod args;

/// Exit status code used for successful compilation and help output.
pub const EXIT_SUCCESS: i32 = 0;
Expand Down Expand Up @@ -139,14 +140,22 @@ impl Callbacks for TimePassesCallbacks {
// See comments on CompilerCalls below for details about the callbacks argument.
// The FileLoader provides a way to load files from sources other than the file system.
pub fn run_compiler(
args: &[String],
at_args: &[String],
callbacks: &mut (dyn Callbacks + Send),
file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
emitter: Option<Box<dyn Write + Send>>
) -> interface::Result<()> {
let mut args = Vec::new();
for arg in at_args {
match args::arg_expand(arg.clone()) {
Ok(arg) => args.extend(arg),
Err(err) => early_error(ErrorOutputType::default(),
&format!("Failed to load argument file: {}", err)),
}
}
let diagnostic_output = emitter.map(|emitter| DiagnosticOutput::Raw(emitter))
.unwrap_or(DiagnosticOutput::Default);
let matches = match handle_options(args) {
let matches = match handle_options(&args) {
Some(matches) => matches,
None => return Ok(()),
};
Expand Down Expand Up @@ -777,13 +786,19 @@ fn usage(verbose: bool, include_unstable_options: bool) {
} else {
"\n --help -v Print the full set of options rustc accepts"
};
println!("{}\nAdditional help:
let at_path = if verbose && nightly_options::is_nightly_build() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why only in the verbose case?

Copy link
Contributor Author

@jsgf jsgf Aug 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing the rationale for verbose-only --help options is ones that are likely to be only used when rustc is invoked by some other tool, vs more "common" ones. But I don't really know what the rationale one way or the other is.

Either way @path seemed like it was on the obscure end of the spectrum.

" @path Read newline separated options from `path`\n"
} else {
""
};
println!("{options}{at_path}\nAdditional help:
-C help Print codegen options
-W help \
Print 'lint' options and default settings{}{}\n",
options.usage(message),
nightly_help,
verbose_help);
Print 'lint' options and default settings{nightly}{verbose}\n",
options = options.usage(message),
at_path = at_path,
nightly = nightly_help,
verbose = verbose_help);
}

fn print_wall_help() {
Expand Down Expand Up @@ -1008,6 +1023,12 @@ pub fn handle_options(args: &[String]) -> Option<getopts::Matches> {
// (unstable option being used on stable)
nightly_options::check_nightly_options(&matches, &config::rustc_optgroups());

// Late check to see if @file was used without unstable options enabled
if crate::args::used_unstable_argsfile() && !nightly_options::is_unstable_enabled(&matches) {
early_error(ErrorOutputType::default(),
"@path is unstable - use -Z unstable-options to enable its use");
}

if matches.opt_present("h") || matches.opt_present("help") {
// Only show unstable options in --help if we accept unstable options.
usage(matches.opt_present("verbose"), nightly_options::is_unstable_enabled(&matches));
Expand Down Expand Up @@ -1188,7 +1209,7 @@ pub fn main() {
let result = report_ices_to_stderr_if_any(|| {
let args = env::args_os().enumerate()
.map(|(i, arg)| arg.into_string().unwrap_or_else(|arg| {
early_error(ErrorOutputType::default(),
early_error(ErrorOutputType::default(),
&format!("Argument {} is not valid Unicode: {:?}", i, arg))
}))
.collect::<Vec<_>>();
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/commandline-argfile-badutf8.args
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--cfg
unbroken�
13 changes: 13 additions & 0 deletions src/test/ui/commandline-argfile-badutf8.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Check to see if we can get parameters from an @argsfile file
//
// build-fail
// compile-flags: --cfg cmdline_set @{{src-base}}/commandline-argfile-badutf8.args

#[cfg(not(cmdline_set))]
compile_error!("cmdline_set not set");

#[cfg(not(unbroken))]
compile_error!("unbroken not set");

fn main() {
}
2 changes: 2 additions & 0 deletions src/test/ui/commandline-argfile-badutf8.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
error: Failed to load argument file: Utf8 error in $DIR/commandline-argfile-badutf8.args

16 changes: 16 additions & 0 deletions src/test/ui/commandline-argfile-missing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Check to see if we can get parameters from an @argsfile file
//
// ignore-tidy-linelength
// build-fail
// normalize-stderr-test: "os error \d+" -> "os error $$ERR"
// normalize-stderr-test: "commandline-argfile-missing.args:[^(]*" -> "commandline-argfile-missing.args: $$FILE_MISSING "
// compile-flags: --cfg cmdline_set @{{src-base}}/commandline-argfile-missing.args

#[cfg(not(cmdline_set))]
compile_error!("cmdline_set not set");

#[cfg(not(unbroken))]
compile_error!("unbroken not set");

fn main() {
}
2 changes: 2 additions & 0 deletions src/test/ui/commandline-argfile-missing.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
error: Failed to load argument file: IO Error: $DIR/commandline-argfile-missing.args: $FILE_MISSING (os error $ERR)

2 changes: 2 additions & 0 deletions src/test/ui/commandline-argfile.args
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--cfg
unbroken
13 changes: 13 additions & 0 deletions src/test/ui/commandline-argfile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Check to see if we can get parameters from an @argsfile file
//
// build-pass
// compile-flags: --cfg cmdline_set @{{src-base}}/commandline-argfile.args

#[cfg(not(cmdline_set))]
compile_error!("cmdline_set not set");

#[cfg(not(unbroken))]
compile_error!("unbroken not set");

fn main() {
}