Skip to content

[BOLT] Resolve symlink for library lookup #126386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 8, 2025
Merged

[BOLT] Resolve symlink for library lookup #126386

merged 1 commit into from
Feb 8, 2025

Conversation

yozhu
Copy link
Contributor

@yozhu yozhu commented Feb 8, 2025

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Feb 8, 2025

@llvm/pr-subscribers-bolt

Author: YongKang Zhu (yozhu)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/126386.diff

1 Files Affected:

  • (modified) bolt/lib/RuntimeLibs/RuntimeLibrary.cpp (+18)
diff --git a/bolt/lib/RuntimeLibs/RuntimeLibrary.cpp b/bolt/lib/RuntimeLibs/RuntimeLibrary.cpp
index 336c6768a7f712..8f5719e84ecea8 100644
--- a/bolt/lib/RuntimeLibs/RuntimeLibrary.cpp
+++ b/bolt/lib/RuntimeLibs/RuntimeLibrary.cpp
@@ -18,6 +18,7 @@
 #include "llvm/Object/Archive.h"
 #include "llvm/Object/ObjectFile.h"
 #include "llvm/Support/Path.h"
+#include "llvm/Support/Program.h"
 
 #define DEBUG_TYPE "bolt-rtlib"
 
@@ -38,6 +39,23 @@ std::string RuntimeLibrary::getLibPathByToolPath(StringRef ToolPath,
     llvm::sys::path::append(LibPath, "lib" LLVM_LIBDIR_SUFFIX);
   }
   llvm::sys::path::append(LibPath, LibFileName);
+  if (!llvm::sys::fs::exists(LibPath)) {
+    // If it is a symlink, check the directory that the symlink points to.
+    if (llvm::sys::fs::is_symlink_file(ToolPath)) {
+      SmallString<256> RealPath;
+      llvm::sys::fs::real_path(ToolPath, RealPath);
+      if (llvm::ErrorOr<std::string> P =
+              llvm::sys::findProgramByName(RealPath)) {
+        outs() << "BOLT-INFO: library not found: " << LibPath << "\n"
+               << "BOLT-INFO: " << ToolPath << " is a symlink; will look up "
+               << LibFileName
+               << " at the target directory that the symlink points to\n";
+        return getLibPath(*P, LibFileName);
+      }
+    }
+    errs() << "BOLT-ERROR: library not found: " << LibPath << "\n";
+    exit(1);
+  }
   return std::string(LibPath);
 }
 

Copy link
Member

@dcci dcci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is fine, but I was wondering what scenario you hit where the library is symlinked and you need to perform the extra step to resovle.

@maksfb maksfb changed the title [BOLT] Extra lookup of library on target directory if bolt is invoked from symlink [BOLT] Resolve symlink for library lookup Feb 8, 2025
@yozhu
Copy link
Contributor Author

yozhu commented Feb 8, 2025

The code is fine, but I was wondering what scenario you hit where the library is symlinked and you need to perform the extra step to resovle.

I hit this in our internal build system where a symlink to llvm-bolt is created and invoked by the build. The original library lookup logic is to check the directory of ../lib/ from the directory where bolt tool is located, and this patch adds an additional lookup at the target directory that the symlink points to.

@dcci
Copy link
Member

dcci commented Feb 8, 2025

The code is fine, but I was wondering what scenario you hit where the library is symlinked and you need to perform the extra step to resovle.

I hit this in our internal build system where a symlink to llvm-bolt is created and invoked by the build. The original library lookup logic is to check the directory of ../lib/ from the directory where bolt tool is located, and this patch adds an additional lookup at the target directory that the symlink points to.

LG, thanks.

@yozhu yozhu merged commit 1e0a489 into llvm:main Feb 8, 2025
9 checks passed
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants