[Lldb-commits] [lldb] [lldb][windows] print an error if python.dll is not in the DLL search path (PR #164893)
Charles Zablit via lldb-commits
lldb-commits at lists.llvm.org
Mon Oct 27 23:59:19 PDT 2025
================
@@ -447,30 +448,65 @@ inline std::wstring GetPathToExecutableW() {
return L"";
}
-/// Resolve the full path of the directory defined by
+/// \brief Resolve the full path of the directory defined by
/// LLDB_PYTHON_DLL_RELATIVE_PATH. If it exists, add it to the list of DLL
/// search directories.
-void AddPythonDLLToSearchPath() {
+/// \return `true` if the library was added to the search path.
+/// `false` otherwise.
+bool AddPythonDLLToSearchPath() {
std::wstring modulePath = GetPathToExecutableW();
- if (modulePath.empty()) {
- llvm::errs() << "error: unable to find python.dll." << '\n';
- return;
- }
+ if (modulePath.empty())
+ return false;
SmallVector<char, MAX_PATH> utf8Path;
if (sys::windows::UTF16ToUTF8(modulePath.c_str(), modulePath.length(),
utf8Path))
- return;
+ return false;
sys::path::remove_filename(utf8Path);
sys::path::append(utf8Path, LLDB_PYTHON_DLL_RELATIVE_PATH);
sys::fs::make_absolute(utf8Path);
SmallVector<wchar_t, 1> widePath;
if (sys::windows::widenPath(utf8Path.data(), widePath))
- return;
+ return false;
if (sys::fs::exists(utf8Path))
- SetDllDirectoryW(widePath.data());
+ return SetDllDirectoryW(widePath.data());
+ return false;
+}
+#endif
+
+#ifdef LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME
+/// Returns whether `python3x.dll` is in the DLL search path.
+bool IsPythonDLLInPath() {
+#define WIDEN2(x) L##x
+#define WIDEN(x) WIDEN2(x)
+ WCHAR foundPath[MAX_PATH];
+ DWORD result =
+ SearchPathW(nullptr, WIDEN(LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME), nullptr,
+ MAX_PATH, foundPath, nullptr);
+#undef WIDEN2
+#undef WIDEN
+
+ return result > 0;
+}
+#endif
+
+void SetupPythonRuntimeLibrary() {
+#ifdef LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME
+ if (IsPythonDLLInPath())
+ return;
+#ifdef LLDB_PYTHON_DLL_RELATIVE_PATH
+ if (AddPythonDLLToSearchPath())
+ return;
+#endif
+ llvm::errs() << "error: unable to find '"
+ << LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME << "'.\n";
+ return;
+#elif defined(LLDB_PYTHON_DLL_RELATIVE_PATH)
+ if (!AddPythonDLLToSearchPath())
+ llvm::errs() << "error: unable to find the Python runtime library.\n";
+#endif
----------------
charles-zablit wrote:
We want to be sure to check that `python.dll` is in the Path and/or try to add it to the Path whether `LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME` and `LLDB_PYTHON_DLL_RELATIVE_PATH` are both set or only one or the other.
If only `LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME` is set:
- Check if `python.dll` is in the Path
- Print a warning if it's not.
If only `LLDB_PYTHON_DLL_RELATIVE_PATH` is set:
- Try to add `python.dll` to the Path
If both are set:
- Check if `python.dll` is in the Path.
- If it's not try to add it.
- Check again if it's in the Path (not implemented yet).
- Print a warning if it's not.
https://github.com/llvm/llvm-project/pull/164893
More information about the lldb-commits
mailing list