Search code examples
qmakepkg-config

QMake and pkg-config


I am using libqtermwidget in one of my Qt applications. It so happens that in version 0.8.0 of the library, some new features have been introduced, which are absent in 0.6.0 version.

Since libqtermwidget does not provide any version macros, I would like to use pkg-config to check its version, something like this, in qmake:

# i would like a functionality like this
if pkg-config --version qtermwidget5 < 0.8.0
    DEFINES += OLD_QTERMWIDGET

This of course could be used inside the cpp file:

#ifndef OLD_QTERMWIDGET
    ... code for 0.8.0 and higher ...
#endif

Solution

  • You should use $$system() to invoke pkg-config and to read stdout (if any). But let's program it in a bit more "generic" way:

    # finds package version by invoking 'pkg-config'
    # $$1 = package
    # note: stores value in cache (stash) file for subsequent use
    defineReplace(findPackage) {
        # using <package>Version variable
        pkg = $${1}Version
        !defined($$pkg, var) {
            # cache miss
            # note: $$pkgConfigExecutable() is an undocumented function from qt_functions.prf
            $$pkg = $$system($$pkgConfigExecutable() --modversion $$1)
            # cannot store the empty value
            isEmpty($$pkg): $$pkg = 0
            # save to the stash file
            cache($$pkg, stash)
        }
        # return value of <package>Version
        return($$eval($$pkg))
    }
    
    
    # now using this...
    qtw5 = $$findPackage(qtermwidget5)
    equals(qtw5, 0) {
        error("qtermwidget5 is not installed")
    } else:!versionAtLeast(qtw5, 0.8.0) {
        warning("Found an old version of qtermwidget5 ($$qtw5)")
        DEFINES += OLD_QTERMWIDGET
    } else {
        # nothing
    }