I would like to execute a task in several directories but found no “makefile-like” solution up to now. I know this is an often asked question and I know how to solve it for sub-makfiles and so on, but I am looking for something simpler.
Instead of doing
copy:
cd module1 && mkdir foo
cd module2 && mkdir foo
cd module3 && mkdir foo
I would like to have something like
directories = module1 module2 module3
copy: $(directories)
cd $< && mkdir foo
but that does not work, since the receipe is called only once with the first directory. I came up with this solution which works but is probably not in the style of Makefiles:
directories = module1 module2 module3
copy:
for d in $(directories); do cd $$d && mkdir foo && cd ..; done
How can I do this more nicely?
There are lots of ways of doing this.
You can do what Andrew suggests without hard-coding a prefix:
which gives
You can also generate the
copytarget from the makefile:This gives:
You could also generate the commands, not the target:
which results in