2009-12-09 88 views
11

如果在检查工具版本时找不到某个字符串,我正在寻找一种拯救makefile的方法。如何根据grep结果条件化makefile?

grep的表达我在寻找匹配的是:

dplus -VV | grep 'build date and time: Nov 1 2009 19:31:28' 

如果安装DPLUS的正确版本,返回匹配的行。

如何根据此表达式将条件工作到我的makefile中?

+0

这是哪一种? GNU? – Davide 2009-12-09 21:09:20

+0

是的。特别是在Cygwin中,但那只是GNU。 – 2009-12-10 14:13:22

回答

12

这里的另一种方式在GNU Make中有效:

 
DPLUSVERSION = $(shell dplus -VV | grep 'build date and time: Nov 1 2009 19:31:28') 

target_of_interest: do_things do_things_that_uses_dplus 

do_things: 
    ... 


do_things_that_uses_dplus: 
ifeq ($(DPLUSVERSION),) 
    $(error proper version of dplus not installed) 
endif 
    ... 

这个目标可以是真实的,也可以是PHONY的真实目标。

+0

工作了一个魅力,和ifeq ... $(错误...)让我发出一个错误消息,让开发人员知道他们的构建被杀害。 – 2009-12-10 21:49:02

+0

警告! '$(错误)'在**评估**时触发。这意味着,如果'DPLUSVERSION'触发错误条件,'do_things'将**从不**运行。编辑来解决这个问题。 https://www.gnu.org/software/make/manual/html_node/Make-Control-Functions.html – gcb 2015-12-01 01:26:48

+0

@gcb:我不认为你测试了你的解决方案。 – Beta 2015-12-01 02:38:22

3

这里有一种方法:

.PHONY: check_dplus 

check_dplus: 
    dplus -VV | grep -q "build date and time: Nov 1 2009 19:31:28" 

如果grep的没有找到匹配,它应该给

make: *** [check_dplus] Error 1 

然后让你的其他目标取决于check_dplus目标。

2

如果这是gnu make,你可以做

your-target: $(objects) 
    ifeq (your-condition) 
     do-something 
    else 
     do-something-else 
    endif 

在这里看到Makefile contionals

如果你化妆不支持条件句,你总是可以做

your-target: 
    dplus -VV | grep -q "build date and time: Nov 1 2009 19:31:28" || $(MAKE) -s another-target; exit 0 
    do-something 

another-target: 
    do-something-else