Is there a way to specify, in a .pro file, extra commands to be added to a standard target in the Makefile that qmake
generates? For example, con
There are two straightforward ways to accomplish this, depending on how self-contained / portable you want your solution to be and how lenient you want to be with the order of command execution.
The first option is to create a custom target in the .pro file for the new commands, then add that target as a prerequisite to the standard target that you are modifying. Going back to the distclean
example, let's say you want to add a command to remove all *~ files:
Create a custom target in your .pro file. Note that you have to escape quotes and slashes in .pro files. For example, add:
extraclean.commands = find . -name \"*~\" -exec rm -v {} \\;
Add this target as a dependency of the target you are modifying:
distclean.depends = extraclean
This won't actually modify the distclean
rule just yet, as this method can't be used to modify existing rules. However...
Add both your new target and the target you are modifying as extra targets:
QMAKE_EXTRA_TARGETS += distclean extraclean
This will add a second specification of distclean
to the Makefile
, but this works because you can add dependencies to existing targets in make in separate rules, even though you can't add commands that way. If you were to also specify distclean.commands
in your .pro file, you would break the existing distclean
by replacing its default recipe.
So, putting that all together, in the .pro file:
extraclean.commands = find . -name \"*~\" -exec rm -v {} \\;
distclean.depends = extraclean
QMAKE_EXTRA_TARGETS += distclean extraclean
Where extraclean
is some custom target with the commands you want to add, and distclean
is the existing target that you wish to modify.
Pros:
qmake
.Cons:
distclean
example, with the version of qmake
that I'm using, this places the commands after the source tree clean but before Makefile itself is deleted (which is the only action the default recipe takes). This is not an issue for this example, but may be an issue for you.The second option is to change the name of the Makefile that qmake
generates, and create your own custom Makefile that defers to the generated one, rather than includes + overrides it. This is also a straightforward option; while not as self-contained as option 1, it gives you the ability to execute commands both before and after the default generated recipe.
You don't want to include + override the existing Makefile, because you don't want to replace the default recipes. If you do, you have to re-implement the default, but this can be an issue as that default may change (and you have to keep up with the changes). It's best to let qmake
do as much work as possible, and not repeat its work.
To do this:
First, change the name of the file that qmake
generates. This can be accomplished by adding a line such as this to the .pro file:
MAKEFILE = RealMakefile
That will cause qmake
to output RealMakefile instead of Makefile.
The next step is to create your own Makefile
with your custom commands. However, there are some caveats here. First, a full example, again using distclean
. In a file named Makefile
:
.DEFAULT_GOAL := all
%:
@$(MAKE) -f RealMakefile $@
distclean:
@$(MAKE) -f RealMakefile $@
@find . -name "*~" -exec rm -v {} \;
Some notes about this:
.DEFAULT_GOAL
because otherwise distclean
would be the default. An alternative to this, if you're not comfortable with .DEFAULT_GOAL
, is to specify an all
rule using @$(MAKE) -f RealMakefile $@
as the recipe. %
target matches any target that isn't otherwise defined in this Makefile. It simply delegates processing to RealMakefile.distclean
target is where we add our commands. We still need to delegate to RealMakefile, but additional commands can be added both before and after that happens.Pros:
Cons:
qmake
, and also I'm not actually sure what parts are specific to GNU make
here (comments welcome).So, while this answer may be a little long, both of these methods are very straightforward. I would prefer option 1 unless the command execution order is an issue.