I am trying to use SECONDEXPANSION feature https://www.gnu.org/software/make/manual/make.html#Secondary-Expansion
to dynamically select dependencies. So far I have the following fragment of code. However, I want to avoid duplication when defining expression $(word 4, $(subst -, , $@))
.
.SECONDEXPANSION:
mydep-4v1:
echo 4v1
mydep-4v2:
echo 4v2
foo-%: mydep-$$(word 4, $$(subst -, , $$@))
X=$(word 4, $(subst -, , $@)) && \
echo foo: $$X
make foo-a-b-4v1-c-d # foo: 4v1
make foo-2-3-4v1-5-6 # foo: 4v1
make foo-a-b-4v2-c-d # foo: 4v2
make foo-2-3-4v2-5-6 # foo: 4v2
I have no opinions on the "rightness" of this, but it's trivial to do what you want. Just put the expression into a variable:
get-word = $(word 4, $(subst -, , $@))
foo-%: mydep-$$(get-word)
X='$(get-word)' && echo foo: $$X
Since you escape the call of $(get-word)
in the prerequisite list it won't be expanded until secondary expansion. Of course you could also use:
foo-%: mydep-$$(get-word)
X='$(patsubst mydep-%,%,$<)' && echo foo: $$X
Or do it all in the shell as John suggests.