使用 cpack 和 windeployqt 创建安装程序

Creating installer using cpack and windeployqt

当我执行我的 CMakelists.txt 时,目标可执行文件既没有与自动生成的 .zip 文件也没有与安装程序一起打包,它位于构建目录的根目录中。 .zip 和安装程序中包含的所有内容都是适当的 .dll 等。有人可以解释我做错了什么吗?

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)

windeployqt只复制可执行文件的依赖,所以你需要添加可执行文件的安装。请注意,如果您使用 DESTINATION . 作为二进制文件的目标文件夹,这可能会导致在将项目移植到其他平台时出现问题。在这种情况下,最好使用 TYPE BIN 而不是 DESTINATION .

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

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

##CPack

您还可以在第一个 install() 之前添加 include(GNUInstallDirs) 以方便携带。

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()