Skip to content

Commit

Permalink
tidy up and improve consistency, line lengths
Browse files Browse the repository at this point in the history
  • Loading branch information
berniev committed Nov 18, 2022
1 parent d527d6f commit c29ef73
Showing 1 changed file with 71 additions and 48 deletions.
119 changes: 71 additions & 48 deletions cMake/FreeCAD_Helpers/SetupShibokenAndPyside.cmake
@@ -1,5 +1,4 @@
macro(SetupShibokenAndPyside)
# -------------------------------- Shiboken/PySide ------------------------

if(DEFINED MACPORTS_PREFIX)
find_package(Shiboken REQUIRED HINTS "${PYTHON_LIBRARY_DIR}/cmake")
Expand All @@ -21,12 +20,15 @@ macro(SetupShibokenAndPyside)
set (SAVE_BUILD_TYPE ${CMAKE_BUILD_TYPE})
find_package(Shiboken${SHIBOKEN_MAJOR_VERSION} QUIET)
set (CMAKE_BUILD_TYPE ${SAVE_BUILD_TYPE})

if (Shiboken${SHIBOKEN_MAJOR_VERSION}_FOUND)
# Shiboken config file was found but it may use the wrong Python version
# Try to get the matching config suffix and repeat finding the package
set(SHIBOKEN_PATTERN .cpython-${Python3_VERSION_MAJOR}${Python3_VERSION_MINOR})

