|
22 | 22 | #include "lldb/Host/MainLoop.h" |
23 | 23 | #include "lldb/Host/MainLoopBase.h" |
24 | 24 | #include "lldb/Utility/Status.h" |
| 25 | +#include "llvm/ADT/SmallString.h" |
25 | 26 | #include "llvm/ADT/StringRef.h" |
| 27 | +#include "llvm/Support/ConvertUTF.h" |
| 28 | +#include "llvm/Support/FileSystem.h" |
26 | 29 | #include "llvm/Support/Format.h" |
27 | 30 | #include "llvm/Support/InitLLVM.h" |
28 | 31 | #include "llvm/Support/Path.h" |
29 | 32 | #include "llvm/Support/Signals.h" |
30 | 33 | #include "llvm/Support/WithColor.h" |
31 | 34 | #include "llvm/Support/raw_ostream.h" |
32 | 35 |
|
| 36 | +#ifdef _WIN32 |
| 37 | +#include "llvm/Support/Windows/WindowsSupport.h" |
| 38 | +#endif |
| 39 | + |
33 | 40 | #include <algorithm> |
34 | 41 | #include <atomic> |
35 | 42 | #include <bitset> |
@@ -436,6 +443,47 @@ SBError Driver::ProcessArgs(const opt::InputArgList &args, bool &exiting) { |
436 | 443 | return error; |
437 | 444 | } |
438 | 445 |
|
| 446 | +#ifdef _WIN32 |
| 447 | +/// Returns the full path to the lldb.exe executable. |
| 448 | +inline std::wstring GetPathToExecutableW() { |
| 449 | + // Iterate until we reach the Windows API maximum path length (32,767). |
| 450 | + std::vector<WCHAR> buffer; |
| 451 | + buffer.resize(MAX_PATH /*=260*/); |
| 452 | + while (buffer.size() < 32767) { |
| 453 | + if (GetModuleFileNameW(NULL, buffer.data(), buffer.size()) < buffer.size()) |
| 454 | + return std::wstring(buffer.begin(), buffer.end()); |
| 455 | + buffer.resize(buffer.size() * 2); |
| 456 | + } |
| 457 | + return L""; |
| 458 | +} |
| 459 | + |
| 460 | +/// Resolve the full path of the directory defined by |
| 461 | +/// LLDB_PYTHON_DLL_RELATIVE_PATH. If it exists, add it to the list of DLL |
| 462 | +/// search directories. |
| 463 | +void AddPythonDLLToSearchPath() { |
| 464 | + std::wstring modulePath = GetPathToExecutableW(); |
| 465 | + if (modulePath.empty()) { |
| 466 | + llvm::errs() << "error: unable to find python.dll." << '\n'; |
| 467 | + return; |
| 468 | + } |
| 469 | + |
| 470 | + SmallVector<char, MAX_PATH> utf8Path; |
| 471 | + if (sys::windows::UTF16ToUTF8(modulePath.c_str(), modulePath.length(), |
| 472 | + utf8Path)) |
| 473 | + return; |
| 474 | + sys::path::remove_filename(utf8Path); |
| 475 | + sys::path::append(utf8Path, LLDB_PYTHON_DLL_RELATIVE_PATH); |
| 476 | + sys::fs::make_absolute(utf8Path); |
| 477 | + |
| 478 | + SmallVector<wchar_t, 1> widePath; |
| 479 | + if (sys::windows::widenPath(utf8Path.data(), widePath)) |
| 480 | + return; |
| 481 | + |
| 482 | + if (sys::fs::exists(utf8Path)) |
| 483 | + SetDllDirectoryW(widePath.data()); |
| 484 | +} |
| 485 | +#endif |
| 486 | + |
439 | 487 | std::string EscapeString(std::string arg) { |
440 | 488 | std::string::size_type pos = 0; |
441 | 489 | while ((pos = arg.find_first_of("\"\\", pos)) != std::string::npos) { |
@@ -738,6 +786,10 @@ int main(int argc, char const *argv[]) { |
738 | 786 | "~/Library/Logs/DiagnosticReports/.\n"); |
739 | 787 | #endif |
740 | 788 |
|
| 789 | +#if defined(_WIN32) && defined(LLDB_PYTHON_DLL_RELATIVE_PATH) |
| 790 | + AddPythonDLLToSearchPath(); |
| 791 | +#endif |
| 792 | + |
741 | 793 | // Parse arguments. |
742 | 794 | LLDBOptTable T; |
743 | 795 | unsigned MissingArgIndex; |
|
0 commit comments