From 7070827be1fc6bbc02e932c1768bcf70aa823f04 Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Sat, 20 Jun 2015 00:57:12 +0000 Subject: LibDriver: implement /libpath and $LIB; ignore /ignore and /machine. llvm-svn: 240203 --- llvm/lib/LibDriver/LibDriver.cpp | 47 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) (limited to 'llvm/lib/LibDriver/LibDriver.cpp') diff --git a/llvm/lib/LibDriver/LibDriver.cpp b/llvm/lib/LibDriver/LibDriver.cpp index fb8e0a8..c9857b0 100644 --- a/llvm/lib/LibDriver/LibDriver.cpp +++ b/llvm/lib/LibDriver/LibDriver.cpp @@ -21,6 +21,7 @@ #include "llvm/Support/CommandLine.h" #include "llvm/Support/StringSaver.h" #include "llvm/Support/Path.h" +#include "llvm/Support/Process.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; @@ -68,6 +69,40 @@ static std::string getOutputPath(llvm::opt::InputArgList *Args) { llvm_unreachable("internal error"); } +static std::vector getSearchPaths(llvm::opt::InputArgList *Args, + StringSaver &Saver) { + std::vector Ret; + // Add current directory as first item of the search path. + Ret.push_back(""); + + // Add /libpath flags. + for (auto *Arg : Args->filtered(OPT_libpath)) + Ret.push_back(Arg->getValue()); + + // Add $LIB. + Optional EnvOpt = sys::Process::GetEnv("LIB"); + if (!EnvOpt.hasValue()) + return Ret; + StringRef Env = Saver.save(*EnvOpt); + while (!Env.empty()) { + StringRef Path; + std::tie(Path, Env) = Env.split(';'); + Ret.push_back(Path); + } + return Ret; +} + +static Optional findInputFile(StringRef File, + ArrayRef Paths) { + for (auto Dir : Paths) { + SmallString<128> Path = Dir; + sys::path::append(Path, File); + if (sys::fs::exists(Path)) + return Path.str().str(); + } + return Optional(); +} + int llvm::libDriverMain(int Argc, const char **Argv) { SmallVector NewArgv(Argv, Argv + Argc); BumpPtrAllocator Alloc; @@ -96,10 +131,18 @@ int llvm::libDriverMain(int Argc, const char **Argv) { return 1; } + std::vector SearchPaths = getSearchPaths(Args.get(), Saver); + std::vector Members; - for (auto *Arg : Args->filtered(OPT_INPUT)) - Members.emplace_back(Arg->getValue(), + for (auto *Arg : Args->filtered(OPT_INPUT)) { + Optional Path = findInputFile(Arg->getValue(), SearchPaths); + if (!Path.hasValue()) { + llvm::errs() << Arg->getValue() << ": no such file or directory\n"; + return 1; + } + Members.emplace_back(Saver.save(*Path), llvm::sys::path::filename(Arg->getValue())); + } std::pair Result = llvm::writeArchive( getOutputPath(Args.get()), Members, /*WriteSymtab=*/true); -- cgit v1.1