GNU make manual 翻译( 一百四十)

继续翻译

5.5 Errors in Recipes

=====================



After each shell invocation returns, `make' looks at its exit status.

If the shell completed successfully (the exit status is zero), the next

line in the recipe is executed in a new shell; after the last line is

finished, the rule is finished.



   If there is an error (the exit status is nonzero), `make' gives up on

the current rule, and perhaps on all rules.



   Sometimes the failure of a certain recipe line does not indicate a

problem.  For example, you may use the `mkdir' command to ensure that a

directory exists.  If the directory already exists, `mkdir' will report

an error, but you probably want `make' to continue regardless.



   To ignore errors in a recipe line, write a `-' at the beginning of

the line's text (after the initial tab).  The `-' is discarded before

the line is passed to the shell for execution.



   For example,



     clean:

             -rm -f *.o



This causes `make' to continue even if `rm' is unable to remove a file.



   When you run `make' with the `-i' or `--ignore-errors' flag, errors

are ignored in all recipes of all rules.  A rule in the makefile for

the special target `.IGNORE' has the same effect, if there are no

prerequisites.  These ways of ignoring errors are obsolete because `-'

is more flexible.



   When errors are to be ignored, because of either a `-' or the `-i'

flag, `make' treats an error return just like success, except that it

prints out a message that tells you the status code the shell exited

with, and says that the error has been ignored.



   When an error happens that `make' has not been told to ignore, it

implies that the current target cannot be correctly remade, and neither

can any other that depends on it either directly or indirectly.  No

further recipes will be executed for these targets, since their

preconditions have not been achieved.



   Normally `make' gives up immediately in this circumstance, returning

a nonzero status.  However, if the `-k' or `--keep-going' flag is

specified, `make' continues to consider the other prerequisites of the

pending targets, remaking them if necessary, before it gives up and

returns nonzero status.  For example, after an error in compiling one

object file, `make -k' will continue compiling other object files even

though it already knows that linking them will be impossible.  *Note

Summary of Options: Options Summary.



   The usual behavior assumes that your purpose is to get the specified

targets up to date; once `make' learns that this is impossible, it

might as well report the failure immediately.  The `-k' option says

that the real purpose is to test as many of the changes made in the

program as possible, perhaps to find several independent problems so

that you can correct them all before the next attempt to compile.  This

is why Emacs' `compile' command passes the `-k' flag by default.  



   Usually when a recipe line fails, if it has changed the target file

at all, the file is corrupted and cannot be used--or at least it is not

completely updated.  Yet the file's time stamp says that it is now up to

date, so the next time `make' runs, it will not try to update that

file.  The situation is just the same as when the shell is killed by a

signal; *note Interrupts::.  So generally the right thing to do is to

delete the target file if the recipe fails after beginning to change

the file.  `make' will do this if `.DELETE_ON_ERROR' appears as a

target.  This is almost always what you want `make' to do, but it is

not historical practice; so for compatibility, you must explicitly

request it.

5.5 片段中的错误
=====================

每一个shell 激活返回后,make 察看退出时的状态了。如果shell 成功完成(exit zhuangtai 为零),片段的下一行会在一个新的shell中执行,最后一行结束后,规则就结束了。

If there is an error (the exit status is nonzero), `make' gives up on
the current rule, and perhaps on all rules.

如果执行中出现了一个错误(退出状态非零),make 放弃现有的规则,也许会放弃所有规则。

有时候,特得的片段行失败并不意味着一个问题。例如,你可以使用 mkdir 命令去确认一个目录存在。如果目录已经存在,mkdir将会报告一个错误,但是你可能只是想要做个标记,然后无视之并继续运行。

为了忽略片段行中的错误,可以在行文本之前(初始tab符号之后),写一个 - ,当此行传递给shell执行之前,- 会被去除。例如,

clean:
  -rm -f *.o

这使得即便 rm 没能删除文件,make 将继续运行。

当你用 -i 或者 --ignore-errors 标记来运行 make 的时候,所有规则中的所有片段都忽略错误。makefile的特殊目的 .IGNORE的规则有同样的效果,如果它没有前提条件的话。这些方式比较过时,因为 - 更加灵活。

因为 一个 - 或者 -i标志的缘故,当错误被忽略,make 把错误的返回值当成是正确的,除了那些打印一个消息告诉你shell退出时状态来说明那个错误课余被忽略的。

当一个make没有被告知忽略的错误发生的时候,它暗示当前的目的无法被正确地重新完成,并且没有任何一个直接或间接依赖于它的目的会被正确的重新构建。对这些目的,其它的更多的片段也不会再执行,因为他们的前提条件没有得到满足。

通常,make 在此场合立即放弃,返回一个非零的状态。但是,如果 -k 或者 --keep-going 标志被指定,make 继续考虑依赖此目的的目的的其它前提条件,在放弃并返回非零状态之前,如果有必要就重新建立它们。例如,当变异一个目标文件时发生了错误,make -k 将会继续编译其它目标文件,尽管它已经知道链接它们已经不可能。*Note Summary of Options: Options Summary

通常的行为模式假定你的目的是获得特定的最新目的,一旦 make 了解到这已经不可能,它将立刻报告错误。-k选项说明 真正的目的是测试程序中产生了多少可能的变更,也许还是找出界各独立的问题,这样你可以在下一次试图编译前,修正所有的错误。这就是为何 Emacs compile 命令缺省时传递 -K 标志的原因。

通常当一个片段行失败时,它已经改变了目的文件,文件是损坏状态并且无法使用--或者至少它没有完全被更改。然而文件的时间戳表明它已经是最新的,因此当下次make 运行的时候,它不会试图去变更此文件。

当shell 被一个信号杀死的时候,状况也是一样的; *note Interrupts:: 因此通常要做的正确的事情是如果片段开始改变文件后失败了,应当删除此目的文件。如果 .DELETE_ON_ERROR 目的被设置,那么make 就会这么做。这差不多总是你想要make 去做的事情,但是这不是一个历史悠久的实践;为了兼容性,你必须显式地发出请求。

后文待续

你可能感兴趣的:(Make)