user2052436
user2052436

Reputation: 4765

Makefile: conditionals inside define

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?

Upvotes: 1

Views: 335

Answers (2)

HardcoreHenry
HardcoreHenry

Reputation: 6387

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

Upvotes: 1

MadScientist
MadScientist

Reputation: 101051

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.

Upvotes: 1

Related Questions