pennyBoy
pennyBoy

Reputation: 397

Creating installer using cpack and windeployqt

When I execute my CMakelists.txt the target executable is not packaged with neither the autogenerated .zip or the installer, it is located in the root of the build directory. All that is included within the .zip and the installer is the appropriate .dlls, etc. Can someone explain what I'm doing wrong?

cmake_minimum_required(VERSION 3.10.0)

project(TranslationVerification VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

find_package(Qt5 5.15 COMPONENTS Widgets REQUIRED)
if(Qt5Widgets_FOUND)
    if(${Qt5Widgets_VERSION} VERSION_LESS 5.15)
        message(FATAL_ERROR "Minimum supported Qt5 version is 5.15!")
     endif()
endif()

include(FetchContent)

FetchContent_Declare(qonlinetranslator
    GIT_REPOSITORY https://github.com/crow-translate/QOnlineTranslator.git
)

FetchContent_GetProperties(qonlinetranslator)
if(NOT qonlinetranslator_POPULATED)
    FetchContent_Populate(qonlinetranslator)
    add_subdirectory(${qonlinetranslator_SOURCE_DIR} ${qonlinetranslator_BINARY_DIR})
endif()

set(PROJECT_SOURCES
    src/main.cpp
    src/addlanguagedialog.cpp
    src/addlanguagedialog.ui
    src/verification.cpp
    src/widgetwindow.ui
    src/widgetwindow.cpp
)

if(QT_VERSION_MAJOR GREATER_EQUAL 5)
    qt_add_executable(translationVerification
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES})
else()
    add_executable(translationVerification
        ${PROJECT_SOURCES})
endif()

target_include_directories(translationVerification PUBLIC
    "${qonlinetranslator_BINARY_DIR}/src"
    "${qonlinetranslator_SOURCE_DIR}/src")

target_link_libraries(translationVerification PUBLIC QOnlineTranslator PRIVATE Qt5::Widgets)

if(QT_VERSION_MAJOR EQUAL 5)
    qt_finalize_executable(translationVerification)
endif()

##CPack
get_target_property(_qmake_executable Qt5::qmake IMPORTED_LOCATION)
get_filename_component(_qt_bin_dir "${_qmake_executable}" DIRECTORY)

set(CPACK_PACKAGE_NAME "Translation Verification")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Translation Verification Installation")
set(CPACK_PACKAGE_VERSION "1.0.0") # Version of installer

if(CMAKE_SYSTEM_NAME MATCHES "Windows")
    set(CPACK_PACKAGE_INSTALL_DIRECTORY "TranslationVerification")
    set(CPACK_NSIS_DISPLAY_NAME ${CMAKE_PACKAGE_NAME})
    set(CPACK_NSIS_COMPRESSOR lzma)
    set(CPACK_NSIS_INSTALLED_ICON_NAME TranslationVerification.exe)
    set(CMAKE_INSTALL_SYSTEM_RUNTIME_DESTINATION .)
    set(CMAKE_INSTALL_UCRT_LIBRARIES TRUE)
    include(InstallRequiredSystemLibraries)
    find_program(WINDEPLOYQT_EXECUTABLE windeployqt HINTS "${_qt_bin_dir}")

    add_custom_command(TARGET translationVerification POST_BUILD
                       COMMAND "${CMAKE_COMMAND}" -E remove_directory "${CMAKE_CURRENT_BINARY_DIR}/qtDeploy/"
                       COMMAND "${CMAKE_COMMAND}" -E
                               env PATH="${_qt_bin_dir}" "${WINDEPLOYQT_EXECUTABLE}"
                               --verbose 0
                               --no-compiler-runtime
                               --no-angle
                               --no-webkit2
                               --no-quick-import
                               --no-translations
                               --dir "${CMAKE_CURRENT_BINARY_DIR}/qtDeploy/" $<TARGET_FILE:translationVerification>
                       COMMENT "Deploying Qt..."
    )

    install(
        DIRECTORY "${CMAKE_BINARY_DIR}/qtDeploy/"
        DESTINATION .
    )
    set(CPACK_GENERATOR "ZIP;NSIS")

endif()

set(CPACK_SOURCE_PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}-src")
set(CPACK_SOURCE_GENERATOR "ZIP;TGZ")
include(CPack)

Upvotes: 1

Views: 1104

Answers (1)

u-235
u-235

Reputation: 458

The windeployqt copies only the executable's dependencies, so you need to add the executable's installation. Note that if you use DESTINATION . for the destination folder of the binary, this can cause problems when porting the project to other platforms. In such cases it is better to use TYPE BIN instead of DESTINATION .

if(QT_VERSION_MAJOR EQUAL 5)
    qt_finalize_executable(translationVerification)
endif()

install(PROGRAMS
        $<TARGET_FILE:translationVerification>
        DESTINATION .)

##CPack

You can also add include(GNUInstallDirs) before the first install() for portability purposes.

if(QT_VERSION_MAJOR EQUAL 5)
    qt_finalize_executable(translationVerification)
endif()

include(GNUInstallDirs)
install(PROGRAMS
        $<TARGET_FILE:translationVerification>
        TYPE BIN)

##CPack
.
.
.
    install(
        DIRECTORY "${CMAKE_BINARY_DIR}/qtDeploy/"
        TYPE BIN
    )
    set(CPACK_GENERATOR "ZIP;NSIS")

endif()

Upvotes: 1

Related Questions