From 4cf1dbe05e12087cd7f6a64212d3af48582cf320 Mon Sep 17 00:00:00 2001 From: Hans Wennborg Date: Wed, 18 Jun 2014 17:21:50 +0000 Subject: [PATCH] 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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@211189 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Driver/Driver.cpp | 34 ++++++++++++++++++++++++++ test/Driver/Inputs/cl-libs/cl-test.lib | 0 test/Driver/cl-inputs.c | 9 +++++++ 3 files changed, 43 insertions(+) create mode 100644 test/Driver/Inputs/cl-libs/cl-test.lib diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp index dc07e641ac..34c48d4e38 100644 --- a/lib/Driver/Driver.cpp +++ b/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 @@ -927,6 +929,35 @@ void Driver::BuildUniversalActions(const ToolChain &TC, } } +/// \brief Check whether the file referenced by Value exists in the LIB +/// environment variable. +static bool ExistsInLibDir(StringRef Value) { + llvm::Optional OptPath = llvm::sys::Process::GetEnv("LIB"); + if (!OptPath.hasValue()) + return false; + +#ifdef LLVM_ON_WIN32 + const StringRef PathSeparators = ";"; +#else + const StringRef PathSeparators = ":"; +#endif + + SmallVector 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 @@ static bool DiagnoseInputExistence(const Driver &D, const DerivedArgList &Args, 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; } diff --git a/test/Driver/Inputs/cl-libs/cl-test.lib b/test/Driver/Inputs/cl-libs/cl-test.lib new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/Driver/cl-inputs.c b/test/Driver/cl-inputs.c index 066e0e32d9..9f6d2ef842 100644 --- a/test/Driver/cl-inputs.c +++ b/test/Driver/cl-inputs.c @@ -40,4 +40,13 @@ // RUN: %clang_cl -### /Tc - 2>&1 | FileCheck -check-prefix=STDINTc %s // STDINTc: "-x" "c" +// RUN: env LIB=%S/Inputs/cl-libs %clang_cl -### -- %s cl-test.lib 2>&1 | FileCheck -check-prefix=LIBINPUT %s +// LIBINPUT: "link.exe" +// LIBINPUT: "cl-test.lib" + +// RUN: env LIB=%S/Inputs/cl-libs %clang_cl -### -- %s cl-test2.lib 2>&1 | FileCheck -check-prefix=LIBINPUT2 %s +// LIBINPUT2: error: no such file or directory: 'cl-test2.lib' +// LIBINPUT2: "link.exe" +// LIBINPUT2-NOT: "cl-test2.lib" + void f(); -- 2.40.0