Dynamic Prerequisite Makefile Recipe – First Silicon Designs

Dynamic Prerequisite Makefile Recipe

Dynamically generate a GNU Make prerequisite only if it would be different.

.PHONY: $(1)_$(2)_FILEGEN
$(1)_$(2)_FILEGEN : $(3).fresh $(3)
. . . $(3).fresh :
. . . $(3) : @mkdir -p $$(@D) @touch $$@

That last rule creates the intermediate target file (passed as parameter 3) either the first time the rules are executed or after a ‘make clean’ removes the file. Without this rule, Make will report ‘no rule to make target’ during prerequisite checking for the pseudo-target (the first rule shown above). The pseudo-target $(1)_$(2)_FILEGEN lists as prerequisites both the actual file $(3) and our temporary “test” file $(3).fresh. Recall that psuedo-targets (.PHONY) are not actual files, meaning this rule will always be executed if present in the makefile.

During the initial pass in the calling makefile, the psuedo-target is used as the prerequisite of the downstream target. During the second pass, the real prerequisite file (arg 3 to this function) is used, while omitting these rules that generate it. We’ll show how to use the function later in the article. As discussed elsewhere, we have to escape the dollar signs for the automatic variables $@ and $(@D) in the recipe.

Now let’s see how to define the remaining two recipes in the snippet above.