|
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"
|
| 31 | +#include "llvm/Support/Windows/WindowsSupport.h" |
28 | 32 | #include "llvm/Support/WithColor.h"
|
29 | 33 | #include "llvm/Support/raw_ostream.h"
|
30 | 34 |
|
@@ -423,6 +427,48 @@ SBError Driver::ProcessArgs(const opt::InputArgList &args, bool &exiting) {
|
423 | 427 | return error;
|
424 | 428 | }
|
425 | 429 |
|
| 430 | +#ifdef _WIN32 |
| 431 | +// Returns the full path to the lldb.exe executable |
| 432 | +inline std::wstring GetPathToExecutableW() { |
| 433 | + // Iterate until we reach the Windows max path length (32,767). |
| 434 | + std::vector<WCHAR> buffer; |
| 435 | + buffer.resize(MAX_PATH); |
| 436 | + while (buffer.size() < 32767) { |
| 437 | + if (GetModuleFileNameW(NULL, buffer.data(), buffer.size()) < buffer.size()) |
| 438 | + return std::wstring(buffer.begin(), buffer.end()); |
| 439 | + buffer.resize(buffer.size() * 2); |
| 440 | + } |
| 441 | + return L""; |
| 442 | +} |
| 443 | + |
| 444 | +// Resolve the Embeddable Python directory bundled with the Swift toolchain. |
| 445 | +// If it exists, add it to the list of DLL search directories. |
| 446 | +// The installation of Embeddable Python is optional when installing the Swift |
| 447 | +// toolchain, therefore the directory might not exist. |
| 448 | +void AddPythonDLLToSearchPath() { |
| 449 | + std::wstring modulePath = GetPathToExecutableW(); |
| 450 | + if (modulePath.empty()) { |
| 451 | + WithColor::error() << "Unable to find python: " << GetLastError() << '\n'; |
| 452 | + return; |
| 453 | + } |
| 454 | + |
| 455 | + SmallVector<char, MAX_PATH> utf8Path; |
| 456 | + if (sys::windows::UTF16ToUTF8(modulePath.c_str(), modulePath.length(), |
| 457 | + utf8Path)) |
| 458 | + return; |
| 459 | + sys::path::remove_filename(utf8Path); |
| 460 | + sys::path::append(utf8Path, LLDB_PYTHON_DLL_RELATIVE_PATH); |
| 461 | + sys::fs::make_absolute(utf8Path); |
| 462 | + |
| 463 | + SmallVector<wchar_t, 1> widePath; |
| 464 | + if (sys::windows::widenPath(utf8Path.data(), widePath)) |
| 465 | + return; |
| 466 | + |
| 467 | + if (sys::fs::exists(utf8Path)) |
| 468 | + SetDllDirectoryW(widePath.data()); |
| 469 | +} |
| 470 | +#endif |
| 471 | + |
426 | 472 | std::string EscapeString(std::string arg) {
|
427 | 473 | std::string::size_type pos = 0;
|
428 | 474 | while ((pos = arg.find_first_of("\"\\", pos)) != std::string::npos) {
|
@@ -753,6 +799,10 @@ int main(int argc, char const *argv[]) {
|
753 | 799 | "~/Library/Logs/DiagnosticReports/.\n");
|
754 | 800 | #endif
|
755 | 801 |
|
| 802 | +#ifdef _WIN32 |
| 803 | + AddPythonDLLToSearchPath(); |
| 804 | +#endif |
| 805 | + |
756 | 806 | // Parse arguments.
|
757 | 807 | LLDBOptTable T;
|
758 | 808 | unsigned MissingArgIndex;
|
|
0 commit comments