Allow using static Qt libraries

This commit is contained in:
Martchus 2016-08-19 16:16:10 +02:00
parent 55ad0b4de1
commit d97e112f0d
4 changed files with 54 additions and 10 deletions

View File

@ -8,6 +8,7 @@ set(HEADER_FILES
misc/adoptlocker.h
misc/dialogutils.h
misc/desktoputils.h
misc/importplugin.h
models/checklistmodel.h
resources/qtconfigarguments.h
resources/resources.h

View File

@ -21,14 +21,18 @@ if(DBUS_FILES)
list(APPEND QT_MODULES DBus)
endif()
# currently linking statically against Qt with CMake seems not to be officially supported
set(QT_LINKAGE "SHARED") # hence always to link dynamically, no matter whether STATIC_LINKAGE is set
set(QT_LINKAGE "AUTO_LINKAGE")
# this kind of selection between static/shared Qt would be nice but has not been not implemented (yet) on the Qt side
if((("${QT_LINKAGE}" STREQUAL "AUTO_LINKAGE" AND ((STATIC_LINKAGE AND "${META_PROJECT_TYPE}" STREQUAL "application") OR (STATIC_LIBRARY_LINKAGE AND ("${META_PROJECT_TYPE}" STREQUAL "" OR "${META_PROJECT_TYPE}" STREQUAL "library")))) OR ("${QT_LINKAGE}" STREQUAL "STATIC")))
if(BUILD_STATIC_LIBS AND BUILD_SHARED_LIBS)
message(FATAL_ERROR "When using Qt/KDE modules it is not possible to build shared and static libraries at the same time.")
endif()
# set USE_STATIC_QT_BUILD variable to ON to use static Qt, this only works with patched mingw-w64-qt5-* packages found in my PKGBUILDs repository
# in any other environment you must ensure that the available Qt version is in accordance to the specified STATIC_LINKAGE/STATIC_LIBRARY_LINKAGE options
if(BUILD_STATIC_LIBS OR ("${QT_LINKAGE}" STREQUAL "AUTO_LINKAGE" AND ((STATIC_LINKAGE AND "${META_PROJECT_TYPE}" STREQUAL "application") OR (STATIC_LIBRARY_LINKAGE AND ("${META_PROJECT_TYPE}" STREQUAL "" OR "${META_PROJECT_TYPE}" STREQUAL "library")))) OR ("${QT_LINKAGE}" STREQUAL "STATIC"))
set(USE_STATIC_QT_BUILD ON)
message(STATUS "Linking ${META_PROJECT_NAME} statically against Qt 5.")
elseif(("${QT_LINKAGE}" STREQUAL "AUTO_LINKAGE" OR ("${LINKAGE}" STREQUAL "SHARED")))
elseif(("${QT_LINKAGE}" STREQUAL "AUTO_LINKAGE") OR ("${QT_LINKAGE}" STREQUAL "SHARED"))
set(USE_STATIC_QT_BUILD OFF)
message(STATUS "Linking ${META_PROJECT_NAME} dynamically against Qt 5.")
endif()
@ -45,6 +49,21 @@ foreach(KF_MODULE ${KF_MODULES})
list(APPEND STATIC_LIBRARIES KF5::${KF_MODULE})
endforeach()
# include further plugins statically
if(USE_STATIC_QT_BUILD)
if(WIDGETS_GUI OR QUICK_GUI)
if(WIN32)
list(APPEND LIBRARIES Qt5::QWindowsIntegrationPlugin)
list(APPEND STATIC_LIBRARIES Qt5::QWindowsIntegrationPlugin)
endif()
if(SVG_SUPPORT)
find_package(Qt5Svg REQUIRED)
list(APPEND LIBRARIES Qt5::Svg Qt5::QSvgPlugin)
list(APPEND STATIC_LIBRARIES Qt5::Svg Qt5::QSvgPlugin)
endif()
endif()
endif()
# option for built-in translations
option(BUILTIN_TRANSLATIONS "enables/disables built-in translations" OFF)
@ -194,13 +213,13 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON)
if(WIDGETS_UI_FILES AND WIDGETS_GUI)
set(CMAKE_AUTOUIC ON)
if(INSTALL_UI_HEADER)
# also add installd for header files generated by uic
# also add install for header files generated by uic
foreach(UI_FILE WIDGETS_UI_FILES)
get_filename_component(UI_NAME "${UI_FILE}" NAME_WE)
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/ui_${UI_NAME}.h"
DESTINATION "include/${META_PROJECT_NAME}/ui"
COMPONENT header
COMPONENT ui-header
)
endforeach()
endif()

View File

@ -36,10 +36,12 @@ else()
message(STATUS "Building WITHOUT Qt Quick GUI.")
endif()
# set "GUI-type" to WIN32 to hide console under windows
if(WIN32 AND (WIDGETS_GUI OR QUICK_GUI))
if(WIDGETS_GUI OR QUICK_GUI)
list(APPEND QT_MODULES Gui)
set(GUI_TYPE WIN32)
# set "GUI-type" to WIN32 to hide console under windows
if(WIN32)
set(GUI_TYPE WIN32)
endif()
endif()
# add source files requried by both GUI variants
@ -47,3 +49,9 @@ if(WIDGETS_GUI OR QUICK_GUI)
list(APPEND SRC_FILES ${GUI_SRC_FILES})
list(APPEND ADDITIONAL_HEADER_FILES ${GUI_HEADER_FILES})
endif()
# add option for enabling/disabling svg support
option(SVG_SUPPORT "enables/disables svg support (only affects static builds where QSvgPlugin will be built-in if enabled)" ON)
if(SVG_SUPPORT)
add_definitions(-DSVG_SUPPORT)
endif()

16
misc/importplugin.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef MISC_UTILS_IMPORT_PLUGIN_H
#define MISC_UTILS_IMPORT_PLUGIN_H
#ifdef QT_STATIC
# if defined(GUI_QTWIDGETS) || defined(GUI_QTQUICK)
# include <QtPlugin>
# ifdef PLATFORM_WINDOWS
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
# endif
# ifdef SVG_SUPPORT
Q_IMPORT_PLUGIN(QSvgPlugin)
# endif
# endif
#endif
#endif // MISC_UTILS_IMPORT_PLUGIN_H