GNU-make check if element exists in list/array GNU-make check if element exists in list/array arrays arrays

GNU-make check if element exists in list/array


You can use the filter function to do this:

PARAMS := a b cifneq ($(filter $(ENV_PARAM),$(PARAMS)),)    $(info $(ENV_PARAM) exists in $(PARAMS))else    $(info $(ENV_PARAM) does not exist in $(PARAMS))endif

Read: "if the result of searching for the ENV_PARAM value in PARAMS is not empty, run the 'true' block else run the 'false' block".

UPDATE

Your second question really cannot be answered fully with the information you've provided. In order to know the best way to do it we need to know what you are really going to do inside the if-statement, when the condition is true and when it is false. Are you going to declare more variables? Create some rules? Something else? There are many ways to do what you want and the cleanest one may be different depending on what you want to do.

However, a general solution would involve using define to create the content of the loop, then using foreach and eval, something like this:

KEYS    := PARAMS FACTORSPARAMS  := a b cFACTORS := x y zdefine LOOPBODY  ifneq ($$(filter $$(ENV_$(v)),$(v)),)    $$(info $$(ENV_$(v)) exists in $(v))  else    $$(info $$(ENV_$(v)) does not exist in $(v))  endifendef$(foreach v,$(KEYS),$(eval $(LOOPBODY)))

You might be interested in a set of posts I made regarding metaprogramming in GNU make: http://make.mad-scientist.net/category/metaprogramming/