如果未找到文件夹,则中止生成文件



我有一个简单的Makefile,它依赖于特定的文件夹结构。我想添加一个测试,以便 Makefile 失败,并显示一条消息,告知用户文件夹丢失。

我的目标如下所示:

check_folders:
test -d ../some_folder || $(error The ../some_folder folder does not exist)
.PHONY: check_folders

我期待这里有短路逻辑,所以如果第一个语句通过(并且文件夹存在(,则不会执行第二个语句。但它不起作用,即使文件夹存在也会引发错误:

$ mkdir ../some_folder
$ make check_folders
makefile:24: *** The ../some_folder folder does not exist.  Stop.

任何帮助不胜感激!

谢谢!

你使用的是 make 功能来引发错误,而不是 shell 功能。 在调用配方之前,配方中的所有 make 变量和函数都会首先展开。

您有两种选择。 第一种是完全切换到配方失败:如果配方因失败而退出,则 make 将停止。 因此,您可以像这样编写规则:

check_folders:
test -d ../some_folder || { echo The ../some_folder folder does not exist; exit 1; }

另一种是完全切换进行测试;这将在调用任何配方之前发生,因为 makefile 被解析:

$(if $(wildcard ../some_folder/.),,$(error The ../some_folder folder does not exist))

最新更新