mgorny updated this revision to Diff 221388.
mgorny retitled this revision from "[lldb] [cmake] Fix installing Python 
modules on systems using /usr/lib" to "[lldb] [cmake] Unify and correct Python 
module installation paths".
mgorny edited the summary of this revision.
mgorny added a comment.

O-kay, please try this patch instead. I think it should fix all the issues and 
avoid inconsistencies between various places this is used. However, I'm not 
sure if it doesn't break exotic platforms.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D67890/new/

https://reviews.llvm.org/D67890

Files:
  lldb/scripts/CMakeLists.txt
  lldb/scripts/Python/prepare_binding_Python.py
  lldb/scripts/get_relative_lib_dir.py
  lldb/scripts/prepare_bindings.py
  lldb/source/Plugins/ScriptInterpreter/Python/CMakeLists.txt
  lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp

Index: lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
===================================================================
--- lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
+++ lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
@@ -312,18 +312,11 @@
 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(
Index: lldb/source/Plugins/ScriptInterpreter/Python/CMakeLists.txt
===================================================================
--- lldb/source/Plugins/ScriptInterpreter/Python/CMakeLists.txt
+++ lldb/source/Plugins/ScriptInterpreter/Python/CMakeLists.txt
@@ -1,15 +1,4 @@
-if (NOT CMAKE_SYSTEM_NAME MATCHES "Windows")
-  # 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_definitions(-DLLDB_PYTHON_RELATIVE_LIBDIR="${LLDB_PYTHON_RELATIVE_PATH}")
 
 add_lldb_library(lldbPluginScriptInterpreterPython PLUGIN
   PythonDataObjects.cpp
Index: lldb/scripts/prepare_bindings.py
===================================================================
--- lldb/scripts/prepare_bindings.py
+++ lldb/scripts/prepare_bindings.py
@@ -129,6 +129,10 @@
     parser.add_argument(
         "--prefix",
         help="Override path where the LLDB module is placed.")
+    parser.add_argument(
+        "--python-relative-path",
+        "--pythonRelativePath",
+        help="Path to store Python modules in, relative to targetDir.")
     parser.add_argument(
         "--src-root",
         "--srcRoot",
Index: lldb/scripts/get_relative_lib_dir.py
===================================================================
--- lldb/scripts/get_relative_lib_dir.py
+++ /dev/null
@@ -1,44 +0,0 @@
-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)
Index: lldb/scripts/Python/prepare_binding_Python.py
===================================================================
--- lldb/scripts/Python/prepare_binding_Python.py
+++ lldb/scripts/Python/prepare_binding_Python.py
@@ -280,12 +280,10 @@
             "Python",
             "lldb")
     else:
-        from distutils.sysconfig import get_python_lib
-
-        if options.prefix is not None:
-            module_path = get_python_lib(True, False, options.prefix)
-        else:
-            module_path = get_python_lib(True, False)
+        module_path = options.python_relative_path
+        if module_path is None:
+            from distutils.sysconfig import get_python_lib
+            module_path = get_python_lib(False, False, '')
         return os.path.normcase(
             os.path.join(module_path, "lldb"))
 
Index: lldb/scripts/CMakeLists.txt
===================================================================
--- lldb/scripts/CMakeLists.txt
+++ lldb/scripts/CMakeLists.txt
@@ -19,6 +19,14 @@
   message(FATAL_ERROR "LLDB requires swig ${SWIG_MIN_VERSION}, your version is ${SWIG_VERSION}.")
 endif()
 
+execute_process(
+  COMMAND ${PYTHON_EXECUTABLE}
+      -c "import distutils.sysconfig; print(distutils.sysconfig.get_python_lib(False, False, ''))"
+  OUTPUT_VARIABLE LLDB_PYTHON_DEFAULT_RELATIVE_PATH
+  OUTPUT_STRIP_TRAILING_WHITESPACE)
+set(LLDB_PYTHON_RELATIVE_PATH ${LLDB_PYTHON_DEFAULT_RELATIVE_PATH}
+  CACHE STRING "Path where Python modules are installed, relative to install prefix")
+
 add_custom_command(
   OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/LLDBWrapPython.cpp
   OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/lldb.py
@@ -33,6 +41,7 @@
       --cfgBldDir=${CMAKE_CURRENT_BINARY_DIR}
       --prefix=${CMAKE_BINARY_DIR}
       --swigExecutable=${SWIG_EXECUTABLE}
+      --pythonRelativePath=${LLDB_PYTHON_RELATIVE_PATH}
   VERBATIM
   COMMENT "Python script building LLDB Python wrapper")
 
@@ -42,15 +51,7 @@
 )
 
 if(NOT LLDB_BUILD_FRAMEWORK)
-  if(CMAKE_SYSTEM_NAME MATCHES "Windows")
-    set(swig_python_subdir site-packages)
-  else()
-    set(swig_python_subdir python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR})
-  endif()
-
-  set(SWIG_PYTHON_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${swig_python_subdir})
-  set(SWIG_INSTALL_DIR lib${LLVM_LIBDIR_SUFFIX})
-
   # Install the LLDB python module
-  install(DIRECTORY ${SWIG_PYTHON_DIR} DESTINATION ${SWIG_INSTALL_DIR})
+  install(DIRECTORY ${CMAKE_BINARY_DIR}/${LLDB_PYTHON_RELATIVE_PATH}/
+          DESTINATION ${SWIG_INSTALL_DIR}/${LLDB_PYTHON_RELATIVE_PATH})
 endif()
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to