diff options
Diffstat (limited to 'examples/widgetbinding')
| -rw-r--r-- | examples/widgetbinding/CMakeLists.txt | 192 | ||||
| -rw-r--r-- | examples/widgetbinding/doc/widgetbinding.md | 2 | ||||
| -rw-r--r-- | examples/widgetbinding/doc/widgetbinding.pyproject | 3 |
3 files changed, 41 insertions, 156 deletions
diff --git a/examples/widgetbinding/CMakeLists.txt b/examples/widgetbinding/CMakeLists.txt index f65c19d1e..acaa897d0 100644 --- a/examples/widgetbinding/CMakeLists.txt +++ b/examples/widgetbinding/CMakeLists.txt @@ -50,77 +50,24 @@ set(generated_sources ${CMAKE_CURRENT_BINARY_DIR}/${bindings_library}/wigglywidget_wrapper.cpp) -# ================================== Shiboken detection ====================================== -# Use provided python interpreter if given. -if(NOT python_interpreter) - if(WIN32 AND "${CMAKE_BUILD_TYPE}" STREQUAL "Debug") - find_program(python_interpreter "python_d") - if(NOT python_interpreter) - message(FATAL_ERROR - "A debug Python interpreter could not be found, which is a requirement when " - "building this example in a debug configuration. Make sure python_d.exe is in " - "PATH.") - endif() - else() - find_program(python_interpreter "python") - if(NOT python_interpreter) - message(FATAL_ERROR - "No Python interpreter could be found. Make sure python is in PATH.") - endif() - endif() -endif() -message(STATUS "Using python interpreter: ${python_interpreter}") - -# Macro to get various pyside / python include / link flags and paths. -# Uses the not entirely supported utils/pyside_config.py file. -macro(pyside_config option output_var) - if(${ARGC} GREATER 2) - set(is_list ${ARGV2}) - else() - set(is_list "") - endif() - - execute_process( - COMMAND ${python_interpreter} "${CMAKE_SOURCE_DIR}/../utils/pyside_config.py" - ${option} - OUTPUT_VARIABLE ${output_var} - OUTPUT_STRIP_TRAILING_WHITESPACE) - - if ("${${output_var}}" STREQUAL "") - message(FATAL_ERROR "Error: Calling pyside_config.py ${option} returned no output.") - endif() - if(is_list) - string (REPLACE " " ";" ${output_var} "${${output_var}}") - endif() -endmacro() - -# Query for the shiboken generator path, Python path, include paths and linker flags. -pyside_config(--shiboken-module-path shiboken_module_path) -pyside_config(--shiboken-generator-path shiboken_generator_path) -pyside_config(--pyside-path pyside_path) -pyside_config(--pyside-include-path pyside_include_dir 1) -pyside_config(--python-include-path python_include_dir) -pyside_config(--shiboken-include-path shiboken_include_dir 1) -pyside_config(--shiboken-module-shared-libraries-cmake shiboken_shared_libraries 0) -pyside_config(--python-link-flags-cmake python_linking_data 0) -pyside_config(--pyside-shared-libraries-cmake pyside_shared_libraries 0) - -# Print the computed variables -message(STATUS "Shiboken module path: ${shiboken_module_path}") -message(STATUS "Shiboken generator path: ${shiboken_generator_path}") -message(STATUS "PySide path: ${pyside_path}") -message(STATUS "PySide include dir: ${pyside_include_dir}") -message(STATUS "Python include path: ${python_include_dir}") -message(STATUS "Shiboken include dir: ${shiboken_include_dir}") -message(STATUS "Shiboken shared libraries: ${shiboken_shared_libraries}") -message(STATUS "Python linking data: ${python_linking_data}") -message(STATUS "PySide shared libraries: ${pyside_shared_libraries}") - -set(shiboken_path "${shiboken_generator_path}/shiboken6${CMAKE_EXECUTABLE_SUFFIX}") -if(NOT EXISTS ${shiboken_path}) - message(FATAL_ERROR "Shiboken executable not found at path: ${shiboken_path}") -endif() - +# ================================== Dependency detection ====================================== + +# Find required packages +find_package(Python COMPONENTS Interpreter Development REQUIRED) +# On RHEL and some other distros, Python wheels and site-packages may be installed under 'lib64' +# instead of 'lib'. The FindPython CMake module may set Python_SITELIB to 'lib', which is incorrect +# for these cases. To ensure compatibility, we override Python_SITELIB by querying Python directly. +# This guarantees the correct site-packages path is used regardless of platform or Python build. +execute_process( + COMMAND ${Python_EXECUTABLE} -c + "import site; print(next(p for p in site.getsitepackages() if 'site-packages' in p))" + OUTPUT_VARIABLE Python_SITELIB + OUTPUT_STRIP_TRAILING_WHITESPACE +) +list(APPEND CMAKE_PREFIX_PATH + "${Python_SITELIB}/shiboken6_generator/lib/cmake" +) +find_package(Shiboken6Tools REQUIRED) # ==================================== RPATH configuration ==================================== @@ -132,7 +79,7 @@ endif() # Enable rpaths so that the built shared libraries find their dependencies. set(CMAKE_SKIP_BUILD_RPATH FALSE) set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) -set(CMAKE_INSTALL_RPATH ${shiboken_module_path} ${CMAKE_CURRENT_SOURCE_DIR}) +set(CMAKE_INSTALL_RPATH ${CMAKE_CURRENT_SOURCE_DIR}) set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) # ============================================================================================= # !!! End of dubious section. @@ -176,81 +123,23 @@ set_property(TARGET ${wiggly_library} PROPERTY PREFIX "") # library can't link to the wiggly library. target_compile_definitions(${wiggly_library} PRIVATE BINDINGS_BUILD) +target_link_libraries(${wiggly_library} PRIVATE Qt6::Widgets) -# ====================== Shiboken target for generating binding C++ files ==================== - - -# Set up the options to pass to shiboken. -set(shiboken_options --generator-set=shiboken --enable-parent-ctor-heuristic - --enable-pyside-extensions --enable-return-value-heuristic --use-isnull-as-nb_nonzero - --avoid-protected-hack - ${INCLUDES} - -I${CMAKE_SOURCE_DIR} - -T${CMAKE_SOURCE_DIR} - -T${pyside_path}/typesystems - --output-directory=${CMAKE_CURRENT_BINARY_DIR} - ) - -set(generated_sources_dependencies ${wrapped_header} ${typesystem_file}) - -# Add custom target to run shiboken to generate the binding cpp files. -add_custom_command(OUTPUT ${generated_sources} - COMMAND ${shiboken_path} - ${shiboken_options} ${wrapped_header} ${typesystem_file} - DEPENDS ${generated_sources_dependencies} - #IMPLICIT_DEPENDS CXX ${wrapped_header} - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} - COMMENT "Running generator for ${typesystem_file}.") - - -# =============================== CMake target - bindings_library ============================= - - -# Set the cpp files which will be used for the bindings library. -set(${bindings_library}_sources ${generated_sources}) -# Define and build the bindings library. -add_library(${bindings_library} SHARED ${${bindings_library}_sources}) +# ====================== Shiboken target for generating binding C++ files ==================== +# Define Qt modules needed +set(qt_modules Core Gui Widgets) -# Apply relevant include and link flags. -target_include_directories(${bindings_library} PRIVATE ${pyside_additional_includes}) -target_include_directories(${bindings_library} PRIVATE ${pyside_include_dir}) -target_include_directories(${bindings_library} PRIVATE ${python_include_dir}) -target_include_directories(${bindings_library} PRIVATE ${shiboken_include_dir}) - -target_link_libraries(${wiggly_library} PRIVATE Qt6::Widgets) -target_link_libraries(${bindings_library} PRIVATE Qt6::Widgets) -target_link_libraries(${bindings_library} PRIVATE ${wiggly_library}) -target_link_libraries(${bindings_library} PRIVATE ${pyside_shared_libraries}) -target_link_libraries(${bindings_library} PRIVATE ${shiboken_shared_libraries}) - -# Adjust the name of generated module. -set_property(TARGET ${bindings_library} PROPERTY PREFIX "") -set_property(TARGET ${bindings_library} PROPERTY OUTPUT_NAME - "${bindings_library}${PYTHON_EXTENSION_SUFFIX}") -if(WIN32) - if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug") - set_property(TARGET ${bindings_library} PROPERTY SUFFIX "_d.pyd") - else() - set_property(TARGET ${bindings_library} PROPERTY SUFFIX ".pyd") - endif() -endif() - -# Make sure the linker doesn't complain about not finding Python symbols on macOS. -if(APPLE) - set_target_properties(${bindings_library} PROPERTIES LINK_FLAGS "-undefined dynamic_lookup") -endif(APPLE) - -# Find and link to the python import library only on Windows. -# On Linux and macOS, the undefined symbols will get resolved by the dynamic linker -# (the symbols will be picked up in the Python executable). -if (WIN32) - list(GET python_linking_data 0 python_libdir) - list(GET python_linking_data 1 python_lib) - find_library(python_link_flags ${python_lib} PATHS ${python_libdir} HINTS ${python_libdir}) - target_link_libraries(${bindings_library} PRIVATE ${python_link_flags}) -endif() +# Create Python bindings using Shiboken6Tools function +shiboken_generator_create_binding( + EXTENSION_TARGET ${bindings_library} + GENERATED_SOURCES ${generated_sources} + HEADERS ${wrapped_header} + TYPESYSTEM_FILE ${typesystem_file} + LIBRARY_TARGET ${wiggly_library} + QT_MODULES Core Gui Widgets +) # ================================= Dubious deployment section ================================ @@ -277,15 +166,14 @@ if(WIN32) set_target_properties(${bindings_library} PROPERTIES LINK_FLAGS "${python_additional_link_flags}") - # Compile a list of shiboken shared libraries to be installed, so that - # the user doesn't have to set the PATH manually to point to the PySide package. - foreach(library_path ${shiboken_shared_libraries}) - string(REGEX REPLACE ".lib$" ".dll" library_path ${library_path}) - file(TO_CMAKE_PATH ${library_path} library_path) - list(APPEND windows_shiboken_shared_libraries "${library_path}") - endforeach() - # ========================================================================================= - # !!! End of dubious section. + # Get the correct DLL path for the current build type + if(CMAKE_BUILD_TYPE STREQUAL "Debug") + get_target_property(dll_path Shiboken6::libshiboken IMPORTED_LOCATION_DEBUG) + else() + get_target_property(dll_path Shiboken6::libshiboken IMPORTED_LOCATION_RELEASE) + endif() + file(TO_CMAKE_PATH "${dll_path}" dll_path) + set(windows_shiboken_shared_libraries "${dll_path}") # ========================================================================================= endif() diff --git a/examples/widgetbinding/doc/widgetbinding.md b/examples/widgetbinding/doc/widgetbinding.md index 910961b1e..5f8232fe8 100644 --- a/examples/widgetbinding/doc/widgetbinding.md +++ b/examples/widgetbinding/doc/widgetbinding.md @@ -34,8 +34,6 @@ The most important files are: * `bindings.h` to include the header of the classes we want to expose * `CMakeList.txt`, with all the instructions to build the shared libraries (DLL, or dylib) - * `pyside_config.py` which is located in the utils directory, one level - up, to get the path for Shiboken and PySide. Now create a `build/` directory, and from inside run `cmake` to use the provided `CMakeLists.txt`: diff --git a/examples/widgetbinding/doc/widgetbinding.pyproject b/examples/widgetbinding/doc/widgetbinding.pyproject index da4219d88..ce3f1faec 100644 --- a/examples/widgetbinding/doc/widgetbinding.pyproject +++ b/examples/widgetbinding/doc/widgetbinding.pyproject @@ -8,6 +8,5 @@ "../wigglywidget.cpp", "../wigglywidget.h", "../wigglywidget.py", - "../CMakeLists.txt", - "../../utils/pyside_config.py"] + "../CMakeLists.txt"] } |
