Adding custom commands to existing targets in qmake

后端 未结 1 1414
野趣味
野趣味 2021-02-07 05:31

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

相关标签:
1条回答
  • 2021-02-07 05:59

    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.


    Option 1

    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:

    1. 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 {} \\;
      
    2. 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...

    3. 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:

    • Completely self-contained in a .pro file.
    • As portable as you can get, leaves the actual Makefile syntax and generation up to qmake.

    Cons:

    • Your new commands aren't appended to the existing recipe. Rather, they happen after all prerequisite targets are satisfied but before the existing recipe. In the 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.

    Option 2

    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:

    1. 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.

    2. 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:

      • We set .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.
      • The % target matches any target that isn't otherwise defined in this Makefile. It simply delegates processing to RealMakefile.
      • The 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:

    • More control over command order. Commands can be added both before and after the default recipe.

    Cons:

    • Not self-contained in a .pro.
    • Not as portable: It doesn't leave all Makefile generation up to 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.

    0 讨论(0)
提交回复
热议问题