LibDriver: implement /libpath and $LIB; ignore /ignore and /machine.

llvm-svn: 240203
This commit is contained in:
Peter Collingbourne 2015-06-20 00:57:12 +00:00
parent 73378eb1c7
commit 7070827be1
5 changed files with 70 additions and 2 deletions

View File

@ -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<StringRef> getSearchPaths(llvm::opt::InputArgList *Args,
StringSaver &Saver) {
std::vector<StringRef> 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<std::string> 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<std::string> findInputFile(StringRef File,
ArrayRef<StringRef> 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<std::string>();
}
int llvm::libDriverMain(int Argc, const char **Argv) {
SmallVector<const char *, 20> NewArgv(Argv, Argv + Argc);
BumpPtrAllocator Alloc;
@ -96,10 +131,18 @@ int llvm::libDriverMain(int Argc, const char **Argv) {
return 1;
}
std::vector<StringRef> SearchPaths = getSearchPaths(Args.get(), Saver);
std::vector<llvm::NewArchiveIterator> Members;
for (auto *Arg : Args->filtered(OPT_INPUT))
Members.emplace_back(Arg->getValue(),
for (auto *Arg : Args->filtered(OPT_INPUT)) {
Optional<std::string> 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<StringRef, std::error_code> Result = llvm::writeArchive(
getOutputPath(Args.get()), Members, /*WriteSymtab=*/true);

View File

@ -9,9 +9,15 @@ class F<string name> : Flag<["/", "-", "-?"], name>;
class P<string name, string help> :
Joined<["/", "-", "-?"], name#":">, HelpText<help>;
def libpath: P<"libpath", "Object file search path">;
def out : P<"out", "Path to file to write output">;
//==============================================================================
// The flags below do nothing. They are defined only for lib.exe compatibility.
//==============================================================================
class QF<string name> : Joined<["/", "-", "-?"], name#":">;
def ignore : QF<"ignore">;
def machine: QF<"machine">;
def nologo : F<"nologo">;

View File

@ -0,0 +1,2 @@
.globl a
a:

View File

@ -0,0 +1,2 @@
.globl b
b:

View File

@ -0,0 +1,15 @@
RUN: mkdir -p %T/a %T/b
RUN: llvm-mc -triple=x86_64-pc-windows-msvc -filetype=obj -o %T/a/foo.obj %S/Inputs/a.s
RUN: llvm-mc -triple=x86_64-pc-windows-msvc -filetype=obj -o %T/b/foo.obj %S/Inputs/b.s
RUN: env LIB=%T/a\;%T/b llvm-lib /out:%t1.lib foo.obj
RUN: llvm-nm %t1.lib | FileCheck --check-prefix=A %s
RUN: llvm-lib /out:%t2.lib /libpath:%T/a /libpath:%T/b foo.obj
RUN: llvm-nm %t2.lib | FileCheck --check-prefix=A %s
RUN: env LIB=%T/a llvm-lib /libpath:%T/b /out:%t3.lib foo.obj
RUN: llvm-nm %t3.lib | FileCheck --check-prefix=B %s
A: T a
B: T b