Note that this technique requires a two pass, or recursive, Makefile. In the first pass, prerequisite files are created, and in the second pass they are used for the final, “real” Make target(s). An example use of the function is presented later.
Defining the Dynamic Prerequisite Function
This is a case in which we’ll need to generate Make rules dynamically. As we discuss in another article, we’ll have to create a Make function (multiple line variable). Our function will accept four parameters instead of one, to facilitate its use in a complex build system. Here is the skeleton:
# parameter 1 is a namespace, to allow multiple instances to be created
# parameter 2 is a category name, to allow multiple targets per namespace
# parameter 3 is the target file name with path if appropriate (safer)
# parameter 4 is the variable *name* which contains the file generator command
define gen_file_if_different =
.
.
.
endef
This function, when called using $(call ...)
, will create three target rules:
- A target for the experimental, temporary, “test” file, which will always be generated
- A target that creates an initial version of the intermediate target file
- A psuedo-target used by pass 1 where the update decision is made
The code for these targets is shown below.