[PATCH] D151188: [LLD][COFF] Add LLVM toolchain library paths by default.

Tobias Hieta via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri May 26 06:16:05 PDT 2023


thieta retitled this revision from "[lld] Find resource and lib dir" to "[LLD][COFF] Add LLVM toolchain library paths by default.".
thieta edited the summary of this revision.
thieta updated this revision to Diff 526040.
thieta added a comment.

Implemented much simpler method


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D151188/new/

https://reviews.llvm.org/D151188

Files:
  lld/COFF/Driver.cpp
  lld/COFF/Driver.h


Index: lld/COFF/Driver.h
===================================================================
--- lld/COFF/Driver.h
+++ lld/COFF/Driver.h
@@ -84,6 +84,8 @@
   // config->machine has been set.
   void addWinSysRootLibSearchPaths();
 
+  void addClangLibSearchPaths(const std::string& argv0);
+
   // Used by the resolver to parse .drectve section contents.
   void parseDirectives(InputFile *file);
 
Index: lld/COFF/Driver.cpp
===================================================================
--- lld/COFF/Driver.cpp
+++ lld/COFF/Driver.cpp
@@ -465,8 +465,7 @@
     return filename;
   };
 
-  bool hasPathSep = (filename.find_first_of("/\\") != StringRef::npos);
-  if (hasPathSep)
+  if (sys::path::is_absolute(filename))
     return getFilename(filename);
   bool hasExt = filename.contains('.');
   for (StringRef dir : searchPaths) {
@@ -620,6 +619,26 @@
   }
 }
 
+void LinkerDriver::addClangLibSearchPaths(const std::string& argv0) {
+  std::string lldBinary = sys::fs::getMainExecutable(argv0.c_str(), nullptr);
+  SmallString<128> binDir(lldBinary);
+  sys::path::remove_filename(binDir); // remove lld-link.exe
+  StringRef rootDir = sys::path::parent_path(binDir); // remove 'bin'
+
+  SmallString<128> libDir(rootDir);
+  sys::path::append(libDir, "lib");
+
+  searchPaths.push_back(saver().save(std::string(libDir)));
+
+  SmallString<128> runtimeLibDir(rootDir);
+  sys::path::append(runtimeLibDir, "lib", "clang", std::to_string(LLVM_VERSION_MAJOR), "lib");
+  searchPaths.push_back(saver().save(std::string(runtimeLibDir)));
+
+  SmallString<128> runtimeLibDirWithOS(runtimeLibDir);
+  sys::path::append(runtimeLibDirWithOS, "windows");
+  searchPaths.push_back(saver().save(std::string(runtimeLibDirWithOS)));
+}
+
 void LinkerDriver::addWinSysRootLibSearchPaths() {
   if (!diaPath.empty()) {
     // The DIA SDK always uses the legacy vc arch, even in new MSVC versions.
@@ -1525,6 +1544,8 @@
   if (!args.hasArg(OPT_lldignoreenv) && !args.hasArg(OPT_winsysroot))
     addLibSearchPaths();
 
+  addClangLibSearchPaths(argsArr[0]);
+
   // Handle /ignore
   for (auto *arg : args.filtered(OPT_ignore)) {
     SmallVector<StringRef, 8> vec;
@@ -2042,7 +2063,7 @@
   // Handle /RELEASE
   if (args.hasArg(OPT_release))
     config->writeCheckSum = true;
-  
+
   // Handle /safeseh, x86 only, on by default, except for mingw.
   if (config->machine == I386) {
     config->safeSEH = args.hasFlag(OPT_safeseh, OPT_safeseh_no, !config->mingw);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D151188.526040.patch
Type: text/x-patch
Size: 2459 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230526/899279de/attachment.bin>


More information about the llvm-commits mailing list