Make clang-cl accept .lib inputs (PR20065)

Patch by Ehsan Akhgari!

(Tiny tweak by me: renamed PathSegment to LibDir.)

Differential Revision: http://reviews.llvm.org/D4192

llvm-svn: 211189
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index dc07e64..34c48d4 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -21,6 +21,7 @@
 #include "clang/Driver/ToolChain.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringSet.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/Option/Arg.h"
@@ -33,6 +34,7 @@
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/PrettyStackTrace.h"
+#include "llvm/Support/Process.h"
 #include "llvm/Support/Program.h"
 #include "llvm/Support/raw_ostream.h"
 #include <map>
@@ -927,6 +929,35 @@
   }
 }
 
+/// \brief Check whether the file referenced by Value exists in the LIB
+/// environment variable.
+static bool ExistsInLibDir(StringRef Value) {
+  llvm::Optional<std::string> OptPath = llvm::sys::Process::GetEnv("LIB");
+  if (!OptPath.hasValue())
+    return false;
+
+#ifdef LLVM_ON_WIN32
+  const StringRef PathSeparators = ";";
+#else
+  const StringRef PathSeparators = ":";
+#endif
+
+  SmallVector<StringRef, 8> LibDirs;
+  llvm::SplitString(OptPath.getValue(), LibDirs, PathSeparators);
+
+  for (const auto &LibDir : LibDirs) {
+    if (LibDir.empty())
+      continue;
+
+    SmallString<128> FilePath(LibDir);
+    llvm::sys::path::append(FilePath, Value);
+    if (llvm::sys::fs::exists(Twine(FilePath)))
+      return true;
+  }
+
+  return false;
+}
+
 /// \brief Check that the file referenced by Value exists. If it doesn't,
 /// issue a diagnostic and return false.
 static bool DiagnoseInputExistence(const Driver &D, const DerivedArgList &Args,
@@ -950,6 +981,9 @@
   if (llvm::sys::fs::exists(Twine(Path)))
     return true;
 
+  if (D.IsCLMode() && ExistsInLibDir(Value))
+    return true;
+
   D.Diag(clang::diag::err_drv_no_such_file) << Path.str();
   return false;
 }