Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

qmake pre-build step before ANY compilation

Tags:

qt

qmake

There are several questions on SO regarding how to create a pre-build step for qmake, I can do that with this in my .pro file:

versionTarget.target = ../VersionData/versioning.h versionTarget.depends = FORCE win32: versionTarget.commands = cd $$PWD; python.exe ./version_getter.py -p $$TARGET else:  versionTarget.commands = cd $$PWD; python ./version_getter.py -p $$TARGET  PRE_TARGETDEPS += ../VersionData/versioning.h QMAKE_EXTRA_TARGETS += versionTarget 

Now, the problem is that this approach is not a build step per se but just another build target, so if I have the -j flag configured for make it runs my script in parallel with the other build jobs. This is very bad, because my script creates/updates a header file - having it change part way through the compilation is not acceptable.

So, is there anyway I can have this script executed before any compilation is ran? I know I can create another script and call the version_getter.py and qmake in sequence from that, but this is not desirable as I would have to compile from the command line rather than from within Qt Creator.


Update

The complete .pri file that is included by every one of my sub-projects is below:

CONFIG += thread QT += core \       gui  versionTarget.target = ../VersionData/versioning.h versionTarget.depends = FORCE win32: versionTarget.commands = cd $$PWD; python.exe ./version_getter.py -p $$TARGET else:  versionTarget.commands = cd $$PWD; python ./version_getter.py -p $$TARGET  PRE_TARGETDEPS += ../VersionData/versioning.h QMAKE_EXTRA_TARGETS += versionTarget  DEPENDPATH += ../VersionData INCLUDEPATH += ../VersionData HEADERS += ../VersionData/versioning.h  UI_HEADERS_DIR = $${_PRO_FILE_PWD_}/include/Qui DESTDIR = $(SYREN_PATH)  !win32-msvc {     QMAKE_CXXFLAGS += -std=c++0x } 

But this still results in the same parallel behaviour. I thought it may have been due to my use of ccache, but turning it off made no difference (other than being much slower of course).

like image 859
cmannett85 Avatar asked Apr 07 '13 16:04

cmannett85


People also ask

Is qmake a compiler?

qmake was created by Trolltech (now The Qt Company). It is distributed and integrated with the Qt application framework, and automates the creation of moc (meta object compiler) and rcc (resource compiler) sources, which are used in Qt's meta-object system and in the integration of binary resources (e.g., pictures).

What is qmake command?

QMake is a build system that generates Makefiles for GNU Make or project build files for Microsoft Visual Studio. It is part of the Qt software framework by Trolltech. While it is commonly used to construct Qt-based software, any project can benefit from it.

What does run qmake do?

The behavior of qmake can be customized when it is run by specifying various options on the command line. These allow the build process to be fine-tuned, provide useful diagnostic information, and can be used to specify the target platform for your project.


1 Answers

Another option would be to start with the project file snippet in your original question, and also ensure that qmake is aware that versioning.h is a dependency for the other build targets in your project file —

  • Add the full path to versioning.h to your HEADERS variable.
  • Add the folder in which versioning.h resides to your DEPENDPATH variable.

(Caveat: if you run qmake when versioning.h doesn't exist, it will emit "WARNING: Failure to find: versioning.h" — the only workaround for that warning is to use the system() command, as I described in my other answer.)

Example

Create test.pro containing the following:

versionTarget.target = ../versioning.h versionTarget.depends = FORCE versionTarget.commands = sleep 5s ; touch ../versioning.h PRE_TARGETDEPS += ../versioning.h QMAKE_EXTRA_TARGETS += versionTarget  SOURCES = test.c HEADERS = ../versioning.h DEPENDPATH = .. 

Create test.c containing the following:

#include "../versioning.h" 

Run qmake. It will output WARNING: Failure to find: ../versioning.h.

Run make -j9. It will run versionTarget.commands (which sleeps for 5 seconds to exaggerate any multiprocessing problems), and, after that is done, run the command to compile test.c.

(And if you examine the generated Makefile, you'll see that test.o depends on both test.c and ../versioning.h, so Make should correctly figure out that it can't run the command to compile test.c before the command to create/update ../versioning.h.)

like image 77
smokris Avatar answered Sep 20 '22 05:09

smokris