Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating over an empty item with GNU make's $(foreach)

Tags:

makefile

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.)

like image 663
reinierpost Avatar asked Jul 06 '10 14:07

reinierpost


1 Answers

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.

like image 193
Beta Avatar answered Oct 07 '22 20:10

Beta