Skip to content

Commit

Permalink
First set of build system patches for Qt5 support.
Browse files Browse the repository at this point in the history
These changes enable support for Qt5 in CTK libraries and CTK Qt plugins.
Set the CTK_USE_QT5 CMake variable to ON on specify the Qt5 installaltion
directory in QT5_INSTALL_PREFIX.
  • Loading branch information
saschazelzer committed Dec 16, 2012
1 parent fc9a964 commit 6e53277
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 37 deletions.
51 changes: 45 additions & 6 deletions CMake/ctkMacroBuildLib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#! \ingroup CMakeAPI
macro(ctkMacroBuildLib)
ctkMacroParseArguments(MY
"NAME;EXPORT_DIRECTIVE;SRCS;MOC_SRCS;UI_FORMS;INCLUDE_DIRECTORIES;TARGET_LIBRARIES;RESOURCES;LIBRARY_TYPE"
"NAME;EXPORT_DIRECTIVE;SRCS;MOC_SRCS;GENERATE_MOC_SRCS;UI_FORMS;INCLUDE_DIRECTORIES;QT5_MODULES;TARGET_LIBRARIES;RESOURCES;LIBRARY_TYPE"
"ENABLE_QTTESTING"
${ARGN}
)
Expand All @@ -47,6 +47,20 @@ macro(ctkMacroBuildLib)
if(NOT DEFINED MY_LIBRARY_TYPE)
set(MY_LIBRARY_TYPE "SHARED")
endif()

if(MY_QT5_MODULES)
set(qt5_use_modules_list)
if(NOT CTK_USE_QT5)
message(WARNING "Argument QT5_MODULES ignored because CTK_USE_QT5 is not set.")
else()
foreach(qt5_module ${MY_QT5_MODULES})
find_package(${qt5_module} REQUIRED)
# strip the "Qt5" string from the module name
string(SUBSTRING ${qt5_module} 3 -1 _qt5_module_name)
list(APPEND qt5_use_modules_list ${_qt5_module_name})
endforeach()
endif()
endif()

# Define library name
set(lib_name ${MY_NAME})
Expand Down Expand Up @@ -105,13 +119,34 @@ macro(ctkMacroBuildLib)
if(MY_MOC_SRCS)
# this is a workaround for Visual Studio. The relative include paths in the generated
# moc files can get very long and can't be resolved by the MSVC compiler.
foreach(moc_src ${MY_MOC_SRCS})
QT4_WRAP_CPP(MY_MOC_CPP ${moc_src} OPTIONS -f${moc_src})
endforeach()
if(Qt5Core_FOUND)
foreach(moc_src ${MY_MOC_SRCS})
qt5_wrap_cpp(MY_MOC_CPP ${moc_src} OPTIONS -f${moc_src})
endforeach()
else()
foreach(moc_src ${MY_MOC_SRCS})
QT4_WRAP_CPP(MY_MOC_CPP ${moc_src} OPTIONS -f${moc_src})
endforeach()
endif()
endif()
if(MY_GENERATE_MOC_SRCS)
QT4_GENERATE_MOCS(${MY_GENERATE_MOC_SRCS})
endif()
if(CTK_USE_QT5)
if(Qt5Widgets_FOUND)
qt5_wrap_ui(MY_UI_CPP ${MY_UI_FORMS})
elseif(MY_UI_FORMS)
message(WARNING "Argument UI_FORMS ignored because Qt5Widgets module was not specified")
endif()
else()
QT4_WRAP_UI(MY_UI_CPP ${MY_UI_FORMS})
endif()
QT4_WRAP_UI(MY_UI_CPP ${MY_UI_FORMS})
if(DEFINED MY_RESOURCES)
QT4_ADD_RESOURCES(MY_QRC_SRCS ${MY_RESOURCES})
if(Qt5Core_FOUND)
qt5_add_resources(MY_QRC_SRCS ${MY_RESOURCES})
else()
QT4_ADD_RESOURCES(MY_QRC_SRCS ${MY_RESOURCES})
endif()
endif()

source_group("Resources" FILES
Expand All @@ -132,6 +167,10 @@ macro(ctkMacroBuildLib)
${MY_UI_CPP}
${MY_QRC_SRCS}
)

if(CTK_USE_QT5 AND qt5_use_modules_list)
qt5_use_modules(${lib_name} ${qt5_use_modules_list})
endif()

