/
SetupShibokenAndPyside.cmake
268 lines (243 loc) · 12.5 KB
/
SetupShibokenAndPyside.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
macro(SetupShibokenAndPyside)
# -------------------------------- Shiboken/PySide ------------------------
option(FREECAD_USE_SHIBOKEN "Links to the shiboken library at build time. If OFF its Python module is imported at runtime" ON)
option(FREECAD_USE_PYSIDE "Links to the PySide libraries at build time." ON)
if(DEFINED MACPORTS_PREFIX)
find_package(Shiboken REQUIRED HINTS "${PYTHON_LIBRARY_DIR}/cmake")
find_package(PySide REQUIRED HINTS "${PYTHON_LIBRARY_DIR}/cmake")
endif(DEFINED MACPORTS_PREFIX)
if(FREECAD_QT_MAJOR_VERSION EQUAL 5)
set(SHIBOKEN_MAJOR_VERSION 2)
set(PYSIDE_MAJOR_VERSION 2)
else()
set(SHIBOKEN_MAJOR_VERSION ${FREECAD_QT_MAJOR_VERSION})
set(PYSIDE_MAJOR_VERSION ${FREECAD_QT_MAJOR_VERSION})
endif()
# Shiboken2Config.cmake may explicitly set CMAKE_BUILD_TYPE to Release which causes
# CMake to fail to create Makefiles for a debug build.
# So as a workaround we save and restore the value after checking for Shiboken2.
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")
if (SHIBOKEN_CONFIG)
get_filename_component(SHIBOKEN_CONFIG_SUFFIX ${SHIBOKEN_CONFIG} NAME)
string(SUBSTRING ${SHIBOKEN_CONFIG_SUFFIX} 15 -1 SHIBOKEN_CONFIG_SUFFIX)
string(REPLACE ".cmake" "" PYTHON_CONFIG_SUFFIX ${SHIBOKEN_CONFIG_SUFFIX})
message(STATUS "PYTHON_CONFIG_SUFFIX: ${PYTHON_CONFIG_SUFFIX}")
find_package(Shiboken${SHIBOKEN_MAJOR_VERSION} QUIET)
endif()
endif()
# 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)
if(NOT SHIBOKEN_INCLUDE_DIR)
find_pip_package(Shiboken${SHIBOKEN_MAJOR_VERSION})
if (Shiboken${SHIBOKEN_MAJOR_VERSION}_FOUND)
set(SHIBOKEN_INCLUDE_DIR ${Shiboken${SHIBOKEN_MAJOR_VERSION}_INCLUDE_DIR})
set(SHIBOKEN_LIBRARY ${Shiboken${SHIBOKEN_MAJOR_VERSION}_LIBRARY})
endif()
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(NOT PYSIDE_INCLUDE_DIR AND TARGET PySide${PYSIDE_MAJOR_VERSION}::pyside${PYSIDE_MAJOR_VERSION})
if(NOT PYSIDE_INCLUDE_DIR)
find_pip_package(PySide${PYSIDE_MAJOR_VERSION})
if (PySide${PYSIDE_MAJOR_VERSION}_FOUND)
set(PYSIDE_INCLUDE_DIR ${PySide${PYSIDE_MAJOR_VERSION}_INCLUDE_DIR})
set(PYSIDE_LIBRARY ${PySide${PYSIDE_MAJOR_VERSION}_LIBRARY})
endif()
endif()
find_package(PySide${PYSIDE_MAJOR_VERSION}Tools QUIET) # PySide utilities (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")
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")
if(PYSIDE_MAJOR_VERSION LESS 6)
file(WRITE ${CMAKE_BINARY_DIR}/Ext/PySide/QtSvgWidgets.py "from PySide${PYSIDE_MAJOR_VERSION}.QtSvg import QGraphicsSvgItem\n"
"from PySide${PYSIDE_MAJOR_VERSION}.QtSvg import QSvgWidget\n")
else()
file(WRITE ${CMAKE_BINARY_DIR}/Ext/PySide/QtSvgWidgets.py "from PySide${PYSIDE_MAJOR_VERSION}.QtSvgWidgets import *\n")
endif()
endif()
if(APPLE AND NOT BUILD_WITH_CONDA)
install(
DIRECTORY
${CMAKE_BINARY_DIR}/Ext/PySide
DESTINATION
MacOS
)
else()
install(
DIRECTORY
${CMAKE_BINARY_DIR}/Ext/PySide
DESTINATION
Ext
)
endif()
# If shiboken cannot be found the build option will be set to OFF
if(NOT SHIBOKEN_INCLUDE_DIR)
message(WARNING "Shiboken${PYSIDE_MAJOR_VERSION} include files not found, FREECAD_USE_SHIBOKEN automatically set to OFF")
set(FREECAD_USE_SHIBOKEN OFF)
endif()
# Now try to import the shiboken Python module and print a warning if it can't be loaded
execute_process(
COMMAND ${PYTHON_EXECUTABLE} -c "import shiboken${SHIBOKEN_MAJOR_VERSION}"
RESULT_VARIABLE FAILURE
OUTPUT_VARIABLE PRINT_OUTPUT
)
if(FAILURE)
message(WARNING
"==================================\n"
"Shiboken${SHIBOKEN_MAJOR_VERSION} Python module not found.\n"
"==================================\n")
else()
execute_process(
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(NOT PYSIDE_INCLUDE_DIR)
message(WARNING "PySide${PYSIDE_MAJOR_VERSION} include files not found, FREECAD_USE_PYSIDE automatically set to OFF")
set(FREECAD_USE_PYSIDE OFF)
endif()
# Independent of the build option PySide modules must be loaded at runtime. Print a warning 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='')"
RESULT_VARIABLE FAILURE
OUTPUT_VARIABLE PRINT_OUTPUT
)
if(FAILURE)
message(WARNING
"================================\n"
"PySide${PYSIDE_MAJOR_VERSION} Python module not found.\n"
"================================\n")
else()
execute_process(
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)
# Locate the include directory for a pip-installed package -- uses pip show to find the base pip
# install directory, and then appends the package name and "/include" to the end
macro(find_pip_package PACKAGE)
execute_process(
COMMAND ${PYTHON_EXECUTABLE} -m pip show ${PACKAGE}
RESULT_VARIABLE FAILURE
OUTPUT_VARIABLE PRINT_OUTPUT
)
if(NOT FAILURE)
# Extract Name: and Location: lines and use them to construct the include directory
string(REPLACE "\n" ";" PIP_OUTPUT_LINES ${PRINT_OUTPUT})
foreach(LINE IN LISTS PIP_OUTPUT_LINES)
STRING(FIND "${LINE}" "Name: " NAME_STRING_LOCATION)
STRING(FIND "${LINE}" "Location: " LOCATION_STRING_LOCATION)
if(${NAME_STRING_LOCATION} EQUAL 0)
STRING(SUBSTRING "${LINE}" 6 -1 PIP_PACKAGE_NAME)
elseif(${LOCATION_STRING_LOCATION} EQUAL 0)
STRING(SUBSTRING "${LINE}" 10 -1 PIP_PACKAGE_LOCATION)
endif()
endforeach()
message(STATUS "Found pip-installed ${PACKAGE} in ${PIP_PACKAGE_LOCATION}/${PIP_PACKAGE_NAME}")
file(TO_NATIVE_PATH "${PIP_PACKAGE_LOCATION}/${PIP_PACKAGE_NAME}/include" INCLUDE_DIR)
file(TO_NATIVE_PATH "${PIP_PACKAGE_LOCATION}/${PIP_PACKAGE_NAME}/lib" LIBRARY)
if(EXISTS ${INCLUDE_DIR})
set(${PACKAGE}_INCLUDE_DIR ${INCLUDE_DIR})
else()
message(STATUS "${PACKAGE} include directory '${INCLUDE_DIR}' does not exist")
endif()
if(EXISTS ${LIBRARY})
set(${PACKAGE}_LIBRARY ${LIBRARY})
else()
message(STATUS "${PACKAGE} library directory '${LIBRARY}' does not exist")
endif()
set(${PACKAGE}_FOUND TRUE)
endif()
endmacro()
# Macros similar to FindQt4.cmake's WRAP_UI and WRAP_RC, for the automatic generation of Python
# code from Qt4's user interface ('.ui') and resource ('.qrc') files. These macros are called:
# - PYSIDE_WRAP_UI
# - PYSIDE_WRAP_RC
MACRO(PYSIDE_WRAP_UI outfiles)
if (NOT PYSIDE_UIC_EXECUTABLE)
message(FATAL_ERROR "Qt uic is required for generating ${ARGN}")
endif()
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)
if(WIN32 OR APPLE)
ADD_CUSTOM_COMMAND(OUTPUT ${outfile}
COMMAND ${PYSIDE_UIC_EXECUTABLE} ${UICOPTIONS} ${infile} -o ${outfile}
MAIN_DEPENDENCY ${infile}
)
else()
# Especially on Open Build Service we don't want changing date like
# pyside2-uic generates in comments at beginning., which is why
# we follow the tool command with a POSIX-friendly sed.
ADD_CUSTOM_COMMAND(OUTPUT ${outfile}
COMMAND "${PYSIDE_UIC_EXECUTABLE}" ${UICOPTIONS} "${infile}" -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_UI)
MACRO(PYSIDE_WRAP_RC outfiles)
if (NOT PYSIDE_RCC_EXECUTABLE)
message(FATAL_ERROR "Qt rcc is required for generating ${ARGN}")
endif()
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")
#ADD_CUSTOM_TARGET(${it} ALL
# DEPENDS ${outfile}
#)
if(WIN32 OR APPLE)
ADD_CUSTOM_COMMAND(OUTPUT ${outfile}
COMMAND ${PYSIDE_RCC_EXECUTABLE} ${RCCOPTIONS} ${infile} -o ${outfile}
MAIN_DEPENDENCY ${infile}
)
else()
# Especially on Open Build Service we don't want changing date like
# 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 sed "/^# /d" "${outfile}" >"${outfile}.tmp" && mv "${outfile}.tmp" "${outfile}"
MAIN_DEPENDENCY "${infile}"
)
endif()
list(APPEND ${outfiles} ${outfile})
ENDFOREACH(it)
ENDMACRO (PYSIDE_WRAP_RC)