开发者

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

开发者 https://www.devze.com 2023-01-05 17:30 出处:网络
I ju开发者_开发知识库st discovered GNU make\'s $(foreach) function, and I\'m following the foreach-eval-call pattern used in the documentation; for instance,

I ju开发者_开发知识库st 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.)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消