pre-requisites in makefiles

Haritha Mohan,tool
target definition: prerequisites
recipe

target definition = ex: a file name or something that is representative of this target (if the file name is used, you can easily refer to it in the recipe, ideal for signifying the output)

prerequisites = normal | order only pre reqs, aka ingredients for the recipe of the current target definition

normal = if the pre req (ex: file) DNE than the makefile will ensure it is first created/taken care of/excutes its corresponding target recipe before moving on with the current target definition

order only = follows the pipe character, will just make sure it exists before moving on with the current target recipe (timestamp/time at which it was created does not matter in this case..ex: a directory being created..directory time of creation does not impact on current target handling)

recipe = series of steps that correspond with the target, aka invoking the usage of a tool; the recipe will only be executed if either the current target definition DNE or is older/out of date compared to the definition’s pre-reqs (if the pre-reqs have been updated since the recipe was last executed, the recipe will have to run again now with the updated pre-req versions)

example:

directoryPath/output.txt : prereq1.txt | directoryPath tool-to-transform-txt --input prereq1.txt --output $$@

side note: --output $$@ refers to “directoryPath/output.txt” being passed as the value for the output parameter for the specified tool

Why do we care? By taking advantage of this mechanism in makefiles, things like invoking a tool and managing dependencies becomes a lot more straightforward. For ex, let’s say you are trying to invoke a tool that modifies a file. By invoking it via a target definition in the makefile, the tool can just focus on doing its particular job and make will automatically handle a lot of the behind-the-scenes edge cases. What if one of the inputs changed since the last execution run of the tool? What if a directory needs to be created first in the output path of the resulting file? What if the output file of said tool/file defined in the target definition identifier is a pre-req for a different target definition in the build process?

Rather than having to manually keep track of these cases and the corresponding resulting behavior, make makes this a much simpler and maintainable feat.

© Haritha Mohan.RSS