Skip to content

Commit 5d1e09f

Browse files
author
Tor Hovland
committed
Added the --temps-dir option.
1 parent 18bc4be commit 5d1e09f

File tree

7 files changed

+49
-4
lines changed

7 files changed

+49
-4
lines changed

compiler/rustc_driver/src/lib.rs

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

216216
let cfg = interface::parse_cfgspecs(matches.opt_strs("cfg"));
217217
let (odir, ofile) = make_output(&matches);
218+
let temps_dir = make_temps_dir(&matches);
218219
let mut config = interface::Config {
219220
opts: sopts,
220221
crate_cfg: cfg,
221222
input: Input::File(PathBuf::new()),
222223
input_path: None,
223224
output_file: ofile,
224225
output_dir: odir,
226+
temps_dir,
225227
file_loader,
226228
diagnostic_output,
227229
stderr: None,
@@ -267,6 +269,7 @@ fn run_compiler(
267269
None,
268270
compiler.output_dir(),
269271
compiler.output_file(),
272+
compiler.temps_dir(),
270273
);
271274

272275
if should_stop == Compilation::Stop {
@@ -295,6 +298,7 @@ fn run_compiler(
295298
Some(compiler.input()),
296299
compiler.output_dir(),
297300
compiler.output_file(),
301+
compiler.temps_dir(),
298302
)
299303
.and_then(|| {
300304
RustcDefaultCalls::list_metadata(
@@ -454,6 +458,11 @@ fn make_output(matches: &getopts::Matches) -> (Option<PathBuf>, Option<PathBuf>)
454458
(odir, ofile)
455459
}
456460

461+
// Extract temporary directory from matches.
462+
fn make_temps_dir(matches: &getopts::Matches) -> Option<PathBuf> {
463+
matches.opt_str("temps-dir").map(|o| PathBuf::from(&o))
464+
}
465+
457466
// Extract input (string or file and optional path) from matches.
458467
fn make_input(
459468
error_format: ErrorOutputType,
@@ -647,6 +656,7 @@ impl RustcDefaultCalls {
647656
input: Option<&Input>,
648657
odir: &Option<PathBuf>,
649658
ofile: &Option<PathBuf>,
659+
temps_dir: &Option<PathBuf>,
650660
) -> Compilation {
651661
use rustc_session::config::PrintRequest::*;
652662
// PrintRequest::NativeStaticLibs is special - printed during linking
@@ -685,7 +695,7 @@ impl RustcDefaultCalls {
685695
});
686696
let attrs = attrs.as_ref().unwrap();
687697
let t_outputs = rustc_interface::util::build_output_filenames(
688-
input, odir, ofile, attrs, sess,
698+
input, odir, ofile, temps_dir, attrs, sess,
689699
);
690700
let id = rustc_session::output::find_crate_name(sess, attrs, input);
691701
if *req == PrintRequest::CrateName {

compiler/rustc_interface/src/interface.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub struct Compiler {
3636
pub(crate) input_path: Option<PathBuf>,
3737
pub(crate) output_dir: Option<PathBuf>,
3838
pub(crate) output_file: Option<PathBuf>,
39+
pub(crate) temps_dir: Option<PathBuf>,
3940
pub(crate) register_lints: Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>>,
4041
pub(crate) override_queries:
4142
Option<fn(&Session, &mut ty::query::Providers, &mut ty::query::ExternProviders)>,
@@ -57,6 +58,9 @@ impl Compiler {
5758
pub fn output_file(&self) -> &Option<PathBuf> {
5859
&self.output_file
5960
}
61+
pub fn temps_dir(&self) -> &Option<PathBuf> {
62+
&self.temps_dir
63+
}
6064
pub fn register_lints(&self) -> &Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>> {
6165
&self.register_lints
6266
}
@@ -65,7 +69,14 @@ impl Compiler {
6569
sess: &Session,
6670
attrs: &[ast::Attribute],
6771
) -> OutputFilenames {
68-
util::build_output_filenames(&self.input, &self.output_dir, &self.output_file, attrs, sess)
72+
util::build_output_filenames(
73+
&self.input,
74+
&self.output_dir,
75+
&self.output_file,
76+
&self.temps_dir,
77+
attrs,
78+
sess,
79+
)
6980
}
7081
}
7182

@@ -132,6 +143,7 @@ pub struct Config {
132143
pub input_path: Option<PathBuf>,
133144
pub output_dir: Option<PathBuf>,
134145
pub output_file: Option<PathBuf>,
146+
pub temps_dir: Option<PathBuf>,
135147
pub file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
136148
pub diagnostic_output: DiagnosticOutput,
137149

@@ -193,6 +205,7 @@ pub fn create_compiler_and_run<R>(config: Config, f: impl FnOnce(&Compiler) -> R
193205
input_path: config.input_path,
194206
output_dir: config.output_dir,
195207
output_file: config.output_file,
208+
temps_dir: config.temps_dir,
196209
register_lints: config.register_lints,
197210
override_queries: config.override_queries,
198211
};

compiler/rustc_interface/src/passes.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,7 @@ pub fn prepare_outputs(
692692
&compiler.input,
693693
&compiler.output_dir,
694694
&compiler.output_file,
695+
&compiler.temps_dir,
695696
&krate.attrs,
696697
sess,
697698
);
@@ -734,6 +735,12 @@ pub fn prepare_outputs(
734735
return Err(ErrorReported);
735736
}
736737
}
738+
if let Some(ref dir) = compiler.temps_dir {
739+
if fs::create_dir_all(dir).is_err() {
740+
sess.err("failed to find or create the directory specified by `--temps-dir`");
741+
return Err(ErrorReported);
742+
}
743+
}
737744
}
738745

739746
Ok(outputs)

compiler/rustc_interface/src/util.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ pub fn build_output_filenames(
604604
input: &Input,
605605
odir: &Option<PathBuf>,
606606
ofile: &Option<PathBuf>,
607+
temps_dir: &Option<PathBuf>,
607608
attrs: &[ast::Attribute],
608609
sess: &Session,
609610
) -> OutputFilenames {
@@ -626,6 +627,7 @@ pub fn build_output_filenames(
626627
dirpath,
627628
stem,
628629
None,
630+
temps_dir.clone(),
629631
sess.opts.cg.extra_filename.clone(),
630632
sess.opts.output_types.clone(),
631633
)
@@ -654,6 +656,7 @@ pub fn build_output_filenames(
654656
out_file.parent().unwrap_or_else(|| Path::new("")).to_path_buf(),
655657
out_file.file_stem().unwrap_or_default().to_str().unwrap().to_string(),
656658
ofile,
659+
temps_dir.clone(),
657660
sess.opts.cg.extra_filename.clone(),
658661
sess.opts.output_types.clone(),
659662
)

compiler/rustc_session/src/config.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ pub struct OutputFilenames {
578578
pub out_directory: PathBuf,
579579
filestem: String,
580580
pub single_output_file: Option<PathBuf>,
581+
pub temps_directory: Option<PathBuf>,
581582
pub outputs: OutputTypes,
582583
}
583584

@@ -592,12 +593,14 @@ impl OutputFilenames {
592593
out_directory: PathBuf,
593594
out_filestem: String,
594595
single_output_file: Option<PathBuf>,
596+
temps_directory: Option<PathBuf>,
595597
extra: String,
596598
outputs: OutputTypes,
597599
) -> Self {
598600
OutputFilenames {
599601
out_directory,
600602
single_output_file,
603+
temps_directory,
601604
outputs,
602605
filestem: format!("{}{}", out_filestem, extra),
603606
}
@@ -643,11 +646,17 @@ impl OutputFilenames {
643646
extension.push_str(ext);
644647
}
645648

646-
self.with_extension(&extension)
649+
let temps_directory = self.temps_directory.as_ref().unwrap_or(&self.out_directory);
650+
651+
self.with_directory_and_extension(&temps_directory, &extension)
647652
}
648653

649654
pub fn with_extension(&self, extension: &str) -> PathBuf {
650-
let mut path = self.out_directory.join(&self.filestem);
655+
self.with_directory_and_extension(&self.out_directory, extension)
656+
}
657+
658+
fn with_directory_and_extension(&self, directory: &PathBuf, extension: &str) -> PathBuf {
659+
let mut path = directory.join(&self.filestem);
651660
path.set_extension(extension);
652661
path
653662
}
@@ -1094,6 +1103,7 @@ pub fn rustc_short_optgroups() -> Vec<RustcOptGroup> {
10941103
in <dir>",
10951104
"DIR",
10961105
),
1106+
opt::opt_s("", "temps-dir", "Write temporary output files to <dir>", "DIR"),
10971107
opt::opt_s(
10981108
"",
10991109
"explain",

src/librustdoc/core.rs

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

src/librustdoc/doctest.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ crate fn run(options: Options) -> Result<(), ErrorReported> {
9494
input_path: None,
9595
output_file: None,
9696
output_dir: None,
97+
temps_dir: None,
9798
file_loader: None,
9899
diagnostic_output: DiagnosticOutput::Default,
99100
stderr: None,

0 commit comments

Comments
 (0)