Multiple using of % pattern within the same target rule in Makefile

Solution 1:

GNU make's secondary expansion can sometimes be used instead of foreach-eval-call:

.SECONDEXPANSION:

prefix/%.txt: prefix/$$(notdir $$*)/$$(notdir $$*)_input.txt
    @echo '$<'

Demo:

$ make prefix/FOO/FOO_suffix/FOO.txt prefix/BAR/BAR_suffix/BAR.txt
prefix/FOO/FOO_input.txt
prefix/BAR/BAR_input.txt

But if the reason you do not want to use foreach-eval-call is that you find the double expansion difficult to understand and maintain, secondary expansion is maybe not that simpler. Compare:

MY_MACRO = prefix/$1/$1_suffix/$1.txt: prefix/$1/$1_input.txt

$(foreach t,FOO BAR,$(eval $(call MY_MACRO,$t)))

%.txt:
    @echo '$<'

See? Not even a single define or $$...