From 7070827be1fc6bbc02e932c1768bcf70aa823f04 Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Sat, 20 Jun 2015 00:57:12 +0000 Subject: [PATCH] LibDriver: implement /libpath and $LIB; ignore /ignore and /machine. llvm-svn: 240203 --- llvm/lib/LibDriver/LibDriver.cpp | 47 ++++++++++++++++++++++++++++++++++++++-- llvm/lib/LibDriver/Options.td | 6 +++++ llvm/test/LibDriver/Inputs/a.s | 2 ++ llvm/test/LibDriver/Inputs/b.s | 2 ++ llvm/test/LibDriver/libpath.test | 15 +++++++++++++ 5 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 llvm/test/LibDriver/Inputs/a.s create mode 100644 llvm/test/LibDriver/Inputs/b.s create mode 100644 llvm/test/LibDriver/libpath.test 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); diff --git a/llvm/lib/LibDriver/Options.td b/llvm/lib/LibDriver/Options.td index 0db84bc..0aa1aff 100644 --- a/llvm/lib/LibDriver/Options.td +++ b/llvm/lib/LibDriver/Options.td @@ -9,9 +9,15 @@ class F : Flag<["/", "-", "-?"], name>; class P : Joined<["/", "-", "-?"], name#":">, HelpText; +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 : Joined<["/", "-", "-?"], name#":">; + +def ignore : QF<"ignore">; +def machine: QF<"machine">; def nologo : F<"nologo">; diff --git a/llvm/test/LibDriver/Inputs/a.s b/llvm/test/LibDriver/Inputs/a.s new file mode 100644 index 0000000..88258e2 --- /dev/null +++ b/llvm/test/LibDriver/Inputs/a.s @@ -0,0 +1,2 @@ +.globl a +a: diff --git a/llvm/test/LibDriver/Inputs/b.s b/llvm/test/LibDriver/Inputs/b.s new file mode 100644 index 0000000..4890c92 --- /dev/null +++ b/llvm/test/LibDriver/Inputs/b.s @@ -0,0 +1,2 @@ +.globl b +b: diff --git a/llvm/test/LibDriver/libpath.test b/llvm/test/LibDriver/libpath.test new file mode 100644 index 0000000..22d4f58 --- /dev/null +++ b/llvm/test/LibDriver/libpath.test @@ -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 -- 2.7.4