Added the --temps-dir option.

This commit is contained in:
Tor Hovland 2021-11-02 22:41:34 +01:00
parent 18bc4bee97
commit 5d1e09f44a
7 changed files with 49 additions and 4 deletions

View file

@ -215,6 +215,7 @@ fn run_compiler(
let cfg = interface::parse_cfgspecs(matches.opt_strs("cfg")); let cfg = interface::parse_cfgspecs(matches.opt_strs("cfg"));
let (odir, ofile) = make_output(&matches); let (odir, ofile) = make_output(&matches);
let temps_dir = make_temps_dir(&matches);
let mut config = interface::Config { let mut config = interface::Config {
opts: sopts, opts: sopts,
crate_cfg: cfg, crate_cfg: cfg,
@ -222,6 +223,7 @@ fn run_compiler(
input_path: None, input_path: None,
output_file: ofile, output_file: ofile,
output_dir: odir, output_dir: odir,
temps_dir,
file_loader, file_loader,
diagnostic_output, diagnostic_output,
stderr: None, stderr: None,
@ -267,6 +269,7 @@ fn run_compiler(
None, None,
compiler.output_dir(), compiler.output_dir(),
compiler.output_file(), compiler.output_file(),
compiler.temps_dir(),
); );
if should_stop == Compilation::Stop { if should_stop == Compilation::Stop {
@ -295,6 +298,7 @@ fn run_compiler(
Some(compiler.input()), Some(compiler.input()),
compiler.output_dir(), compiler.output_dir(),
compiler.output_file(), compiler.output_file(),
compiler.temps_dir(),
) )
.and_then(|| { .and_then(|| {
RustcDefaultCalls::list_metadata( RustcDefaultCalls::list_metadata(
@ -454,6 +458,11 @@ fn make_output(matches: &getopts::Matches) -> (Option<PathBuf>, Option<PathBuf>)
(odir, ofile) (odir, ofile)
} }
// Extract temporary directory from matches.
fn make_temps_dir(matches: &getopts::Matches) -> Option<PathBuf> {
matches.opt_str("temps-dir").map(|o| PathBuf::from(&o))
}
// Extract input (string or file and optional path) from matches. // Extract input (string or file and optional path) from matches.
fn make_input( fn make_input(
error_format: ErrorOutputType, error_format: ErrorOutputType,
@ -647,6 +656,7 @@ impl RustcDefaultCalls {
input: Option<&Input>, input: Option<&Input>,
odir: &Option<PathBuf>, odir: &Option<PathBuf>,
ofile: &Option<PathBuf>, ofile: &Option<PathBuf>,
temps_dir: &Option<PathBuf>,
) -> Compilation { ) -> Compilation {
use rustc_session::config::PrintRequest::*; use rustc_session::config::PrintRequest::*;
// PrintRequest::NativeStaticLibs is special - printed during linking // PrintRequest::NativeStaticLibs is special - printed during linking
@ -685,7 +695,7 @@ impl RustcDefaultCalls {
}); });
let attrs = attrs.as_ref().unwrap(); let attrs = attrs.as_ref().unwrap();
let t_outputs = rustc_interface::util::build_output_filenames( let t_outputs = rustc_interface::util::build_output_filenames(
input, odir, ofile, attrs, sess, input, odir, ofile, temps_dir, attrs, sess,
); );
let id = rustc_session::output::find_crate_name(sess, attrs, input); let id = rustc_session::output::find_crate_name(sess, attrs, input);
if *req == PrintRequest::CrateName { if *req == PrintRequest::CrateName {

View file

@ -36,6 +36,7 @@ pub struct Compiler {
pub(crate) input_path: Option<PathBuf>, pub(crate) input_path: Option<PathBuf>,
pub(crate) output_dir: Option<PathBuf>, pub(crate) output_dir: Option<PathBuf>,
pub(crate) output_file: Option<PathBuf>, pub(crate) output_file: Option<PathBuf>,
pub(crate) temps_dir: Option<PathBuf>,
pub(crate) register_lints: Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>>, pub(crate) register_lints: Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>>,
pub(crate) override_queries: pub(crate) override_queries:
Option<fn(&Session, &mut ty::query::Providers, &mut ty::query::ExternProviders)>, Option<fn(&Session, &mut ty::query::Providers, &mut ty::query::ExternProviders)>,
@ -57,6 +58,9 @@ impl Compiler {
pub fn output_file(&self) -> &Option<PathBuf> { pub fn output_file(&self) -> &Option<PathBuf> {
&self.output_file &self.output_file
} }
pub fn temps_dir(&self) -> &Option<PathBuf> {
&self.temps_dir
}
pub fn register_lints(&self) -> &Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>> { pub fn register_lints(&self) -> &Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>> {
&self.register_lints &self.register_lints
} }
@ -65,7 +69,14 @@ impl Compiler {
sess: &Session, sess: &Session,
attrs: &[ast::Attribute], attrs: &[ast::Attribute],
) -> OutputFilenames { ) -> OutputFilenames {
util::build_output_filenames(&self.input, &self.output_dir, &self.output_file, attrs, sess) util::build_output_filenames(
&self.input,
&self.output_dir,
&self.output_file,
&self.temps_dir,
attrs,
sess,
)
} }
} }
@ -132,6 +143,7 @@ pub struct Config {
pub input_path: Option<PathBuf>, pub input_path: Option<PathBuf>,
pub output_dir: Option<PathBuf>, pub output_dir: Option<PathBuf>,
pub output_file: Option<PathBuf>, pub output_file: Option<PathBuf>,
pub temps_dir: Option<PathBuf>,
pub file_loader: Option<Box<dyn FileLoader + Send + Sync>>, pub file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
pub diagnostic_output: DiagnosticOutput, pub diagnostic_output: DiagnosticOutput,
@ -193,6 +205,7 @@ pub fn create_compiler_and_run<R>(config: Config, f: impl FnOnce(&Compiler) -> R
input_path: config.input_path, input_path: config.input_path,
output_dir: config.output_dir, output_dir: config.output_dir,
output_file: config.output_file, output_file: config.output_file,
temps_dir: config.temps_dir,
register_lints: config.register_lints, register_lints: config.register_lints,
override_queries: config.override_queries, override_queries: config.override_queries,
}; };

View file

@ -692,6 +692,7 @@ pub fn prepare_outputs(
&compiler.input, &compiler.input,
&compiler.output_dir, &compiler.output_dir,
&compiler.output_file, &compiler.output_file,
&compiler.temps_dir,
&krate.attrs, &krate.attrs,
sess, sess,
); );
@ -734,6 +735,12 @@ pub fn prepare_outputs(
return Err(ErrorReported); return Err(ErrorReported);
} }
} }
if let Some(ref dir) = compiler.temps_dir {
if fs::create_dir_all(dir).is_err() {
sess.err("failed to find or create the directory specified by `--temps-dir`");
return Err(ErrorReported);
}
}
} }
Ok(outputs) Ok(outputs)

View file

@ -604,6 +604,7 @@ pub fn build_output_filenames(
input: &Input, input: &Input,
odir: &Option<PathBuf>, odir: &Option<PathBuf>,
ofile: &Option<PathBuf>, ofile: &Option<PathBuf>,
temps_dir: &Option<PathBuf>,
attrs: &[ast::Attribute], attrs: &[ast::Attribute],
sess: &Session, sess: &Session,
) -> OutputFilenames { ) -> OutputFilenames {
@ -626,6 +627,7 @@ pub fn build_output_filenames(
dirpath, dirpath,
stem, stem,
None, None,
temps_dir.clone(),
sess.opts.cg.extra_filename.clone(), sess.opts.cg.extra_filename.clone(),
sess.opts.output_types.clone(), sess.opts.output_types.clone(),
) )
@ -654,6 +656,7 @@ pub fn build_output_filenames(
out_file.parent().unwrap_or_else(|| Path::new("")).to_path_buf(), out_file.parent().unwrap_or_else(|| Path::new("")).to_path_buf(),
out_file.file_stem().unwrap_or_default().to_str().unwrap().to_string(), out_file.file_stem().unwrap_or_default().to_str().unwrap().to_string(),
ofile, ofile,
temps_dir.clone(),
sess.opts.cg.extra_filename.clone(), sess.opts.cg.extra_filename.clone(),
sess.opts.output_types.clone(), sess.opts.output_types.clone(),
) )

View file

@ -578,6 +578,7 @@ pub struct OutputFilenames {
pub out_directory: PathBuf, pub out_directory: PathBuf,
filestem: String, filestem: String,
pub single_output_file: Option<PathBuf>, pub single_output_file: Option<PathBuf>,
pub temps_directory: Option<PathBuf>,
pub outputs: OutputTypes, pub outputs: OutputTypes,
} }
@ -592,12 +593,14 @@ impl OutputFilenames {
out_directory: PathBuf, out_directory: PathBuf,
out_filestem: String, out_filestem: String,
single_output_file: Option<PathBuf>, single_output_file: Option<PathBuf>,
temps_directory: Option<PathBuf>,
extra: String, extra: String,
outputs: OutputTypes, outputs: OutputTypes,
) -> Self { ) -> Self {
OutputFilenames { OutputFilenames {
out_directory, out_directory,
single_output_file, single_output_file,
temps_directory,
outputs, outputs,
filestem: format!("{}{}", out_filestem, extra), filestem: format!("{}{}", out_filestem, extra),
} }
@ -643,11 +646,17 @@ impl OutputFilenames {
extension.push_str(ext); extension.push_str(ext);
} }
self.with_extension(&extension) let temps_directory = self.temps_directory.as_ref().unwrap_or(&self.out_directory);
self.with_directory_and_extension(&temps_directory, &extension)
} }
pub fn with_extension(&self, extension: &str) -> PathBuf { pub fn with_extension(&self, extension: &str) -> PathBuf {
let mut path = self.out_directory.join(&self.filestem); self.with_directory_and_extension(&self.out_directory, extension)
}
fn with_directory_and_extension(&self, directory: &PathBuf, extension: &str) -> PathBuf {
let mut path = directory.join(&self.filestem);
path.set_extension(extension); path.set_extension(extension);
path path
} }
@ -1094,6 +1103,7 @@ pub fn rustc_short_optgroups() -> Vec<RustcOptGroup> {
in <dir>", in <dir>",
"DIR", "DIR",
), ),
opt::opt_s("", "temps-dir", "Write temporary output files to <dir>", "DIR"),
opt::opt_s( opt::opt_s(
"", "",
"explain", "explain",

View file

@ -259,6 +259,7 @@ crate fn create_config(
input_path: cpath, input_path: cpath,
output_file: None, output_file: None,
output_dir: None, output_dir: None,
temps_dir: None,
file_loader: None, file_loader: None,
diagnostic_output: DiagnosticOutput::Default, diagnostic_output: DiagnosticOutput::Default,
stderr: None, stderr: None,

View file

@ -94,6 +94,7 @@ crate fn run(options: Options) -> Result<(), ErrorReported> {
input_path: None, input_path: None,
output_file: None, output_file: None,
output_dir: None, output_dir: None,
temps_dir: None,
file_loader: None, file_loader: None,
diagnostic_output: DiagnosticOutput::Default, diagnostic_output: DiagnosticOutput::Default,
stderr: None, stderr: None,