Important: Please read the Qt Code of Conduct -

Howto have a pre-build target generated by qmake

  • Hi Forum

    Let qmake gerenate a Makefile that calls a pre-build process.

    I would like to have a Makefile generated that contains a rule

    all: pre_build_process Makefile.Debug $(TARGET)

    pre_build_process could do something with files that $(TARGET) might depend upon, e.g. change a header file that contains the current subversion revision number.

    Would be glad to get an answer on this.

  • [quote author="sigrid" date="1308209643"]"QMAKE_EXTRA_TARGETS": or "QMAKE_EXTRA_COMPILERS": should help here. The following "blog": contains some more details on these variables.[/quote]

    Thanks for your answer, but my problem is, that every time I hit 'make' this pre-build-process should be started.

    With the examples in your links you get the extra compiler runs once, but not a second time, because the output files are there in the second run (as long as the input files don't change).

    But in my scenario the to be created (output) file might change from 'make' to 'make' with out a dependency on a file, but more on the outcome of svnversion which is used to create the output file.

    Any idea on this?

  • If you are using QMAKE_EXTRA_COMPILERS then you can use "depend_command": or depends to set an extra dependency on the compiler to force it to build again.

  • [quote author="sigrid" date="1308306646"]If you are using QMAKE_EXTRA_COMPILERS then you can use "depend_command": or depends to set an extra dependency on the compiler to force it to build again.[/quote]

    Thanks for this hint.

    However I ran into deep problem with the different makefile generators:

    • make (linux)
    • nmake
    • jom
    • msbuild

    It seems to be not possible to trigger an extra build for these for backends with one generic pro file.

    I finally ended up in:


    Rules to update a header file: windows_version_revision.rc with the current svn revision.

    Have an qmake extra compiler called for each run of make to update windows_version_revision.rc,

    but only if needed, to prevent dependent compiles.

    A special script is used: update_svn_revision (for details look into the script)

    - replace placeholders in windows_version_revision.rc.SubWCRev with the current svn revision number

    - output a temporary file and if this is different to windows_version_revision.rc, then copy it over (updating the time stamp)

    It is importand to have the .variable_out set to HEADERS so that the Makefile rules recognize the dependency on the


    To trigger this conditional update of windows_version_revision.rc we need a dependency with an extra target that is never fullfilled.

    This forces make to call this target each time.

    .depends on extra target: preprocesssvnrev_check

    preprocesssvnrev_check.commands just echos

    However under windows everything is different:

    have special handling for MS IDE backend

    there we need an extra PRE_TAGETDEPS that must not be used under nmake

    nmake on the other side does not recognize that the conditional touched file was not touched in each nmake

    and fires a relink anyway, while jom does not .... clever jom :-)

    create the resource file from a template.

    PREPROCESS_FILES = windows_version_revision.rc.SubWCRev = "Update svn revision number in windows_version_revision.rc (if needed)."
    preprocesssvnrev.input = PREPROCESS_FILES
    preprocesssvnrev.output = ${QMAKE_FILE_BASE}

    update revision from the current directory

    win32:preprocesssvnrev.commands = $${PWD}/update_svn_revision.bat $${PWD} $${PWD}/${QMAKE_FILE_IN} $${PWD}/${QMAKE_FILE_BASE}
    unix: preprocesssvnrev.commands = @sh $${PWD}/ $${PWD} $${PWD}/${QMAKE_FILE_IN} $${PWD}/${QMAKE_FILE_BASE}

    trigger creation when outside MS IDE (part 1)

    contains(TEMPLATE,lib) {
    preprocesssvnrev.variable_out = HEADERS
    preprocesssvnrev.depends = preprocesssvnrev_check

    message (Creating extra dependencies for nmake/make)


    QMAKE_EXTRA_COMPILERS += preprocesssvnrev

    trigger creation when inside MS IDE

    contains(TEMPLATE,vclib) {

    message (Creating extra dependencies for MS IDE)

    PRE_TARGETDEPS += compiler_preprocesssvnrev_make_all

    trigger creation when outside MS IDE (part 2)

    contains(TEMPLATE,lib) {
    preprocesssvnrev_check.commands = @echo Trigger conditional svn revision update.
    preprocesssvnrev_check.CONFIG += recursive
    QMAKE_EXTRA_TARGETS += preprocesssvnrev_check

    message (Creating extra dependencies for nmake/make)


Log in to reply