diff options
author | Pavel Labath <labath@google.com> | 2018-06-20 08:35:45 +0000 |
---|---|---|
committer | Pavel Labath <labath@google.com> | 2018-06-20 08:35:45 +0000 |
commit | 2df331b0f78b90a5b0be1e247f3cce97709af964 (patch) | |
tree | 3db24e1ba9d0a8ef84761453bd883172a98d587c /lldb/source/Plugins/ScriptInterpreter | |
parent | f29d777f847b2a3d0aec2a499490fa34815fc918 (diff) | |
download | bcm5719-llvm-2df331b0f78b90a5b0be1e247f3cce97709af964.tar.gz bcm5719-llvm-2df331b0f78b90a5b0be1e247f3cce97709af964.zip |
Remove dependency from Host to python
Summary:
The only reason python was used in the Host module was to compute the
python path. I resolve this the same way as D47384 did for clang, by
moving the path computation into the python plugin and modifying
SBHostOS class to call into this module for ePathTypePythonDir.
Reviewers: zturner, jingham, davide
Subscribers: mgorny, lldb-commits
Differential Revision: https://reviews.llvm.org/D48215
llvm-svn: 335104
Diffstat (limited to 'lldb/source/Plugins/ScriptInterpreter')
3 files changed, 85 insertions, 1 deletions
diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/CMakeLists.txt b/lldb/source/Plugins/ScriptInterpreter/Python/CMakeLists.txt index c337fc7b587..039b5e6eef7 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Python/CMakeLists.txt +++ b/lldb/source/Plugins/ScriptInterpreter/Python/CMakeLists.txt @@ -1,3 +1,16 @@ +if (NOT CMAKE_SYSTEM_NAME MATCHES "Windows" AND NOT LLDB_DISABLE_PYTHON) + # Call a python script to gather the arch-specific libdir for + # modules like the lldb module. + execute_process( + COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/../../../../scripts/get_relative_lib_dir.py + RESULT_VARIABLE get_libdir_status + OUTPUT_VARIABLE relative_libdir + ) + if (get_libdir_status EQUAL 0) + add_definitions(-DLLDB_PYTHON_RELATIVE_LIBDIR="${relative_libdir}") + endif() +endif() + add_lldb_library(lldbPluginScriptInterpreterPython PLUGIN PythonDataObjects.cpp PythonExceptionState.cpp diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp index 101e07b07bb..9ae631717af 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp +++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp @@ -347,6 +347,71 @@ const char *ScriptInterpreterPython::GetPluginDescriptionStatic() { return "Embedded Python interpreter"; } +void ScriptInterpreterPython::ComputePythonDirForApple( + llvm::SmallVectorImpl<char> &path) { + auto style = llvm::sys::path::Style::posix; + + llvm::StringRef path_ref(path.begin(), path.size()); + auto rbegin = llvm::sys::path::rbegin(path_ref, style); + auto rend = llvm::sys::path::rend(path_ref); + auto framework = std::find(rbegin, rend, "LLDB.framework"); + if (framework == rend) { + ComputePythonDirForPosix(path); + return; + } + path.resize(framework - rend); + llvm::sys::path::append(path, style, "LLDB.framework", "Resources", "Python"); +} + +void ScriptInterpreterPython::ComputePythonDirForPosix( + llvm::SmallVectorImpl<char> &path) { + auto style = llvm::sys::path::Style::posix; +#if defined(LLDB_PYTHON_RELATIVE_LIBDIR) + // Build the path by backing out of the lib dir, then building with whatever + // the real python interpreter uses. (e.g. lib for most, lib64 on RHEL + // x86_64). + llvm::sys::path::remove_filename(path, style); + llvm::sys::path::append(path, style, LLDB_PYTHON_RELATIVE_LIBDIR); +#else + llvm::sys::path::append(path, style, + "python" + llvm::Twine(PY_MAJOR_VERSION) + "." + + llvm::Twine(PY_MINOR_VERSION), + "site-packages"); +#endif +} + +void ScriptInterpreterPython::ComputePythonDirForWindows( + llvm::SmallVectorImpl<char> &path) { + auto style = llvm::sys::path::Style::windows; + llvm::sys::path::remove_filename(path, style); + llvm::sys::path::append(path, style, "lib", "site-packages"); + + // This will be injected directly through FileSpec.GetDirectory().SetString(), + // so we need to normalize manually. + std::replace(path.begin(), path.end(), '\\', '/'); +} + +FileSpec ScriptInterpreterPython::GetPythonDir() { + static FileSpec g_spec = []() { + FileSpec spec = HostInfo::GetShlibDir(); + if (!spec) + return FileSpec(); + llvm::SmallString<64> path; + spec.GetPath(path); + +#if defined(__APPLE__) + ComputePythonDirForApple(path); +#elif defined(_WIN32) + ComputePythonDirForWindows(path); +#else + ComputePythonDirForPosix(path); +#endif + spec.GetDirectory().SetString(path); + return spec; + }(); + return g_spec; +} + lldb_private::ConstString ScriptInterpreterPython::GetPluginName() { return GetPluginNameStatic(); } @@ -3098,7 +3163,7 @@ void ScriptInterpreterPython::InitializePrivate() { // that use a backslash as the path separator, this will result in executing // python code containing paths with unescaped backslashes. But Python also // accepts forward slashes, so to make life easier we just use that. - if (FileSpec file_spec = HostInfo::GetPythonDir()) + if (FileSpec file_spec = GetPythonDir()) AddToSysPath(AddLocation::Beginning, file_spec.GetPath(false)); if (FileSpec file_spec = HostInfo::GetShlibDir()) AddToSysPath(AddLocation::Beginning, file_spec.GetPath(false)); diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h index a71fcea7519..628b71f3f47 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h +++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h @@ -445,6 +445,8 @@ public: static const char *GetPluginDescriptionStatic(); + static FileSpec GetPythonDir(); + //------------------------------------------------------------------ // PluginInterface protocol //------------------------------------------------------------------ @@ -509,6 +511,10 @@ protected: static void AddToSysPath(AddLocation location, std::string path); + static void ComputePythonDirForApple(llvm::SmallVectorImpl<char> &path); + static void ComputePythonDirForPosix(llvm::SmallVectorImpl<char> &path); + static void ComputePythonDirForWindows(llvm::SmallVectorImpl<char> &path); + bool EnterSession(uint16_t on_entry_flags, FILE *in, FILE *out, FILE *err); void LeaveSession(); |