[lld] r287455 - Make buildSysrootedPath file-scoped.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 19 11:23:56 PST 2016


Author: ruiu
Date: Sat Nov 19 13:23:56 2016
New Revision: 287455

URL: http://llvm.org/viewvc/llvm-project?rev=287455&view=rev
Log:
Make buildSysrootedPath file-scoped.

This patch also changes its return type to simplify callers.

Modified:
    lld/trunk/ELF/Driver.h
    lld/trunk/ELF/DriverUtils.cpp

Modified: lld/trunk/ELF/Driver.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.h?rev=287455&r1=287454&r2=287455&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.h (original)
+++ lld/trunk/ELF/Driver.h Sat Nov 19 13:23:56 2016
@@ -74,7 +74,6 @@ std::string createResponseFile(const llv
 
 std::string findFromSearchPaths(StringRef Path);
 std::string searchLibrary(StringRef Path);
-std::string buildSysrootedPath(llvm::StringRef Dir, llvm::StringRef File);
 
 } // namespace elf
 } // namespace lld

Modified: lld/trunk/ELF/DriverUtils.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/DriverUtils.cpp?rev=287455&r1=287454&r2=287455&view=diff
==============================================================================
--- lld/trunk/ELF/DriverUtils.cpp (original)
+++ lld/trunk/ELF/DriverUtils.cpp Sat Nov 19 13:23:56 2016
@@ -18,6 +18,7 @@
 #include "Memory.h"
 #include "lld/Config/Version.h"
 #include "lld/Core/Reproduce.h"
+#include "llvm/ADT/Optional.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/Triple.h"
 #include "llvm/Option/Option.h"
@@ -123,41 +124,38 @@ std::string elf::createResponseFile(cons
   return Data.str();
 }
 
+// Find a file by concatenating given paths. If a resulting path
+// starts with "=", the character is replaced with a --sysroot value.
+static Optional<std::string> findFile(StringRef Path1, const Twine &Path2) {
+  SmallString<128> S;
+  if (Path1.startswith("="))
+    path::append(S, Config->Sysroot, Path1.substr(1), Path2);
+  else
+    path::append(S, Path1, Path2);
+
+  if (fs::exists(S))
+    return S.str().str();
+  return None;
+}
+
 std::string elf::findFromSearchPaths(StringRef Path) {
-  for (StringRef Dir : Config->SearchPaths) {
-    std::string FullPath = buildSysrootedPath(Dir, Path);
-    if (fs::exists(FullPath))
-      return FullPath;
-  }
+  for (StringRef Dir : Config->SearchPaths)
+    if (Optional<std::string> S = findFile(Dir, Path))
+      return *S;
   return "";
 }
 
-// Searches a given library from input search paths, which are filled
-// from -L command line switches. Returns a path to an existent library file.
-std::string elf::searchLibrary(StringRef Path) {
-  if (Path.startswith(":"))
-    return findFromSearchPaths(Path.substr(1));
+// This is for -lfoo. We'll look for libfoo.so or libfoo.a from
+// search paths.
+std::string elf::searchLibrary(StringRef Name) {
+  if (Name.startswith(":"))
+    return findFromSearchPaths(Name.substr(1));
   for (StringRef Dir : Config->SearchPaths) {
-    if (!Config->Static) {
-      std::string S = buildSysrootedPath(Dir, ("lib" + Path + ".so").str());
-      if (fs::exists(S))
-        return S;
-    }
-    std::string S = buildSysrootedPath(Dir, ("lib" + Path + ".a").str());
-    if (fs::exists(S))
-      return S;
+    if (!Config->Static)
+      if (Optional<std::string> S = findFile(Dir, "lib" + Name + ".so"))
+        return *S;
+    if (Optional<std::string> S = findFile(Dir, "lib" + Name + ".a"))
+      return *S;
   }
   return "";
 }
-
-// Makes a path by concatenating Dir and File.
-// If Dir starts with '=' the result will be preceded by Sysroot,
-// which can be set with --sysroot command line switch.
-std::string elf::buildSysrootedPath(StringRef Dir, StringRef File) {
-  SmallString<128> Path;
-  if (Dir.startswith("="))
-    path::append(Path, Config->Sysroot, Dir.substr(1), File);
-  else
-    path::append(Path, Dir, File);
-  return Path.str();
-}




More information about the llvm-commits mailing list