@@ -433,7 +433,8 @@ SBError Driver::ProcessArgs(const opt::InputArgList &args, bool &exiting) {
433433 return error;
434434}
435435
436- #if defined(_WIN32) && defined(LLDB_PYTHON_DLL_RELATIVE_PATH)
436+ #ifdef _WIN32
437+ #ifdef LLDB_PYTHON_DLL_RELATIVE_PATH
437438// / Returns the full path to the lldb.exe executable.
438439inline std::wstring GetPathToExecutableW () {
439440 // Iterate until we reach the Windows API maximum path length (32,767).
@@ -447,32 +448,51 @@ inline std::wstring GetPathToExecutableW() {
447448 return L" " ;
448449}
449450
450- // / Resolve the full path of the directory defined by
451+ // / \brief Resolve the full path of the directory defined by
451452// / LLDB_PYTHON_DLL_RELATIVE_PATH. If it exists, add it to the list of DLL
452453// / search directories.
453- void AddPythonDLLToSearchPath () {
454+ // / \return `true` if the library was added to the search path.
455+ // / `false` otherwise.
456+ bool AddPythonDLLToSearchPath () {
454457 std::wstring modulePath = GetPathToExecutableW ();
455458 if (modulePath.empty ()) {
456- llvm::errs () << " error: unable to find python.dll." << ' \n ' ;
457- return ;
459+ return false ;
458460 }
459461
460462 SmallVector<char , MAX_PATH> utf8Path;
461463 if (sys::windows::UTF16ToUTF8 (modulePath.c_str (), modulePath.length (),
462464 utf8Path))
463- return ;
465+ return false ;
464466 sys::path::remove_filename (utf8Path);
465467 sys::path::append (utf8Path, LLDB_PYTHON_DLL_RELATIVE_PATH);
466468 sys::fs::make_absolute (utf8Path);
467469
468470 SmallVector<wchar_t , 1 > widePath;
469471 if (sys::windows::widenPath (utf8Path.data (), widePath))
470- return ;
472+ return false ;
471473
472474 if (sys::fs::exists (utf8Path))
473- SetDllDirectoryW (widePath.data ());
475+ return SetDllDirectoryW (widePath.data ());
476+ return false ;
477+ }
478+ #endif
479+
480+ #ifdef LLDB_PYTHON_SHARED_LIBRARY_FILENAME
481+ // / Returns whether `python3x.dll` is in the DLL search path.
482+ bool IsPythonDLLInPath () {
483+ #define WIDEN2 (x ) L##x
484+ #define WIDEN (x ) WIDEN2(x)
485+ WCHAR foundPath[MAX_PATH];
486+ DWORD result =
487+ SearchPathW (nullptr , WIDEN (LLDB_PYTHON_SHARED_LIBRARY_FILENAME), nullptr ,
488+ MAX_PATH, foundPath, nullptr );
489+ #undef WIDEN2
490+ #undef WIDEN
491+
492+ return result > 0 ;
474493}
475494#endif
495+ #endif
476496
477497std::string EscapeString (std::string arg) {
478498 std::string::size_type pos = 0 ;
@@ -776,8 +796,13 @@ int main(int argc, char const *argv[]) {
776796 " ~/Library/Logs/DiagnosticReports/.\n " );
777797#endif
778798
779- #if defined(_WIN32) && defined(LLDB_PYTHON_DLL_RELATIVE_PATH)
780- AddPythonDLLToSearchPath ();
799+ #if defined(_WIN32) && defined(LLDB_PYTHON_SHARED_LIBRARY_FILENAME)
800+ if (!IsPythonDLLInPath ())
801+ #ifdef LLDB_PYTHON_DLL_RELATIVE_PATH
802+ if (!AddPythonDLLToSearchPath ())
803+ #endif
804+ llvm::errs () << " error: unable to find "
805+ << LLDB_PYTHON_SHARED_LIBRARY_FILENAME << " .\n " ;
781806#endif
782807
783808 // Parse arguments.
0 commit comments