diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs index 6aff15301048d..83e0cae93179e 100644 --- a/src/librustc/lib.rs +++ b/src/librustc/lib.rs @@ -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) diff --git a/src/test/run-make/rustc-outfile-error/Makefile b/src/test/run-make/rustc-outfile-error/Makefile new file mode 100644 index 0000000000000..4e620ae06f9b1 --- /dev/null +++ b/src/test/run-make/rustc-outfile-error/Makefile @@ -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" diff --git a/src/test/run-make/rustc-outfile-error/foo.rs b/src/test/run-make/rustc-outfile-error/foo.rs new file mode 100644 index 0000000000000..2661b1f4eb49b --- /dev/null +++ b/src/test/run-make/rustc-outfile-error/foo.rs @@ -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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +pub fn foo() {} diff --git a/src/test/run-make/rustc-output-dir-error/Makefile b/src/test/run-make/rustc-output-dir-error/Makefile new file mode 100644 index 0000000000000..0f069ccda3e88 --- /dev/null +++ b/src/test/run-make/rustc-output-dir-error/Makefile @@ -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" diff --git a/src/test/run-make/rustc-output-dir-error/foo.rs b/src/test/run-make/rustc-output-dir-error/foo.rs new file mode 100644 index 0000000000000..2661b1f4eb49b --- /dev/null +++ b/src/test/run-make/rustc-output-dir-error/foo.rs @@ -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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +pub fn foo() {} diff --git a/src/test/run-make/tools.mk b/src/test/run-make/tools.mk index 26e6e06c2ed8a..3d96c400444f0 100644 --- a/src/test/run-make/tools.mk +++ b/src/test/run-make/tools.mk @@ -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)