[llvm] r240203 - LibDriver: implement /libpath and $LIB; ignore /ignore and /machine.

Peter Collingbourne peter at pcc.me.uk
Fri Jun 19 17:57:12 PDT 2015


Author: pcc
Date: Fri Jun 19 19:57:12 2015
New Revision: 240203

URL: http://llvm.org/viewvc/llvm-project?rev=240203&view=rev
Log:
LibDriver: implement /libpath and $LIB; ignore /ignore and /machine.

Added:
    llvm/trunk/test/LibDriver/
    llvm/trunk/test/LibDriver/Inputs/
    llvm/trunk/test/LibDriver/Inputs/a.s
    llvm/trunk/test/LibDriver/Inputs/b.s
    llvm/trunk/test/LibDriver/libpath.test
Modified:
    llvm/trunk/lib/LibDriver/LibDriver.cpp
    llvm/trunk/lib/LibDriver/Options.td

Modified: llvm/trunk/lib/LibDriver/LibDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LibDriver/LibDriver.cpp?rev=240203&r1=240202&r2=240203&view=diff
==============================================================================
--- llvm/trunk/lib/LibDriver/LibDriver.cpp (original)
+++ llvm/trunk/lib/LibDriver/LibDriver.cpp Fri Jun 19 19:57:12 2015
@@ -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::o
   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
     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);

Modified: llvm/trunk/lib/LibDriver/Options.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LibDriver/Options.td?rev=240203&r1=240202&r2=240203&view=diff
==============================================================================
--- llvm/trunk/lib/LibDriver/Options.td (original)
+++ llvm/trunk/lib/LibDriver/Options.td Fri Jun 19 19:57:12 2015
@@ -9,9 +9,15 @@ class F<string name> : Flag<["/", "-", "
 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">;

Added: llvm/trunk/test/LibDriver/Inputs/a.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/LibDriver/Inputs/a.s?rev=240203&view=auto
==============================================================================
--- llvm/trunk/test/LibDriver/Inputs/a.s (added)
+++ llvm/trunk/test/LibDriver/Inputs/a.s Fri Jun 19 19:57:12 2015
@@ -0,0 +1,2 @@
+.globl a
+a:

Added: llvm/trunk/test/LibDriver/Inputs/b.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/LibDriver/Inputs/b.s?rev=240203&view=auto
==============================================================================
--- llvm/trunk/test/LibDriver/Inputs/b.s (added)
+++ llvm/trunk/test/LibDriver/Inputs/b.s Fri Jun 19 19:57:12 2015
@@ -0,0 +1,2 @@
+.globl b
+b:

Added: llvm/trunk/test/LibDriver/libpath.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/LibDriver/libpath.test?rev=240203&view=auto
==============================================================================
--- llvm/trunk/test/LibDriver/libpath.test (added)
+++ llvm/trunk/test/LibDriver/libpath.test Fri Jun 19 19:57:12 2015
@@ -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





More information about the llvm-commits mailing list