I am trying to check that variable value is yes or no, but the following always fails:
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)
What am I doing wrong?
-
For one, you add a space to the first parameter...HardcoreHenry– HardcoreHenry2019年10月16日 20:25:06 +00:00Commented Oct 16, 2019 at 20:25
2 Answers 2
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)
Notice that you added a space before FLAG1, which means $$(1) resolves to $( FLAG1), which in turn resolves to blank. The next part is that I'm not sure about is the use if ifneq inside of a define. You can use $(if ) instead
---- EDIT ------
Actually, it's a combination of the missing space and @MadScientists answer... The following also works:
define check_
ifneq ($(filter $($(1)),$(2)),$($(1)))
$$(error Bad $(1) argument [$($(1))] / [$(filter $($(1)),$(2))])
endif
endef
$(eval $(call check_,FLAG1,yes no))
Thus ifneq can be used inside of a macro... (and as @MadScientist pointed out, you have to escape the $ in front of $(error) to prevent it from being expanded by call...)
5 Comments
ifeq/ifneq do not work inside define, as you said.filter function is that make FLAG1="yes no" passes the test. Is there a simple fix?$(words) like so: $(if $(filter-out $(2),$($(1)))$(filter-out 1,$(words $($(1)))),$(error blah)). This would fail for any FLAG1 with more than one word.$(filter $(addsuffix s,$(addprefix p,$(2))),p$($(1))s)ifneq ($$(filter $$($(1)),$(2)),$$($(1)))? Doesn't call remove one layer for everything but the $(1) $(2) etc it inserts?You can't use plain call with a multi-line macro. You have to use $(eval $(call ...)) if the result of the call function consists of more than one line of makefile content.
You can use this:
define check_
ifneq ($$(filter $(2),$$($(1))),$$($(1)))
$$(error Bad $(1) argument)
endif
endef
Basically, anything you want to be interpreted by the eval needs to be escaped so that call doesn't see it.
2 Comments
check_ it translates $$(1) into 1ドル instead of $(FLAG1)