1

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?

asked Oct 16, 2019 at 19:50
1
  • For one, you add a space to the first parameter... Commented Oct 16, 2019 at 20:25

2 Answers 2

1
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...)

answered Oct 16, 2019 at 20:38
Sign up to request clarification or add additional context in comments.

5 Comments

Space was a typo, did not work without space either. Apparently, ifeq/ifneq do not work inside define, as you said.
The only issue with filter function is that make FLAG1="yes no" passes the test. Is there a simple fix?
You would have to do some trickery with $(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.
I came up with this: $(filter $(addsuffix s,$(addprefix p,$(2))),p$($(1))s)
Why do you not need to double the $ in front of filter and before the $(1) like so: ifneq ($$(filter $$($(1)),$(2)),$$($(1)))? Doesn't call remove one layer for everything but the $(1) $(2) etc it inserts?
1

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.

answered Oct 16, 2019 at 20:10

2 Comments

$(eval $(call ...)) did not help. Looks like inside check_ it translates $$(1) into 1ドル instead of $(FLAG1)
If you use eval you need an extra level of escaping. I'll update the answer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.