Simple Makefiles

When it comes to build tools, the list of contenders seems endless. Yet few match the simplicity or elegance of their 40 year old forefather make.

Running make will search the directory for a file named Makefile and execute the first rule inside it. Other rules can run by providing them as parameters such as make css/main.css.

A make rule has a few basic components. The target which specifies the file to build. The dependencies which list files required to build the target. A list of commands to run which will build the target.

target: dependencies
    command1
    command2

Note: The examples use spaces for indentation, make requires use of tabs.

An example – building css/main.css from css/main.scss which in turn imports css/_partial.scss.

css/main.css: css/main.scss css/_partial.scss
    node_modules/.bin/node-sass css/main.scss css/main.scss

When you run make css/main.css it checks if any of the dependencies have changed since the target was last built. If so it will run the commands and generate the file.

To compile other files, you add more rules. If a dependency is the target of another rule, then make will try and execute that first.

css/all.css: css/main.css css/other.css
    cat css/main.css css/other.css > css/all.css

css/main.css: css/main.scss css/_partial.scss
    node_modules/.bin/node-sass css/main.scss css/main.scss

css/other.css: css/other.scss css/_partial.scss
    node_modules/.bin/node-sass css/other.scss css/other.scss