Skip to content

Commit 8807d68

Browse files
[lldb][windows] delay loading python.dll
1 parent 840bc2a commit 8807d68

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

lldb/cmake/modules/AddLLDB.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,12 @@ function(add_lldb_executable name)
283283
)
284284

285285
target_link_libraries(${name} PRIVATE ${ARG_LINK_LIBS})
286+
if(WIN32)
287+
list(FIND ARG_LINK_LIBS liblldb LIBLLD_INDEX)
288+
if(NOT LIBLLD_INDEX EQUAL -1)
289+
target_link_options(${name} PRIVATE "/DELAYLOAD:$<TARGET_FILE_BASE_NAME:liblldb>.dll")
290+
endif()
291+
endif()
286292
if(CLANG_LINK_CLANG_DYLIB)
287293
target_link_libraries(${name} PRIVATE clang-cpp)
288294
else()

lldb/tools/driver/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ add_dependencies(lldb
2828
${tablegen_deps}
2929
)
3030

31+
if(DEFINED Python3_VERSION)
32+
target_compile_definitions(lldb PRIVATE Python3_VERSION=${Python3_VERSION})
33+
endif()
34+
3135
if(LLDB_BUILD_FRAMEWORK)
3236
# In the build-tree, we know the exact path to the framework directory.
3337
# The installed framework can be in different locations.

lldb/tools/driver/Driver.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
#include "lldb/API/SBStructuredData.h"
2121
#include "lldb/Host/Config.h"
2222

23+
#include "llvm/ADT/SmallString.h"
2324
#include "llvm/ADT/StringRef.h"
25+
#include "llvm/Support/ConvertUTF.h"
26+
#include "llvm/Support/FileSystem.h"
2427
#include "llvm/Support/Format.h"
2528
#include "llvm/Support/InitLLVM.h"
2629
#include "llvm/Support/Path.h"
@@ -423,6 +426,60 @@ SBError Driver::ProcessArgs(const opt::InputArgList &args, bool &exiting) {
423426
return error;
424427
}
425428

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+
426483
std::string EscapeString(std::string arg) {
427484
std::string::size_type pos = 0;
428485
while ((pos = arg.find_first_of("\"\\", pos)) != std::string::npos) {
@@ -753,6 +810,10 @@ int main(int argc, char const *argv[]) {
753810
"~/Library/Logs/DiagnosticReports/.\n");
754811
#endif
755812

813+
#ifdef _WIN32
814+
AddPythonDLLToSearchPath();
815+
#endif
816+
756817
// Parse arguments.
757818
LLDBOptTable T;
758819
unsigned MissingArgIndex;

0 commit comments

Comments
 (0)