I find I'm writing a lot of Makefiles that could be cleaned up with the use of n-tuple lists. But I can't find any way to do this properly (and cleanly). So far I've only been able to come up with using $(shell ...) and tr, sed, or otherwise non-Makefile standards.
For example, I'd like to do this:
XYZs = \
dog.c pull_tail bark \
duck.c chase quack \
cow.c tip moo
all:
@- $(foreach X Y Z,$(XYZs), \
$(CC) $X -o bully/$Y ; \
ln bully/$Y sounds/$Z ; \
)
Is there a good way to iterate n-tuple lists in Makefiles? Thanks!
You're doing it backwards.
You're trying to treat make like it's a script. It's not, instead its a set of rules on how to create X given Y. Then the make engine figures out what needs to happen to get that result.
For the example given, you really should be using a script for the generation steps. Perhaps calling that from make, but let make handle the CC stuff.
Thanks for the hints -- after some hacking I think this is more what I was hoping for:
Can anyone do better?
Makefiles are essentially declarative in nature, so I don't think that make itself provides what you want. However, you seem to be wanting to associate some string values with specific targets, so maybe the Target specific variable values feature of GNU make will be of interest. This is an extract from the manual:
If you haven't already read it, the GNU make manual is pretty damn good.
Edit: To do what you asked about in your comment:
use:
None that I know of, but that is because you're trying to force make to work ans an imperative language, when that is not what it is.
In GNU make you'd probably want to do something like:
Or better yet, redefine the default rule for .c files to handle the linking for you, but the strange structure of your names (the program names don't have a lexical relationship to the source names) makes that hard.
If what you want to be able to rebuild this quickly without an lot of hand editing, you probably want to write a script to regenerate the makefile framgment from data and use the
include
feature of GNU make...You can use default rules for a set of files with the same extension as in for compiling each
c
file to ano
. Of course you are not restricted to any special file extensions. For compiling a set of.c
files you could do it like this:I'd check the GNU Make manual on foreach. here are some random snips that I've used in a different project... the example is incomplete, but maybe it will give you some ideas? I might clean this up later if I've got more time...