2016-07-01 82 views

回答

3

自卸stdout,测试退出状态,以及错误出在失败:

ifneq (0,$(shell >/dev/null command doesnotexist ; echo $$?)) 
    $(error "not good") 
endif 

这里是失败的样子:

[[email protected]]$ make 
/bin/sh: doesnotexist: command not found 
Makefile:6: *** not good. Stop. 
[[email protected]]$ 

如果你想看到stdout,那么你可以将它保存到一个变量和只测试lastword

FOOBAR_OUTPUT := $(shell echo "I hope this works" ; command foobar ; echo $$?) 
$(info $$(FOOBAR_OUTPUT) == $(FOOBAR_OUTPUT)) 
$(info $$(lastword $$(FOOBAR_OUTPUT)) == $(lastword $(FOOBAR_OUTPUT))) 
ifneq (0,$(lastword $(FOOBAR_OUTPUT))) 
    $(error not good) 
endif 

wh ich给出

$ make 
/bin/sh: foobar: command not found 
$(FOOBAR_OUTPUT) == I hope this works 127 
$(lastword $(FOOBAR_OUTPUT)) == 127 
Makefile:12: *** not good. Stop. 
+1

谢谢,这工作得很好,但我怎么让它也显示标准输出控制台以及stderr? –

+0

@CameronMartin,优点。我用一个技巧修改了我的答案,以捕获'stdout'并通过'lastword'测试。 – rubicks