[Qt-interest] Makefile rules in .pro file?
Phil Dumont
phil at solidstatescientific.com
Thu Aug 11 19:39:33 CEST 2011
What I had worked for in-source builds, but not out-of-source.
I got it to work (sort of) for out-of-source builds with this:
QT -= gui
TARGET = myapp
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SRCDIR = $$system(pwd)
system(echo SRCDIR is $$SRCDIR)
system(echo QMAKE_MAKEFILE is $$QMAKE_MAKEFILE)
# Create a dummy of the generated header, so qmake won't complain that
# it doesn't exist when we add it to HEADERS, but with an early
# timestamp so it still gets generated on the first make.
!exists(myapp_gen.h): system(touch --date "'1970-01-01 00:00:00'"
myapp_gen.h)
SOURCES += myapp.cpp
HEADERS += myapp.h \
myapp_gen.h
gensrc.target = myapp_gen.h
gensrc.depends = $$SRCDIR/gen_header.py $$SRCDIR/myapp.ini
gensrc.commands = $$SRCDIR/gen_header.py
QMAKE_EXTRA_TARGETS += gensrc
PRE_TARGETDEPS += myapp_gen.h
There are a couple of things I don't like about this, though:
1. I don't much like my definition of SRCDIR. I would have hoped that
there would be some built-in qmake variable that said where the source
directory was, but I could not find it. What I did only works because I was
lucky enough to find out by accident that qmake happens to chdir to the
source directory fairly early on. And who knows whether I can count on this
behavior in other releases?
2. This puts a derived object (the generated file, myapp_gen.h) in the
source directory, not the build directory, which goes against the purpose of
an out-of-source build. Of course, I could change my generator script to
put its output in the build directory, but then, what about all the
references to myapp_gen.h in the .pro file? I'd have to change them to
include the path to the file. And I had even less luck getting qmake to
tell me where the build dir is than where the source dir is. MOC_DIR looked
like it might work (just because other projects I've built put all the moc
files at the top of the build directory), but this particular project
doesn't have any moc files, so qmake doesn't seem to be bothering to define
MOC_DIR. I even turned on debuging (qmake -d [-d ...]) to see which
variables were being defined to reference the build directory. The only
thing I found was that QMAKE_MAKEFILE gave a full path to the Makefile. So
I thought I could use $$dirname($$QMAKE_MAKEFILE). But that didn't pan out,
because QMAKE_MAKEFILE is not being defined until after at least one of my
attempted references was resolved.
So, as in my first post, I ask again, any help?
Thanks,
phil
On Fri, Aug 5, 2011 at 2:46 PM, Phil Dumont
<phil at solidstatescientific.com>wrote:
> It's a bit kludgey, but adding this line:
>
> !exists(myapp_gen.h): system(touch --reference myapp.ini myapp_gen.h; touch
> myapp.ini)
>
>
> anywhere before I add myapp_gen.h to the HEADERS variable stops qmake from
> complaining and still makes the source generation script run on the first
> make.
>
> phil
>
>
>
> On Fri, Aug 5, 2011 at 1:16 PM, Phil Dumont <phil at solidstatescientific.com
> > wrote:
>
>> I've got it working *almost* perfectly.
>>
>> My .pro file looks something like this:
>>
>> QT -= gui
>> TARGET = myapp
>> CONFIG += console
>> CONFIG -= app_bundle
>> TEMPLATE = app
>>
>> SOURCES += myapp.cpp
>>
>> HEADERS += myapp.h \
>> myapp_gen.h
>>
>> gensrc.target = myapp_gen.h
>> gensrc.depends = gen_header.py myapp.ini
>> gensrc.commands = gen_header.py
>>
>> QMAKE_EXTRA_TARGETS += gensrc
>> PRE_TARGETDEPS += myapp_gen.h
>>
>>
>> The only thing keeping me from being completely happy is, when I run
>> qmake, it complains thusly:
>>
>> WARNING: Failure to find: myapp_gen.h
>>
>>
>> Ordinarily, qmake complaining about a missing header would be good. But
>> under current circumstances, not so much. Is there any way to turn it off?
>> Alternatively, I could throw a message() in the .pro file that tells the
>> next person to build the project (maybe even me, after I've had long enough
>> to forget) that the ominous looking message is nothing to worry about. But
>> that wouldn't be quite a clean.
>>
>> Anyway, despite the warning, the Makefile is generated, and does the right
>> thing.
>>
>> phil
>>
>>
>> On Fri, Aug 5, 2011 at 12:31 PM, Phil Dumont <
>> phil at solidstatescientific.com> wrote:
>>
>>> After hitting send, I looked just a little more... Looks like
>>>
>>> http://doc.trolltech.com/4.7/qmake-environment-reference.html#customizing
>>>
>>> might be what I'm looking for.
>>>
>>> phil
>>>
>>>
>>> On Fri, Aug 5, 2011 at 12:02 PM, Phil Dumont <
>>> phil at solidstatescientific.com> wrote:
>>>
>>>> Hi all,
>>>>
>>>> I'm starting on a Qt project where one of the source files (a header) is
>>>> going to be generated by a script. I would like a rule in my Makefile that
>>>> runs the script when the target is missing or older than it's dependencies.
>>>> I've looked quite a lot through qmake documentation, but have been unable
>>>> to find what to put in my .pro file to get this done. Any help?
>>>>
>>>> Thanks,
>>>> phil
>>>>
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110811/b017f1b8/attachment.html
More information about the Qt-interest-old
mailing list