[Lldb-commits] [lldb] r335104 - Remove dependency from Host to python

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Wed Jun 20 01:35:45 PDT 2018


Author: labath
Date: Wed Jun 20 01:35:45 2018
New Revision: 335104

URL: http://llvm.org/viewvc/llvm-project?rev=335104&view=rev
Log:
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

Modified:
    lldb/trunk/include/lldb/Host/macosx/HostInfoMacOSX.h
    lldb/trunk/include/lldb/Host/posix/HostInfoPosix.h
    lldb/trunk/include/lldb/Host/windows/HostInfoWindows.h
    lldb/trunk/source/API/SBHostOS.cpp
    lldb/trunk/source/Host/CMakeLists.txt
    lldb/trunk/source/Host/common/HostInfoBase.cpp
    lldb/trunk/source/Host/macosx/objcxx/HostInfoMacOSX.mm
    lldb/trunk/source/Host/posix/HostInfoPosix.cpp
    lldb/trunk/source/Host/windows/HostInfoWindows.cpp
    lldb/trunk/source/Plugins/ScriptInterpreter/Python/CMakeLists.txt
    lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
    lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h

Modified: lldb/trunk/include/lldb/Host/macosx/HostInfoMacOSX.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/macosx/HostInfoMacOSX.h?rev=335104&r1=335103&r2=335104&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/macosx/HostInfoMacOSX.h (original)
+++ lldb/trunk/include/lldb/Host/macosx/HostInfoMacOSX.h Wed Jun 20 01:35:45 2018
@@ -37,7 +37,6 @@ protected:
   static void ComputeHostArchitectureSupport(ArchSpec &arch_32,
                                              ArchSpec &arch_64);
   static bool ComputeHeaderDirectory(FileSpec &file_spec);
-  static bool ComputePythonDirectory(FileSpec &file_spec);
   static bool ComputeSystemPluginsDirectory(FileSpec &file_spec);
   static bool ComputeUserPluginsDirectory(FileSpec &file_spec);
 };

Modified: lldb/trunk/include/lldb/Host/posix/HostInfoPosix.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/posix/HostInfoPosix.h?rev=335104&r1=335103&r2=335104&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/posix/HostInfoPosix.h (original)
+++ lldb/trunk/include/lldb/Host/posix/HostInfoPosix.h Wed Jun 20 01:35:45 2018
@@ -39,7 +39,6 @@ public:
 protected:
   static bool ComputeSupportExeDirectory(FileSpec &file_spec);
   static bool ComputeHeaderDirectory(FileSpec &file_spec);
-  static bool ComputePythonDirectory(FileSpec &file_spec);
 };
 }
 

Modified: lldb/trunk/include/lldb/Host/windows/HostInfoWindows.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/windows/HostInfoWindows.h?rev=335104&r1=335103&r2=335104&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/windows/HostInfoWindows.h (original)
+++ lldb/trunk/include/lldb/Host/windows/HostInfoWindows.h Wed Jun 20 01:35:45 2018
@@ -39,9 +39,6 @@ public:
 
   static bool GetEnvironmentVar(const std::string &var_name, std::string &var);
 
-protected:
-  static bool ComputePythonDirectory(FileSpec &file_spec);
-
 private:
   static FileSpec m_program_filespec;
 };

Modified: lldb/trunk/source/API/SBHostOS.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBHostOS.cpp?rev=335104&r1=335103&r2=335104&view=diff
==============================================================================
--- lldb/trunk/source/API/SBHostOS.cpp (original)
+++ lldb/trunk/source/API/SBHostOS.cpp Wed Jun 20 01:35:45 2018
@@ -18,6 +18,7 @@
 #include "lldb/Utility/Log.h"
 
 #include "Plugins/ExpressionParser/Clang/ClangHost.h"
+#include "Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h"
 
 #include "llvm/ADT/SmallString.h"
 #include "llvm/Support/Path.h"
