DEV Community

Discussion on: How to become more productive using Makefile

Collapse
 
tartley profile image
Jonathan Hartley • Edited

I love Makefiles and use them in almost all my projects, but I think the main benefits of them is walking DAGs of dependencies, which the article doesn't really mention.

For example, if your toolchain generates files:

source-file
   |
   tool-a
   |
intermediate-file
   |
   tool-b
   |
final-file

Then you can define some Makefile targets:

# final-file depends upon the state of intermediate-file
final-file: intermediate-file
     tool-b intermediate-file >final-file

# intermediate file depends on source-file
intermediate-file: source-file
    tool-a source-file >intermediate-file

Then the command 'make final-file' will run tool-b to generate the intermediate file, and knows that it first needs to run tool-a to generate the intermediate-file, and (the clever bit) it only runs each step if the output of the step is older than any of its inputs.

This means you eliminate needless typing (from running a tree of pre-requisite steps), and you eliminate needless (and possibly slow) rebuilding of files which don't need it. This is the core value of make, and if you can't take advantage of this, then there isn't much value over a set of script files.

I'd classify my current employer (Canonical) as high on the technically savvy scale, and my team uses a Makefile or two on every project. Even in cases where we can't make use of the above dependency analysis, it's nice for us to standardize on one approach to controlling how to build/test/etc projects.

Collapse
 
xarala221 profile image
Ousseynou Diop

Very interesting, thanks.

Collapse
 
tartley profile image
Jonathan Hartley

I added some missing "dependencies" (the parts after the colon) in my Makefile example.