Skip to content

Commit c76bdbd

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

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

lldb/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ if (LLDB_ENABLE_PYTHON)
8484
"Path to python interpreter exectuable, relative to python's install prefix")
8585
set(cachestring_LLDB_PYTHON_EXT_SUFFIX
8686
"Filename extension for native code python modules")
87+
set(cachestring_LLDB_PYTHON_DLL_RELATIVE_PATH
88+
"Path where python.dll is installed, relative to LLDB's install prefix")
8789

8890
foreach(var LLDB_PYTHON_RELATIVE_PATH LLDB_PYTHON_EXE_RELATIVE_PATH LLDB_PYTHON_EXT_SUFFIX)
8991
if(NOT DEFINED ${var} AND NOT CMAKE_CROSSCOMPILING)

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 LLDB_PYTHON_DLL_RELATIVE_PATH)
32+
target_compile_definitions(lldb PRIVATE LLDB_PYTHON_DLL_RELATIVE_PATH="${LLDB_PYTHON_DLL_RELATIVE_PATH}")
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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@
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"
2730
#include "llvm/Support/Signals.h"
31+
#include "llvm/Support/Windows/WindowsSupport.h"
2832
#include "llvm/Support/WithColor.h"
2933
#include "llvm/Support/raw_ostream.h"
3034

@@ -423,6 +427,48 @@ SBError Driver::ProcessArgs(const opt::InputArgList &args, bool &exiting) {
423427
return error;
424428
}
425429

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+
426472
std::string EscapeString(std::string arg) {
427473
std::string::size_type pos = 0;
428474
while ((pos = arg.find_first_of("\"\\", pos)) != std::string::npos) {
@@ -753,6 +799,10 @@ int main(int argc, char const *argv[]) {
753799
"~/Library/Logs/DiagnosticReports/.\n");
754800
#endif
755801

802+
#ifdef _WIN32
803+
AddPythonDLLToSearchPath();
804+
#endif
805+
756806
// Parse arguments.
757807
LLDBOptTable T;
758808
unsigned MissingArgIndex;

0 commit comments

Comments
 (0)