|
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> |
@@ -426,6 +433,47 @@ SBError Driver::ProcessArgs(const opt::InputArgList &args, bool &exiting) { |
426 | 433 | return error; |
427 | 434 | } |
428 | 435 |
|
| 436 | +#ifdef _WIN32 |
| 437 | +/// Returns the full path to the lldb.exe executable. |
| 438 | +inline std::wstring GetPathToExecutableW() { |
| 439 | + // Iterate until we reach the Windows API maximum path length (32,767). |
| 440 | + std::vector<WCHAR> buffer; |
| 441 | + buffer.resize(MAX_PATH /*=260*/); |
| 442 | + while (buffer.size() < 32767) { |
| 443 | + if (GetModuleFileNameW(NULL, buffer.data(), buffer.size()) < buffer.size()) |
| 444 | + return std::wstring(buffer.begin(), buffer.end()); |
| 445 | + buffer.resize(buffer.size() * 2); |
| 446 | + } |
| 447 | + return L""; |
| 448 | +} |
| 449 | + |
| 450 | +/// Resolve the full path of the directory defined by |
| 451 | +/// LLDB_PYTHON_DLL_RELATIVE_PATH. If it exists, add it to the list of DLL |
| 452 | +/// search directories. |
| 453 | +void AddPythonDLLToSearchPath() { |
| 454 | + std::wstring modulePath = GetPathToExecutableW(); |
| 455 | + if (modulePath.empty()) { |
| 456 | + llvm::errs() << "error: unable to find python.dll." << '\n'; |
| 457 | + return; |
| 458 | + } |
| 459 | + |
| 460 | + SmallVector<char, MAX_PATH> utf8Path; |
| 461 | + if (sys::windows::UTF16ToUTF8(modulePath.c_str(), modulePath.length(), |
| 462 | + utf8Path)) |
| 463 | + return; |
| 464 | + sys::path::remove_filename(utf8Path); |
| 465 | + sys::path::append(utf8Path, LLDB_PYTHON_DLL_RELATIVE_PATH); |
| 466 | + sys::fs::make_absolute(utf8Path); |
| 467 | + |
| 468 | + SmallVector<wchar_t, 1> widePath; |
| 469 | + if (sys::windows::widenPath(utf8Path.data(), widePath)) |
| 470 | + return; |
| 471 | + |
| 472 | + if (sys::fs::exists(utf8Path)) |
| 473 | + SetDllDirectoryW(widePath.data()); |
| 474 | +} |
| 475 | +#endif |
| 476 | + |
429 | 477 | std::string EscapeString(std::string arg) { |
430 | 478 | std::string::size_type pos = 0; |
431 | 479 | while ((pos = arg.find_first_of("\"\\", pos)) != std::string::npos) { |
@@ -728,6 +776,10 @@ int main(int argc, char const *argv[]) { |
728 | 776 | "~/Library/Logs/DiagnosticReports/.\n"); |
729 | 777 | #endif |
730 | 778 |
|
| 779 | +#if defined(_WIN32) && defined(LLDB_PYTHON_DLL_RELATIVE_PATH) |
| 780 | + AddPythonDLLToSearchPath(); |
| 781 | +#endif |
| 782 | + |
731 | 783 | // Parse arguments. |
732 | 784 | LLDBOptTable T; |
733 | 785 | unsigned MissingArgIndex; |
|
0 commit comments