From 48260cc534656d88a283a05984e8b3477d68484f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Sun, 10 Mar 2019 23:21:50 +0100 Subject: [PATCH] Implement finding PySide2 and Shiboken2 without config --- src/CMakeLists.txt | 4 ++-- src/cmake/FindPySide2.cmake | 22 +++++++++++++++++--- src/cmake/FindShiboken2.cmake | 20 ++++++++++++++++-- src/cmake/PythonInfo.cmake | 38 +++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 src/cmake/PythonInfo.cmake diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7e53e1fe..081d9885 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -86,8 +86,8 @@ if(CUTTER_ENABLE_PYTHON) if(CUTTER_ENABLE_PYTHON_BINDINGS) find_package(PythonInterp REQUIRED) - find_package(Shiboken2 REQUIRED) - find_package(PySide2 REQUIRED) + find_package(Shiboken2 "${Qt5_VERSION}" REQUIRED) + find_package(PySide2 "${Qt5_VERSION}" REQUIRED) set(PYSIDE_LIBRARY PySide2::pyside2) get_target_property(PYSIDE_INCLUDE_DIR PySide2::pyside2 INTERFACE_INCLUDE_DIRECTORIES) diff --git a/src/cmake/FindPySide2.cmake b/src/cmake/FindPySide2.cmake index 5c693383..c9f71072 100644 --- a/src/cmake/FindPySide2.cmake +++ b/src/cmake/FindPySide2.cmake @@ -1,16 +1,32 @@ set(_module PySide2) -find_package(${_module} ${${_module}_FIND_VERSION} CONFIG) +find_package(${_module} ${${_module}_FIND_VERSION} CONFIG QUIET) if(NOT ${_module}_FOUND) - # TODO + include(PythonInfo) + find_python_site_packages(PYTHON_SITE_PACKAGES) + get_python_extension_suffix(PYTHON_EXTENSION_SUFFIX) + + find_library(PYSIDE_LIBRARY + NAMES + "pyside2${PYTHON_EXTENSION_SUFFIX}" + "pyside2${PYTHON_EXTENSION_SUFFIX}.${${_module}_FIND_VERSION_MAJOR}.${${_module}_FIND_VERSION_MINOR}" + PATH_SUFFIXES "${PYTHON_SITE_PACKAGES}/PySide2") + + find_path(PYSIDE_INCLUDE_DIR + pyside.h + PATH_SUFFIXES "${PYTHON_SITE_PACKAGES}/PySide2/include") + + find_path(PYSIDE_TYPESYSTEMS + typesystem_core.xml + PATH_SUFFIXES "${PYTHON_SITE_PACKAGES}/PySide2/typesystems") endif() include(FindPackageHandleStandardArgs) find_package_handle_standard_args(${_module} FOUND_VAR ${_module}_FOUND - REQUIRED_VARS PYSIDE_LIBRARY PYSIDE_INCLUDE_DIR + REQUIRED_VARS PYSIDE_LIBRARY PYSIDE_INCLUDE_DIR PYSIDE_TYPESYSTEMS VERSION_VAR ${_module}_VERSION) if(NOT TARGET ${_module}::pyside2) diff --git a/src/cmake/FindShiboken2.cmake b/src/cmake/FindShiboken2.cmake index 0df7bd03..41356b17 100644 --- a/src/cmake/FindShiboken2.cmake +++ b/src/cmake/FindShiboken2.cmake @@ -1,10 +1,26 @@ set(_module Shiboken2) -find_package(${_module} ${${_module}_FIND_VERSION} CONFIG) +find_package(${_module} ${${_module}_FIND_VERSION} CONFIG QUIET) if(NOT ${_module}_FOUND) - # TODO + include(PythonInfo) + find_python_site_packages(PYTHON_SITE_PACKAGES) + get_python_extension_suffix(PYTHON_EXTENSION_SUFFIX) + + find_library(SHIBOKEN_LIBRARY + NAMES + "shiboken2${PYTHON_EXTENSION_SUFFIX}" + "shiboken2${PYTHON_EXTENSION_SUFFIX}.${${_module}_FIND_VERSION_MAJOR}.${${_module}_FIND_VERSION_MINOR}" + PATH_SUFFIXES "${PYTHON_SITE_PACKAGES}/shiboken2") + + find_path(SHIBOKEN_INCLUDE_DIR + shiboken.h + PATH_SUFFIXES "${PYTHON_SITE_PACKAGES}/shiboken2_generator/include") + + find_file(SHIBOKEN_BINARY + shiboken2 + PATH_SUFFIXES "${PYTHON_SITE_PACKAGES}/shiboken2_generator") endif() include(FindPackageHandleStandardArgs) diff --git a/src/cmake/PythonInfo.cmake b/src/cmake/PythonInfo.cmake new file mode 100644 index 00000000..acd43bf8 --- /dev/null +++ b/src/cmake/PythonInfo.cmake @@ -0,0 +1,38 @@ + +function(find_python_site_packages VAR) + if(Python_SITELIB) + set("${VAR}" "${Python_SITELIB}" PARENT_SCOPE) + return() + endif() + + execute_process( + COMMAND "${PYTHON_EXECUTABLE}" -c "if True: + from distutils import sysconfig + print(sysconfig.get_python_lib(prefix='', plat_specific=True))" + OUTPUT_VARIABLE "${VAR}" + OUTPUT_STRIP_TRAILING_WHITESPACE) + set("${VAR}" "${${VAR}}" PARENT_SCOPE) +endfunction() + +function(get_python_extension_suffix VAR) + # from PySide2 CMakeLists.txt + # Result of imp.get_suffixes() depends on the platform, but generally looks something like: + # [('.cpython-34m-x86_64-linux-gnu.so', 'rb', 3), ('.cpython-34m.so', 'rb', 3), + # ('.abi3.so', 'rb', 3), ('.so', 'rb', 3), ('.py', 'r', 1), ('.pyc', 'rb', 2)] + # We pick the first most detailed one, strip of the file extension part. + + execute_process( + COMMAND "${PYTHON_EXECUTABLE}" -c "if True: + import importlib.machinery, re + first_suffix = importlib.machinery.EXTENSION_SUFFIXES[0] + res = re.search(r'^(.+)\\.', first_suffix) + if res: + first_suffix = res.group(1) + else: + first_suffix = '' + print(first_suffix) + " + OUTPUT_VARIABLE "${VAR}" + OUTPUT_STRIP_TRAILING_WHITESPACE) + set("${VAR}" "${${VAR}}" PARENT_SCOPE) +endfunction() \ No newline at end of file