抑制并忽略Makefile的输出? [英] Suppress and ignore output for Makefile?

查看:140
本文介绍了抑制并忽略Makefile的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道@前缀会禁止Makefiles中shell命令的输出,并且-前缀也会忽略shell命令中的错误.有没有办法将两者结合起来,即可以抑制输出并忽略错误的前缀?我认为@--@无效.

解决方案

实际上,@--@都可以,但是会显示make: [target] Error 1 (ignored)警告.

相反,您可以使用

@command || true

或者,因为:是shell中true的简写,

@command ||:

这通常是更好的选择,因为它避免了Make的混淆警告,即在不可见的命令中忽略了错误.

考虑以下两种最常见的情况,您可能想忽略命令的返回值:

  1. 部分构建已损坏,无论如何您都想继续,在这种情况下您已经学到了一些东西要做.该版本已损坏,需要修复,而不要以无法维护的方式束缚.
  2. 即使命令确实执行了您想要的操作,命令仍会返回非零的退出代码,在这种情况下,您实际上并不想让Make发出警告.

对于第二种情况,请考虑对命令生成的日志文件中的警告进行grepping的示例. grep如果找不到匹配项,则将返回错误,这不是您想要的:

.PHONY: all one two three

all: at-warning at-success or-success or-warning

at-%: %.log
    @echo Making $@
    @-grep ^Warning $<

or-%: %.log
    @echo Making $@
    @grep ^Warning $< ||:

success.log:
    echo 'Success!' > $@

warning.log:
    echo 'Warning: foo' > $@

clean::
    rm -f {success,warning.log}

产生:

echo 'Warning: foo' > warning.log
Making at-warning
Warning: foo
Making at-success
make: [at-success] Error 1 (ignored)
Making or-success
Making or-warning
Warning: foo

成功使用@-会产生无意义的忽略错误警告,而|| true会同时处理警告和没有警告的情况.

理论上,使用|| true的速度比使用@-的速度慢,但是在设计良好且维护良好的构建系统中,这种开销不太可能成为瓶颈.绝大部分时间都应该花在构建或检查没有构建内容的时间戳上,而不是运行成千上万个返回值都被忽略的快速命令,这对于产生可衡量的性能影响是必需的. >

I know that the @ prefix suppresses output from a shell command in Makefiles, and also that the - prefix will ignore errors from a shell command. Is there a way to combine the two, i.e. a prefix that suppresses output and ignores errors? I don't think @- or -@ works.

解决方案

Actually, @- and -@ both do work, but will print a make: [target] Error 1 (ignored) warning.

Instead, you can use

@command || true

or, since : is shorthand for true in shell,

@command ||:

This often a better thing to do, because it avoid Make’s confusing warning that an error was ignored in an invisible command.

Consider the two most common cases where you might want to ignore the return value of a command:

  1. Part of the build is broken and you want to continue anyway, in which case you’ve got some learning to do. The build is broken and needs to be fixed, not bandaided in an unmaintainable way.
  2. A command returns a non-zero exit code even though it did exactly what you wanted, in which case you don’t really want Make to issue a warning.

For the second case, consider the example of grepping for warnings in the log file produced by a command. grep will return an error if it does not find a match, which is not what you want:

.PHONY: all one two three

all: at-warning at-success or-success or-warning

at-%: %.log
    @echo Making $@
    @-grep ^Warning $<

or-%: %.log
    @echo Making $@
    @grep ^Warning $< ||:

success.log:
    echo 'Success!' > $@

warning.log:
    echo 'Warning: foo' > $@

clean::
    rm -f {success,warning.log}

produces:

echo 'Warning: foo' > warning.log
Making at-warning
Warning: foo
Making at-success
make: [at-success] Error 1 (ignored)
Making or-success
Making or-warning
Warning: foo

Using @- produces a nonsensical ignored error warning when there is success, while || true handles both warnings and the absence of warnings without complaint.

Theoretically using || true is slower than using @-, but this overhead is unlikely to be a bottleneck in well-designed and -maintained build systems. The vast majority of the time should be spent building, or checking timestamps when there is nothing to build, not in running the thousands of quick commands whose return values all get ignored that would be necessary for this to have a measurable performance impact.

这篇关于抑制并忽略Makefile的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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