Makefile检查列表中是否存在变量

时间:2012-10-17 06:14:24

标签: makefile

我有一个Makefile:

#Build Configurations
CONFIGS = Debug Release Profile

#Config flags
Debug_Flags=dasd
Release_Flags=

.SECONDEXPANSION:

#Debug_safety_check Release_safety_check Profile_safety_check targets
$(addsuffix _safety_check,$(CONFIGS)):
#Check existence of variable
ifeq '$(origin $(subst safety_check,FLAGS,$@))' 'undefined'
    $(error $(subst safety_check,FLAGS,$@) variable undefined)
endif 

#How to make our configurations (do corresponding safety_checks)
$(CONFIGS): $$@_safety_check

此行不正确:

ifeq '$(origin $(subst safety_check,FLAGS,$@))' 'undefined'

我认为,这是因为在调用相应的安全检查时会发生 $ @ 扩展。但ifeq扩展“立即”发生,所以,事实上,我们得到这样的界限:

ifeq '$(origin ' ') 'undefined'

是否存在从列表中定义检查变量的方法?

1 个答案:

答案 0 :(得分:0)

Ufff,陷阱。

#Build Configurations
CONFIGS = Debug Release Profile

#Config flags
Debug_Flags=dasd
Release_Flags=
#Profile_Flags=

#Adds _flags suffix for each variable, 
#get it's origin ('undefined' for undefined variables).
#If $(findstring) a lookup for the word 'undefined' succeeds, 
#adds to the result variable
undef_flags = $(foreach c, $(CONFIGS), \
                $(if $(findstring undefined, $(origin $c_Flags)), $c))

#Count words for undefined configs, must be not '0'
ifneq ($(words $(undef_flags)),0)
#strip because if flag is undefined we'll get the trash spaces
$(error Flags ($(strip $(undef_flags))) must be defined)
endif