aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexandru Croitor <alexandru.croitor@qt.io>2017-04-06 18:05:16 +0200
committerAlexandru Croitor <alexandru.croitor@qt.io>2017-04-11 12:58:02 +0000
commit20cc149d52d13721e9465c3c8a3fbc523b328b46 (patch)
tree770e86224cd623fdabaea3783dc5177e52c9dc13
parent9abbb0d49556bae1507456e20509137d01abe457 (diff)
Fix Python extension suffix selection on Linux
This allows building Shiboken in any configuration (debug or release) against any version of a Python interpreter (debug or release). Previously specifying --debug forced the user to use a debug version of the Python interpreter. The patch also removes an unnecessary cmake file, which was used in the past to find the location of Python libraries. This has been handled by CMake itself for a while now. Change-Id: I70edcdfee5fde246c1c41cd5b36353cc18aa9bdd Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io> Reviewed-by: Christian Tismer <tismer@stackless.com>
-rw-r--r--CMakeLists.txt108
-rw-r--r--cmake/Modules/FindPythonInterpWithDebug.cmake57
2 files changed, 76 insertions, 89 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 96fa61e75..5fd2d9e42 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -28,50 +28,94 @@ else()
endif()
## For debugging the PYTHON* variables
-#message("PYTHONLIBS_FOUND: " ${PYTHONLIBS_FOUND})
-#message("PYTHON_LIBRARIES: " ${PYTHON_LIBRARIES})
-#message("PYTHON_INCLUDE_DIRS: " ${PYTHON_INCLUDE_DIRS})
-#message("PYTHON_DEBUG_LIBRARIES: " ${PYTHON_DEBUG_LIBRARIES})
-#message("PYTHONINTERP_FOUND: " ${PYTHONINTERP_FOUND})
-#message("PYTHON_EXECUTABLE: " ${PYTHON_EXECUTABLE})
-#message("PYTHON_VERSION_MAJOR: " ${PYTHON_VERSION_MAJOR})
-#message("PYTHON_VERSION_MINOR: " ${PYTHON_VERSION_MINOR})
-#message("PYTHON_VERSION_PATCH: " ${PYTHON_VERSION_PATCH})
+message("PYTHONLIBS_FOUND: " ${PYTHONLIBS_FOUND})
+message("PYTHON_LIBRARIES: " ${PYTHON_LIBRARIES})
+message("PYTHON_INCLUDE_DIRS: " ${PYTHON_INCLUDE_DIRS})
+message("PYTHON_DEBUG_LIBRARIES: " ${PYTHON_DEBUG_LIBRARIES})
+message("PYTHONINTERP_FOUND: " ${PYTHONINTERP_FOUND})
+message("PYTHON_EXECUTABLE: " ${PYTHON_EXECUTABLE})
+message("PYTHON_VERSION: " ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}.${PYTHON_VERSION_PATCH})
+
+# Queries the python sysconfig for the abi flags which need to be inserted into extension suffixes.
+# Only present starting with Python 3.2.
+# Corresponding configure switches to single letter flags:
+# --with-pymalloc -> m
+# --with-pydebug -> d
+# --with-unicode -> u (rare)
+macro(get_python3_abi_flags)
+ if (NOT PYTHON_ABI_FLAGS)
+ execute_process(
+ COMMAND ${PYTHON_EXECUTABLE} -c "if True:
+ import sysconfig
+ print(sysconfig.get_config_var('abiflags'))
+ "
+ OUTPUT_VARIABLE PYTHON_ABI_FLAGS
+ OUTPUT_STRIP_TRAILING_WHITESPACE)
+ endif()
+ message("PYTHON_ABI_FLAGS: " ${PYTHON_ABI_FLAGS})
+endmacro()
-if (UNIX AND NOT APPLE)
- # TODO: This part needs more testing first to be available on OSX and WIN
- # Also note the quirk that UNIX includes Apple!
+macro(get_python_multi_arch_suffix)
+ # TODO: This part needs testing to check if it is available on Windows.
+ # It is present on macOS, but is not used yet.
+ # Result is something like 'x86_64-linux-gnu'.
if (NOT PYTHON_MULTIARCH_SUFFIX)
execute_process(
- COMMAND ${PYTHON_EXECUTABLE} -c "if True:
- import sysconfig
- print(sysconfig.get_config_var('MULTIARCH'))
- "
- OUTPUT_VARIABLE PYTHON_MULTIARCH_SUFFIX
- OUTPUT_STRIP_TRAILING_WHITESPACE)
+ COMMAND ${PYTHON_EXECUTABLE} -c "if True:
+ import sysconfig
+ print(sysconfig.get_config_var('MULTIARCH'))
+ "
+ OUTPUT_VARIABLE PYTHON_MULTIARCH_SUFFIX
+ OUTPUT_STRIP_TRAILING_WHITESPACE)
endif()
-
+ message("PYTHON_MULTIARCH_SUFFIX: " ${PYTHON_MULTIARCH_SUFFIX})
+endmacro()
+
+macro(get_python2_release_suffix)
+ # Result of imp.get_suffixes() is something like:
+ # [('_d.so', 'rb', 3), ('module_d.so', 'rb', 3), ('.x86_64-linux-gnu_d.so', 'rb', 3)]
+ # or alternatively the same but withut the '_d' part.
+ # The list comprehension is used to choose which suffix to include in library names.
+ execute_process(
+ COMMAND ${PYTHON_EXECUTABLE} -c "if True:
+ import imp
+ print('_d' if any([tup[0].endswith('_d.so') for tup in imp.get_suffixes()]) else '')
+ "
+ OUTPUT_VARIABLE PYTHON_MODULE_RELEASE_SUFFIX
+ OUTPUT_STRIP_TRAILING_WHITESPACE)
+ message("PYTHON_MODULE_RELEASE_SUFFIX: " ${PYTHON_MODULE_RELEASE_SUFFIX})
+endmacro()
+
+# Note the quirk that UNIX includes Apple!
+if (UNIX AND NOT APPLE)
if (NOT PYTHON_EXTENSION_SUFFIX)
+ get_python_multi_arch_suffix()
+ # The suffix added to .so libraries should be differenet between Python 2 and 3.
+ # The position of the multiarch suffix is different, and the way the debug flag is set
+ # computed differently.
+ # In Python 2 there is no standard way to query if the python interpeter was built in debug or
+ # release build (sysconfig.get_config_var('Py_Debug') can have a different value than you would
+ # expect if you do a custom Python build). The solution is to query for the import
+ # suffixes and check if _d is present there. It is present on Linux distribution
+ # packages of Python, but not in custom built Python builds, because the distros apply their
+ # custom patches too append the '_d's.
+ # In Python 3 (starting with 3.2) there is a standard way to check if '_d' needs to be added,
+ # as well as any other letters, by querying the abiflags sysconfig variable.
if (PYTHON_VERSION_MAJOR EQUAL 2)
- if(CMAKE_BUILD_TYPE STREQUAL "Debug")
- set(PYTHON_RELEASE_SUFFIX "-dbg")
- else()
- set(PYTHON_RELEASE_SUFFIX "")
+ get_python2_release_suffix()
+ if(PYTHON_MULTIARCH_SUFFIX)
+ set(PYTHON_EXTENSION_SUFFIX ".${PYTHON_MULTIARCH_SUFFIX}")
endif()
- set(PYTHON_EXTENSION_SUFFIX "-python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}${PYTHON_RELEASE_SUFFIX}")
+ set(PYTHON_EXTENSION_SUFFIX "${PYTHON_EXTENSION_SUFFIX}${PYTHON_MODULE_RELEASE_SUFFIX}")
elseif (PYTHON_VERSION_MAJOR EQUAL 3)
- if(CMAKE_BUILD_TYPE STREQUAL "Debug")
- set(PYTHON_RELEASE_SUFFIX "dm")
- else()
- set(PYTHON_RELEASE_SUFFIX "m")
+ get_python3_abi_flags()
+ set(PYTHON_EXTENSION_SUFFIX ".cpython-${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR}${PYTHON_ABI_FLAGS}")
+ if(PYTHON_MULTIARCH_SUFFIX)
+ set(PYTHON_EXTENSION_SUFFIX "${PYTHON_EXTENSION_SUFFIX}-${PYTHON_MULTIARCH_SUFFIX}")
endif()
- set(PYTHON_EXTENSION_SUFFIX ".cpython-${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR}${PYTHON_RELEASE_SUFFIX}")
else()
message(FATAL_ERROR "Unsupported PYTHON_VERSION=${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}.${PYTHON_VERSION_PATCH}!")
endif()
- if(PYTHON_MULTIARCH_SUFFIX)
- set(PYTHON_EXTENSION_SUFFIX "${PYTHON_EXTENSION_SUFFIX}-${PYTHON_MULTIARCH_SUFFIX}")
- endif()
endif()
message(STATUS "PYTHON_EXTENSION_SUFFIX: ${PYTHON_EXTENSION_SUFFIX}")
endif ()
diff --git a/cmake/Modules/FindPythonInterpWithDebug.cmake b/cmake/Modules/FindPythonInterpWithDebug.cmake
deleted file mode 100644
index df01366dd..000000000
--- a/cmake/Modules/FindPythonInterpWithDebug.cmake
+++ /dev/null
@@ -1,57 +0,0 @@
-find_program(PYTHON_EXECUTABLE NAMES python3.6 python3.5 python3.4 python3.3 python3.2 python2.7 python2.6 python2.5)
-
-if (NOT PYTHON_EXECUTABLE)
- find_package(PythonInterp REQUIRED)
-else()
- set(PYTHONINTERP_FOUND 1)
-endif()
-
-if (PYTHONINTERP_FOUND AND UNIX AND CMAKE_BUILD_TYPE STREQUAL "Debug")
- # This is for Debian
- set(PYTHON_EXECUTABLE_TMP "${PYTHON_EXECUTABLE}-dbg")
-
- if (NOT EXISTS "${PYTHON_EXECUTABLE_TMP}")
- # On Fedora we usually have the suffix as debug. As we didn't
- # find python interpreter with the suffix dbg we'll fall back
- # to the suffix as debug.
- set(PYTHON_EXECUTABLE_TMP "${PYTHON_EXECUTABLE}-debug")
- endif()
- # Falling back to the standard interpreter.
- if (NOT EXISTS "${PYTHON_EXECUTABLE_TMP}")
- set(PYTHON_EXECUTABLE_TMP "${PYTHON_EXECUTABLE}")
- endif()
-
- set(PYTHON_EXECUTABLE "${PYTHON_EXECUTABLE_TMP}")
-endif()
-
-# Detect if the python libs were compiled in debug mode
-execute_process(
- COMMAND ${PYTHON_EXECUTABLE} -c "if True:
- from distutils import sysconfig
- print(bool(sysconfig.get_config_var('Py_DEBUG')))
- "
- OUTPUT_VARIABLE PYTHON_WITH_DEBUG
- OUTPUT_STRIP_TRAILING_WHITESPACE)
-
-execute_process(
- COMMAND ${PYTHON_EXECUTABLE} -c "if True:
- import sys
- from distutils import sysconfig
- vr = sys.version_info
- suffix = '-dbg' if bool(sysconfig.get_config_var('Py_DEBUG')) else ''
- print('-python%d.%d%s' % (vr[0], vr[1], suffix))
- "
- OUTPUT_VARIABLE PYTHON_SUFFIX
- OUTPUT_STRIP_TRAILING_WHITESPACE)
-
-# Fix missing variable on UNIX env
-if (NOT PYTHON_DEBUG_LIBRARIES AND UNIX)
- string(REPLACE "-dbg" "" PYTHON_NAME_TMP ${PYTHON_SUFFIX})
- string(REPLACE "-python" "python" PYTHON_NAME ${PYTHON_NAME_TMP})
- find_library(LIBRARY_FOUND ${PYTHON_NAME}_d)
- if (LIBRARY_FOUND)
- set(PYTHON_DEBUG_LIBRARIES "${LIBRARY_FOUND}")
- else()
- set(PYTHON_DEBUG_LIBRARIES "${PYTHON_LIBRARIES}")
- endif()
-endif()