June 27, 2014

How to List Targets In a Makefile

In the open source world, software is often distributed as source code, which must be built by the end user. Most often, builds are directed using Makefiles. One of the oft-required tasks is to list the targets available within a makefile. Using grep, it's no big deal to complete this task. The command you need is grep -E '^[a-zA-Z]+:' Makefile. An explanation of the expression:

^Anchor at the beginning of the line -- this is what necessitates running using so-called extended regular expressions
[a-zA-Z]Match anything between the [] only once. The - denotes a character range.
+One of more of the preceding expression is required

And a sample run:

% /usr/bin/grep -Ee '^[a-zA-Z]+:' ${HOME}/Makefile
all:
linksReport:

The first target ('all' in this case) is the default target because it does not start with a '/' or a '.'.

No comments:

Post a Comment