Makefile:定义中的条件
Makefile: conditionals inside define
我正在尝试检查变量值是 yes
还是 no
,但以下总是失败:
FLAG1 ?= no
FLAG2 ?= yes
define check_
ifneq ($(filter $(2),$($(1))),$($(1)))
$(error Bad $(1) argument)
endif
endef
$(call check_,FLAG1,yes no)
$(call check_,FLAG2,yes no)
我做错了什么?
您不能将纯 call
与多行宏一起使用。如果 call
函数的结果包含不止一行 makefile 内容,则必须使用 $(eval $(call ...))
。
你可以使用这个:
define check_
ifneq ($$(filter $(2),$$($(1))),$$($(1)))
$$(error Bad $(1) argument)
endif
endef
基本上,任何您想要由 eval
解释的内容都需要转义,这样 call
就看不到它。
FLAG1 ?= no
FLAG2 ?= yes
define check_
$(if $(filter $($(1)),$(2)),,$(error BAD $(1) argument))
endef
$(call check_,FLAG1,yes no)
$(call check_,FLAG2,yes no)
请注意,您在 FLAG1
之前添加了一个 space,这意味着 $$(1)
解析为 $( FLAG1)
,后者又解析为空白。下一部分是我不确定是否在定义中使用 if ifneq
。您可以使用 $(if )
代替
---- 编辑 ------
实际上,它是缺少的 space 和@MadScientists 答案的组合...以下也有效:
define check_
ifneq ($(filter $($(1)),$(2)),$($(1)))
$$(error Bad $(1) argument [$($(1))] / [$(filter $($(1)),$(2))])
endif
endef
$(eval $(call check_,FLAG1,yes no))
因此 ifneq
可以在宏内部使用...(正如@MadScientist 指出的那样,您必须转义 $(error)
前面的 $
以防止它通过调用扩展......)
我正在尝试检查变量值是 yes
还是 no
,但以下总是失败:
FLAG1 ?= no
FLAG2 ?= yes
define check_
ifneq ($(filter $(2),$($(1))),$($(1)))
$(error Bad $(1) argument)
endif
endef
$(call check_,FLAG1,yes no)
$(call check_,FLAG2,yes no)
我做错了什么?
您不能将纯 call
与多行宏一起使用。如果 call
函数的结果包含不止一行 makefile 内容,则必须使用 $(eval $(call ...))
。
你可以使用这个:
define check_
ifneq ($$(filter $(2),$$($(1))),$$($(1)))
$$(error Bad $(1) argument)
endif
endef
基本上,任何您想要由 eval
解释的内容都需要转义,这样 call
就看不到它。
FLAG1 ?= no
FLAG2 ?= yes
define check_
$(if $(filter $($(1)),$(2)),,$(error BAD $(1) argument))
endef
$(call check_,FLAG1,yes no)
$(call check_,FLAG2,yes no)
请注意,您在 FLAG1
之前添加了一个 space,这意味着 $$(1)
解析为 $( FLAG1)
,后者又解析为空白。下一部分是我不确定是否在定义中使用 if ifneq
。您可以使用 $(if )
代替
---- 编辑 ------
实际上,它是缺少的 space 和@MadScientists 答案的组合...以下也有效:
define check_
ifneq ($(filter $($(1)),$(2)),$($(1)))
$$(error Bad $(1) argument [$($(1))] / [$(filter $($(1)),$(2))])
endif
endef
$(eval $(call check_,FLAG1,yes no))
因此 ifneq
可以在宏内部使用...(正如@MadScientist 指出的那样,您必须转义 $(error)
前面的 $
以防止它通过调用扩展......)