Skip to content

@pvdb /

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
List all targets (sometimes incorrectly referred to as "goals") in a GNU Makefile
# this gist can be used to list all targets, or - more correctly - rules,
# that are defined in a Makefile (and possibly other included Makefiles)
# and is inspired by Jack Kelly's reply to a StackOverflow question:
# I also found this script - - which does
# something similar using awk, but it extracts targets from the "static" rules from a single
# Makefile, meaning it ignores any included Makefiles, as well as targets from "dynamic" rules
# Notes:
# (1) the sed expression is "scraping" the make output, and will hence break of the output format ever changes
# (2) the "-r" option excludes the built-in rules, as these are typically not relevant, but isn't required
# (3) to improve the readability of the output, "| egrep -v '^.PHONY:'" can be appended to the pipeline
# (4) implementation as a shell alias or as a Makefile target left as an exercise to the reader :-)
# in the directory containing your Makefile:
make -rpn | sed -n -e '/^$/ { n ; /^[^ ]*:/p }'
# ... or add a touch of color by highlighting the targets in the rules:
make -rpn | sed -n -e '/^$/ { n ; /^[^ ]*:/p }' | egrep --color '^[^ ]*:'

This doesn't work on Mac OSX. I get:

sed: 1: "/^$/ { n ; /^[^ ]*:/p }\n": extra characters at the end of p command

Could be useful :)


It lists ".PHONY:", I think it should not


so change the regex...
make -rpn | sed -n -e '/^$/ { n ; /^[^ ]*:/p }' | egrep --color '^[^ .]*:'
should list everything EXCEPT lines that start with spaces or dots. This will exclude .PHONY


Does not work with FreeBSD: sed: 1: "/^$/ { n ; /^[^ ]*:/p }
": extra characters at the end of p command


To make the command work with BSD sed (as used on OSX and FreeBSD), simply append ; after the p - for more on the differences between GNU sed and BSD sed, see


OS X compatible: make -qp | awk -F":" "/^[a-zA-Z0-9][^$#\/\t=]*:([^=]|$)/ {split(\$1,A,/ /);for(i in A)print A[i]}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.