Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

qmake rules for generated code

Tags:

qt

qmake

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?

like image 835
swarfrat Avatar asked Jan 05 '10 14:01

swarfrat


2 Answers

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.

like image 182
Caleb Huitt - cjhuitt Avatar answered Oct 22 '22 15:10

Caleb Huitt - cjhuitt


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.

like image 37
milianw Avatar answered Oct 22 '22 13:10

milianw