makefilegnu-makeprerequisitesautomatic-variable

GNUMake - Using text functions with match pattern as prerequisite


I want to use the match pattern of a Makefile rule in a text function in the perquisites list.

Let's say I want to build a file, and that files perquisites are any other files with the same name in a list.

list_of_files=a/x b/x a/y b/y c/z 

%:
    echo 0 $@
    touch $@

build/%: $(filter %/$*,$(list_of_files))
    echo 1 target $@
    echo 1 prereq $?
    touch $@

.PHONY:
all: build/x
    echo 

In the above case, I want for a/x and b/x rules to also be triggered as they are filtered out of the list, however they are not.


Solution

  • The docs are pretty clear that what you're trying won't work as-is:

    It’s very important that you recognize the limited scope in which automatic variable values are available: they only have values within the recipe. In particular, you cannot use them anywhere within the target list of a rule; they have no value there and will expand to the empty string. Also, they cannot be accessed directly within the prerequisite list of a rule.

    One way to deal with this (as mentioned in the above documentation) is to use the secondary expansion capability:

    .SECONDEXPANSION:
    
    getprereqs = $(filter %/$*,$(list_of_files))
    
    build/%: $$(getprereqs)