List Targets in Makefile

Updated: 2 minute read

Background/Problem

Even though I don’t like the syntax of Makefiles, I do like what you can do with them. Therefore I use make in some of my projects and I think, I will use them even more in the future. At some point I thought it would be convenient to have a special target (called “help”) in my Makefiles, that prints out all available targets in that Makefile.

So, the idea was to type:

make help

and get a list of all targets.

Solution

Unfortunately I can’t remember where I got the idea for the solution to my problem. Sorry for not giving the author credit for it or linking my source. Somewhere online I read an article, blog post, or what ever it was that described something similar to what I needed. I adjusted it to my needs and now it works almost the way I want. Checkout the example below.

Example Makefile:

# -----------------------------------------------------------------------------
# targets
# -----------------------------------------------------------------------------

.PHONY: help example_short example_long

## help: Show this help
help: Makefile
	@echo ""
	@echo "The following targets exist:"
	@sed -n -e '/^## \S/ s/^## //p' -e 's/^## \s\+/: /p' $< | \
		awk -F ": " '{printf "\033[33m%-20s\033[0m%s\n", $$1, $$2};'
	@echo ""


## example_short: This is an example target with a short description.
example_short:
	echo short


## example_long: This is an example target with a long description that
##               spanns multiple lines. Two lines are not even enought to
##               explain what this target does.
example_long:
	echo long

So the special target help parses the Makefile itself and basically looks for lines that start with “##”. That is supposed to be an indicator for a comment that describes a target. In the example you can see, how such a description has to be written in order for sed in help to find it. There are two scripts that sed runs. The first one finds the lines with the target name and the description. The second finds lines that extent a target description. All these lines are modified in order for awk to be able to print the output.

Now when I type:

make help

I get the following output (Well the text highlighting looks a little different in my terminal, but you get the idea :-)):

The following targets exist:
help                Show this help
example_short       This is an example target with a short description.
example_long        This is an example target with a long description that
                    spanns multiple lines. Two lines are not even enought to
                    explain what this target does.

Actually I also wanted to sort the targets alphabetically, but the possible long description of a target made that kind of complicated. It’s easy if you’re are sure that all the target description fit into one line. In that case a simple sort can be added to the help target:

## help: Show this help
help: Makefile
	@echo ""
	@echo "The following targets exist:"
	@sed -n -e '/^## \S/ s/^## //p' -e 's/^## \s\+/: /p' $< | \
		sort | \
		awk -F ": " '{printf "\033[33m%-20s\033[0m%s\n", $$1, $$2};'
	@echo ""

Change Log

2022-09-20:

  • Changed wording a little bit.



Take care,
Andreas

Updated:

Leave a comment