如何在 gnumake 文件中强制执行错误

我希望在 makefile 中检测工具版本错误的情况,并强制 make 失败,同时出现一条错误消息,指示该项目不是正确的版本。

有人能举个例子吗?

我尝试了以下方法,但语法不对:

ifeq "$(shell svnversion --version | sed s/[^0-9\.]*://)" "1.4"
$error("Bad svnversion v1.4, please install v1.6")
endif

谢谢。

78398 次浏览

From the manual:

$(error Bad svn version v1.4, please install v1.6)

This will result make to a fatal error:

$ make
Makefile:2: *** Bad svn version v1.4, please install v1.6.  Stop.

The conditional needs some attention too.

ifeq ($(shell svnversion --version | sed s/[^0-9\.]*://), 1.4)
$(error Bad svnversion v1.4, please install v1.6)
endif

While $(error... works, sometimes its easier to use a rule that fails

test_svn_version:
@if [ $$(svn --version --quiet | \
perl -ne '@a=split(/\./); \
print $$a[0]*10000 + $$a[1]*100 + $$a[2]') \
-lt 10600 ]; \
then \
echo >&2 "Svn version $$(svn --version --quiet) too old; upgrade to v1.6";
false; \
fi

Then you make test_svn_version a prerequisite of your top level target.