# Set labels associated with the target.
set_target_properties(${lib_name} PROPERTIES LABELS ${lib_name})
Expand Down
44 changes: 39 additions & 5 deletions CMake/ctkMacroBuildQtPlugin.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ macro(ctkMacroBuildQtPlugin)
cmake_parse_arguments(MY
"" # no options
"NAME;EXPORT_DIRECTIVE;PLUGIN_DIR" # one value args
"SRCS;MOC_SRCS;UI_FORMS;INCLUDE_DIRECTORIES;TARGET_LIBRARIES;RESOURCES" # multi value args
"SRCS;MOC_SRCS;UI_FORMS;INCLUDE_DIRECTORIES;TARGET_LIBRARIES;QT5_MODULES;RESOURCES" # multi value args
${ARGN}
)

Expand All @@ -43,6 +43,20 @@ macro(ctkMacroBuildQtPlugin)
message(FATAL_ERROR "PLUGIN_DIR (e.g. designer, iconengines, imageformats...) is mandatory")
endif()
set(MY_LIBRARY_TYPE "MODULE")

if(MY_QT5_MODULES)
set(qt5_use_modules_list)
if(NOT CTK_USE_QT5)
message(WARNING "Argument QT5_MODULES ignored because CTK_USE_QT5 is not set.")
else()
foreach(qt5_module ${MY_QT5_MODULES})
find_package(${qt5_module} REQUIRED)
# strip the "Qt5" string from the module name
string(SUBSTRING ${qt5_module} 3 -1 _qt5_module_name)
list(APPEND qt5_use_modules_list ${_qt5_module_name})
endforeach()
endif()
endif()

# Define library name
set(lib_name ${MY_NAME})
Expand Down Expand Up @@ -82,11 +96,27 @@ macro(ctkMacroBuildQtPlugin)
set(MY_QRC_SRCS)

# Wrap
QT4_WRAP_CPP(MY_MOC_CPP ${MY_MOC_SRCS})
QT4_WRAP_UI(MY_UI_CPP ${MY_UI_FORMS})
set(MY_QRC_SRCS "")
if(DEFINED MY_RESOURCES)
QT4_ADD_RESOURCES(MY_QRC_SRCS ${MY_RESOURCES})
if(Qt5Core_FOUND)
qt5_wrap_cpp(MY_MOC_CPP ${MY_MOC_SRCS})
if(DEFINED MY_RESOURCES)
qt5_add_resources(MY_QRC_SRCS ${MY_RESOURCES})
endif()
else()
QT4_WRAP_CPP(MY_MOC_CPP ${MY_MOC_SRCS})
if(DEFINED MY_RESOURCES)
QT4_ADD_RESOURCES(MY_QRC_SRCS ${MY_RESOURCES})
endif()
endif()

if(CTK_USE_QT5)
if(Qt5Widgets_FOUND)
qt5_wrap_ui(MY_UI_CPP ${MY_UI_FORMS})
elseif(MY_UI_FORMS)
message(WARNING "Argument UI_FORMS ignored because Qt5Widgets module was not specified")
endif()
else()
QT4_WRAP_UI(MY_UI_CPP ${MY_UI_FORMS})
endif()

source_group("Resources" FILES
Expand All @@ -106,6 +136,10 @@ macro(ctkMacroBuildQtPlugin)
${MY_UI_CPP}
${MY_QRC_SRCS}
)

if(CTK_USE_QT5 AND qt5_use_modules_list)
qt5_use_modules(${lib_name} ${qt5_use_modules_list})
endif()

# Extract library name associated with the plugin and use it as label
string(REGEX REPLACE "(.*)Plugin[s]?" "\\1" label ${lib_name})
Expand Down
10 changes: 9 additions & 1 deletion CMake/ctkMacroGenerateMocs.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
macro(QT4_GENERATE_MOCS)
foreach(file ${ARGN})
set(moc_file moc_${file})
QT4_GENERATE_MOC(${file} ${moc_file})
if(CTK_USE_QT5)
qt5_generate_moc(${file} ${moc_file})
else()
QT4_GENERATE_MOC(${file} ${moc_file})
endif()

get_filename_component(source_name ${file} NAME_WE)
get_filename_component(source_ext ${file} EXT)
Expand All @@ -19,3 +23,7 @@ macro(QT4_GENERATE_MOCS)
endforeach()
endmacro()

# create a Qt5 alias
macro(QT5_GENERATE_MOCS)
QT4_GENERATE_MOCS(${ARGN})
endmacro()
59 changes: 34 additions & 25 deletions CMake/ctkMacroSetupQt.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,42 @@
#! \ingroup CMakeUtilities
macro(ctkMacroSetupQt)

