Search code examples
bashmakefilegnu-make

Makefile conditional execution of command based on variable value


Currently I'm working on a makefile where I want to execute a function based on the fact if the value is present in a predefined list.

test: VALUES=A B C
test:
   $(eval FOUND=$(filter $(VALUE),$(VALUES)))
ifneq ($(FOUND),"")
   $(call myFunction,arg1,arg2)
endif
# do other things...

Calling the make command as follows:

make VALUE=A test

But for some reason, the call function is never called. My guess is that the FOUND value is only known by the shell, but I have no idea how to get the desired result.

I've tried not using the eval function, and using bash conditional function, but for the later, I cannot call the 'call myFunction' because this is only known by make itself.


Solution

  • You seem to be mixing make and shell syntax and confusing yourself mightily along the way.

    I'm guessing you are looking for something like

    VALUES=A B C
    ifneq ("$(filter $(VALUE),$(VALUES))","")
       call=$$(call myFunction,arg1,arg2)
    else
       call=# nothing
    endif
    test:
        $(eval $(call))
    

    Embedding make functions in a recipe like this is dubious. Probably restrict yourself to shell syntax in recipes unless you know precisely what you are doing.