DEV Community

Cover image for TIL: How to list all the targets on a Makefile
👨‍💻Ioannis Diamantidis
👨‍💻Ioannis Diamantidis

Posted on • Originally published at diamantidis.github.io

TIL: How to list all the targets on a Makefile

make is great tool to orchestrate the setup and build process of a project. It expects a Makefile, where we define targets to execute, like for example install and run. Then we can use make install and make run to execute those tasks.

While target names like install are quite common, the problems arise when we have to deal with a lengthy Makefile, and we are not aware of all the available targets.

Hopefully, with a slight modification of our current Makefile and the addition of a new target, we can expose this information and access it from the Terminal app. Let's see how!

First, we will have to document each of the existing targets. To do so, we will add a comment starting with ## right after the target's name.

install: ## Install 
    @echo "Installing..."

run: ## Run
    @echo "Running..."
Enter fullscreen mode Exit fullscreen mode

Then, we will use the grep and sed command to get the name of the target and the documentation, like in the following snippet:

.DEFAULT_GOAL := help
.PHONY: help

help:
    @grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
    | sed -n 's/^\(.*\): \(.*\)##\(.*\)/\1\3/p' \
    | column -t  -s ' '
Enter fullscreen mode Exit fullscreen mode

We will also set the .PHONY and the .DEFAULT_GOAL variables. The last one will make help the default target when running make without a specific target.

Now, if we head back to the Terminal app, and run make, we will get the list of the documented targets as an output 🚀

install  Install
run      Run
Enter fullscreen mode Exit fullscreen mode

➡️ This post was originally published on my blog.

Top comments (0)