Search code examples
qtqmakecomparison-operators

Floating point comparison in qmake


I'm trying to detect the version of a library in qmake and based on that set a preprocessor variable. I obtain the version number (a decimal number) using pkg-config and would like to compare it to a minimum version. I can use bc in shell script to do the floating point comparison but cannot get it to work using $$system in qmake.

LIBSTATGRAB_OLDAPI = $$system(echo "`pkg-config --modversion libstatgrab ` < 0.90 " | bc)
equals(LIBSTATGRAB_OLDAPI,"1") {
    message("Using old libstatgrab API")
    DEFINES += LIBSTATGRAB_OLD
}

The problem is that the shell command run by $$system seems to interpret the < character in the quotes as a readfile command and does not pipe the whole quoted block to bc.

sh: 1: cannot open 0.90: No such file

How can I get around this?


Solution

  • Using bc for this is not reliable in case if version number has more than one delimiter, e.g. 0.90.1. My solution uses sort:

    LIBSTATGRAB_OLDAPI = $$system(echo "`pkg-config --modversion libstatgrab` \\\\n 0.90" | sort -VbC; echo $?)
    

    Explanation

    sort is trying to check whether e.g. 0.95 \n 0.90 is unsorted, if so it exists with code 1.

    \n is escaped with four backslashes, this is a qmake requirement. \n is also surrounded with spaces for readability purpose, sort -b takes care of whitespace.

    sort -V - designed for version sorting

    sort -b - ignores whitespace

    sort -C - silent check, we need only the exit code

    echo $? - prints the exit code