forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 349
[lldb][windows] delay loading liblldb.dll and manually add Python's DLL directory #11571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
charles-zablit
wants to merge
1
commit into
swiftlang:swift/release/6.2.1
Choose a base branch
from
charles-zablit:charles-zablit/windows/delay-python-loading
base: swift/release/6.2.1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+71
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -20,7 +20,10 @@ | |||||
#include "lldb/API/SBStructuredData.h" | ||||||
#include "lldb/Host/Config.h" | ||||||
|
||||||
#include "llvm/ADT/SmallString.h" | ||||||
#include "llvm/ADT/StringRef.h" | ||||||
#include "llvm/Support/ConvertUTF.h" | ||||||
#include "llvm/Support/FileSystem.h" | ||||||
#include "llvm/Support/Format.h" | ||||||
#include "llvm/Support/InitLLVM.h" | ||||||
#include "llvm/Support/Path.h" | ||||||
|
@@ -423,6 +426,60 @@ SBError Driver::ProcessArgs(const opt::InputArgList &args, bool &exiting) { | |||||
return error; | ||||||
} | ||||||
|
||||||
#ifdef _WIN32 | ||||||
inline std::wstring GetPathToExecutableW() { | ||||||
// Iterate until we max out the Windows max path length (256*2^7 > 32,767). | ||||||
static const size_t maximumIterations = 7; | ||||||
std::vector<WCHAR> buffer; | ||||||
DWORD bufferSize = MAX_PATH; | ||||||
for (size_t i = 0; i < maximumIterations; i++) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if this is better written as:
Suggested change
|
||||||
buffer.resize(bufferSize); | ||||||
if (GetModuleFileNameW(NULL, buffer.data(), buffer.size()) < buffer.size()) | ||||||
return std::wstring(buffer.begin(), buffer.end()); | ||||||
bufferSize *= 2; | ||||||
} | ||||||
return L""; | ||||||
} | ||||||
|
||||||
// Resolve the Embeddable Python directory bundled with the Swift toolchain. | ||||||
// If it exists, add it to the list of DLL search directories. | ||||||
// The installation of Embeddable Python is optional when installing the Swift | ||||||
// toolchain, therefore the directory might not exist. | ||||||
void AddPythonDLLToSearchPath() { | ||||||
std::wstring modulePath = GetPathToExecutableW(); | ||||||
if (modulePath.empty()) { | ||||||
WithColor::error() << "Unable to find python: " << GetLastError() << '\n'; | ||||||
return; | ||||||
} | ||||||
|
||||||
SmallVector<char, MAX_PATH> utf8Path; | ||||||
if (sys::windows::UTF16ToUTF8(modulePath.c_str(), modulePath.length(), | ||||||
utf8Path)) | ||||||
return; | ||||||
|
||||||
SmallString<MAX_PATH> pythonDir(utf8Path.begin(), utf8Path.end()); | ||||||
// Python-${Version} is located in the Swift directory and lldb.exe is in: | ||||||
// `Swift/Toolchains/x.x.x/usr/bin/lldb.exe`. | ||||||
for (int i = 0; i < 5; i++) { | ||||||
sys::path::remove_filename(pythonDir); | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we can make this a CMake configuration option for a relative path, and then upstream this. |
||||||
#define TO_STRING_IMPL(x) #x | ||||||
#define TO_STRING(x) TO_STRING_IMPL(x) | ||||||
std::string pythonDirName = "Python-" TO_STRING(Python3_VERSION); | ||||||
#undef TO_STRING_IMPL | ||||||
#undef TO_STRING | ||||||
|
||||||
sys::path::append(pythonDir, pythonDirName); | ||||||
|
||||||
SmallVector<wchar_t, 1> widePythonDir; | ||||||
if (sys::windows::UTF8ToUTF16(pythonDir.str(), widePythonDir)) | ||||||
return; | ||||||
compnerd marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
if (sys::fs::exists(pythonDir)) | ||||||
SetDllDirectoryW(widePythonDir.data()); | ||||||
} | ||||||
#endif | ||||||
|
||||||
std::string EscapeString(std::string arg) { | ||||||
std::string::size_type pos = 0; | ||||||
while ((pos = arg.find_first_of("\"\\", pos)) != std::string::npos) { | ||||||
|
@@ -753,6 +810,10 @@ int main(int argc, char const *argv[]) { | |||||
"~/Library/Logs/DiagnosticReports/.\n"); | ||||||
#endif | ||||||
|
||||||
#ifdef _WIN32 | ||||||
AddPythonDLLToSearchPath(); | ||||||
#endif | ||||||
|
||||||
// Parse arguments. | ||||||
LLDBOptTable T; | ||||||
unsigned MissingArgIndex; | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.