QT Deployment IV – Generating a .deb package with CPack

In the previous post I demonstrated how to deploy with terminal commands and it’s a primitive way not an automated one. This time it’s time to use CPack utility of CMake to create our own deployable .deb package. You can get sample project from GitHub repository.

Setting CMakeLists.txt

Let’s open up our favorite editor or QT Creator IDE to edit CMakeLists.txt file. I’ll add a project version to display below CMAKE_PREFIX_PATH and set it to 1.0.1. Then it’s time to specify CPACK parameters and including CPack. Note that you have to include it after target is created. It’s not about build time. CPack doesn’t related with build time. It’ll call later with make package command from terminal after make.

cmake_minimum_required(VERSION 3.10)

project(DeploymentProject LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

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

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_PREFIX_PATH "/home/muhammet/Qt/5.15.0/gcc_64")
set(PROJECT_VERSION 1.0.1)

find_package(Qt5 COMPONENTS Core Quick REQUIRED)

configure_file("${CMAKE_SOURCE_DIR}/deployment/deployment.json.in" "${CMAKE_BINARY_DIR}/deployment.json")

add_executable(DeploymentProject
  main.cpp
  qml.qrc
)

target_compile_definitions(DeploymentProject
  PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(DeploymentProject
  PRIVATE Qt5::Core Qt5::Quick)

add_custom_command(TARGET ${CMAKE_PROJECT_NAME}
                   POST_BUILD
                   COMMAND cqtdeployer -confFile "${CMAKE_BINARY_DIR}/deployment.json")

install(DIRECTORY "${CMAKE_BINARY_DIR}/DeploymentPackage/" DESTINATION "/opt/${CMAKE_PROJECT_NAME}")
set(CPACK_GENERATOR "DEB")
set(CPACK_DEBIAN_PACKAGE_NAME "${CMAKE_PROJECT_NAME}")
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Muhammet Kucuk")
set(CPACK_DEBIAN_FILE_NAME "${CMAKE_PROJECT_NAME}.deb")
set(CPACK_DEBIAN_PACKAGE_VERSION ${PROJECT_VERSION})
set(CPACK_DEBIAN_PACKAGE_HOMEPAGE "https://muhammetkucuk.com")
set(CPACK_DEBIAN_PACKAGE_DESCRIPTION "Debian package test with CPack")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Test for Deb")
set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA
    "${CMAKE_SOURCE_DIR}/deployment/prerm;${CMAKE_SOURCE_DIR}/deployment/postinst")
include(CPack)

If you pay attention, this CPack parameters are kind of our control file. So we don’t need to work around our control file. But we need prerm and postinst scripts too as control files. For deb packaging we have to set CPACK_GENERATOR as “DEB” and we have to set CPACK_DEBIAN_FILE_NAME with “.deb” extension. Maintainer is a must.

In this template of project C QT Deployer tool will deploy our package under “${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}” so install command will copy files to our debian package’s “/opt/${CMAKE_PROJECT_NAME}” sounds easy right? I’ll demonstrate it with a slide show then run it under a Virtual Machine.

mkdir build
cd build
cmake ..
make -j8
make package

All the time test your deployment packages on an empty Virtual Machine. Your OS already has libraries with relative and linked paths. A virtual machine is a vanilia environment to test, if you succeeded your package.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *