qt - Adding custom commands to existing targets in qmake -
is there way specify, in .pro file, commands added standard target in makefile qmake generates? example, consider distclean, commands might desired to:
- remove *~ files.
- clean out runtime-generated output files source tree.
- etc.
i want use normal target , not custom target because want transparent in workflow. (again using distclean example), don't want to...
- ... require knowledge in multi-project setup makefiles use custom rule instead of
distclean. - ... document custom rules, stand-alone projects,
distcleanwell-known , intuitive†.
i found how add custom targets in qmake generated makefile?, describes adding custom targets (which already documented, back in 4.6) rather adding rules existing targets. while contain hints, of them require adding new custom targets, specifying same target more once in makefile replaces (not adds) commands previous target.
the thing think of try add target.commands += new commands .pro file wild guess (e.g distclean.commands += rm \"*~\"). has no effect.
how can transparently add custom commands existing targets qmake?
† for distclean example: while maintainer-clean on "standard target" list, in practice have found used, , in case qmake doesn't generate default; consider unsuitable.
there 2 straightforward ways accomplish this, depending on how self-contained / portable want solution , how lenient want order of command execution.
option 1
the first option create custom target in .pro file new commands, add target prerequisite standard target modifying. going distclean example, let's want add command remove *~ files:
create custom target in .pro file. note have escape quotes , slashes in .pro files. example, add:
extraclean.commands = find . -name \"*~\" -exec rm -v {} \\;add target dependency of target modifying:
distclean.depends = extracleanthis won't modify
distcleanrule yet, method can't used modify existing rules. however...add both new target , target modifying targets:
qmake_extra_targets += distclean extracleanthis add second specification of
distcleanmakefile, works because you can add dependencies existing targets inmakein separate rules, though can't add commands way. if specifydistclean.commandsin .pro file, break existingdistcleanreplacing default recipe.
so, putting together, in .pro file:
extraclean.commands = find . -name \"*~\" -exec rm -v {} \\; distclean.depends = extraclean qmake_extra_targets += distclean extraclean where extraclean custom target commands want add, , distclean existing target wish modify.
pros:
- completely self-contained in .pro file.
- as portable can get, leaves actual makefile syntax , generation
qmake.
cons:
- your new commands aren't appended existing recipe. rather, happen after prerequisite targets satisfied before existing recipe. in
distcleanexample, version ofqmakei'm using, places commands after source tree clean before makefile deleted (which action default recipe takes). not issue example, may issue you.
option 2
the second option change name of makefile qmake generates, , create own custom makefile defers generated one, rather includes + overrides it. straightforward option; while not self-contained option 1, gives ability execute commands both before , after default generated recipe.
you don't want include + override existing makefile, because don't want replace default recipes. if do, have re-implement default, can issue default may change (and have keep changes). it's best let qmake work possible, , not repeat work.
to this:
first, change name of file
qmakegenerates. can accomplished adding line such .pro file:makefile = realmakefilethat cause
qmakeoutput realmakefile instead of makefile.the next step create own
makefilecustom commands. however, there caveats here. first, full example, again usingdistclean. in file namedmakefile:.default_goal := %: @$(make) -f realmakefile $@ distclean: @$(make) -f realmakefile $@ @find . -name "*~" -exec rm -v {} \;some notes this:
- we set
.default_goalbecause otherwisedistcleandefault. alternative this, if you're not comfortable.default_goal, specifyallrule using@$(make) -f realmakefile $@recipe. - the
%target matches target isn't otherwise defined in makefile. delegates processing realmakefile. - the
distcleantarget add our commands. still need delegate realmakefile, additional commands can added both before , after happens.
- we set
pros:
- more control on command order. commands can added both before , after default recipe.
cons:
- not self-contained in .pro.
- not portable: doesn't leave makefile generation
qmake, , i'm not sure parts specific gnumakehere (comments welcome).
so, while answer may little long, both of these methods straightforward. prefer option 1 unless command execution order issue.
Comments
Post a Comment