kaligne
kaligne

Reputation: 3278

gmake - loop through and process runtime variables

I have a makefile with the following content:

M_ONE = 1
M_TWO = 2
M_THREE = 3

NUMBERS = $(foreach v, $(shell echo "$(.VARIABLES)" | sed 's/ /'$$"\n"'/g' | grep "^M_" | xargs), $(info $(v)=$($(v))))
get-numbers:
        numbers=$(NUMBERS) ;\
        echo -e "numbers: \n$$numbers" ;\
        echo -e "NUMBERS: \n$(NUMBERS)"

I have variables that start with the same pattern, here "M_". I want to retrieve all the values of those variables a run-time, and to execute some shell tasks with each one of them.

When I execute the command here is what I get:

$ gmake --silent get-numbers
M_TWO=2
M_ONE=1
M_THREE=3
M_TWO=2
M_ONE=1
M_THREE=3
numbers: 

NUMBERS: 

It's as if the variable 'numbers' were empty. I don't understand why since I escaped it's declaration. And actually, it's as if even the gmake variable 'NUMBERS' were empty too.

What i want to do is to loop through the "key=values" and to do some file processing (sedding). How can I solve this, or what would be another approach to consider ot do so?

Upvotes: 0

Views: 61

Answers (1)

Mischa
Mischa

Reputation: 2298

That seems like a lot of work for what you want to do. Your NUMBERS =... expression is not assigning the '$(info...)' command output to NUMBERS; it's immediately writing to stdout; then the echo commands are being executed (hence the out of sequence output).

A simple way to get what you want, using what GMAKE gives you, might be:

M_ONE = 1
M_TWO = 2
M_THREE = 3
get-numbers : $(filter M_%, ${.VARIABLES:=.print})
%.print     :;@ echo $*=${$*}

Yes, this does not also provide the numbers: NUMBERS: labels. Is it fair to assume you want the info more than the formatting?

HTH

Upvotes: 1

Related Questions