Question

I realized my earlier question was a little confused about the rules and dependencies. The following .pro file generates a makefile which works correctly IF the source files in the directory 'generated' exist at the time qmake runs.

idl.target   = generated/qmtest.h
idl.commands = code_generator 
idl.config   = no_link
idl.depends  = $$SOURCES $$HEADERS $$FORMS

TEMPLATE       = app
INCLUDEPATH    += generated
SOURCES        += generated/*.cpp
PRE_TARGETDEPS += generated/qmtest.h
QMAKE_EXTRA_UNIX_TARGETS += idl

But when qmake runs, its only generating a makefile, and PRE_TARGETDEPS & QMAKE_EXTRA_UNIX_TARGETS don't help me. How can I get qmake to generate a makefile which will add the contents of generated/ to SOURCES?

Was it helpful?

Solution

You may need to do this in two passes.

In your qmake file, add the following line:

include( generated/generated.pri )

Then, at the end of your code_generator script, add the sources to the generated.pri file (using bash for the example, but the idea is the same for almost all languages):

rm generated/generated.pri
for file in $( ls generated/*.cpp ); do
    echo "SOURCES += ${file}" >> generated/generated.pri
done

The first time you run the qmake file, generated/generated.pri will presumably be empty. When you run make, it will populate the generated.pri file. The second time, it will recreate the make file (as a source .pri file changed), then compile again. You might be able to fiddle around with other commands which would do the second stage for you.

OTHER TIPS

I had the same issue just now, but for a simpler usecase of just a single generated file. For that, I found a much simpler way to achieve this by using GENERATED_SOURCES instead of SOURCES:

dummyfile.target = dummy.cpp
dummyfile.commands = touch $$dummyfile.target
QMAKE_EXTRA_TARGETS += dummyfile
GENERATED_SOURCES += $$dummyfile.target

Probably one could push that into a qmake loop and generate the proper targets for multiple files as well.

I've come up with a solution that I believe to be robust and general, that relies on qmake generating a GNU Makefile.

Say we've got all extra SOURCES and HEADERS assignments in a file named Makefile.pri, which gets generated by executing the script generate_Makefile.pri.sh that also generates the sources and/or headers mentioned in Makefile.pri.

We want this file automatically and reliably generated by the build system itself, so to be included in the .pro file, so that qmake can take into account its content and generate the Makefile with the proper dependencies.

This is then what we have to put in our .pro file.

Makefile_pri.target   = Makefile.pri
Makefile_pri.commands = generate_Makefile.pri.sh
Makefile_pri.depends  = FORCE

Makefile.target  = $$MAKEFILE
Makefile.depends = Makefile_pri

include($$Makefile_pri.target)

QMAKE_EXTRA_TARGETS = Makefile Makefile_pri

This way:

  1. if there's no Makefile.pri, then it gets created;
  2. If Makefile.pri gets created or updated, then qmake runs again and takes into account its content, regenerating the Makefile

As said, this does depend on qmake generating a GNU Makefile, but the same approach I believe could be adapted to work also with other target build systems.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top