|
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"
|
@@ -423,6 +426,60 @@ SBError Driver::ProcessArgs(const opt::InputArgList &args, bool &exiting) {
|
423 | 426 | return error;
|
424 | 427 | }
|
425 | 428 |
|
| 429 | +#ifdef _WIN32 |
| 430 | +inline std::wstring GetPathToExecutableW() { |
| 431 | + // Iterate until we max out the Windows max path length (256*2^7 > 32,767). |
| 432 | + static const size_t maximumIterations = 7; |
| 433 | + std::vector<WCHAR> buffer; |
| 434 | + DWORD bufferSize = MAX_PATH; |
| 435 | + for (size_t i = 0; i < maximumIterations; i++) { |
| 436 | + buffer.resize(bufferSize); |
| 437 | + if (GetModuleFileNameW(NULL, buffer.data(), buffer.size()) < buffer.size()) |
| 438 | + return std::wstring(buffer.begin(), buffer.end()); |
| 439 | + bufferSize *= 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 | + |
| 460 | + SmallString<MAX_PATH> pythonDir(utf8Path.begin(), utf8Path.end()); |
| 461 | + // Python-${Version} is located in the Swift directory and lldb.exe is in: |
| 462 | + // `Swift/Toolchains/x.x.x/usr/bin/lldb.exe`. |
| 463 | + for (int i = 0; i < 5; i++) { |
| 464 | + sys::path::remove_filename(pythonDir); |
| 465 | + } |
| 466 | +#define TO_STRING_IMPL(x) #x |
| 467 | +#define TO_STRING(x) TO_STRING_IMPL(x) |
| 468 | + std::string pythonDirName = "Python-" TO_STRING(Python3_VERSION); |
| 469 | +#undef TO_STRING_IMPL |
| 470 | +#undef TO_STRING |
| 471 | + |
| 472 | + sys::path::append(pythonDir, pythonDirName); |
| 473 | + |
| 474 | + SmallVector<wchar_t, 1> widePythonDir; |
| 475 | + if (sys::windows::UTF8ToUTF16(pythonDir.str(), widePythonDir)) |
| 476 | + return; |
| 477 | + |
| 478 | + if (sys::fs::exists(pythonDir)) |
| 479 | + SetDllDirectoryW(widePythonDir.data()); |
| 480 | +} |
| 481 | +#endif |
| 482 | + |
426 | 483 | std::string EscapeString(std::string arg) {
|
427 | 484 | std::string::size_type pos = 0;
|
428 | 485 | while ((pos = arg.find_first_of("\"\\", pos)) != std::string::npos) {
|
@@ -753,6 +810,10 @@ int main(int argc, char const *argv[]) {
|
753 | 810 | "~/Library/Logs/DiagnosticReports/.\n");
|
754 | 811 | #endif
|
755 | 812 |
|
| 813 | +#ifdef _WIN32 |
| 814 | + AddPythonDLLToSearchPath(); |
| 815 | +#endif |
| 816 | + |
756 | 817 | // Parse arguments.
|
757 | 818 | LLDBOptTable T;
|
758 | 819 | unsigned MissingArgIndex;
|
|
0 commit comments