COFF: Fix precedence between LIB and /libpath.
authorRui Ueyama <ruiu@google.com>
Fri, 19 Jun 2015 22:39:48 +0000 (22:39 +0000)
committerRui Ueyama <ruiu@google.com>
Fri, 19 Jun 2015 22:39:48 +0000 (22:39 +0000)
/libpath should take precedence over LIB.
Previously, LIB took precedence over /libpath.

llvm-svn: 240182

lld/COFF/Driver.cpp
lld/COFF/Driver.h
lld/test/COFF/libpath.test

index d89624c..c03102e 100644 (file)
@@ -191,20 +191,16 @@ Optional<StringRef> LinkerDriver::findLib(StringRef Filename) {
 }
 
 // Parses LIB environment which contains a list of search paths.
-std::vector<StringRef> LinkerDriver::getSearchPaths() {
-  std::vector<StringRef> Ret;
-  // Add current directory as first item of the search paths.
-  Ret.push_back("");
+void LinkerDriver::addLibSearchPaths() {
   Optional<std::string> EnvOpt = Process::GetEnv("LIB");
   if (!EnvOpt.hasValue())
-    return Ret;
+    return;
   StringRef Env = Alloc.save(*EnvOpt);
   while (!Env.empty()) {
     StringRef Path;
     std::tie(Path, Env) = Env.split(';');
-    Ret.push_back(Path);
+    SearchPaths.push_back(Path);
   }
-  return Ret;
 }
 
 static WindowsSubsystem inferSubsystem() {
@@ -251,6 +247,12 @@ bool LinkerDriver::link(int Argc, const char *Argv[]) {
     return false;
   }
 
+  // Construct search path list.
+  SearchPaths.push_back("");
+  for (auto *Arg : Args->filtered(OPT_libpath))
+    SearchPaths.push_back(Arg->getValue());
+  addLibSearchPaths();
+
   // Handle /out
   if (auto *Arg = Args->getLastArg(OPT_out))
     Config->OutputFile = Arg->getValue();
@@ -287,10 +289,6 @@ bool LinkerDriver::link(int Argc, const char *Argv[]) {
   }
   Config->MachineType = MTOrErr.get();
 
-  // Handle /libpath
-  for (auto *Arg : Args->filtered(OPT_libpath))
-    SearchPaths.push_back(Arg->getValue());
-
   // Handle /nodefaultlib:<filename>
   for (auto *Arg : Args->filtered(OPT_nodefaultlib))
     Config->NoDefaultLibs.insert(doFindLib(Arg->getValue()));
index 0918dcf..53bac00 100644 (file)
@@ -64,7 +64,7 @@ private:
 
 class LinkerDriver {
 public:
-  LinkerDriver() : Alloc(AllocAux), SearchPaths(getSearchPaths()) {}
+  LinkerDriver() : Alloc(AllocAux) {}
   bool link(int Argc, const char *Argv[]);
 
   // Used by the resolver to parse .drectve section contents.
@@ -86,10 +86,11 @@ private:
   StringRef doFindLib(StringRef Filename);
 
   // Parses LIB environment which contains a list of search paths.
-  // The returned list always contains "." as the first element.
-  std::vector<StringRef> getSearchPaths();
+  void addLibSearchPaths();
 
+  // Library search path. The first element is always "" (current directory).
   std::vector<StringRef> SearchPaths;
+
   std::set<std::string> VisitedFiles;
 
   // Driver is the owner of all opened files.
index 8db5389..b304dc3 100644 (file)
@@ -1,26 +1,18 @@
-# RUN: mkdir -p %t/a %t/b
+# RUN: mkdir -p %t/a %t/b %t/c
 # RUN: cp %p/Inputs/std64.lib %t/a/
 # RUN: cp %p/Inputs/std64.lib %t/b/
+# RUN: cp %p/Inputs/std64.lib %t/c/
 
 # RUN: env LIB=%t/a lld -flavor link2 /out:%t.exe /entry:main /verbose \
-# RUN:   std64.lib /subsystem:console \
-# RUN:   %p/Inputs/hello64.obj /libpath:%t/b >& %t.log
+# RUN:   std64.lib /subsystem:console %p/Inputs/hello64.obj \
+# RUN:   /libpath:%t/b /libpath:%t/c > %t.log
 # RUN: FileCheck -check-prefix=CHECK1 %s < %t.log
 
-# RUN: lld -flavor link2 /out:%t.exe /entry:main /verbose std64.lib \
-# RUN:   /subsystem:console %p/Inputs/hello64.obj \
-# RUN:   /libpath:%t/a /libpath:%t/b >& %t.log
-# RUN: FileCheck -check-prefix=CHECK1 %s < %t.log
-
-CHECK1: Reading {{.*}}/a/std64.lib
-
-# RUN: env LIB=%t/b lld -flavor link2 /out:%t.exe /entry:main /verbose \
-# RUN:   /subsystem:console std64.lib %p/Inputs/hello64.obj \
-# RUN:   /libpath:%t/a >& %t.log
-# RUN: FileCheck -check-prefix=CHECK2 %s < %t.log
+CHECK1: b{{[/\\]}}std64.lib
 
-# RUN: lld -flavor link2 /out:%t.exe /entry:main /verbose /subsystem:console \
-# RUN:   std64.lib %p/Inputs/hello64.obj /libpath:%t/b /libpath:%t/a >& %t.log
+# RUN: lld -flavor link2 /out:%t.exe /entry:main /verbose \
+# RUN:   std64.lib /subsystem:console %p/Inputs/hello64.obj \
+# RUN:   /libpath:%t/a /libpath:%t/b /libpath:%t/c > %t.log
 # RUN: FileCheck -check-prefix=CHECK2 %s < %t.log
 
-CHECK2: Reading {{.*}}/b/std64.lib
+CHECK2: a{{[/\\]}}std64.lib