4
votes

I want qmake to run a (python) script automatically. This script modifies the makefiles, so it has to be executed after qmake generates the makefiles but before make.

So far I've only found 2 ways to run a script from qmake:

Using system() in my .pro file, but this runs before qmake - too soon:

win32: PYTHON=python.exe
else:  PYTHON=python
system($$PYTHON ./test.py) 

or via custom build target using QMAKE_EXTRA_TARGETS, but this is invoked by make (too late).

Is there any way to run a script from qmake after it generates the makefiles?

3
here is same old question, but without answer. IMO it would be best if you explain why you need this strange feature? Maybe there is an alternative solution? For example cmake instead of qmake. - Marek R
Why not just have a shell script/batch file that runs qmake and then your script? - MrEricSir
@MarekR: I'm working on a unity build for quite a large project - I need to include all cpp files into one file and only this one file is built. But I also want to include generated moc files, but I don't know if it's possible to know all the moc files before qmake is run. - Jaa-c
@MrEricSir: It's a big project and it would be currently quite complicated to switch everything from qmake to running some script file. - Jaa-c
@RedOctober: I get that, it's possible, but not very maintainable. Qmake is being run from ide, automated builders, different scripts etc. I would have to change all those cases, which I'd like to avoid, if possible. - Jaa-c

3 Answers

3
votes

Since we are using TEMPLATE = subdirs for our project, I solved this by creating new subdir, that is parsed by qmake as a last one. In its pro file I'm using TEMPLATE = aux and running the script by system() call.

It's not the best solution, but it works quite well.

1
votes

The following has worked well for me for several years.

  1. Create a .cmd or .sh script that invokes qmake, and then your script:

    %QTDIR%\bin\qmake %* python.exe test.py

  2. Save the script where it can be found via the PATH environment
  3. In your .pro file add the following:

    QMAKE_QMAKE = myqmake

  4. Then simply invoke myqmake rather than qmake

If the script will be run outside the Qt enviornment, (such as from an IDE), then it may need to define the QTDIR and QMAKESPEC environments.

-1
votes

Cotire (compile time reducer) for CMake might me your friend.

It has the following the feature you're looking for to speed up builds:

Automatically generates a single compilation unit (aka unity source file) for a CMake target.

I did not use it but it is recommended in a C++ best practice list.