Last active
August 16, 2024 15:26
-
-
Save pvdb/777954 to your computer and use it in GitHub Desktop.
List all targets (sometimes incorrectly referred to as "goals") in a GNU Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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: | |
# | |
# http://stackoverflow.com/questions/3063507/list-goals-targets-in-gnu-make/3632592#3632592 | |
# | |
# I also found this script - http://www.shelldorado.com/scripts/cmds/targets - 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) 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 '^[^ ]*:' | |
# | |
# ... or if you prefer just the list of targets, WITHOUT the dependencies: | |
# | |
make -rpn | sed -n -e '/^$/ { n ; /^[^ .#][^ ]*:/ { s/:.*$// ; p ; } ; }' | |
Building on top of that I was able to use it to mark all targets as .PHONY
.PHONY: $(shell sed -n -e '/^$$/ { n ; /^[^ .\#][^ ]*:/ { s/:.*$$// ; p ; } ; }' $(MAKEFILE_LIST))
Works perfectly even for targets mentioned as dependencies.
Here there's a good option
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Embedding into makefile
Thank you for your implementation, it was very helpful as I'm not familiar with sed etc. However, it's a bit tricky in case someone wants to embed this piece into his makefile, so here is the solution to save time:
make -rpn | sed -n -e '/^$/ { n ; /^[^ .#][^ ]*:/ { s/:.*$// ; p ; } ; }'
->
make -rpn | sed -n -e '/^$$/ { n ; /^[^ .#][^ ]*:/ { s/:.*$$// ; p ; } ; }'
Apparently make tries to expand single $ (dollar signs), so they need to be escaped with another $ (dollar sign).