@@ -48,7 +49,7 @@ SBFileSpec SBHostOS::GetLLDBPath(lldb::P
     fspec = HostInfo::GetHeaderDir();
     break;
   case ePathTypePythonDir:
-    fspec = HostInfo::GetPythonDir();
+    fspec = ScriptInterpreterPython::GetPythonDir();
     break;
   case ePathTypeLLDBSystemPlugins:
     fspec = HostInfo::GetSystemPluginDir();

Modified: lldb/trunk/source/Host/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/CMakeLists.txt?rev=335104&r1=335103&r2=335104&view=diff
==============================================================================
--- lldb/trunk/source/Host/CMakeLists.txt (original)
+++ lldb/trunk/source/Host/CMakeLists.txt Wed Jun 20 01:35:45 2018
@@ -51,11 +51,6 @@ add_host_subdirectory(common
   common/UDPSocket.cpp
   )
 
-# Keep track of whether we want to provide a define for the
-# Python's architecture-specific lib path (i.e. where a
-# Python lldb module would go).
-set (get_python_libdir 0)
-
 if (NOT LLDB_DISABLE_LIBEDIT)
   add_host_subdirectory(common
     common/Editline.cpp
@@ -66,10 +61,6 @@ add_host_subdirectory(posix
   posix/ConnectionFileDescriptorPosix.cpp
   )
 
-if(NOT LLDB_DISABLE_PYTHON)
-  list(APPEND LLDB_PLUGINS lldbPluginScriptInterpreterPython)
-endif()
-
 if (CMAKE_SYSTEM_NAME MATCHES "Windows")
   add_host_subdirectory(windows
     windows/ConnectionGenericFileWindows.cpp
@@ -86,11 +77,6 @@ if (CMAKE_SYSTEM_NAME MATCHES "Windows")
     windows/Windows.cpp
     )
 else()
-  if (NOT LLDB_DISABLE_PYTHON)
-    # We'll grab the arch-specific python libdir on POSIX systems.
-    set (get_python_libdir 1)
-  endif()
-
   add_host_subdirectory(posix
     posix/DomainSocket.cpp
     posix/FileSystem.cpp
@@ -155,19 +141,6 @@ else()
   endif()
 endif()
 
-if (${get_python_libdir})
-  # 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()
-
 set(EXTRA_LIBS)
 if (CMAKE_SYSTEM_NAME MATCHES "NetBSD")
   list(APPEND EXTRA_LIBS kvm)
@@ -194,7 +167,6 @@ add_lldb_library(lldbHost
     lldbSymbol
     lldbTarget
     lldbUtility
-    ${LLDB_PLUGINS}
     ${EXTRA_LIBS}
     ${LLDBObjCLibs}
 

Modified: lldb/trunk/source/Host/common/HostInfoBase.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/HostInfoBase.cpp?rev=335104&r1=335103&r2=335104&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/HostInfoBase.cpp (original)
+++ lldb/trunk/source/Host/common/HostInfoBase.cpp Wed Jun 20 01:35:45 2018
@@ -58,7 +58,6 @@ struct HostInfoBaseFields {
   FileSpec m_lldb_so_dir;
   FileSpec m_lldb_support_exe_dir;
   FileSpec m_lldb_headers_dir;
-  FileSpec m_lldb_python_dir;
   FileSpec m_lldb_clang_resource_dir;
   FileSpec m_lldb_system_plugin_dir;
   FileSpec m_lldb_user_plugin_dir;
@@ -145,17 +144,6 @@ FileSpec HostInfoBase::GetHeaderDir() {
   return success ? g_fields->m_lldb_headers_dir : FileSpec();
 }
 
-FileSpec HostInfoBase::GetPythonDir() {
-  static llvm::once_flag g_once_flag;
-  static bool success = false;
-  llvm::call_once(g_once_flag, []() {
-    success = HostInfo::ComputePythonDirectory(g_fields->m_lldb_python_dir);
-    Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
-    LLDB_LOG(log, "python dir -> `{0}`", g_fields->m_lldb_python_dir);
-  });
-  return success ? g_fields->m_lldb_python_dir : FileSpec();
-}
-
 FileSpec HostInfoBase::GetSystemPluginDir() {
   static llvm::once_flag g_once_flag;
   static bool success = false;

Modified: lldb/trunk/source/Host/macosx/objcxx/HostInfoMacOSX.mm
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/macosx/objcxx/HostInfoMacOSX.mm?rev=335104&r1=335103&r2=335104&view=diff
==============================================================================
--- lldb/trunk/source/Host/macosx/objcxx/HostInfoMacOSX.mm (original)
+++ lldb/trunk/source/Host/macosx/objcxx/HostInfoMacOSX.mm Wed Jun 20 01:35:45 2018
@@ -7,10 +7,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-#if !defined(LLDB_DISABLE_PYTHON)
-#include "Plugins/ScriptInterpreter/Python/lldb-python.h"
-#endif
-
 #include "lldb/Host/HostInfo.h"
 #include "lldb/Host/macosx/HostInfoMacOSX.h"
 #include "lldb/Utility/Args.h"
@@ -188,36 +184,6 @@ bool HostInfoMacOSX::ComputeHeaderDirect
   return true;
 }
 
-bool HostInfoMacOSX::ComputePythonDirectory(FileSpec &file_spec) {
-#ifndef LLDB_DISABLE_PYTHON
-  FileSpec lldb_file_spec = GetShlibDir();
-  if (!lldb_file_spec)
-    return false;
-
-  std::string raw_path = lldb_file_spec.GetPath();
-
-  size_t framework_pos = raw_path.find("LLDB.framework");
-  if (framework_pos != std::string::npos) {
-    framework_pos += strlen("LLDB.framework");
-    raw_path.resize(framework_pos);
-    raw_path.append("/Resources/Python");
-  } else {
-    llvm::SmallString<256> python_version_dir;
-    llvm::raw_svector_ostream os(python_version_dir);
-    os << "/python" << PY_MAJOR_VERSION << '.' << PY_MINOR_VERSION
-       << "/site-packages";
-
-    // We may get our string truncated. Should we protect this with an assert?
-    raw_path.append(python_version_dir.c_str());
-  }
-  file_spec.GetDirectory().SetString(
-      llvm::StringRef(raw_path.c_str(), raw_path.size()));
-  return true;
-#else
-  return false;
-#endif
-}
-
 bool HostInfoMacOSX::ComputeSystemPluginsDirectory(FileSpec &file_spec) {
   FileSpec lldb_file_spec = GetShlibDir();
   if (!lldb_file_spec)

Modified: lldb/trunk/source/Host/posix/HostInfoPosix.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/posix/HostInfoPosix.cpp?rev=335104&r1=335103&r2=335104&view=diff
==============================================================================
--- lldb/trunk/source/Host/posix/HostInfoPosix.cpp (original)
+++ lldb/trunk/source/Host/posix/HostInfoPosix.cpp Wed Jun 20 01:35:45 2018
@@ -7,10 +7,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-#if !defined(LLDB_DISABLE_PYTHON)
-#include "Plugins/ScriptInterpreter/Python/lldb-python.h"
-#endif
-
 #include "lldb/Host/posix/HostInfoPosix.h"
 #include "lldb/Utility/Log.h"
 
@@ -172,46 +168,6 @@ bool HostInfoPosix::ComputeHeaderDirecto
   return true;
 }
 
-bool HostInfoPosix::ComputePythonDirectory(FileSpec &file_spec) {
-#ifndef LLDB_DISABLE_PYTHON
-  FileSpec lldb_file_spec = GetShlibDir();
-  if (!lldb_file_spec)
-    return false;
-
-  char raw_path[PATH_MAX];
-  lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
-
-#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).
-  char python_path[PATH_MAX];
-  ::snprintf(python_path, sizeof(python_path), "%s/../%s", raw_path,
-             LLDB_PYTHON_RELATIVE_LIBDIR);
-
-  char final_path[PATH_MAX];
-  realpath(python_path, final_path);
-  file_spec.GetDirectory().SetCString(final_path);
-
-  return true;
-#else
-  llvm::SmallString<256> python_version_dir;
-  llvm::raw_svector_ostream os(python_version_dir);
-  os << "/python" << PY_MAJOR_VERSION << '.' << PY_MINOR_VERSION
-     << "/site-packages";
-
-  // We may get our string truncated. Should we protect this with an assert?
-  ::strncat(raw_path, python_version_dir.c_str(),
-            sizeof(raw_path) - strlen(raw_path) - 1);
-
-  file_spec.GetDirectory().SetCString(raw_path);
-  return true;
-#endif
-#else
-  return false;
-#endif
-}
-
 bool HostInfoPosix::GetEnvironmentVar(const std::string &var_name,
                                       std::string &var) {
   if (const char *pvar = ::getenv(var_name.c_str())) {

Modified: lldb/trunk/source/Host/windows/HostInfoWindows.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/windows/HostInfoWindows.cpp?rev=335104&r1=335103&r2=335104&view=diff
==============================================================================
--- lldb/trunk/source/Host/windows/HostInfoWindows.cpp (original)
+++ lldb/trunk/source/Host/windows/HostInfoWindows.cpp Wed Jun 20 01:35:45 2018
@@ -103,18 +103,6 @@ FileSpec HostInfoWindows::GetDefaultShel
   return FileSpec(shell, false);
 }
 
-bool HostInfoWindows::ComputePythonDirectory(FileSpec &file_spec) {
-  FileSpec lldb_file_spec = GetShlibDir();
-  if (!lldb_file_spec)
-    return false;
-  llvm::SmallString<64> path(lldb_file_spec.GetDirectory().AsCString());
-  llvm::sys::path::remove_filename(path);
-  llvm::sys::path::append(path, "lib", "site-packages");
-  std::replace(path.begin(), path.end(), '\\', '/');
-  file_spec.GetDirectory().SetString(path.c_str());
-  return true;
-}
-
 bool HostInfoWindows::GetEnvironmentVar(const std::string &var_name,
                                         std::string &var) {
   std::wstring wvar_name;

Modified: lldb/trunk/source/Plugins/ScriptInterpreter/Python/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/CMakeLists.txt?rev=335104&r1=335103&r2=335104&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/CMakeLists.txt (original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/CMakeLists.txt Wed Jun 20 01:35:45 2018
@@ -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

Modified: lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp?rev=335104&r1=335103&r2=335104&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp (original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp Wed Jun 20 01:35:45 2018
@@ -347,6 +347,71 @@ const char *ScriptInterpreterPython::Get
   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::Initialize
   // 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));

Modified: lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h?rev=335104&r1=335103&r2=335104&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h (original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h Wed Jun 20 01:35:45 2018
@@ -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();




More information about the lldb-commits mailing list