Search code examples
c++qtqt-creatorqmake

Running script from qmake to get build number


I am trying to add a build number to my Qt project, by including that number as a DEFINE. I've found a few examples, but I can't seem to get them to work. Basically the build.sh is called from the command line, echoing the current build number. That number should be set as a #DEFINE in qmake, and included in my code

I have the following code in the .pro file

unix: {
    BUILDNO = $$system(./build.sh)
    DEFINES += BUILD_NUM=$${BUILDNO}
}
else:
    DEFINES += BUILD_NUM=0

And this code in my build.sh file

#!/bin/bash    
n=25;#the variable that I want to be incremented
next_n=$[$n+1]
sed -i "/#the variable that I want to be incremented$/s/=.*#/=$next_n;#/" ${0}
echo $n

And an example line from my code:

a.setApplicationVersion(QString("%1.%2.%3").arg(VERSION_MAJ).arg(VERSION_MIN).arg(BUILD_NUM));

Running the build.sh script from the command line works just fine. It echos a single number to the command line, and increments the number in the script. That number increments when I build my project, so I know's its being executed. But the number that is echoed is not showing up in my code when I print the DEFINE variable. But... on Linux, it is always 0


Solution

  • Your .pro syntax is incorrect since the "else" command should not use ":" but "{}", so the previous command is always overridden.

    unix: {
        BUILDNO = $$system(sh $$(PWD)/build.sh)
        DEFINES += BUILD_NUM=$${BUILDNO}
    }
    else {
        DEFINES += BUILD_NUM=0
    }
    

    As @Matt points out, you can use ":" if the command is on the same line:

    unix: {
        BUILDNO = $$system(sh $$(PWD)/build.sh)
        DEFINES += BUILD_NUM=$${BUILDNO}
    }
    else: DEFINES += BUILD_NUM=0