views:

60

answers:

3

We have a fairly large code-base. The vast majority of the code is compiled using qmake to produce the makefiles. However, there are some sub-projects that get produced by running batch files or running other programs.

I'd like to be able to have everything compiled using qmake, but I can't figure out how to get qmake to simply run a script.

One thing that I've tried is using QMAKE_EXTRA_TARGETS in my pro file, like so:

TEMPLATE = lib
SOURCES = placeholder.cpp
CONFIG += no_link staticlib
batch_runner.target   = placeholder.cpp
batch_runner.commands = my_batch_file.bat
QMAKE_EXTRA_TARGETS   = batch_runner

I then have to have the batch file produce placeholder.cpp like so:

# do the real work here
# ...
# create placeholder.cpp so qmake and nmake are happy
echo // dummy >> placeholder.cpp

This seems to work fine. The trouble is that it is somewhat hokey. If I don't specify batch_runner.target (i.e. I leave it blank) or don't put placeholder.cpp in SOURCES then the batch file never gets run. This is because qmake isn't making batch_runner.commands the action for any other dependency in the Makefile.

Is there any better way to get QMake to construct a Makefile such that a script is run when the Makefile executes?

+2  A: 

Try the system() command.

Arnold Spence
Interesting. That works, but the command gets run when 'qmake' is run rather than when 'nmake' is run. It also happens to be run three times rather than the once it should really be run.
Tom
You may have to add some scope to ensure it is only run once.
Arnold Spence
A: 

You could use the SUBDIRS configuration to run multiple different targets, even from the same makefile. This might work especially well with your extra targets, as a subdir configuration can specific a specific target in the makefile to run (see undocumented qmake for details). In this case, I would put all of the "regular" build commands in one .pro file, the external build commands in another, and a subdirs .pro file to build all of them. I haven't tested anything quite like this, but it should work.

regular.pro:

SOURCES += main.cpp
TARGET = regular.exe

external.pro:

batch_runner.commands = my_batch_file.bat
QMAKE_EXTRA_TARGETS   += batch_runner

other_runner.commands = other_batch_file.bat
QMAKE_EXTRA_TARGETS   += other_runner

do_it_all.pro:

TEMPLATE = subdirs
CONFIG += ordered

regular.file = regular.pro
SUBDIRS += regular

batch.file = external.pro
batch.target = batch_runner
SUBDIRS += batch

other.file = external.pro
other.target = other_runner
SUBDIRS += other
Caleb Huitt - cjhuitt
This has the same problem that I outlined in my original question. The extra target won't run unless the .target is specified (you can't seem to just specify the commands) and the .target needs to actually be required by some other part of the Makefile, otherwise it won't run.
Tom
+1  A: 

It looks like QMAKE_POST_LINK works well for this sort of thing.

This seems to get the job done. my_batch_file.bat runs when nmake runs (rather than when qmake runs) and I don't need to do anything funny with placeholder targets or files.

It's quite likely that I don't need all of the items listed in 'CONFIG'.

TEMPLATE = lib
TARGET   = 
CONFIG  += no_link target_predeps staticlib

QMAKE_POST_LINK  = my_batch_file.bat
QMAKE_CLEAN     += batch_output.obj
Tom