Skip to content

Commit 4191853

Browse files
author
Tor Hovland
committed
Added the --temps-dir option.
1 parent 28e2b29 commit 4191853

File tree

7 files changed

+42
-3
lines changed

7 files changed

+42
-3
lines changed

compiler/rustc_driver/src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,15 @@ fn run_compiler(
217217

218218
let cfg = interface::parse_cfgspecs(matches.opt_strs("cfg"));
219219
let (odir, ofile) = make_output(&matches);
220+
let temps_dir = make_temps_dir(&matches);
220221
let mut config = interface::Config {
221222
opts: sopts,
222223
crate_cfg: cfg,
223224
input: Input::File(PathBuf::new()),
224225
input_path: None,
225226
output_file: ofile,
226227
output_dir: odir,
228+
temps_dir,
227229
file_loader,
228230
diagnostic_output,
229231
stderr: None,
@@ -269,6 +271,7 @@ fn run_compiler(
269271
None,
270272
&compiler.output_dir(),
271273
&compiler.output_file(),
274+
&compiler.temps_dir(),
272275
);
273276

274277
if should_stop == Compilation::Stop {
@@ -297,6 +300,7 @@ fn run_compiler(
297300
Some(compiler.input()),
298301
compiler.output_dir(),
299302
compiler.output_file(),
303+
compiler.temps_dir(),
300304
)
301305
.and_then(|| {
302306
RustcDefaultCalls::list_metadata(
@@ -462,6 +466,11 @@ fn make_output(matches: &getopts::Matches) -> (Option<PathBuf>, Option<PathBuf>)
462466
(odir, ofile)
463467
}
464468

469+
// Extract temporary directory from matches.
470+
fn make_temps_dir(matches: &getopts::Matches) -> Option<PathBuf> {
471+
matches.opt_str("temps-dir").map(|o| PathBuf::from(&o))
472+
}
473+
465474
// Extract input (string or file and optional path) from matches.
466475
fn make_input(
467476
error_format: ErrorOutputType,
@@ -651,6 +660,7 @@ impl RustcDefaultCalls {
651660
input: Option<&Input>,
652661
odir: &Option<PathBuf>,
653662
ofile: &Option<PathBuf>,
663+
temps_dir: &Option<PathBuf>,
654664
) -> Compilation {
655665
use rustc_session::config::PrintRequest::*;
656666
// PrintRequest::NativeStaticLibs is special - printed during linking
@@ -692,7 +702,7 @@ impl RustcDefaultCalls {
692702
});
693703
let attrs = attrs.as_ref().unwrap();
694704
let t_outputs = rustc_interface::util::build_output_filenames(
695-
input, odir, ofile, attrs, sess,
705+
input, odir, ofile, temps_dir, attrs, sess,
696706
);
697707
let id = rustc_session::output::find_crate_name(sess, attrs, input);
698708
if *req == PrintRequest::CrateName {

compiler/rustc_interface/src/interface.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub struct Compiler {
3535
pub(crate) input_path: Option<PathBuf>,
3636
pub(crate) output_dir: Option<PathBuf>,
3737
pub(crate) output_file: Option<PathBuf>,
38+
pub(crate) temps_dir: Option<PathBuf>,
3839
pub(crate) register_lints: Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>>,
3940
pub(crate) override_queries:
4041
Option<fn(&Session, &mut ty::query::Providers, &mut ty::query::Providers)>,
@@ -56,6 +57,9 @@ impl Compiler {
5657
pub fn output_file(&self) -> &Option<PathBuf> {
5758
&self.output_file
5859
}
60+
pub fn temps_dir(&self) -> &Option<PathBuf> {
61+
&self.temps_dir
62+
}
5963
pub fn register_lints(&self) -> &Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>> {
6064
&self.register_lints
6165
}
@@ -68,6 +72,7 @@ impl Compiler {
6872
&self.input,
6973
&self.output_dir,
7074
&self.output_file,
75+
&self.temps_dir,
7176
&attrs,
7277
&sess,
7378
)
@@ -134,6 +139,7 @@ pub struct Config {
134139
pub input_path: Option<PathBuf>,
135140
pub output_dir: Option<PathBuf>,
136141
pub output_file: Option<PathBuf>,
142+
pub temps_dir: Option<PathBuf>,
137143
pub file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
138144
pub diagnostic_output: DiagnosticOutput,
139145

@@ -195,6 +201,7 @@ pub fn create_compiler_and_run<R>(config: Config, f: impl FnOnce(&Compiler) -> R
195201
input_path: config.input_path,
196202
output_dir: config.output_dir,
197203
output_file: config.output_file,
204+
temps_dir: config.temps_dir,
198205
register_lints: config.register_lints,
199206
override_queries: config.override_queries,
200207
};

compiler/rustc_interface/src/passes.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ pub fn prepare_outputs(
660660
&compiler.input,
661661
&compiler.output_dir,
662662
&compiler.output_file,
663+
&compiler.temps_dir,
663664
&krate.attrs,
664665
sess,
665666
);
@@ -702,6 +703,12 @@ pub fn prepare_outputs(
702703
return Err(ErrorReported);
703704
}
704705
}
706+
if let Some(ref dir) = compiler.temps_dir {
707+
if fs::create_dir_all(dir).is_err() {
708+
sess.err("failed to find or create the directory specified by `--temps-dir`");
709+
return Err(ErrorReported);
710+
}
711+
}
705712
}
706713

707714
Ok(outputs)

compiler/rustc_interface/src/util.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ pub fn build_output_filenames(
645645
input: &Input,
646646
odir: &Option<PathBuf>,
647647
ofile: &Option<PathBuf>,
648+
temps_dir: &Option<PathBuf>,
648649
attrs: &[ast::Attribute],
649650
sess: &Session,
650651
) -> OutputFilenames {
@@ -667,6 +668,7 @@ pub fn build_output_filenames(
667668
dirpath,
668669
stem,
669670
None,
671+
temps_dir.clone(),
670672
sess.opts.cg.extra_filename.clone(),
671673
sess.opts.output_types.clone(),
672674
)
@@ -695,6 +697,7 @@ pub fn build_output_filenames(
695697
out_file.parent().unwrap_or_else(|| Path::new("")).to_path_buf(),
696698
out_file.file_stem().unwrap_or_default().to_str().unwrap().to_string(),
697699
ofile,
700+
temps_dir.clone(),
698701
sess.opts.cg.extra_filename.clone(),
699702
sess.opts.output_types.clone(),
700703
)

compiler/rustc_session/src/config.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ pub struct OutputFilenames {
563563
pub out_directory: PathBuf,
564564
filestem: String,
565565
pub single_output_file: Option<PathBuf>,
566+
pub temps_directory: Option<PathBuf>,
566567
pub outputs: OutputTypes,
567568
}
568569

@@ -577,12 +578,14 @@ impl OutputFilenames {
577578
out_directory: PathBuf,
578579
out_filestem: String,
579580
single_output_file: Option<PathBuf>,
581+
temps_directory: Option<PathBuf>,
580582
extra: String,
581583
outputs: OutputTypes,
582584
) -> Self {
583585
OutputFilenames {
584586
out_directory,
585587
single_output_file,
588+
temps_directory,
586589
outputs,
587590
filestem: format!("{}{}", out_filestem, extra),
588591
}
@@ -628,11 +631,17 @@ impl OutputFilenames {
628631
extension.push_str(ext);
629632
}
630633

631-
self.with_extension(&extension)
634+
let temps_directory = self.temps_directory.as_ref().unwrap_or(&self.out_directory);
635+
636+
self.with_directory_and_extension(&temps_directory, &extension)
632637
}
633638

634639
pub fn with_extension(&self, extension: &str) -> PathBuf {
635-
let mut path = self.out_directory.join(&self.filestem);
640+
self.with_directory_and_extension(&self.out_directory, extension)
641+
}
642+
643+
fn with_directory_and_extension(&self, directory: &PathBuf, extension: &str) -> PathBuf {
644+
let mut path = directory.join(&self.filestem);
636645
path.set_extension(extension);
637646
path
638647
}
@@ -1071,6 +1080,7 @@ pub fn rustc_short_optgroups() -> Vec<RustcOptGroup> {
10711080
in <dir>",
10721081
"DIR",
10731082
),
1083+
opt::opt_s("", "temps-dir", "Write temporary output files to <dir>", "DIR"),
10741084
opt::opt_s(
10751085
"",
10761086
"explain",

src/librustdoc/core.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ crate fn create_config(
258258
input_path: cpath,
259259
output_file: None,
260260
output_dir: None,
261+
temps_dir: None,
261262
file_loader: None,
262263
diagnostic_output: DiagnosticOutput::Default,
263264
stderr: None,

src/librustdoc/doctest.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ crate fn run(options: Options) -> Result<(), ErrorReported> {
9292
input_path: None,
9393
output_file: None,
9494
output_dir: None,
95+
temps_dir: None,
9596
file_loader: None,
9697
diagnostic_output: DiagnosticOutput::Default,
9798
stderr: None,

0 commit comments

Comments
 (0)