tfiala created this revision.
tfiala added reviewers: labath, dawn.
tfiala added a subscriber: lldb-commits.

This change fixes 'lldb -P' (retrieve lldb-module python path) to work 
correctly on cmake-based builds on POSIX systems.

Previously, this would assume the python module installation path was in the 
lldb lib directory.  This is not accurate for RHEL, which uses an 
architecture-specific directory name (i.e. lib64 for x86_64 versions).

Now, with this change, the python identified during build is called with a 
script at configure time and builds with a #define for the relative directory 
path used for this system's particular python style.  On RHEL, this turns into 
lib64/python{maj}.{min}/..., while it will be lib/python{maj}.{min}/... on 
other systems.  This removes the lldb code from guessing it in this case.

The old behavior is preserved in the event that the configure-time python 
script fails to determine the relative python module lib dir.

This fixes:
https://llvm.org/bugs/show_bug.cgi?id=25134

http://reviews.llvm.org/D13625

Files:
  scripts/get_relative_lib_dir.py
  source/Host/CMakeLists.txt
  source/Host/posix/HostInfoPosix.cpp
  www/build.html

Index: www/build.html
===================================================================
--- www/build.html
+++ www/build.html
@@ -202,7 +202,7 @@
               <li><a href="http://www.python.org";>Python</a></li>
             </ul>
             <p>So for example, on a Fedora system one might run:</p>
-            <code>&gt; yum install swig python-devel libedit-devel</code>
+            <code>&gt; yum install libedit-devel libxml2-devel ncurses-devel python-devel swig</code>
             <p>On a Debian or Ubuntu system one might run:</p>
             <code>&gt; sudo apt-get install build-essential subversion swig python2.7-dev libedit-dev libncurses5-dev </code>
             <p>or</p>
Index: source/Host/posix/HostInfoPosix.cpp
===================================================================
--- source/Host/posix/HostInfoPosix.cpp
+++ source/Host/posix/HostInfoPosix.cpp
@@ -22,6 +22,7 @@
 #include <mutex>
 #include <netdb.h>
 #include <pwd.h>
+#include <stdlib.h>
 #include <sys/types.h>
 #include <unistd.h>
 
@@ -214,6 +215,19 @@
     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";
@@ -223,6 +237,7 @@
 
     file_spec.GetDirectory().SetCString(raw_path);
     return true;
+#endif
 #else
     return false;
 #endif
Index: source/Host/CMakeLists.txt
===================================================================
--- source/Host/CMakeLists.txt
+++ source/Host/CMakeLists.txt
@@ -41,6 +41,11 @@
   common/XML.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
@@ -70,6 +75,11 @@
     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/FileSystem.cpp
     posix/HostInfoPosix.cpp
@@ -133,4 +143,17 @@
   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()
+
 add_lldb_library(lldbHost ${HOST_SOURCES})
Index: scripts/get_relative_lib_dir.py
===================================================================
--- /dev/null
+++ scripts/get_relative_lib_dir.py
@@ -0,0 +1,44 @@
+import distutils.sysconfig
+import os
+import platform
+import re
+import sys
+
+
+def get_python_relative_libdir():
+    """Returns the appropropriate python libdir relative to the build directory.
+
+    @param exe_path the path to the lldb executable
+
+    @return the python path that needs to be added to sys.path (PYTHONPATH)
+    in order to find the lldb python module.
+    """
+    if platform.system() != 'Linux':
+        return None
+
+    # We currently have a bug in lldb -P that does not account for
+    # architecture variants in python paths for
+    # architecture-specific modules.  Handle the lookup here.
+    # When that bug is fixed, we should just ask lldb for the
+    # right answer always.
+    arch_specific_libdir = distutils.sysconfig.get_python_lib(True, False)
+    split_libdir = arch_specific_libdir.split(os.sep)
+    lib_re = re.compile(r"^lib.+$")
+
+    for i in range(len(split_libdir)):
+        match = lib_re.match(split_libdir[i])
+        if match is not None:
+            # We'll call this the relative root of the lib dir.
+            # Things like RHEL will have an arch-specific python
+            # lib dir, which isn't 'lib' on x86_64.
+            return os.sep.join(split_libdir[i:])
+    # Didn't resolve it.
+    return None
+
+if __name__ == '__main__':
+    lib_dir = get_python_relative_libdir()
+    if lib_dir is not None:
+        sys.stdout.write(lib_dir)
+        sys.exit(0)
+    else:
+        sys.exit(1)
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to