file(GLOB SHIBOKEN_CONFIG "${Shiboken${SHIBOKEN_MAJOR_VERSION}_DIR}/Shiboken${SHIBOKEN_MAJOR_VERSION}Config${SHIBOKEN_PATTERN}*.cmake")
file(GLOB SHIBOKEN_CONFIG
"${Shiboken${SHIBOKEN_MAJOR_VERSION}_DIR}\
/Shiboken${SHIBOKEN_MAJOR_VERSION}Config${SHIBOKEN_PATTERN}*.cmake")
if (SHIBOKEN_CONFIG)
get_filename_component(SHIBOKEN_CONFIG_SUFFIX ${SHIBOKEN_CONFIG} NAME)
string(SUBSTRING ${SHIBOKEN_CONFIG_SUFFIX} 15 -1 SHIBOKEN_CONFIG_SUFFIX)
Expand All @@ -38,46 +40,60 @@ macro(SetupShibokenAndPyside)
# pyside2 changed its cmake files, this is the dance we have
# to dance to be compatible with the old (<5.12) and the new versions (>=5.12)
if(NOT SHIBOKEN_INCLUDE_DIR AND TARGET Shiboken${SHIBOKEN_MAJOR_VERSION}::libshiboken)
get_property(SHIBOKEN_INCLUDE_DIR TARGET Shiboken${SHIBOKEN_MAJOR_VERSION}::libshiboken PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
endif(NOT SHIBOKEN_INCLUDE_DIR AND TARGET Shiboken${SHIBOKEN_MAJOR_VERSION}::libshiboken)
get_property(SHIBOKEN_INCLUDE_DIR
TARGET Shiboken${SHIBOKEN_MAJOR_VERSION}::libshiboken PROPERTY
INTERFACE_INCLUDE_DIRECTORIES)
endif()

if(NOT SHIBOKEN_INCLUDE_DIR)
find_pip_package("SHIBOKEN", ${SHIBOKEN_MAJOR_VERSION})
endif()

find_package(PySide${PYSIDE_MAJOR_VERSION} QUIET)
if(NOT PYSIDE_INCLUDE_DIR
AND TARGET PySide${PYSIDE_MAJOR_VERSION}::pyside${PYSIDE_MAJOR_VERSION})
get_property(PYSIDE_INCLUDE_DIR
TARGET PySide${PYSIDE_MAJOR_VERSION}::pyside${PYSIDE_MAJOR_VERSION}
PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
endif()

if(NOT PYSIDE_INCLUDE_DIR AND TARGET PySide${PYSIDE_MAJOR_VERSION}::pyside${PYSIDE_MAJOR_VERSION})
get_property(PYSIDE_INCLUDE_DIR TARGET PySide${PYSIDE_MAJOR_VERSION}::pyside${PYSIDE_MAJOR_VERSION} PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
endif(NOT PYSIDE_INCLUDE_DIR AND TARGET PySide${PYSIDE_MAJOR_VERSION}::pyside${PYSIDE_MAJOR_VERSION})

# try pip installed version
if(NOT PYSIDE_INCLUDE_DIR)
find_pip_package("PYSIDE", ${PYSIDE_MAJOR_VERSION})
endif()

find_package(PySide${PYSIDE_MAJOR_VERSION}Tools QUIET) # PySide utilities (uic & rcc executables)
find_package(PySide${PYSIDE_MAJOR_VERSION}Tools QUIET) # uic & rcc executables
if(NOT PYSIDE_TOOLS_FOUND)
message("=======================\n"
"PySide${PYSIDE_MAJOR_VERSION}Tools not found.\n"
"=======================\n")
endif()

file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/Ext/PySide)
file(WRITE ${CMAKE_BINARY_DIR}/Ext/PySide/__init__.py "# PySide wrapper\n"
"from PySide${PYSIDE_MAJOR_VERSION} import __version__\n"
"from PySide${PYSIDE_MAJOR_VERSION} import __version_info__\n")
file(WRITE ${CMAKE_BINARY_DIR}/Ext/PySide/QtCore.py "from PySide${PYSIDE_MAJOR_VERSION}.QtCore import *\n\n"
"#QCoreApplication.CodecForTr=0\n"
"#QCoreApplication.UnicodeUTF8=1\n")
file(WRITE ${CMAKE_BINARY_DIR}/Ext/PySide/QtNetwork.py "from PySide${PYSIDE_MAJOR_VERSION}.QtNetwork import *\n")
file(WRITE ${CMAKE_BINARY_DIR}/Ext/PySide/__init__.py
"# PySide wrapper\n"
"from PySide${PYSIDE_MAJOR_VERSION} import __version__\n"
"from PySide${PYSIDE_MAJOR_VERSION} import __version_info__\n")
file(WRITE ${CMAKE_BINARY_DIR}/Ext/PySide/QtCore.py
"from PySide${PYSIDE_MAJOR_VERSION}.QtCore import *\n\n"
"#QCoreApplication.CodecForTr=0\n"
"#QCoreApplication.UnicodeUTF8=1\n")
file(WRITE ${CMAKE_BINARY_DIR}/Ext/PySide/QtNetwork.py
"from PySide${PYSIDE_MAJOR_VERSION}.QtNetwork import *\n")

if(BUILD_GUI)
file(WRITE ${CMAKE_BINARY_DIR}/Ext/PySide/QtGui.py "from PySide${PYSIDE_MAJOR_VERSION}.QtGui import *\n"
"from PySide${PYSIDE_MAJOR_VERSION}.QtWidgets import *\n"
"QHeaderView.setResizeMode = QHeaderView.setSectionResizeMode\n")
file(WRITE ${CMAKE_BINARY_DIR}/Ext/PySide/QtSvg.py "from PySide${PYSIDE_MAJOR_VERSION}.QtSvg import *\n")
file(WRITE ${CMAKE_BINARY_DIR}/Ext/PySide/QtUiTools.py "from PySide${PYSIDE_MAJOR_VERSION}.QtUiTools import *\n")
file(WRITE ${CMAKE_BINARY_DIR}/Ext/PySide/QtWidgets.py "from PySide${PYSIDE_MAJOR_VERSION}.QtWidgets import *\n")
file(WRITE ${CMAKE_BINARY_DIR}/Ext/PySide/QtWebEngineWidgets.py "from PySide${PYSIDE_MAJOR_VERSION}.QtWebEngineWidgets import *\n")
file(WRITE ${CMAKE_BINARY_DIR}/Ext/PySide/QtGui.py
"from PySide${PYSIDE_MAJOR_VERSION}.QtGui import *\n"
"from PySide${PYSIDE_MAJOR_VERSION}.QtWidgets import *\n"
"QHeaderView.setResizeMode = QHeaderView.setSectionResizeMode\n")
file(WRITE ${CMAKE_BINARY_DIR}/Ext/PySide/QtSvg.py
"from PySide${PYSIDE_MAJOR_VERSION}.QtSvg import *\n")
file(WRITE ${CMAKE_BINARY_DIR}/Ext/PySide/QtUiTools.py
"from PySide${PYSIDE_MAJOR_VERSION}.QtUiTools import *\n")
file(WRITE ${CMAKE_BINARY_DIR}/Ext/PySide/QtWidgets.py
"from PySide${PYSIDE_MAJOR_VERSION}.QtWidgets import *\n")
file(WRITE ${CMAKE_BINARY_DIR}/Ext/PySide/QtWebEngineWidgets.py
"from PySide${PYSIDE_MAJOR_VERSION}.QtWebEngineWidgets import *\n")
endif()

if(APPLE AND NOT BUILD_WITH_CONDA)
Expand All @@ -96,15 +112,17 @@ macro(SetupShibokenAndPyside)
)
endif()

# If shiboken cannot be found the build option will be set to OFF
option(FREECAD_USE_SHIBOKEN
"Links to the shiboken library at build time.\
If OFF its Python module is imported at runtime" OFF)

option(FREECAD_USE_PYSIDE "Links to the PySide libraries at build time." OFF)

if(SHIBOKEN_INCLUDE_DIR)
option(FREECAD_USE_SHIBOKEN "Links to the shiboken library at build time. If OFF its Python module is imported at runtime" ON)
else()
message(WARNING "Shiboken${PYSIDE_MAJOR_VERSION} include files not found, FREECAD_USE_SHIBOKEN automatically set to OFF")
option(FREECAD_USE_SHIBOKEN "Links to the shiboken library at build time. If OFF its Python module is imported at runtime" OFF)
set(FREECAD_USE_SHIBOKEN ON)
endif()

# Now try to import the shiboken Python module and print an error if it can't be loaded
# Try to import the shiboken Python module and print an error if it can't be loaded
execute_process(
COMMAND ${PYTHON_EXECUTABLE} -c "import shiboken${SHIBOKEN_MAJOR_VERSION}"
RESULT_VARIABLE FAILURE
Expand All @@ -118,23 +136,25 @@ macro(SetupShibokenAndPyside)
"==================================\n")
else()
execute_process(
COMMAND ${PYTHON_EXECUTABLE} -c "import shiboken${SHIBOKEN_MAJOR_VERSION};print(shiboken${SHIBOKEN_MAJOR_VERSION}.__version__, end='')"
COMMAND ${PYTHON_EXECUTABLE} -c
"import shiboken${SHIBOKEN_MAJOR_VERSION};\
print(shiboken${SHIBOKEN_MAJOR_VERSION}.__version__, end='')"
RESULT_VARIABLE FAILURE
OUTPUT_VARIABLE Shiboken_VERSION
)
endif()

# If PySide cannot be found the build option will be set to OFF

if(PYSIDE_INCLUDE_DIR)
option(FREECAD_USE_PYSIDE "Links to the PySide libraries at build time." ON)
else()
message(WARNING "PySide${PYSIDE_MAJOR_VERSION} include files not found, FREECAD_USE_PYSIDE automatically set to OFF")
option(FREECAD_USE_PYSIDE "Links to the PySide libraries at build time." OFF)
set(FREECAD_USE_PYSIDE ON)
endif()

# Independent of the build option PySide modules must be loaded at runtime. Print an error if it fails.
# Independent of the build option PySide modules must be loaded at runtime.
# Print an error if it fails.
execute_process(
COMMAND ${PYTHON_EXECUTABLE} -c "import PySide${PYSIDE_MAJOR_VERSION};import os;print(os.path.dirname(PySide${PYSIDE_MAJOR_VERSION}.__file__), end='')"
COMMAND ${PYTHON_EXECUTABLE} -c
"import PySide${PYSIDE_MAJOR_VERSION};\
import os;print(os.path.dirname(PySide${PYSIDE_MAJOR_VERSION}.__file__), end='')"
RESULT_VARIABLE FAILURE
OUTPUT_VARIABLE PRINT_OUTPUT
)
Expand All @@ -145,14 +165,16 @@ macro(SetupShibokenAndPyside)
"================================\n")
else()
execute_process(
COMMAND ${PYTHON_EXECUTABLE} -c "import PySide${PYSIDE_MAJOR_VERSION};print(PySide${PYSIDE_MAJOR_VERSION}.__version__, end='')"
COMMAND ${PYTHON_EXECUTABLE} -c
"import PySide${PYSIDE_MAJOR_VERSION};print(PySide${PYSIDE_MAJOR_VERSION}
.__version__, end='')"
RESULT_VARIABLE FAILURE
OUTPUT_VARIABLE PySide_VERSION
)
message(STATUS "PySide ${PySide_VERSION} Python module found at ${PRINT_OUTPUT}.\n")
endif()

