|
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,47 @@ SBError Driver::ProcessArgs(const opt::InputArgList &args, bool &exiting) {
|
423 | 426 | return error;
|
424 | 427 | }
|
425 | 428 |
|
| 429 | +#ifdef _WIN32 |
| 430 | +// Resolve the Embeddable Python directory bundled with the Swift toolchain. |
| 431 | +// If it exists, add it to the list of DLL search directories. |
| 432 | +// The installation of Embeddable Python is optional when installing the Swift |
| 433 | +// toolchain, therefore the directory might not exist. |
| 434 | +void AddPythonDLLToSearchPath() { |
| 435 | + wchar_t buffer[MAX_PATH]; |
| 436 | + DWORD length = GetModuleFileNameW(NULL, buffer, MAX_PATH); |
| 437 | + if (length == 0 || length == MAX_PATH) { |
| 438 | + WithColor::error() << "Failed to get module file name. Error: " |
| 439 | + << GetLastError() << "\n"; |
| 440 | + return; |
| 441 | + } |
| 442 | + |
| 443 | + SmallVector<char, MAX_PATH> utf8Path; |
| 444 | + if (sys::windows::UTF16ToUTF8(buffer, length, utf8Path)) |
| 445 | + return; |
| 446 | + |
| 447 | + SmallString<MAX_PATH> pythonDir(utf8Path.begin(), utf8Path.end()); |
| 448 | + // Python-${Version} is located in the Swift directory and lldb.exe is in: |
| 449 | + // `Swift/Toolchains/x.x.x/usr/bin/lldb.exe`. |
| 450 | + for (int i = 0; i < 5; i++) { |
| 451 | + sys::path::remove_filename(pythonDir); |
| 452 | + } |
| 453 | +#define TO_STRING_IMPL(x) #x |
| 454 | +#define TO_STRING(x) TO_STRING_IMPL(x) |
| 455 | + std::string pythonDirName = "Python-" TO_STRING(Python3_VERSION); |
| 456 | +#undef TO_STRING_IMPL |
| 457 | +#undef TO_STRING |
| 458 | + |
| 459 | + sys::path::append(pythonDir, pythonDirName); |
| 460 | + |
| 461 | + SmallVector<wchar_t, 1> widePythonDir; |
| 462 | + if (sys::windows::UTF8ToUTF16(pythonDir.str(), widePythonDir)) |
| 463 | + return; |
| 464 | + |
| 465 | + if (sys::fs::exists(pythonDir)) |
| 466 | + SetDllDirectoryW(widePythonDir.data()); |
| 467 | +} |
| 468 | +#endif |
| 469 | + |
426 | 470 | std::string EscapeString(std::string arg) {
|
427 | 471 | std::string::size_type pos = 0;
|
428 | 472 | while ((pos = arg.find_first_of("\"\\", pos)) != std::string::npos) {
|
@@ -753,6 +797,10 @@ int main(int argc, char const *argv[]) {
|
753 | 797 | "~/Library/Logs/DiagnosticReports/.\n");
|
754 | 798 | #endif
|
755 | 799 |
|
| 800 | +#ifdef _WIN32 |
| 801 | + AddPythonDLLToSearchPath(); |
| 802 | +#endif |
| 803 | + |
756 | 804 | // Parse arguments.
|
757 | 805 | LLDBOptTable T;
|
758 | 806 | unsigned MissingArgIndex;
|
|
0 commit comments