@@ -430,7 +430,8 @@ SBError Driver::ProcessArgs(const opt::InputArgList &args, bool &exiting) {
430430 return error;
431431}
432432
433- #if defined(_WIN32) && defined(LLDB_PYTHON_DLL_RELATIVE_PATH)
433+ #ifdef _WIN32
434+ #ifdef LLDB_PYTHON_DLL_RELATIVE_PATH
434435// / Returns the full path to the lldb.exe executable.
435436inline std::wstring GetPathToExecutableW () {
436437 // Iterate until we reach the Windows API maximum path length (32,767).
@@ -444,30 +445,73 @@ inline std::wstring GetPathToExecutableW() {
444445 return L" " ;
445446}
446447
447- // / Resolve the full path of the directory defined by
448+ // / \brief Resolve the full path of the directory defined by
448449// / LLDB_PYTHON_DLL_RELATIVE_PATH. If it exists, add it to the list of DLL
449450// / search directories.
450- void AddPythonDLLToSearchPath () {
451+ // / \return `true` if the library was added to the search path.
452+ // / `false` otherwise.
453+ bool AddPythonDLLToSearchPath () {
451454 std::wstring modulePath = GetPathToExecutableW ();
452- if (modulePath.empty ()) {
453- llvm::errs () << " error: unable to find python.dll." << ' \n ' ;
454- return ;
455- }
455+ if (modulePath.empty ())
456+ return false ;
456457
457458 SmallVector<char , MAX_PATH> utf8Path;
458459 if (sys::windows::UTF16ToUTF8 (modulePath.c_str (), modulePath.length (),
459460 utf8Path))
460- return ;
461+ return false ;
461462 sys::path::remove_filename (utf8Path);
462463 sys::path::append (utf8Path, LLDB_PYTHON_DLL_RELATIVE_PATH);
463464 sys::fs::make_absolute (utf8Path);
464465
465466 SmallVector<wchar_t , 1 > widePath;
466467 if (sys::windows::widenPath (utf8Path.data (), widePath))
467- return ;
468+ return false ;
468469
469470 if (sys::fs::exists (utf8Path))
470- SetDllDirectoryW (widePath.data ());
471+ return SetDllDirectoryW (widePath.data ());
472+ return false ;
473+ }
474+ #endif
475+
476+ #ifdef LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME
477+ // / Returns whether `python3x.dll` is in the DLL search path.
478+ bool IsPythonDLLInPath () {
479+ #define WIDEN2 (x ) L##x
480+ #define WIDEN (x ) WIDEN2(x)
481+ WCHAR foundPath[MAX_PATH];
482+ DWORD result =
483+ SearchPathW (nullptr , WIDEN (LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME), nullptr ,
484+ MAX_PATH, foundPath, nullptr );
485+ #undef WIDEN2
486+ #undef WIDEN
487+
488+ return result > 0 ;
489+ }
490+ #endif
491+
492+ // / Try to setup the DLL search path for the Python Runtime Library
493+ // / (python3xx.dll).
494+ // /
495+ // / If `LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME` is set, we first check if
496+ // / python3xx.dll is in the search path. If it's not, we try to add it and
497+ // / check for it a second time.
498+ // / If only `LLDB_PYTHON_DLL_RELATIVE_PATH` is set, we try to add python3xx.dll
499+ // / to the search path python.dll is already in the search path or not.
500+ void SetupPythonRuntimeLibrary () {
501+ #ifdef LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME
502+ if (IsPythonDLLInPath ())
503+ return ;
504+ #ifdef LLDB_PYTHON_DLL_RELATIVE_PATH
505+ if (AddPythonDLLToSearchPath () && IsPythonDLLInPath ())
506+ return ;
507+ #endif
508+ llvm::errs () << " error: unable to find '"
509+ << LLDB_PYTHON_RUNTIME_LIBRARY_FILENAME << " '.\n " ;
510+ return ;
511+ #elif defined(LLDB_PYTHON_DLL_RELATIVE_PATH)
512+ if (!AddPythonDLLToSearchPath ())
513+ llvm::errs () << " error: unable to find the Python runtime library.\n " ;
514+ #endif
471515}
472516#endif
473517
@@ -801,8 +845,8 @@ int main(int argc, char const *argv[]) {
801845 " ~/Library/Logs/DiagnosticReports/.\n " );
802846#endif
803847
804- #if defined( _WIN32) && defined(LLDB_PYTHON_DLL_RELATIVE_PATH)
805- AddPythonDLLToSearchPath ();
848+ #ifdef _WIN32
849+ SetupPythonRuntimeLibrary ();
806850#endif
807851
808852 // Parse arguments.
0 commit comments