2

Possible Duplicate:
arithmetic in a Makefile

I'm very much a beginner using Makefile. I would like to update a variable, like this more or less:

LEVEL=0

train:
     while (eval_score > previous_eval_score)
     make iterate

iterate:
     do stuff
     write files to /path/to/$(LEVEL)/asdf.txt
     $(LEVEL)++

In other words, every iteration writes some files to a directory, first to /path/to/0, then /path/to/1, etc. until some threshold is reached.

So apparently it's not allowed to update or reassign a variable inside a function. I have searched for a while now but can't find any satisfactory answers. Maybe I have but I don't understand everything. I have also tried stuff like calculating in bash but eventually I still have to update the variable. Perhaps I should try something like update a file instead and for every iteration, just read from the file?

Thanks for your time.

Community
  • 1
  • 1
Gideon
  • 21
  • 2
  • Could you please explain why pure shell would not suffice here? `while (eval_score > previous_eval_score); do stuff; write files; done` seems easier and more expressive, so I assume you have a reason for going make. – thiton Dec 22 '11 at 15:17
  • Hi thiton, thanks for your reply. The makefile also contains a few other functions which in turn call some scripts, and some of them are hierarchical, which suits make very well in my opinion. I would also prefer make since I find it more elegant and easier to manage. But you are probably right that I can also use a shell script. The main issue which I'd like to solve is the one involving adding 1 to the $(LEVEL) variable whenever an iteration is complete. I am just curious to see if it can be done without having to make significant changes. Regards, Gideon – Gideon Dec 22 '11 at 17:33

1 Answers1

0

The main problem with your Makefile is that each make iterate spawns a sub-make, which could update its environment variables, but with no effect on the parent make and thus no effect on the next iteration. In addition, you cannot update variables from within rules, but that is a secondary problem.

You can do iterative make just like you suggested and increment the level via the shell:

train:
    LEVEL=0; while need_more_iterations; do \
       $(MAKE) LEVEL=$$LEVEL iterate; \
       LEVEL=`echo $$LEVEL + 1 | bc`; \
    done
thiton
  • 35,651
  • 4
  • 70
  • 100