@@ -11,6 +11,39 @@ cmake_minimum_required(VERSION 2.8)
1111project (PythonQt)
1212#-----------------------------------------------------------------------------
1313
14+ #----------------------------------------------------------------------------
15+ # Qt version
16+
17+ # Sanity checks
18+ if (DEFINED Qt5_DIR AND DEFINED QT_QMAKE_EXECUTABLE)
19+ message (FATAL_ERROR
20+ "${PROJECT_NAME} shoult NOT be configured setting both Qt5_DIR and QT_QMAKE_EXECUTABLE options.
21+ To build with Qt4, specify QT_QMAKE_EXECUTABLE. To build with Qt5, specify Qt5_DIR." )
22+ endif ()
23+
24+ # Set PythonQt_QT_VERSION
25+ if (DEFINED Qt5_DIR)
26+ message (STATUS "${PROJECT_NAME} : Setting PythonQt_QT_VERSION to 5 because Qt5_DIR is defined." )
27+ set (PythonQt_QT_VERSION 5)
28+ elseif (DEFINED QT_QMAKE_EXECUTABLE)
29+ message (STATUS "${PROJECT_NAME} : Setting PythonQt_QT_VERSION to 4 because QT_QMAKE_EXECUTABLE is defined." )
30+ set (PythonQt_QT_VERSION 4)
31+ else ()
32+ set (PythonQt_QT_VERSION 4 CACHE STRING "Pick a version of Qt to use: 4 or 5" )
33+ # Set the possible values of Qt version for cmake-gui
34+ set_property (CACHE PythonQt_QT_VERSION PROPERTY STRINGS "4" "5" )
35+ endif ()
36+
37+ # Requirements
38+ set (minimum_required_qt5_version "5.3.0" )
39+ set (minimum_required_qt4_version "4.6.2" )
40+ set (minimum_required_qt_version ${minimum_required_qt${PythonQt_QT_VERSION} _version})
41+
42+ # Qt components
43+ set (qt5libs Core Widgets Network OpenGL Sql Svg UiTools WebKitWidgets Xml XmlPatterns)
44+ set (qt4libs core gui network opengl sql svg uitools webkit xml xmlpatterns)
45+ set (qtlibs ${qt${PythonQt_QT_VERSION} libs})
46+
1447#-----------------------------------------------------------------------------
1548# Python libraries
1649
@@ -40,17 +73,41 @@ if(NOT DEFINED PythonQt_INSTALL_INCLUDE_DIR)
4073 set (PythonQt_INSTALL_INCLUDE_DIR include /PythonQt)
4174endif ()
4275
76+ # Since the Qt bindings sources used for both Qt4 and Qt5 are
77+ # grouped using Qt4 naming convention, qt_wrapped_libs variables are the
78+ # same for the two Qt versions.
79+ set (qt4_wrapped_libs ${qt4libs} )
80+ set (qt5_wrapped_libs ${qt4libs} )
81+ set (qt_wrapped_libs ${qt${PythonQt_QT_VERSION} _wrapped_libs})
82+
83+ set (qt5_wrapped_lib_depends_gui Multimedia)
84+
85+ set (qtlib_to_wraplib_Widgets gui)
86+ set (qtlib_to_wraplib_WebKitWidgets webkit)
87+
88+ # Define PythonQt_Wrap_Qt* options
4389option (PythonQt_Wrap_QtAll "Make all Qt components available in python" OFF )
90+ foreach (qtlib ${qt_wrapped_libs} )
91+ OPTION (PythonQt_Wrap_Qt${qtlib} "Make all of Qt${qtlib} available in python" OFF )
92+ endforeach ()
4493
45- set (qtlibs core gui network opengl sql svg uitools webkit xml xmlpatterns)
94+ # Set qtlib_to_wraplib_* variables
4695foreach (qtlib ${qtlibs} )
47- OPTION (PythonQt_Wrap_Qt${qtlib} "Make all of Qt${qtlib} available in python" OFF )
96+ string (TOLOWER ${qtlib} qtlib_lowercase)
97+ if (DEFINED qtlib_to_wraplib_${qtlib} )
98+ set (qtlib_lowercase ${qtlib_to_wraplib_${qtlib} })
99+ endif ()
100+ set (qtlib_to_wraplib_${qtlib} ${qtlib_lowercase} )
48101endforeach ()
49102
50103# Force option if it applies
51104if (PythonQt_Wrap_QtAll)
52- list (REMOVE_ITEM qtlibs xmlpatterns) # xmlpatterns wrapper does *NOT* build at all :(
53- foreach (qtlib ${qtlibs} )
105+ set (_qt_wrapped_libs ${qt_wrapped_libs} )
106+
107+ # XXX xmlpatterns wrapper does *NOT* build at all :(
108+ list (REMOVE_ITEM _qt_wrapped_libs xmlpatterns)
109+
110+ foreach (qtlib ${_qt_wrapped_libs} )
54111 if (NOT ${PythonQt_Wrap_Qt${qtlib} })
55112 set (PythonQt_Wrap_Qt${qtlib} ON CACHE BOOL "Make all of Qt${qtlib} available in python" FORCE)
56113 message (STATUS "Enabling [PythonQt_Wrap_Qt${qtlib} ] because of [PythonQt_Wrap_QtAll] evaluates to True" )
@@ -68,40 +125,86 @@ endif()
68125#-----------------------------------------------------------------------------
69126# Setup Qt
70127
71- set (minimum_required_qt_version "4.6.2" )
128+ if (PythonQt_QT_VERSION VERSION_GREATER "4" )
129+
130+ # Required components
131+ set (qt_required_components Core Widgets)
132+ foreach (qtlib ${qtlibs} )
133+ set (qt_wrapped_lib ${qtlib_to_wraplib_${qtlib} })
134+ if (${PythonQt_Wrap_Qt${qt_wrapped_lib} })
135+ list (APPEND qt_required_components ${qtlib} ${qt${PythonQt_QT_VERSION} _wrapped_lib_depends_${qt_wrapped_lib} })
136+ endif ()
137+ endforeach ()
138+ if (BUILD_TESTING)
139+ list (APPEND qt_required_components Test )
140+ endif ()
141+ list (REMOVE_DUPLICATES qt_required_components)
72142
73- find_package (Qt4)
143+ message (STATUS "${PROJECT_NAME} : Required Qt components [${qt_required_components} ]" )
144+ find_package (Qt5 ${minimum_required_qt_version} COMPONENTS ${qt_required_components} REQUIRED)
74145
75- if (QT4_FOUND)
146+ set (QT_LIBRARIES )
147+ foreach (qtlib ${qt_required_components} )
148+ include_directories (${Qt5${qtlib} _INCLUDE_DIRS})
149+ add_definitions (${Qt5${qtlib} _DEFINITIONS})
150+ list (APPEND QT_LIBRARIES ${Qt5${qtlib} _LIBRARIES})
151+ endforeach ()
76152
77- set (found_qt_version ${QT_VERSION_MAJOR} .${QT_VERSION_MINOR} .${QT_VERSION_PATCH} )
153+ set (QT_VERSION_MAJOR ${Qt5Core_VERSION_MAJOR} )
154+ set (QT_VERSION_MINOR ${Qt5Core_VERSION_MINOR} )
78155
79- if ( ${found_qt_version} VERSION_LESS ${minimum_required_qt_version} )
80- message (FATAL_ERROR "error: PythonQt requires Qt >= ${minimum_required_qt_version} -- you cannot use Qt ${found_qt_version} ." )
81- endif ()
156+ macro (pythonqt_wrap_cpp )
157+ qt5_wrap_cpp( ${ARGV} )
158+ endmacro ()
82159
83- # Enable required qt module
84- foreach (qtlib network opengl sql svg uitools webkit xml xmlpatterns)
85- string (TOUPPER ${qtlib} qtlib_uppercase)
86- if (NOT ${QT_QT${qtlib_uppercase} _FOUND})
87- message (FATAL_ERROR "QT_QT${qtlib_uppercase} *not* FOUND - Try to disable PythonQt_Wrap_Qt${qtlib} " )
160+ else ()
161+
162+ find_package (Qt4)
163+
164+ if (QT4_FOUND)
165+
166+ set (found_qt_version ${QT_VERSION_MAJOR} .${QT_VERSION_MINOR} .${QT_VERSION_PATCH} )
167+
168+ if (${found_qt_version} VERSION_LESS ${minimum_required_qt_version} )
169+ message (FATAL_ERROR "error: PythonQt requires Qt >= ${minimum_required_qt_version} -- you cannot use Qt ${found_qt_version} ." )
88170 endif ()
89- set (QT_USE_QT${qtlib_uppercase} ${PythonQt_Wrap_Qt${qtlib} })
90- endforeach ()
91171
92- set (QT_USE_QTTEST ${BUILD_TESTING} )
172+ # Enable required qt module
173+ foreach (qtlib ${qt_wrapped_libs} )
174+ string (TOUPPER ${qtlib} qtlib_uppercase)
175+ if (NOT ${QT_QT${qtlib_uppercase} _FOUND})
176+ message (FATAL_ERROR "QT_QT${qtlib_uppercase} *not* FOUND - Try to disable PythonQt_Wrap_Qt${qtlib} " )
177+ endif ()
178+ set (QT_USE_QT${qtlib_uppercase} ${PythonQt_Wrap_Qt${qtlib} })
179+ endforeach ()
180+
181+ # Enable QtTest in Qt4 is the option BUILD_TESTING was activated
182+ set (QT_USE_QTTEST ${BUILD_TESTING} )
183+
184+ include (${QT_USE_FILE} )
185+ else ()
186+ message (FATAL_ERROR "error: Qt4 was not found on your system. You probably need to set the QT_QMAKE_EXECUTABLE variable" )
187+ endif ()
188+
189+ macro (pythonqt_wrap_cpp)
190+ qt4_wrap_cpp(${ARGV} )
191+ endmacro ()
93192
94- include (${QT_USE_FILE} )
95- else ()
96- message (FATAL_ERROR "error: Qt4 was not found on your system. You probably need to set the QT_QMAKE_EXECUTABLE variable" )
97193endif ()
98194
99195#-----------------------------------------------------------------------------
100196# The variable "generated_cpp_suffix" allows to conditionnally compile the generated wrappers
101197# associated with the Qt version being used.
198+
199+ set (generated_cpp_suffix_46 _47)
200+ set (generated_cpp_suffix_52 _50)
201+ set (generated_cpp_suffix_51 _50)
202+
102203set (generated_cpp_suffix "_${QT_VERSION_MAJOR}${QT_VERSION_MINOR} " )
103- if ("${generated_cpp_suffix} " STREQUAL "_46" )
104- set (generated_cpp_suffix "_47" ) # Also use 4.7 wrappers for 4.6.x version
204+ if (DEFINED generated_cpp_suffix_${QT_VERSION_MAJOR}${QT_VERSION_MINOR} )
205+ set (generated_cpp_suffix "${generated_cpp_suffix_${QT_VERSION_MAJOR}${QT_VERSION_MINOR} }" )
206+ elseif (${QT_VERSION_MAJOR} .${QT_VERSION_MINOR} VERSION_GREATER "5.4" )
207+ set (generated_cpp_suffix "_54" )
105208endif ()
106209
107210#-----------------------------------------------------------------------------
@@ -181,15 +284,15 @@ set(moc_sources
181284
182285#-----------------------------------------------------------------------------
183286# Add extra sources
184- foreach (qtlib core gui network opengl sql svg uitools webkit xml xmlpatterns )
287+ foreach (qtlib ${qt_wrapped_libs} )
185288
186289 if (${PythonQt_Wrap_Qt${qtlib} })
187290
188291 ADD_DEFINITIONS (-DPYTHONQT_WRAP_Qt${qtlib} )
189292
190293 set (file_prefix generated_cpp${generated_cpp_suffix} /com_trolltech_qt_${qtlib} /com_trolltech_qt_${qtlib} )
191294
192- foreach (index RANGE 0 11 )
295+ foreach (index RANGE 0 12 )
193296
194297 # Source files
195298 if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR} /${file_prefix}${index} .cpp)
@@ -208,19 +311,9 @@ foreach(qtlib core gui network opengl sql svg uitools webkit xml xmlpatterns)
208311 endif ()
209312endforeach ()
210313
211- #-----------------------------------------------------------------------------
212- # UI files
213- set (ui_sources )
214-
215- #-----------------------------------------------------------------------------
216- # Resources
217- set (qrc_sources )
218-
219314#-----------------------------------------------------------------------------
220315# Do wrapping
221- qt4_wrap_cpp(gen_moc_sources ${moc_sources} )
222- qt4_wrap_ui(gen_ui_sources ${ui_sources} )
223- qt4_add_resources(gen_qrc_sources ${qrc_sources} )
316+ pythonqt_wrap_cpp(gen_moc_sources ${moc_sources} )
224317
225318#-----------------------------------------------------------------------------
226319# Build the library
@@ -232,8 +325,6 @@ include_directories(
232325add_library (PythonQt SHARED
233326 ${sources}
234327 ${gen_moc_sources}
235- ${gen_ui_sources}
236- ${gen_qrc_sources}
237328 )
238329set_target_properties (PythonQt PROPERTIES DEFINE_SYMBOL PYTHONQT_EXPORTS)
239330
@@ -270,12 +361,14 @@ if(BUILD_TESTING)
270361 tests/PythonQtTestMain.cpp
271362 )
272363
364+ set_property (SOURCE tests/PythonQtTestMain.cpp PROPERTY COMPILE_DEFINITIONS "main=tests_PythonQtTestMain" )
365+
273366 list (APPEND test_sources
274367 tests/PythonQtTests.cpp
275368 tests/PythonQtTests.h
276369 )
277370
278- QT4_WRAP_CPP (test_sources
371+ pythonqt_wrap_cpp (test_sources
279372 tests/PythonQtTests.h
280373 )
281374
@@ -286,12 +379,12 @@ if(BUILD_TESTING)
286379 tests/PythonQtTestCleanup.cpp
287380 tests/PythonQtTestCleanup.h
288381 )
289- QT4_WRAP_CPP (test_sources
382+ pythonqt_wrap_cpp (test_sources
290383 tests/PythonQtTestCleanup.h
291384 )
292- endif ()
293385
294- set_property (SOURCE tests/PythonQtTestMain.cpp PROPERTY COMPILE_DEFINITIONS "main=tests_PythonQtTestMain" )
386+ set_property (SOURCE tests/PythonQtTestMain.cpp APPEND PROPERTY COMPILE_DEFINITIONS "PythonQt_Wrap_Qtcore" )
387+ endif ()
295388
296389 add_executable (PythonQtCppTests ${test_sources} )
297390 target_link_libraries (PythonQtCppTests PythonQt)
0 commit comments