tags:

views:

21

answers:

1

I just discovered GNU make's $(foreach) function, and I'm following the foreach-eval-call pattern used in the documentation; for instance,

graphviz_progs := dot neato circo fdp

define LAYOUT_template
%-$(1).dot: %.dot
  $(1) -Tdot $$? > $$@
endef

$(foreach p, $(graphviz_progs), \
  $(eval $(call LAYOUT_template,$(p))) \
)

This works pretty well: $(foreach) function treats $(graphviz_progs) as a space-separated list of items and iterates over each of them.

Now my problem is that I frequently want to iterate over a list of items one of which is the empty string.

Is this possible in GNU make? (I can think of a workaround, but having the empty item in my list would be cleaner.)

+1  A: 

I think the only way to get the behavior you want is by adding a level of indirection. Either crudely:

graphviz_progs := dot neato circo fdp

gplist := gp1 gp2 gp3 gp4 gp5

gp1 := dot
gp2 := neato
gp3 := circo
gp4 := fdp
gp5 :=

$(foreach p, $(gplist), \
  $(eval $(call LAYOUT_template,$($(p)))))

or a little more neatly:

graphviz_progs := dot neato circo fdp

gplist := gp1 gp2 gp3 gp4 gp5

NUMBERS = 1 2 3 4 5

$(foreach n,$(NUMBERS), \
  $(eval $(word $(n),$(gplist)) = $(word $(n),$(graphviz_progs))))

$(foreach p, $(gplist), \
  $(eval $(call LAYOUT_template,$($(p)))))

There are a few more tricks, e.g. to do without NUMBERS or make it automatically, but they get kind of ugly.

Beta
This is a nice idea, thank you. At the moment I'm just prefixing each value with X and stripping it off with $(subst X,$(var)) where I use them.
reinierpost