Skip to content

Commit af218c3

Browse files
[lldb][windows] delay loading liblldb.dll
1 parent 840bc2a commit af218c3

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-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(MSVC)
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(NOT 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: 48 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,47 @@ SBError Driver::ProcessArgs(const opt::InputArgList &args, bool &exiting) {
423426
return error;
424427
}
425428

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+
426470
std::string EscapeString(std::string arg) {
427471
std::string::size_type pos = 0;
428472
while ((pos = arg.find_first_of("\"\\", pos)) != std::string::npos) {
@@ -753,6 +797,10 @@ int main(int argc, char const *argv[]) {
753797
"~/Library/Logs/DiagnosticReports/.\n");
754798
#endif
755799

800+
#ifdef _WIN32
801+
AddPythonDLLToSearchPath();
802+
#endif
803+
756804
// Parse arguments.
757805
LLDBOptTable T;
758806
unsigned MissingArgIndex;

0 commit comments

Comments
 (0)