In a makefile (or GNUmakefile, if you will) there’s always the little job of cleaning up afterward. make knows all about the targets and intermediates it creates … why can’t it automatically clean those up itself, somehow?
Well, it can … with a little help from you. Here’s the trick:
clean : ; rm -rf `$(MAKE) -nps | sed -n '/^[^\#%.][^ %]*: /s/:.*//p'` $(DIRT)
The -p option causes make to dump out its dependency rules, after it has worked out implicit relationships. The sed formula, that admittedly looks like line noise, extracts the name of every target that depends on something (i.e. has something after the colon). DIRT is just a variable of convenience, to which to append anything else that make cannot identify as a dependency; for example, the .gc?? files generated by gcc -pg.
Similarly, you can have make dump out the names of all source files with:
source : ;@ $(MAKE) -nps | sed -n '/Not a target:/{n; /^[.%]/!s/://p;}' | sort