在Makefile中循环ifndef / endif构造

时间:2017-02-09 05:33:00

标签: loops makefile environment-variables

我正在尝试编写一个Makefile规则来检查多个环境变量,以确保它们已被定义。这接近Makefile variable as prerequisite,但我试图循环遍历多个变量。换句话说,我正试着更简洁地写这个:

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

我尝试使用foreach但没有成功,因为在foreach之前评估ifndef

1 个答案:

答案 0 :(得分:3)

生成文件:

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'.