Skip to content

Check if out-dir exists before compiling / closes #12865 #13110

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

Closed
Closed
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
33 changes: 33 additions & 0 deletions src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,41 @@ pub fn run_compiler(args: &[~str]) {

let sopts = d::build_session_options(matches);
let sess = d::build_session(sopts, input_file_path);
// get the output dir, if there is one
// make sure that the directory structure
// exists, e.g. if -out-dir is ./foo/bar/baz, each needs to exist
// otherwise error out early
let odir = matches.opt_str("out-dir").map(|o| Path::new(o));
match odir {
Some(ref odir) => {
if !odir.exists() {
d::early_error(format!("output directory {} does not exist",
odir.display()))
}
// if it exists, is it a file?
if !odir.is_dir() {
d::early_error(format!("specified output directory {} is not a directory",
odir.display()))
}
}
None => {/* continue */ }
}
let ofile = matches.opt_str("o").map(|o| Path::new(o));
match ofile {
Some(ref ofile) => {
// does the directory for -o exist?
if !ofile.dir_path().exists() {
d::early_error(format!("specified output location's directory {} does not exist",
ofile.display()))
}
// does a file exist at -o and is it a directory?
if ofile.is_dir() {
d::early_error(format!("specified output location {} is a directory",
ofile.display()))
}
}
None => {/* continue */ }
}
let cfg = d::build_configuration(&sess);
let pretty = matches.opt_default("pretty", "normal").map(|a| {
d::parse_pretty(&sess, a)
Expand Down
5 changes: 5 additions & 0 deletions src/test/run-make/rustc-outfile-error/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-include ../tools.mk

all:
$(RUSTCNOARGS) foo.rs -o ./baz/bar.out 2>&1 | grep "error: specified output location's directory baz/bar.out does not exist"
$(RUSTCNOARGS) foo.rs -o . 2>&1| grep "error: specified output location . is a directory"
11 changes: 11 additions & 0 deletions src/test/run-make/rustc-outfile-error/foo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

pub fn foo() {}
5 changes: 5 additions & 0 deletions src/test/run-make/rustc-output-dir-error/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-include ../tools.mk

all:
$(RUSTCNOARGS) foo.rs --out-dir ./baz/bar 2>&1 | grep "error: output directory baz/bar does not exist"
$(RUSTCNOARGS) foo.rs --out-dir ./foo.rs 2>&1 | grep "error: specified output directory foo.rs is not a directory"
11 changes: 11 additions & 0 deletions src/test/run-make/rustc-output-dir-error/foo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

pub fn foo() {}
1 change: 1 addition & 0 deletions src/test/run-make/tools.mk
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export LD_LIBRARY_PATH:=$(TMPDIR):$(LD_LIBRARY_PATH)
export DYLD_LIBRARY_PATH:=$(TMPDIR):$(DYLD_LIBRARY_PATH)

RUSTCNOARGS := $(RUSTC)
RUSTC := $(RUSTC) --out-dir $(TMPDIR) -L $(TMPDIR)
CC := $(CC) -L $(TMPDIR)

Expand Down