Implement finding PySide2 and Shiboken2 without config

This commit is contained in:
Florian Märkl 2019-03-10 23:21:50 +01:00
parent defc9ead43
commit 48260cc534
4 changed files with 77 additions and 7 deletions

View File

@ -86,8 +86,8 @@ if(CUTTER_ENABLE_PYTHON)
if(CUTTER_ENABLE_PYTHON_BINDINGS) if(CUTTER_ENABLE_PYTHON_BINDINGS)
find_package(PythonInterp REQUIRED) find_package(PythonInterp REQUIRED)
find_package(Shiboken2 REQUIRED) find_package(Shiboken2 "${Qt5_VERSION}" REQUIRED)
find_package(PySide2 REQUIRED) find_package(PySide2 "${Qt5_VERSION}" REQUIRED)
set(PYSIDE_LIBRARY PySide2::pyside2) set(PYSIDE_LIBRARY PySide2::pyside2)
get_target_property(PYSIDE_INCLUDE_DIR PySide2::pyside2 INTERFACE_INCLUDE_DIRECTORIES) get_target_property(PYSIDE_INCLUDE_DIR PySide2::pyside2 INTERFACE_INCLUDE_DIRECTORIES)

View File

@ -1,16 +1,32 @@
set(_module PySide2) set(_module PySide2)
find_package(${_module} ${${_module}_FIND_VERSION} CONFIG) find_package(${_module} ${${_module}_FIND_VERSION} CONFIG QUIET)
if(NOT ${_module}_FOUND) 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() endif()
include(FindPackageHandleStandardArgs) include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(${_module} find_package_handle_standard_args(${_module}
FOUND_VAR ${_module}_FOUND FOUND_VAR ${_module}_FOUND
REQUIRED_VARS PYSIDE_LIBRARY PYSIDE_INCLUDE_DIR REQUIRED_VARS PYSIDE_LIBRARY PYSIDE_INCLUDE_DIR PYSIDE_TYPESYSTEMS
VERSION_VAR ${_module}_VERSION) VERSION_VAR ${_module}_VERSION)
if(NOT TARGET ${_module}::pyside2) if(NOT TARGET ${_module}::pyside2)

View File

@ -1,10 +1,26 @@
set(_module Shiboken2) set(_module Shiboken2)
find_package(${_module} ${${_module}_FIND_VERSION} CONFIG) find_package(${_module} ${${_module}_FIND_VERSION} CONFIG QUIET)
if(NOT ${_module}_FOUND) 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() endif()
include(FindPackageHandleStandardArgs) include(FindPackageHandleStandardArgs)

View File

@ -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()