makefile

Makefile (counting)


I'm completely stumped on how to do this in a Makefile

Let's say I have a target. Inside the target I have a loop. How do i change a variable to keep track of the iterations?

For example:

COUNTER = 0
target:
    (loop){
        COUNTER++
        echo COUNTER
}

I know that variables in Makefiles are only expanded, and I'm not sure if they can be permanently changed, but there has to be a way to do this, right? :(

Here are some sources that are asking similar questions. It seems like those examples only change the variable temporarily:

Maybe I have to use the eval function somehow?

Maybe I have to append onto a Makefile string a character each time and then use something in the shell to count the characters?


Solution

  • If the variable doesn't have to survive the rule, this should do (I'm assuming bash):

    clean:
        @n=0 ; \
       for x in $(THINGS_TO_BE_DELETED); do \
         if [ -f $$x ] ; then \
           rm $$x; \
           let "n+=1" ; \
         fi ; \
       done ; \
       echo deleted $$n files;