Makefile is a powerful way to declare a chain of interdependent commands that create targets. In this article, we will explain how to create an auto-documenting Makefile that is able to print a help message when invoked without target.
The idea of a self-documenting Makefile comes from this almost perfect stackoverflow answer, if it wasn’t for some peculiarities of the unfortunately different version of macOS’s sed, compared to GNU.
ed as a compatible sed replacement
ed, one of the first three key elements of Unix according to Wikipedia, is the ancestor of sed.
Its legacy is an advantage for us because its interface hasn’t changed since ages, and thus remains a very interesting way of writing multi-platform scripts.
We will use ex, as its proximity with vi helps iterating quickly on the different options.
TL;DR, the Makefile
Here is an example Makefile that prints some help when invoked without target. We signify documentation with regular comments by doubling the hashtags trailing after some targets. For good measure, we also use the strict makefile headers. Continue reading for more details.
SHELL := bash
.ONESHELL:
.SHELLFLAGS := -eu -o pipefail -c
.DELETE_ON_ERROR:
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules
.PHONY:help
help: ## print these help instructions
@ex -s '+%g!/^[\\.A-Za-z_-]\+:[^#]\+##.*$$/d _' '+%s/^\([\\.A-Za-z_-]\+\):.*## \+\(.*\)$$/\"\1\" \"\2\"/' -c'%p|q!' <(cat ${MAKEFILE_LIST}) | xargs printf "make %-20s# %s\n"
a:
echo $@ > $@
b: # some internal help, not to be printed
echo $@ > $@
c.txt: a b ## this target is what you want to generate probably
cat $? > $@
echo "Generation complete"
Read more