tags:

views:

2122

answers:

4

I am putting together a build system for my Qt app using a qmake .pro file that uses the 'subdirs' template. This works fine, and allows me to specify the order that each target is built, so dependencies work nicely. However, I have now added a tool to the project that generates a version number (containing the build date, SVN revision, etc,) that is used by the main app - I can build this version tool first but when it is built I want to execute it before any more targets are built (it generates a header file containing the version number that the main app includes.)

For example, my simple qmake file looks like something this:

TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS = version \
          lib \
          tests \
          mainapp

When 'version' is built I want to execute it (passing some arguments on the command-line) before 'lib' is built.

Does anyone know if this is possible? I see that qmake has a 'system' command that can execute apps, but I don't know how I could leverage this.

A related question concerns my unit tests. These live in the 'test' project and use the QTest framework. I want to execute the tests exe before building 'mainapp' and if the tests fail (i.e. the tests exe doesn't return zero) I want to quit the build process.

I realise that qmake is designed to generate makefiles, so I may be wishing for a little too much here but if anyone can give me some pointers it would be very welcome.

+4  A: 

Hi,

I currently use qmake to exec my unit tests automatically for two years - and it works fine.

Have a look here - I made a mini-howto for that:

Qt: Automated Unit Tests with QMAKE

You can use exactly that mechanism to exec your versioning tool after compile/building - so your questions should be solved :-)

If you have any further questions don't hesitate to ask me :-)

Greetz from Germany,

Chris

PS: Here you can find more (undocumented) tricks around QMake: Undocumented QMake

3DH
Thanks. I just read your blog post and I have another solution for multiple QTest's that means you don't need to edit main.cpp to add new tests - they are 'registered' automatically, similar to how the Boost auto test framework works. Let me know if you're interested.
Rob
That sound really interestig - of course am I interested :)greetz,Chris
3DH
http://qtcreator.blogspot.com/2009/10/running-multiple-unit-tests.html
Rob
A: 

I've tried to do a lot of stuff with qmake as a build system over the years. Eventually I just resorted to having a pre-qmake step. Ie. a configure script.

You can build your version tool in there and then execute it before calling qmake to generate the Makefiles.

I found the easiest way to get data into the pro files, if you need that too, is to generate a .pro.inc file and include it from your main pro.

Max Howell
A: 

As 3DH mentioned, you want a QMAKE_POST_LINK option specified in your .pro files that contain something you want executed. So for your example, I would do something like this with the version.pro file:

TEMPLATE = app
TARGET = version
HEADERS = version.h
SOURCES = version.cpp
QMAKE_POST_LINK=./version

Something similar should work with your test directory.

Caleb Huitt - cjhuitt
+1  A: 

I posted a message on the Qt Interest mailing list about a 'pre build' step and it can be done using a combination of PRE_TARGETDEPS and QMAKE_EXTRA_TARGETS. Here is the response:

You can specify custom build steps, eg. this would call makemyversion.sh to create myversion.cpp every time before it builds something:

versiontarget.target = myversion.cpp
versiontarget.commands = ./makemyversion.sh
versiontarget.depends = FORCE

PRE_TARGETDEPS += myversion.cpp
QMAKE_EXTRA_TARGETS += versiontarget

I am now using something similar to this to generate my app's version number each time it is built.

Rob
pre build is tricky to find for qt, since they only have a built in QMAKE_POST_LINK, QMAKE_PRE_LINK and no QMAKE_PRE_BUILT. Thanks for sharing. +2
count0