QT Deployment II – CMake for Automation

Matter of CMake

In previous blog post I’ve created a QT Quick application with CMake. It was not pointless. CMake allows us to call post build functions and template management.

Post build means when you create your target, I mean your binary is created you can call some functions or do some operations.

Template management is another story. Imagine a workspace with more than one developer. Everyone has different file paths for their project even they use the same OS/Distribution. We will use configure_file() function to solve path issues with CMake.

Another thing is Qt 6 about to say goodbye for QMake. The QT Company aims on CMake instead of QMake. So it’s time to hop in this boat before it’s too late.

Setting up CMake Environment

I’m keeping on the same project. It is available on my personal GitHub. The CMakeLists.txt is not enough as beginning for a general build pattern. If I want to build my project on my terminal, I cannot do that because of it doesn’t have Qt’s path. QT Creator IDE tells it in build time by using it’s environment variables. It’s time to set it by:

set(CMAKE_PREFIX_PATH "/home/muhammet/Qt/5.15.0/gcc_64")

Notice: I gave a hard coded path for my QT location. In professional projects. It’s located under project’s source directory. This is important.

Now I can build up my project via my terminal:

Creating Template File

In the previous post we had deployment.json file. Now I’m creating a deployment path under my project path and moving this file as deployment.json.in and it’s time to edit it. File content was like like below code block.

    "clear": true,
    "deploySystem": true,
    "extractPlugins": true,
    "noCheckRPATH": true,
    "qif": true,
    "bin": "../build-DeploymentProject-Desktop_Qt_5_15_0_GCC_64bit-Debug/DeploymentProject",
    "qmlDir": ".",
    "qmake": "/home/muhammet/Qt/5.15.0/gcc_64/bin/qmake",
    "targetDir" : "../DeploymentPackage"

It has static paths. Let’s think what if there is a secondary developer called Alex and Alex should install his user as Muhammet and create a file pattern like mine for Continuous Development? Isn’t that pointless. I’m about to show a quick magic show off for you now and edit the json template.

    "clear": true,
    "deploySystem": true,
    "extractPlugins": true,
    "noCheckRPATH": true,
    "qif": true,
    "qmlDir": "${CMAKE_SOURCE_DIR}",
    "qmake": "${CMAKE_PREFIX_PATH}/bin/qmake",
    "targetDir" : "${CMAKE_BINARY_DIR}/DeploymentPackage"

“Wowowowow! That’s dark magic!”
Me: Hold my build tool
I knew how it is build up. so I transformed paths with CMake variables for the sake of development life cycle. But it’s useless for C QT Deployer tool. It doesn’t know CMAKE_BINARY_DIR or other paths. Before my secondary trick I’d like to mention about variables:

  • CMAKE_BINARY_DIR: It returns the build directory that contains our binary.
  • CMAKE_PROJECT_NAME: It returns the name of our binary
  • CMAKE_SOURCE_DIR: It’s the root path of our project. In this case our qml files are stored in CMAKE_SOURCE_DIR, this is why I gave this path
  • CMAKE_PREFIX_PATH: I think I already gave the path. It contains our QT Path as Prefix path.

So it’s time to show off configure_file() function from CMake. It copies files and compiles them by default for us. I like to copy file to CMAKE_BINARY_DIR because it is developer specific. It converts it to my hard coded paths. I don’t want to push it with my code to with .git as a normal human being.

configure_file("${CMAKE_SOURCE_DIR}/deployment/deployment.json.in" "${CMAKE_BINARY_DIR}/deployment.json")
File generates after calling cmake from terminal for configuration

Post build for Automation

So I’ve already showed off with some tricks and I hope that I amazed you 🙂 Now it’s time for not calling from terminal. Actually this is a bad behavior to call post_build method. I want to explain this in next chapters of this adventure to improve you and me. Patient is the most valuable weapon attachment to bind with your compiler my friend, trust me more than you can.

At the beginning of this post I was talking about post build. Let me introduce you the add_custom_command function. You can run your customized commands pre or post build of your target. You event can run bash script. Sounds amazing! So I’m adding this code block at the bottom of my CMakeLists.txt file:

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

After my binary is generated, it will call cqtdeployer with the parameters I gave and run deployment process. Don’t you believe me? You can try this at your home.

You may also like...

Leave a Reply

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