Warning, /network/ruqola/cmake/Python/PySide2ModuleBuild.cmake is written in an unsupported language. File is not indexed.

0001 #
0002 # SPDX-FileCopyrightText: 2020 Klarälvdalens Datakonsult AB a KDAB Group company <info@kdab.com>
0003 # Author: Renato Araujo Oliveira Filho <renato.araujo@kdab.com>
0004 #
0005 # SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
0006 #
0007 # Contact KDAB at <info@kdab.com> for commercial licensing options.
0008 #
0009 
0010 if (NOT PYTHON_BINDINGS_INSTALL_PREFIX)
0011     SET(PYTHON_BINDINGS_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX} CACHE FILEPATH "Custom path to install python bindings.")
0012 endif()
0013 
0014 message(STATUS "PYTHON INSTALL PREFIX ${PYTHON_BINDINGS_INSTALL_PREFIX}")
0015 
0016 if (WIN32)
0017     set(PATH_SEP "\;")
0018 else()
0019     set(PATH_SEP ":")
0020 endif()
0021 if (NOT CMAKE_CXX_STANDARD)
0022     set(CMAKE_CXX_STANDARD 17)
0023 endif()
0024 
0025 # On macOS, check if Qt is a framework build. This affects how include paths should be handled.
0026 get_target_property(QtCore_is_framework Qt5::Core FRAMEWORK)
0027 if (QtCore_is_framework)
0028     # Get the path to the framework dir.
0029     list(GET Qt5Core_INCLUDE_DIRS 0 QT_INCLUDE_DIR)
0030     get_filename_component(QT_FRAMEWORK_INCLUDE_DIR "${QT_INCLUDE_DIR}/../" ABSOLUTE)
0031 
0032     # QT_INCLUDE_DIR points to the QtCore.framework directory, so we need to adjust this to point
0033     # to the actual include directory, which has include files for non-framework parts of Qt.
0034     get_filename_component(QT_INCLUDE_DIR "${QT_INCLUDE_DIR}/../../include" ABSOLUTE)
0035 endif()
0036 
0037 # Flags that we will pass to shiboken-generator
0038 # --generator-set=shiboken:  tells the generator that we want to use shiboken to generate code,
0039 #                            a doc generator is also available
0040 # --enable-parent-ctor-heuristic: Enable heuristics to detect parent relationship on constructors,
0041 #                           this try to guess parent ownership based on the arguments of the constructors
0042 # --enable-pyside-extensionsL: This will generate code for Qt based classes, adding extra attributes,
0043 #                           like signal, slot;
0044 # --enable-return-value-heuristic: Similar as --enable-parent-ctor-heuristic this use some logic to guess
0045 #                           parent child relationship based on the returned argument
0046 # --use-isnull-as-nb_nonzero: If a class have an isNull() const method, it will be used to compute
0047 #                           the value of boolean casts.
0048 #                           Example, QImage::isNull() will be used when on python side you do `if (myQImage)`
0049 set(GENERATOR_EXTRA_FLAGS --generator-set=shiboken
0050                           --enable-parent-ctor-heuristic
0051                           --enable-pyside-extensions
0052                           --enable-return-value-heuristic
0053                           --use-isnull-as-nb_nonzero
0054                           -std=c++${CMAKE_CXX_STANDARD})
0055 
0056 # 2017-04-24 The protected hack can unfortunately not be disabled, because
0057 # Clang does produce linker errors when we disable the hack.
0058 # But the ugly workaround in Python is replaced by a shiboken change.
0059 if(WIN32 OR DEFINED AVOID_PROTECTED_HACK)
0060     set(GENERATOR_EXTRA_FLAGS ${GENERATOR_EXTRA_FLAGS} --avoid-protected-hack)
0061     add_definitions(-DAVOID_PROTECTED_HACK)
0062 endif()
0063 
0064 macro(make_path varname)
0065    # accepts any number of path variables
0066    string(REPLACE ";" "${PATH_SEP}" ${varname} "${ARGN}")
0067 endmacro()
0068 
0069 # Creates a PySide module target based on the arguments
0070 # This will:
0071 # 1 - Create a Cmake custom-target that call shiboken-generator passign the correct arguments
0072 # 2 - Create a Cmake library target called "Py${LIBRARY_NAME}" the output name of this target
0073 #     will be changed to match PySide template
0074 # Args:
0075 #   LIBRARY_NAME - The name of the output module
0076 #   TYPESYSTEM_PATHS - A list of paths where shiboken should look for typesystem files
0077 #   INCLUDE_PATHS - Include paths necessary to parse your class. *This is not the same as build*
0078 #   OUTPUT_SOURCES - The files that will be generated by shiboken
0079 #   TARGET_INCLUDE_DIRS - This will be passed to target_include_directories
0080 #   TARGET_LINK_LIBRARIES - This will be passed to target_link_libraries
0081 #   GLOBAL_INCLUDE - A header-file that contains all classes that will be generated
0082 #   TYPESYSTEM_XML - The target binding typesystem (that should be the full path)
0083 #   DEPENDS - This var will be passed to add_custom_command(DEPENDS) so a new generation will be
0084 #             trigger if one of these files changes
0085 #   MODULE_OUTPUT_DIR - Where the library file should be stored
0086 macro(CREATE_PYTHON_BINDINGS
0087         LIBRARY_NAME
0088         TYPESYSTEM_PATHS
0089         INCLUDE_PATHS
0090         OUTPUT_SOURCES
0091         TARGET_INCLUDE_DIRS
0092         TARGET_LINK_LIBRARIES
0093         GLOBAL_INCLUDE
0094         TYPESYSTEM_XML
0095         DEPENDS
0096         MODULE_OUTPUT_DIR)
0097 
0098     # Transform the path separators into something shiboken understands.
0099     make_path(shiboken_include_dirs ${INCLUDE_PATHS})
0100     make_path(shiboken_typesystem_dirs ${TYPESYSTEM_PATHS})
0101     get_property(raw_python_dir_include_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
0102     make_path(python_dir_include_dirs ${raw_python_dir_include_dirs})
0103     set(shiboken_include_dirs "${shiboken_include_dirs}${PATH_SEP}${python_dir_include_dirs}")
0104 
0105     set(shiboken_framework_include_dirs_option "")
0106     if(CMAKE_HOST_APPLE)
0107         set(shiboken_framework_include_dirs "${QT_FRAMEWORK_INCLUDE_DIR}")
0108         make_path(shiboken_framework_include_dirs ${shiboken_framework_include_dirs})
0109         set(shiboken_framework_include_dirs_option "--framework-include-paths=${shiboken_framework_include_dirs}")
0110     endif()
0111     set_property(SOURCE ${OUTPUT_SOURCES} PROPERTY SKIP_AUTOGEN ON)
0112     add_custom_command(OUTPUT ${OUTPUT_SOURCES}
0113         COMMAND $<TARGET_PROPERTY:Shiboken2::shiboken,LOCATION> ${GENERATOR_EXTRA_FLAGS}
0114                 ${GLOBAL_INCLUDE}
0115                 --include-paths=${shiboken_include_dirs}
0116                 --typesystem-paths=${shiboken_typesystem_dirs}
0117                 ${shiboken_framework_include_dirs_option}
0118                 --output-directory=${CMAKE_CURRENT_BINARY_DIR}
0119                 ${TYPESYSTEM_XML}
0120         DEPENDS ${TYPESYSTEM_XML} ${DEPENDS}
0121         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
0122         COMMENT "Running generator for ${LIBRARY_NAME} binding...")
0123 
0124         set(TARGET_NAME "Py${LIBRARY_NAME}")
0125         set(MODULE_NAME "${LIBRARY_NAME}")
0126         add_library(${TARGET_NAME} MODULE ${OUTPUT_SOURCES})
0127 
0128         set_target_properties(${TARGET_NAME} PROPERTIES
0129             PREFIX ""
0130             OUTPUT_NAME ${MODULE_NAME}
0131             LIBRARY_OUTPUT_DIRECTORY ${MODULE_OUTPUT_DIR}
0132         )
0133 
0134         if(WIN32)
0135             set_target_properties(${TARGET_NAME} PROPERTIES SUFFIX ".pyd")
0136         endif()
0137 
0138         target_include_directories(${TARGET_NAME} PUBLIC
0139             ${TARGET_INCLUDE_DIRS}
0140             ${PYSIDE_EXTRA_INCLUDES}
0141         )
0142 
0143         target_link_libraries(${TARGET_NAME}
0144             ${TARGET_LINK_LIBRARIES}
0145             PySide2::pyside2
0146             Shiboken2::libshiboken
0147         )
0148         target_compile_definitions(${TARGET_NAME}
0149             PRIVATE Py_LIMITED_API=0x03050000
0150         )
0151         if(APPLE)
0152             set_property(TARGET ${TARGET_NAME} APPEND PROPERTY
0153                 LINK_FLAGS "-undefined dynamic_lookup")
0154         endif()
0155         install(TARGETS ${TARGET_NAME}
0156             LIBRARY DESTINATION ${PYTHON_BINDINGS_INSTALL_PREFIX}/${TARGET_NAME})
0157 endmacro()