声明所有目标PHONY [英] Declare all targets PHONY

查看:71
本文介绍了声明所有目标PHONY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑具有许多目标的长 makefile ,所有目标都是

Consider a long makefile with numerous targets, all of which are PHONY (meaning that the target name does not represent an existing file).

我可以这样做:

.PHONY: a
a:
    do someting a

.PHONY: b
b:
    do someting b

.PHONY: c
c:
    do someting c

或者:

.PHONY: a b c
a:
    do someting a

b:
    do someting b

c:
    do someting c

第一个选项很麻烦,第二个选项容易出错,因为将来我添加一个目标而忘记将其声明为 PHONY 时.

The first option is cumbersome, and the second option is prone to error, when future me adds a target and forget to declare it as PHONY.

是否有一种标准方法将 makefile 中的所有目标声明为 PHONY ?

Is there a standard way to declare all targets in a makefile as PHONY?

推荐答案

如果真正的所有目标都是 PHONY ,那么这毫无意义. make 的意思是"做什么以更新我的输出",并且如果对什么是必要的答案?的答案始终相同,简单的脚本将以更少的开销完成相同的工作.

If really all targets are PHONY, this is a bit pointless. make is meant for "do what is necessary to update my output", and if the answer to what is necessary? is always the same, simple scripts would do the same job with less overhead.

话虽这么说,我可以想象这样一种情况:打算在命令行中给出的所有目标 应该是 PHONY -假设您要构建一些文档,例如这个:

That being said, I could imagine a situation where all targets intended to be given at the commandline should be PHONY -- Let's assume you want to build some documents like this:

all: doc1.pdf doc2.pdf doc3.pdf doc4.pdf doc5.pdf

manuals: doc3.pdf doc4.pdf

faqs: doc1.pdf

designdocs: doc2.pdf

apidocs: doc5.pdf

developerdocs: doc2.pdf doc5.pdf

userdocs: doc1.pdf doc3.pdf doc4.pdf

%.pdf: %.md
     $(somedocgenerator) -o $@ $<

然后您可以执行以下操作:

.PHONY: all $(MAKECMDGOALS)

这将动态生成在命令行 PHONY 中给定的任何目标.请注意,您必须在此处包括默认目标,因为只需输入 make 即可调用它,而无需明确给出它.

This would dynamically make any target given at the command line PHONY. Note you have to include your default target here as this could be invoked without giving it explicitly, by simply typing make.

我不推荐这种方法,因为它有两个缺点:

I would not recommend this approach because it has two drawbacks:

  • 对于不同口味的 make ,它的可移植性较差.
  • 如果某人决定 make 一个特定的输出文件(例如此处 make doc3.pdf ),这将是错误的行为.
  • 如果您决定让您的 PHONY 目标之一依赖于另一个 PHONY 目标,这也将是错误的行为.
  • It's less portable to different flavors of make.
  • It will misbehave if someone decides to make a specific output file like here make doc3.pdf.
  • It will also misbehave if you decide to have one of your PHONY targets depend on another PHONY one.

因此,最好采用一种方法声明所有 PHONY 目标.如果您的 Makefile 确实很大,则可以将其拆分为多个文件,并使用 include -在每个文件中都有一个 .PHONY:

So, better go with the approach to have one line declaring all the PHONY targets. If your Makefile is really huge, you could split it in several files and use include -- and have one .PHONY: line in each file.

这篇关于声明所有目标PHONY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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