[Lldb-commits] [lldb] [lldb] Find Python relative to LLDB.framework on Darwin (again) (PR #227521)

via lldb-commits lldb-commits at lists.llvm.org
Tue Sep 29 17:01:05 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lldb

Author: Jonas Devlieghere (JDevlieghere)

<details>
<summary>Changes</summary>

The PythonRuntimeLoaderDarwin is responsible for finding the Python library at runtime. Previously, this was using `DEVELOPER_DIR` and `xcrun` to avoid having to hard-code the same list of relative paths that we have as RPATHs for the non-dynamic interpreter build.

However, that leads to some tricky behavior when launching `lldb` with our without `xcrun`. Combined with some odd path combinations, I changed my mind and went back to mimicking the RPATH approach.

While I was at it, I also dropped the fallback for looking for Python in /Applications/Xcode.app. That was always a hack, and now that we support falling back to a Homebrew or Pyhthon.org installed Python.framework, I think we should just rely on that.

rdar://188746227

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


1 Files Affected:

- (modified) lldb/source/Host/macosx/PythonRuntimeLoaderDarwin.cpp (+45-91) 


``````````diff
diff --git a/lldb/source/Host/macosx/PythonRuntimeLoaderDarwin.cpp b/lldb/source/Host/macosx/PythonRuntimeLoaderDarwin.cpp
index ce0cf660eb013..8642c4c3e51c0 100644
--- a/lldb/source/Host/macosx/PythonRuntimeLoaderDarwin.cpp
+++ b/lldb/source/Host/macosx/PythonRuntimeLoaderDarwin.cpp
@@ -12,116 +12,70 @@
 
 #include "../common/PythonRuntimeLoaderInternal.h"
 #include "llvm/ADT/STLFunctionalExtras.h"
-#include "llvm/ADT/ScopeExit.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/Twine.h"
 #include "llvm/Support/FileSystem.h"
-#include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/Path.h"
-#include "llvm/Support/Program.h"
 
-#include <cstdlib>
-#include <memory>
-#include <optional>
-#include <string>
+#include <dlfcn.h>
 
 namespace lldb_private {
 
 namespace {
 
-// Apple ships Python3.framework; python.org and Homebrew ship Python.framework.
-constexpr llvm::StringLiteral kAppleFrameworkSuffix =
-    "Library/Frameworks/Python3.framework/Versions/Current/Python3";
-constexpr llvm::StringLiteral kPythonOrgFrameworkSuffix =
-    "Library/Frameworks/Python.framework/Versions/Current/Python";
-
-bool TryIfExists(llvm::function_ref<bool(const char *)> callback,
-                 llvm::StringRef base, llvm::StringRef relative) {
-  llvm::SmallString<256> path(base);
-  llvm::sys::path::append(path, relative);
-  if (!llvm::sys::fs::exists(path))
-    return false;
-  return callback(path.c_str());
-}
-
-/// Locates the Python3.framework that `xcrun -f python3` points at. Returns
-/// an empty string on any failure or unexpected layout.
-std::string FindPythonViaXcrun() {
-  llvm::ErrorOr<std::string> xcrun = llvm::sys::findProgramByName("xcrun");
-  if (!xcrun)
-    return {};
-
-  llvm::SmallString<128> stdout_path;
-  if (llvm::sys::fs::createTemporaryFile("xcrun-python3", "txt", stdout_path))
-    return {};
-  auto remove_temp =
-      llvm::scope_exit([&] { llvm::sys::fs::remove(stdout_path.str()); });
-
-  std::optional<llvm::StringRef> redirects[3] = {
-      llvm::StringRef(""), llvm::StringRef(stdout_path), llvm::StringRef("")};
-  llvm::StringRef args[] = {*xcrun, "-f", "python3"};
-  int rc =
-      llvm::sys::ExecuteAndWait(*xcrun, args, /*env=*/std::nullopt, redirects);
-  if (rc != 0)
-    return {};
-
-  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> buf =
-      llvm::MemoryBuffer::getFile(stdout_path.str());
-  if (!buf)
-    return {};
-
-  llvm::StringRef python = (*buf)->getBuffer().rtrim();
-  if (python.empty())
-    return {};
-
-  // xcrun is not guaranteed to return an Xcode-layout interpreter, so verify
-  // the .../usr/bin/python3 structure before deriving a developer dir.
-  if (llvm::sys::path::filename(python) != "python3")
-    return {};
-  llvm::StringRef parent = llvm::sys::path::parent_path(python);
-  if (llvm::sys::path::filename(parent) != "bin")
-    return {};
-  llvm::StringRef grandparent = llvm::sys::path::parent_path(parent);
-  if (llvm::sys::path::filename(grandparent) != "usr")
-    return {};
-  llvm::StringRef developer = llvm::sys::path::parent_path(grandparent);
-
-  llvm::SmallString<256> framework(developer);
-  llvm::sys::path::append(framework, kAppleFrameworkSuffix);
-  if (llvm::sys::fs::exists(framework))
-    return std::string(framework);
-
-  return {};
+// Xcode and the Command Line Tools ship Python3.framework. Toolchains and
+// python.org ship Python.framework.
+constexpr llvm::StringLiteral kFrameworks[] = {
+    "Python3.framework/Versions/Current/Python3",
+    "Python.framework/Versions/Current/Python",
+};
+
+// The directories relative to LLDB.framework where Python(3).framework could be
+// present.
+constexpr llvm::StringLiteral kLLDBRelativeDirs[] = {
+    "../../../../../Developer/Library/Frameworks",
+    "../../../../Developer/Library/Frameworks",
+    "../../../../Frameworks",
+    "../../..",
+    "../../../../../../AppleInternal/Library/Frameworks",
+};
+
+/// Checks each Python framework in \p dir until one is found and \p callback
+/// returns true.
+bool TryFrameworks(llvm::function_ref<bool(const char *)> callback,
+                   const llvm::Twine &dir) {
+  for (llvm::StringRef framework : kFrameworks) {
+    llvm::SmallString<256> path;
+    llvm::sys::path::append(path, dir, framework);
+    if (llvm::sys::fs::exists(path) && callback(path.c_str()))
+      return true;
+  }
+  return false;
 }
 
 } // namespace
 
 void ForEachPythonRuntimeCandidate(
     llvm::function_ref<bool(const char *)> callback) {
-  if (const char *developer_dir = std::getenv("DEVELOPER_DIR");
-      developer_dir && *developer_dir)
-    if (TryIfExists(callback, developer_dir, kAppleFrameworkSuffix))
-      return;
 
-  if (TryIfExists(callback, "/Applications/Xcode.app/Contents/Developer",
-                  kAppleFrameworkSuffix))
-    return;
-  if (TryIfExists(callback, "/Library/Developer/CommandLineTools",
-                  kAppleFrameworkSuffix))
-    return;
-  if (TryIfExists(callback, "/", kPythonOrgFrameworkSuffix))
+  // Prefer the Python relative to LLDB. Locate ourself with dladdr because the
+  // alterantive is HostInfo::GetShlibDir which must not run before plugins
+  // register their shared library directory helper.
+  Dl_info info;
+  if (::dladdr(reinterpret_cast<const void *>(&ForEachPythonRuntimeCandidate),
+               &info)) {
+    llvm::StringRef lldb_dir = llvm::sys::path::parent_path(info.dli_fname);
+    for (llvm::StringRef dir : kLLDBRelativeDirs)
+      if (TryFrameworks(callback, lldb_dir + "/" + dir))
+        return;
+  }
+
+  // Fall back to a Python from Python.org.
+  if (TryFrameworks(callback, "/Library/Frameworks"))
     return;
-  if (TryIfExists(callback, "/opt/homebrew", kPythonOrgFrameworkSuffix))
-    return;
-  if (TryIfExists(callback, "/usr/local", kPythonOrgFrameworkSuffix))
-    return;
-
-  // xcrun is a subprocess; only run it when the well-known paths miss.
-  if (std::string xcrun_path = FindPythonViaXcrun(); !xcrun_path.empty())
-    if (callback(xcrun_path.c_str()))
-      return;
 
-  // Bare-name dlopen is the only way to hit the dyld shared cache.
+  // Finally, use a bare dlopen to load it from the dyld shared cache.
   callback("libpython3.dylib");
 }
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/227521


More information about the lldb-commits mailing list