From 79d85dab88536a66efdf550d687591b1bc53c022 Mon Sep 17 00:00:00 2001
From: varkor <github@varkor.com>
Date: Mon, 29 Jan 2018 16:31:14 +0000
Subject: [PATCH] Create a directory for --out-dir if it does not already exist
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Currently if `--out-dir` is set to a non-existent directory, the
compiler will throw unfriendly messages like `error: could not write
output to subdir/example.crate.allocator.rcgu.o: No such file or
directory`, which, while not completely unreadable, isn’t very
user-friendly either. This change creates the directory automatically
if it does not yet exist.
---
 src/librustc_driver/driver.rs | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs
index e97d83ed1ee..ed680feae0a 100644
--- a/src/librustc_driver/driver.rs
+++ b/src/librustc_driver/driver.rs
@@ -163,6 +163,13 @@ pub fn compile_input(trans: Box<TransCrate>,
             return Ok(())
         }
 
+        if let &Some(ref dir) = outdir {
+            if fs::create_dir_all(dir).is_err() {
+                sess.err("failed to find or create the directory specified by --out-dir");
+                return Err(CompileIncomplete::Stopped);
+            }
+        }
+
         let arenas = AllArenas::new();
 
         // Construct the HIR map