endmacro(SetupShibokenAndPyside)
endmacro()

# Find a pip-installed package and get details
function(find_pip_package pkg_name pkg_version)
Expand Down Expand Up @@ -192,11 +214,11 @@ endfunction()
# - PYSIDE_WRAP_UI
# - PYSIDE_WRAP_RC

MACRO(PYSIDE_WRAP_UI outfiles)
macro(PYSIDE_WRAP_UI outfiles)
if (NOT PYSIDE_UIC_EXECUTABLE)
message(FATAL_ERROR "Qt uic is required for generating ${ARGN}")
endif()
FOREACH(it ${ARGN})
foreach(it ${ARGN})
GET_FILENAME_COMPONENT(outfile ${it} NAME_WE)
GET_FILENAME_COMPONENT(infile ${it} ABSOLUTE)
SET(outfile ${CMAKE_CURRENT_BINARY_DIR}/ui_${outfile}.py)
Expand All @@ -216,14 +238,14 @@ MACRO(PYSIDE_WRAP_UI outfiles)
)
endif()
list(APPEND ${outfiles} ${outfile})
ENDFOREACH(it)
ENDMACRO (PYSIDE_WRAP_UI)
endforeach()
endmacro ()

MACRO(PYSIDE_WRAP_RC outfiles)
macro(PYSIDE_WRAP_RC outfiles)
if (NOT PYSIDE_RCC_EXECUTABLE)
message(FATAL_ERROR "Qt rcc is required for generating ${ARGN}")
endif()
FOREACH(it ${ARGN})
foreach(it ${ARGN})
GET_FILENAME_COMPONENT(outfile ${it} NAME_WE)
GET_FILENAME_COMPONENT(infile ${it} ABSOLUTE)
SET(outfile "${CMAKE_CURRENT_BINARY_DIR}/${outfile}_rc.py")
Expand All @@ -240,11 +262,12 @@ MACRO(PYSIDE_WRAP_RC outfiles)
# pyside-rcc generates in comments at beginning, which is why
# we follow the tool command with in-place sed.
ADD_CUSTOM_COMMAND(OUTPUT "${outfile}"
COMMAND "${PYSIDE_RCC_EXECUTABLE}" ${RCCOPTIONS} "${infile}" ${PY_ATTRIBUTE} -o "${outfile}"
COMMAND "${PYSIDE_RCC_EXECUTABLE}"
${RCCOPTIONS} "${infile}" ${PY_ATTRIBUTE} -o "${outfile}"
COMMAND sed "/^# /d" "${outfile}" >"${outfile}.tmp" && mv "${outfile}.tmp" "${outfile}"
MAIN_DEPENDENCY "${infile}"
)
endif()
list(APPEND ${outfiles} ${outfile})
ENDFOREACH(it)
ENDMACRO (PYSIDE_WRAP_RC)
endforeach()
endmacro ()

0 comments on commit c29ef73

Please sign in to comment.