|
20 | 20 | #include "lldb/API/SBStructuredData.h"
|
21 | 21 | #include "lldb/Host/Config.h"
|
22 | 22 |
|
| 23 | +#include "llvm/ADT/SmallString.h" |
23 | 24 | #include "llvm/ADT/StringRef.h"
|
| 25 | +#include "llvm/Support/ConvertUTF.h" |
| 26 | +#include "llvm/Support/FileSystem.h" |
24 | 27 | #include "llvm/Support/Format.h"
|
25 | 28 | #include "llvm/Support/InitLLVM.h"
|
26 | 29 | #include "llvm/Support/Path.h"
|
27 | 30 | #include "llvm/Support/Signals.h"
|
28 | 31 | #include "llvm/Support/WithColor.h"
|
29 | 32 | #include "llvm/Support/raw_ostream.h"
|
30 | 33 |
|
| 34 | +#ifdef _WIN32 |
| 35 | +#include "llvm/Support/Windows/WindowsSupport.h" |
| 36 | +#endif |
| 37 | + |
31 | 38 | #include <algorithm>
|
32 | 39 | #include <atomic>
|
33 | 40 | #include <bitset>
|
@@ -423,6 +430,47 @@ SBError Driver::ProcessArgs(const opt::InputArgList &args, bool &exiting) {
|
423 | 430 | return error;
|
424 | 431 | }
|
425 | 432 |
|
| 433 | +#ifdef _WIN32 |
| 434 | +/// Returns the full path to the lldb.exe executable. |
| 435 | +inline std::wstring GetPathToExecutableW() { |
| 436 | + // Iterate until we reach the Windows API maximum path length (32,767). |
| 437 | + std::vector<WCHAR> buffer; |
| 438 | + buffer.resize(MAX_PATH /*=260*/); |
| 439 | + while (buffer.size() < 32767) { |
| 440 | + if (GetModuleFileNameW(NULL, buffer.data(), buffer.size()) < buffer.size()) |
| 441 | + return std::wstring(buffer.begin(), buffer.end()); |
| 442 | + buffer.resize(buffer.size() * 2); |
| 443 | + } |
| 444 | + return L""; |
| 445 | +} |
| 446 | + |
| 447 | +/// Resolve the full path of the directory defined by |
| 448 | +/// LLDB_PYTHON_DLL_RELATIVE_PATH. If it exists, add it to the list of DLL |
| 449 | +/// search directories. |
| 450 | +void AddPythonDLLToSearchPath() { |
| 451 | + std::wstring modulePath = GetPathToExecutableW(); |
| 452 | + if (modulePath.empty()) { |
| 453 | + llvm::errs() << "error: unable to find python.dll." << '\n'; |
| 454 | + return; |
| 455 | + } |
| 456 | + |
| 457 | + SmallVector<char, MAX_PATH> utf8Path; |
| 458 | + if (sys::windows::UTF16ToUTF8(modulePath.c_str(), modulePath.length(), |
| 459 | + utf8Path)) |
| 460 | + return; |
| 461 | + sys::path::remove_filename(utf8Path); |
| 462 | + sys::path::append(utf8Path, LLDB_PYTHON_DLL_RELATIVE_PATH); |
| 463 | + sys::fs::make_absolute(utf8Path); |
| 464 | + |
| 465 | + SmallVector<wchar_t, 1> widePath; |
| 466 | + if (sys::windows::widenPath(utf8Path.data(), widePath)) |
| 467 | + return; |
| 468 | + |
| 469 | + if (sys::fs::exists(utf8Path)) |
| 470 | + SetDllDirectoryW(widePath.data()); |
| 471 | +} |
| 472 | +#endif |
| 473 | + |
426 | 474 | std::string EscapeString(std::string arg) {
|
427 | 475 | std::string::size_type pos = 0;
|
428 | 476 | while ((pos = arg.find_first_of("\"\\", pos)) != std::string::npos) {
|
@@ -753,6 +801,10 @@ int main(int argc, char const *argv[]) {
|
753 | 801 | "~/Library/Logs/DiagnosticReports/.\n");
|
754 | 802 | #endif
|
755 | 803 |
|
| 804 | +#if defined(_WIN32) && defined(LLDB_PYTHON_DLL_RELATIVE_PATH) |
| 805 | + AddPythonDLLToSearchPath(); |
| 806 | +#endif |
| 807 | + |
756 | 808 | // Parse arguments.
|
757 | 809 | LLDBOptTable T;
|
758 | 810 | unsigned MissingArgIndex;
|
|
0 commit comments