From f050f15a0c92732bba93209900597ca1d028a6cf Mon Sep 17 00:00:00 2001 From: Eugene Shalygin Date: Sun, 17 Apr 2016 00:55:45 +0200 Subject: [PATCH 1/2] cmake: fix Qt resources linkage. Closes #5080 Qt resource is innitialized by a static object constructor (see https://wiki.qt.io/QtResources). When we put resources into a static library, the linker removes that static objects and thus the resources themselves. To correct that we append resources to the main executable sources list. This is done via custom function qbt_target_sources which knows where to read the executable' name. --- cmake/Modules/QbtTargetSources.cmake | 17 +++++++++++++++++ src/CMakeLists.txt | 11 +++++++++-- src/app/CMakeLists.txt | 5 ++--- src/gui/CMakeLists.txt | 5 +++-- src/gui/lineedit/CMakeLists.txt | 4 +++- src/webui/CMakeLists.txt | 7 ++++--- 6 files changed, 38 insertions(+), 11 deletions(-) create mode 100644 cmake/Modules/QbtTargetSources.cmake diff --git a/cmake/Modules/QbtTargetSources.cmake b/cmake/Modules/QbtTargetSources.cmake new file mode 100644 index 000000000..ced0e4b58 --- /dev/null +++ b/cmake/Modules/QbtTargetSources.cmake @@ -0,0 +1,17 @@ +# a helper function which appends source to the main qBt target +# the target name is read from QBT_TARGET_NAME variable +# sources file names are relative to the the ${qbt_executable_SOURCE_DIR} + +function (qbt_target_sources) + set (_sources_rel "") + foreach (_source IN ITEMS ${ARGN}) + if (IS_ABSOLUTE "${_source}") + set(source_abs "${_source}") + else() + get_filename_component(_source_abs "${_source}" ABSOLUTE) + endif() + file (RELATIVE_PATH _source_rel "${qbt_executable_SOURCE_DIR}" "${_source_abs}") + list (APPEND _sources_rel "${_source_rel}") + endforeach() + target_sources (${QBT_TARGET_NAME} PRIVATE "${_sources_rel}") +endfunction (qbt_target_sources) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 15ebe5068..49afb2e19 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,6 +3,7 @@ set(CMAKE_CXX_STANDARD "11") add_definitions(-DBOOST_NO_CXX11_RVALUE_REFERENCES) include(MacroLinkQtComponents) +include(QbtTargetSources) find_package(LibtorrentRasterbar REQUIRED) include_directories(SYSTEM ${LibtorrentRasterbar_INCLUDE_DIRS}) @@ -88,8 +89,12 @@ set(QBT_USES_QT5 ${QT5}) configure_file(config.h.cmakein ${CMAKE_CURRENT_BINARY_DIR}/config.h) +if (GUI) + set(QBT_TARGET_NAME qbittorrent) +else (GUI) + set(QBT_TARGET_NAME qbittorrent-nox) +endif (GUI) -add_subdirectory(base) if (SYSTEM_QTSINGLEAPPLICATION) find_package(QtSingleApplication REQUIRED) @@ -98,6 +103,9 @@ else (SYSTEM_QTSINGLEAPPLICATION) include_directories(app/qtsingleapplication) endif (SYSTEM_QTSINGLEAPPLICATION) +add_subdirectory(app) +add_subdirectory(base) + if (GUI) add_subdirectory(gui) endif (GUI) @@ -106,4 +114,3 @@ if (WEBUI) add_subdirectory(webui) endif (WEBUI) -add_subdirectory(app) diff --git a/src/app/CMakeLists.txt b/src/app/CMakeLists.txt index ff903ba05..0a2e4a954 100644 --- a/src/app/CMakeLists.txt +++ b/src/app/CMakeLists.txt @@ -1,3 +1,4 @@ +project(qbt_executable) include_directories(${CMAKE_CURRENT_BINARY_DIR}) set(QBT_APP_HEADERS @@ -86,13 +87,10 @@ list(APPEND QBT_APP_HEADERS upgrade.h) list(APPEND QBT_TARGET_LIBRARIES qbt_base) if (GUI) - set(QBT_TARGET_NAME qbittorrent) list(APPEND QBT_TARGET_LIBRARIES qbt_searchengine qbt_gui) include_directories(../gui ${CMAKE_CURRENT_BINARY_DIR}/../gui ) -else (GUI) - set(QBT_TARGET_NAME qbittorrent-nox) endif (GUI) if (WEBUI) @@ -152,6 +150,7 @@ add_executable(${QBT_TARGET_NAME} ${QBT_APP_HEADERS} ${QBT_APP_SOURCES} ${QBT_QM set_target_properties(${QBT_TARGET_NAME} PROPERTIES AUTOUIC True + AUTORCC True MACOSX_BUNDLE True ) diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 1457eab29..0223b5eda 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -8,6 +8,7 @@ add_subdirectory(properties) add_subdirectory(powermanagement) add_subdirectory(rss) add_subdirectory(search) + if (UNIX AND NOT APPLE AND DBUS) add_subdirectory(qtnotify) include_directories(qtnotify) @@ -127,7 +128,7 @@ options.ui torrentcreatordlg.ui ) -set(QBT_GUI_RESOURCES about.qrc) +qbt_target_sources(about.qrc) -add_library(qbt_gui STATIC ${QBT_GUI_HEADERS} ${QBT_GUI_SOURCES} ${QBT_GUI_RESOURCES}) +add_library(qbt_gui STATIC ${QBT_GUI_HEADERS} ${QBT_GUI_SOURCES}) target_link_libraries(qbt_gui qbt_lineedit qbt_powermanagement qbt_rss qbt_properties qbt_searchengine ${QBT_GUI_OPTIONAL_LINK_LIBRARIES} qbt_base) diff --git a/src/gui/lineedit/CMakeLists.txt b/src/gui/lineedit/CMakeLists.txt index 8887b855f..ced27991e 100644 --- a/src/gui/lineedit/CMakeLists.txt +++ b/src/gui/lineedit/CMakeLists.txt @@ -10,9 +10,11 @@ set(QBT_LINEEDIT_RESOURCES resources/lineeditimages.qrc ) -add_library(qbt_lineedit STATIC ${QBT_LINEEDIT_SOURCES} ${QBT_LINEEDIT_HEADERS} ${QBT_LINEEDIT_RESOURCES}) +add_library(qbt_lineedit STATIC ${QBT_LINEEDIT_SOURCES} ${QBT_LINEEDIT_HEADERS}) if (QT4_FOUND) target_link_libraries(qbt_lineedit Qt4::QtGui) else (QT4_FOUND) target_link_libraries(qbt_lineedit Qt5::Widgets) endif (QT4_FOUND) + +qbt_target_sources(${QBT_LINEEDIT_RESOURCES}) diff --git a/src/webui/CMakeLists.txt b/src/webui/CMakeLists.txt index 7f282884d..d3b5670f9 100644 --- a/src/webui/CMakeLists.txt +++ b/src/webui/CMakeLists.txt @@ -26,10 +26,11 @@ if (QT4_FOUND) endif(NOT SYSTEM_QJSON) endif (QT4_FOUND) -set(QBT_WEBUI_RESOURCES webui.qrc) -add_library(qbt_webui STATIC ${QBT_WEBUI_HEADERS} ${QBT_WEBUI_SOURCES} ${QBT_WEBUI_RESOURCES}) +qbt_target_sources(webui.qrc) + +add_library(qbt_webui STATIC ${QBT_WEBUI_HEADERS} ${QBT_WEBUI_SOURCES}) target_link_libraries(qbt_webui qbt_base) if (QT4_FOUND) - target_link_libraries(qbt_webui qjson) + target_link_libraries(qbt_webui qjson) endif (QT4_FOUND) From 7c0b5818b1df64c2c627e6fd685a732c7c1350e5 Mon Sep 17 00:00:00 2001 From: Eugene Shalygin Date: Sun, 17 Apr 2016 01:07:15 +0200 Subject: [PATCH 2/2] cmake: add imported target for QtSingleApplication This simplifies cmake code a bit: we remove if's and just generate different target (imported or alias) with the same name and use it unconditionally. --- cmake/Modules/FindQtSingleApplication.cmake | 12 ++++++++++++ src/CMakeLists.txt | 3 +-- src/app/CMakeLists.txt | 9 +-------- src/app/qtsingleapplication/CMakeLists.txt | 4 ++++ src/gui/CMakeLists.txt | 5 ++++- 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/cmake/Modules/FindQtSingleApplication.cmake b/cmake/Modules/FindQtSingleApplication.cmake index 4229e98b4..1865afb5f 100644 --- a/cmake/Modules/FindQtSingleApplication.cmake +++ b/cmake/Modules/FindQtSingleApplication.cmake @@ -79,3 +79,15 @@ ELSE (QTSINGLEAPPLICATION_FOUND) ENDIF (QTSINGLEAPPLICATION_FOUND) MARK_AS_ADVANCED(QTSINGLEAPPLICATION_INCLUDE_DIR QTSINGLEAPPLICATION_LIBRARY) + +if(NOT TARGET QtSingleApplication::QtSingleApplication) + add_library(QtSingleApplication::QtSingleApplication UNKNOWN IMPORTED) + set_target_properties(QtSingleApplication::QtSingleApplication PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${QTSINGLEAPPLICATION_INCLUDE_DIR}" + ) + if(EXISTS "${QTSINGLEAPPLICATION_LIBRARY}") + set_target_properties(QtSingleApplication::QtSingleApplication PROPERTIES + IMPORTED_LINK_INTERFACE_LANGUAGES "CXX" + IMPORTED_LOCATION "${QTSINGLEAPPLICATION_LIBRARY}") + endif() +endif(NOT TARGET QtSingleApplication::QtSingleApplication) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 49afb2e19..196871230 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -98,9 +98,8 @@ endif (GUI) if (SYSTEM_QTSINGLEAPPLICATION) find_package(QtSingleApplication REQUIRED) - include_directories(${QTSINGLEAPPLICATION_INCLUDE_DIR}) else (SYSTEM_QTSINGLEAPPLICATION) - include_directories(app/qtsingleapplication) + add_subdirectory(app/qtsingleapplication) endif (SYSTEM_QTSINGLEAPPLICATION) add_subdirectory(app) diff --git a/src/app/CMakeLists.txt b/src/app/CMakeLists.txt index 0a2e4a954..dffe9ffa1 100644 --- a/src/app/CMakeLists.txt +++ b/src/app/CMakeLists.txt @@ -158,14 +158,7 @@ if (GUI AND WIN32) set_target_properties(${QBT_TARGET_NAME} PROPERTIES WIN32_EXECUTABLE True) endif (GUI AND WIN32) -target_link_libraries(${QBT_TARGET_NAME} ${QBT_TARGET_LIBRARIES}) - -if (SYSTEM_QTSINGLEAPPLICATION) - target_link_libraries(${QBT_TARGET_NAME} ${QTSINGLEAPPLICATION_LIBRARIES}) -else (SYSTEM_QTSINGLEAPPLICATION) - add_subdirectory(qtsingleapplication) - target_link_libraries(${QBT_TARGET_NAME} qtsingleapplication) -endif (SYSTEM_QTSINGLEAPPLICATION) +target_link_libraries(${QBT_TARGET_NAME} ${QBT_TARGET_LIBRARIES} QtSingleApplication::QtSingleApplication) if (APPLE) set(qbt_BUNDLE_NAME "${CMAKE_PROJECT_NAME}") diff --git a/src/app/qtsingleapplication/CMakeLists.txt b/src/app/qtsingleapplication/CMakeLists.txt index 982436b12..72001d671 100644 --- a/src/app/qtsingleapplication/CMakeLists.txt +++ b/src/app/qtsingleapplication/CMakeLists.txt @@ -1,3 +1,5 @@ +project(qtsingleapplication) + set(QBT_QTSINGLEAPPLICATION_HEADERS qtlocalpeer.h ) @@ -15,6 +17,7 @@ else (GUI) endif (GUI) add_library(qtsingleapplication ${QBT_QTSINGLEAPPLICATION_HEADERS} ${QBT_QTSINGLEAPPLICATION_SOURCES}) +target_include_directories(qtsingleapplication INTERFACE "${qtsingleapplication_SOURCE_DIR}") if (QT4_FOUND) target_link_libraries(qtsingleapplication Qt4::QtNetwork) @@ -30,3 +33,4 @@ if (GUI) endif(QT4_FOUND) endif (GUI) +add_library(QtSingleApplication::QtSingleApplication ALIAS qtsingleapplication) diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 0223b5eda..a8f5119e6 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -131,4 +131,7 @@ torrentcreatordlg.ui qbt_target_sources(about.qrc) add_library(qbt_gui STATIC ${QBT_GUI_HEADERS} ${QBT_GUI_SOURCES}) -target_link_libraries(qbt_gui qbt_lineedit qbt_powermanagement qbt_rss qbt_properties qbt_searchengine ${QBT_GUI_OPTIONAL_LINK_LIBRARIES} qbt_base) +target_link_libraries(qbt_gui qbt_lineedit qbt_powermanagement qbt_rss qbt_properties qbt_searchengine + ${QBT_GUI_OPTIONAL_LINK_LIBRARIES} qbt_base + QtSingleApplication::QtSingleApplication +)