循环遍历 Makefile 中的 ifndef/endif 构造



我正在尝试编写一个 Makefile 规则来检查多个环境变量以确保它们被定义。这接近 Makefile 变量作为先决条件,但我正在尝试循环多个变量。换句话说,我试图更简洁地写这个:

check-env:
ifndef ENV1
    $(error ENV1 is undefined)
endif
ifndef ENV2
    $(error ENV2 is undefined)
endif
ifndef ENV3
    $(error ENV3 is undefined)
endif

我尝试使用foreach但没有成功,因为看起来ifndef是在foreach之前进行评估的。

Makefile:

variables := a b c
fatal_if_undefined = $(if $(findstring undefined,$(origin $1)),$(error Error: variable [$1] is undefined))
$(foreach 1,$(variables),$(fatal_if_undefined))
all:

运行:

$ make -f Makefile.sample
Makefile.sample:4: *** Error: variable [a] is undefined.  Stop.
$ make -f Makefile.sample a=10 b=2
Makefile.sample:4: *** Error: variable [c] is undefined.  Stop.
$ make -f Makefile.sample a=10 b=2 c=5
make: Nothing to be done for 'all'.

最新更新