Roberto Aloi
Roberto Aloi

Reputation: 30985

Referring to the target name from the list of the prerequisites

In a Makefile, I would like to refer to the target name from the list of prerequisites and to build something with it. Something of the form:

%.foo: $(addsuffix .bar, $(DATA_%))
  @echo $<

So that, supposing you have:

DATA_test = 1 2 3

When you call it as:

make test

That will expand to:

1.bar 2.bar 3.bar

Is this somehow possible? What would be a better approach to the problem?

Upvotes: 0

Views: 1327

Answers (1)

Beta
Beta

Reputation: 99084

If your version of Make has secondary expansion, this will probably work (I can't test it because today all I have handy is an old version).

.SECONDEXPANSION:

%.foo: $$(addsuffix .bar, $$(DATA_$$*))
    @echo $^

Without that, I don't see any better way to do it than this:

define FOO_RULE
$(1).foo: $(addsuffix .bar,$(DATA_$(1)))
endef

FOO_TYPES = test real whatever

$(foreach t,$(FOO_TYPES),$(eval $(call FOO_RULE,$(t))))

%.foo:
    @echo building $@ from $^

Upvotes: 1

Related Questions