将自定义命令添加到qmake中的现有目标 [英] Adding custom commands to existing targets in qmake

查看:94
本文介绍了将自定义命令添加到qmake中的现有目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 .pro 文件中指定要添加到生成的 Makefile 中的标准目标的其他命令?例如,考虑distclean,可能需要其他命令来完成

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, consider distclean, extra commands might be desired to:

  • 删除 *〜文件.
  • 从源代码树中清除运行时生成的输出文件.
  • 等等.
  • Remove *~ files.
  • Clean out runtime-generated output files from the source tree.
  • Etc.

我想使用普通目标而不是自定义目标,因为我希望它在我的工作流程中是完全透明的.也就是说(再次以distclean为例),我不想...

I want to use the normal target and not a custom target because I want this to be completely transparent in my workflow. That is (again using distclean as an example), I don't want to...

  • ...需要在多项目设置中了解某些 Makefiles 使用自定义规则而不是distclean.
  • ...文档自定义规则,甚至对于独立项目也是如此,因为distclean已经
  • ... require knowledge in a multi-project setup that certain Makefiles use a custom rule instead of distclean.
  • ... document custom rules, even for stand-alone projects, as distclean is already well-known and intuitive.

我发现了如何在qmake生成的Makefile中添加自定义目标?,但这描述了添加自定义目标(已记录,甚至

I found How to add custom targets in a qmake generated Makefile?, but this describes adding custom targets (which is already documented, even back in 4.6) rather than adding rules to existing targets. While it does contain some hints, all of them require adding new custom targets, as specifying the same target more than once in a Makefile replaces (not adds) commands from the previous target.

我真正想到的唯一尝试是将target.commands += new commands添加到 .pro 文件中,这是一个疯狂的猜测(例如distclean.commands += rm \"*~\").这没有作用.

The only thing I could really think of to try was to add target.commands += new commands to the .pro file as a wild guess (e.g distclean.commands += rm \"*~\"). This has no effect.

如何使用qmake透明地将自定义命令添加到现有目标?

How can I transparently add custom commands to existing targets with qmake?

对于distclean示例:尽管maintainer-clean也在该标准目标"列表中,但实际上我发现它很少使用,并且在任何情况下,qmake都不默认生成它;我认为这不合适.

For the distclean example: While maintainer-clean is also on that "standard target" list, in practice I have found it to be rarely used, and in any case qmake doesn't generate it by default; I consider it to be unsuitable.

推荐答案

有两种简单的方法可以完成此任务,具体取决于您希望解决方案的独立性/可移植性以及对订单的宽容度命令执行.

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.

第一个选择是在 .pro 文件中为新命令创建一个自定义目标,然后将该目标作为先决条件添加到您要修改的标准目标中.回到distclean示例,假设您要添加一条命令以删除所有 *〜文件:

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. 在您的 .pro 文件中创建一个自定义目标.请注意,您必须在 .pro 文件中转义引号和斜杠.例如,添加:

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

  • 将此目标添加为您要修改的目标的依赖项:

  • Add this target as a dependency of the target you are modifying:

    distclean.depends = extraclean
    

    这实际上还不会修改distclean规则,因为该方法不能用于修改现有规则.但是...

    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
    

    这会将distclean的第二个规范添加到Makefile,但这是可行的,因为

    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.

    因此,将所有内容放到 .pro 文件中:

    So, putting that all together, in the .pro file:

    extraclean.commands = find . -name \"*~\" -exec rm -v {} \\;
    distclean.depends = extraclean
    QMAKE_EXTRA_TARGETS += distclean extraclean
    

    extraclean是要添加命令的自定义目标,而distclean是要修改的现有目标.

    Where extraclean is some custom target with the commands you want to add, and distclean is the existing target that you wish to modify.

    优点:

    • 完全独立于 .pro 文件.
    • 尽可能地便携,保留实际的 Makefile 语法并生成直到qmake.
    • Completely self-contained in a .pro file.
    • As portable as you can get, leaves the actual Makefile syntax and generation up to qmake.

    缺点:

    • 您的新命令未追加到现有配方中.相反,它们发生在所有先决条件都满足之后,但是在现有配方之前.在distclean示例中,使用我正在使用的qmake版本,这会将命令放置在源代码树干净之后但在删除 Makefile 本身之前(这是默认的唯一操作)食谱需要).对于本示例而言,这不是问题,但可能对您来说是一个问题.
    • 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.

    第二个选项是更改qmake生成的 Makefile 的名称,并创建自己的自定义 Makefile ,将 defers 用作生成的一个,而不是包含+覆盖它.这也是一个简单的选择.虽然不如选项1那样独立,但它使您能够在默认生成的配方之前和之后执行命令.

    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.

    您不想包含+覆盖现有的 Makefile ,因为您不想替换默认配方.如果这样做,则必须重新实现默认设置,但这可能是一个问题,因为该默认设置可能会更改(并且您必须跟上所做的更改).最好让qmake做尽可能多的工作,而不要重复其工作.

    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.

    要这样做:

    1. 首先,更改qmake生成的文件的名称.这可以通过在 .pro 文件中添加这样的行来实现:

    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
    

    这将导致qmake输出 RealMakefile 而不是 Makefile .

    下一步是使用自定义命令创建自己的Makefile.但是,这里有一些警告.首先,是一个完整的示例,再次使用distclean.在名为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 {} \;
    

    关于此的一些注释:

    • 我们设置.DEFAULT_GOAL,因为否则默认为distclean.如果您不熟悉.DEFAULT_GOAL,则另一种选择是使用@$(MAKE) -f RealMakefile $@作为配方来指定all规则.
    • %目标与该 Makefile 中未定义的任何目标匹配.它只是将处理委托给 RealMakefile .
    • distclean目标是我们添加命令的位置.我们仍然需要委托给 RealMakefile ,但是可以在发生这种情况之前和之后添加其他命令.
    • 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.

    优点:

    • 对命令顺序的更多控制.可以在默认配方之前和之后添加命令.

    缺点:

    • 不是自包含在 .pro 中.
    • 不那么可移植:它不会将所有 Makefile 生成都保留到qmake,而且我实际上也不确定在这里哪些部分是GNU make所特有的(欢迎发表评论)
    • 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).

    因此,虽然这个答案可能会有点长,但是这两种方法都非常简单.除非命令执行顺序有问题,否则我宁愿选择选项1.

    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.

    这篇关于将自定义命令添加到qmake中的现有目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