The power of qmake

Having been a support engineer for over seven years now at Trolltech, I've seen my fair share of inquiries about qmake and since Qt 4.x, qmake has become quite powerful and useful. There are a couple of lesser known variables that can be used which can make integrating your own compilers and targets into a makefile a lot easier to achieve.

Let's say you want to have your own pre-processor to run over some files before building occurs. This is where you can use QMAKE_EXTRA_COMPILERS. In this case, all our preprocessor does is rename the file from .cxx to .cpp.

    PREPROCESS_FILES = preprocess.cxx foobar.cxx more.cxx
preprocess.name = Renaming .cxx files to .cpp files
preprocess.input = PREPROCESS_FILES
preprocess.output = ${QMAKE_FILE_BASE}.cpp
preprocess.commands = ren ${QMAKE_FILE_IN} ${QMAKE_FILE_BASE}.cpp
QMAKE_EXTRA_COMPILERS += preprocess

Doing qmake followed by make will show that it is renaming the files, however on inspection of the makefile it will show that its going to consider these files as objects. Since they are still C++ source code files we want to be able to have them compiled first. With the addition of the line:

    preprocess.variable_out = SOURCES

then it indicates that the output should be added to the SOURCES variable which qmake already knows how to handle. In more advanced use cases we could always indicate that the output shouldn't be put in with the objects because it is used for other reasons and that can be done by adding:

    preprocess.CONFIG += no_link

The other lesser known variable is QMAKE_EXTRA_TARGETS, this is useful when you want to be able to add a custom build target to your Makefile. For example, if you wanted to have a target to update your qm file for your application. Then you would have something like:

    updateqm.commands = lrelease myapp_no.ts
updateqm.target = updateqm
QMAKE_EXTRA_TARGETS += updateqm

It is as simple as that, then you can cause it to be invoked by:

    make updateqm

If you have more than one configuration (as is the default on Windows) then if you want to have it inside the configuration specific makefiles (i.e. Makefile.Debug and Makefile.Release) then simply add:

    updateqm.CONFIG += recursive

then you can do:

    make -f Makefile.Debug updateqm

The documentation for these variables is unfortunately minimal for Qt 4.3 and Qt 4.4.0. However, if you check out the snapshots after it is updated tonight then you will see some in-depth documentation on what is available for both these variables.


Blog Topics:

Comments