Makefile : --ignore-errors vs --keep-going

Makefile : --ignore-errors vs --keep-going

我正在经历Makefile documentation,我不清楚--ignore-errors--keep-going之间的区别。

有人可以强调一下两者之间的区别吗?对我来说,两者似乎都在发生错误后继续。

虽然文档中不清楚,但 --ignore-errors 可以具有配方级范围,如果一个配方行失败,下一个将继续执行。 --keep-going 可能具有目标级范围,如果任何目标配方失败,配方执行到此结束,但其他 prerequisites/targets 仍将尝试进行。这只是一种可能的看法,需要对此进行澄清。

https://www.gnu.org/software/make/manual/make.html#Errors

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.

换句话说,make --ignore-errors 的行为就像在所有命令前面都有一个 -

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. See Summary of Options.

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.

举个例子:

target: intermediate-1 intermediate-2 intermediate-3
        cat intermediate-1 intermediate-2 intermediate-3 > target

intermediate-1:
        echo "oh no, I'm failing!"
        false

intermediate-2:
        echo 'hello' > intermediate-2

intermediate-3:
        echo 'world' > intermediate-3

通常当您 运行 make target(并且 none 的文件还存在)时,它会首先尝试制作 intermediate-1。该目标失败,因为关联命令之一 (false) returns 存在非零退出状态。 make然后立即放弃,甚至不看intermediate-2intermediate-3

然而,使用 make --keep-going target,它会注意到 intermediate-1 的失败,但继续进行 intermediate-2intermediate-3,这会成功(创建包含的文件"hello" 和 "world"。

最后它还是放弃了,报告创建失败target,但它已经尝试创建所有中间目标,即使是它知道不会在此运行中使用的目标make 因为另一个先决条件已经失败。

如果您同时使用两个标志 (make --ignore-errors --keep-going),则 --keep-going 将被有效忽略。 --keep-going 仅影响 make 在中间目标中遇到错误时的行为,但 --ignore-errors 意味着 make 永远不会遇到错误。