Skip to content

Finding The Targets In A Makefile

I frequently encounter software packages that need to be built from source. Linux has a mostly-standardized build process which consists of configure, then make, followed by make install. But there are frequently additional targets in the Makefile. How do I find out what these targets are?

You'd think that make would have options to help with this, but it doesn't. So the simplest way I've found is to write a utility of my own. Here it is, in all its glory: #! /bin/bash # maketargets - shell script to output target names found in a Makefile # Chris@TheFreyers.net 3/23/2010 cat Makefile | egrep "^[[:alnum:][:punct:]]+\:" | grep -v ":=" | cut -d":" -f1 Understanding what this script does, by segment:

  • cat the Makefile to stdout
  • find all lines containing at least 1 alphanumeric character or punctuation mark at the beginning of a line, followed by a colon
  • subset the results to lines that do NOT include the ":=" assignment operator
  • interpret the line as a colon-separated list of values, and display only the first field

This works surprisingly well in most Makefiles (good enough to do the job). If anyone has another way to do this, I'd love to hear about it.

Making the Script Useful

The script is useful as it is, but there are other ways to deploy it as well.

  1. Add it as an alias to your ~/.bashrc file, like so:
alias maketargets='cat Makefile | egrep "^[](/alnum/][/punct/)+\\:" | grep -v ":=" | cut -d":" -f1'

Once you login again, all you have to do is cd into a directory where a makefile exists, and type maketargets 2. Add the alias to /etc/bashrc instead. This way, its available to all logged in users on your system who use the bash shell 3. Copy the script to the /usr/bin directory so everyone on your system (including other scripts) can use it