set(minimum_required_qt_version "4.6")

find_package(Qt4)

if(QT4_FOUND)

if("${QT_VERSION_MAJOR}.${QT_VERSION_MINOR}" VERSION_LESS "${minimum_required_qt_version}")
message(FATAL_ERROR "error: CTK requires Qt >= ${minimum_required_qt_version} -- you cannot use Qt ${QT_VERSION_MAJOR}.${QT_VERSION_MINOR}.${QT_VERSION_PATCH}.")
if(CTK_USE_QT5)
cmake_minimum_required(VERSION 2.8.9)
set(QT5_INSTALL_PREFIX "" CACHE PATH "The install location of Qt5")
if(NOT QT5_INSTALL_PREFIX OR NOT EXISTS ${QT5_INSTALL_PREFIX}/bin/qmake)
message(FATAL_ERROR "You must specify the install location of Qt5")
endif()

set(QT_USE_QTNETWORK ON)
set(QT_USE_QTSQL ON)
set(QT_USE_QTOPENGL ON)
set(QT_USE_QTXML ON)
set(QT_USE_QTXMLPATTERNS ON)
set(QT_USE_QTTEST ${BUILD_TESTING})
include(${QT_USE_FILE})

# Set variable QT_INSTALLED_LIBRARY_DIR that will contains
# Qt shared library
set(QT_INSTALLED_LIBRARY_DIR ${QT_LIBRARY_DIR})
if(WIN32)
get_filename_component(QT_INSTALLED_LIBRARY_DIR ${QT_QMAKE_EXECUTABLE} PATH)
set(CMAKE_PREFIX_PATH ${QT5_INSTALL_PREFIX})
else()
set(minimum_required_qt_version "4.6")

find_package(Qt4)

if(QT4_FOUND)

if("${QT_VERSION_MAJOR}.${QT_VERSION_MINOR}" VERSION_LESS "${minimum_required_qt_version}")
message(FATAL_ERROR "error: CTK requires Qt >= ${minimum_required_qt_version} -- you cannot use Qt ${QT_VERSION_MAJOR}.${QT_VERSION_MINOR}.${QT_VERSION_PATCH}.")
endif()

set(QT_USE_QTNETWORK ON)
set(QT_USE_QTSQL ON)
set(QT_USE_QTOPENGL ON)
set(QT_USE_QTXML ON)
set(QT_USE_QTXMLPATTERNS ON)
set(QT_USE_QTTEST ${BUILD_TESTING})
include(${QT_USE_FILE})

# Set variable QT_INSTALLED_LIBRARY_DIR that will contains
# Qt shared library
set(QT_INSTALLED_LIBRARY_DIR ${QT_LIBRARY_DIR})
if(WIN32)
get_filename_component(QT_INSTALLED_LIBRARY_DIR ${QT_QMAKE_EXECUTABLE} PATH)
endif()

else()
message(FATAL_ERROR "error: Qt4 was not found on your system. You probably need to set the QT_QMAKE_EXECUTABLE variable")
endif()

else()
message(FATAL_ERROR "error: Qt4 was not found on your system. You probably need to set the QT_QMAKE_EXECUTABLE variable")
endif()

endmacro()
2 changes: 2 additions & 0 deletions SuperBuild.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ set(ctk_cmake_boolean_args
CTK_BUILD_ALL_LIBRARIES
CTK_BUILD_ALL_PLUGINS
CTK_BUILD_QTDESIGNER_PLUGINS
CTK_USE_QT5
CTK_USE_QTTESTING
CTK_USE_KWSTYLE
WITH_COVERAGE
Expand Down Expand Up @@ -157,6 +158,7 @@ ExternalProject_Add(${proj}
-DCTK_INSTALL_DOC_DIR:STRING=${CTK_INSTALL_DOC_DIR}
-DCTK_EXTERNAL_LIBRARY_DIRS:STRING=${CTK_EXTERNAL_LIBRARY_DIRS}
-DQT_QMAKE_EXECUTABLE:FILEPATH=${QT_QMAKE_EXECUTABLE}
-DQT5_INSTALL_PREFIX:PATH=${QT5_INSTALL_PREFIX}
${CTK_SUPERBUILD_EP_ARGS}
-DCTK_SUPERBUILD_EP_VARNAMES:STRING=${CTK_SUPERBUILD_EP_VARNAMES}
SOURCE_DIR ${CTK_SOURCE_DIR}
Expand Down

0 comments on commit 6e53277

Please sign in to comment.