Gumulka

ein blog

Dec 18, 2019

Help target for Makefile

Translations: de

I started using Makefiles again some time ago and I am learning a few things. You always write them first for yourself, but sometimes in a team as well.

Right now I'm working on a project where that's exactly what's needed and I came across a blog entry that I found extremely helpful, but that needs some small improvements.

They suggest to define a target help there

.PHONY: help

help:
    @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

The whole thing only has a few drawbacks for me. The variable MAKEFILE_LIST contains a list of all Makefiles and grep will output the filename if there are multiple files to be searched. So the -h parameter is missing here.

I also think it would be nice if help itself was already documented.

Also it should be the preferred target, no matter where it is, because I often omit the "sort".

Furthermore, I work with files which can have "." and numbers in the file name.

My help target now looks like this:

# Make the Makefile self-documenting with this targets.
# Targets having a comment with double # are printed out.
.PHONY: help
help: ## Print this help message
    @grep -h -E '^[0-9a-zA-Z._-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
# Make this the default target
.DEFAULT_GOAL := help

So if my team now calls make without parameters it gets a useful message with all parameters documented. And documenting you do everything. ;-)