A clean trick for make

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
Advertisements

About mischasan

I've had the privilege to work in a field where abstract thinking has concrete value. That applies at the macro level --- optimizing actions on terabyte database --- or the micro level --- fast parallel string searches in memory. You can find my documents on production-system radix sort (NOT just for academics!) and some neat little tricks for developers, on my blog https://mischasan.wordpress.com My e-mail sig (since 1976): Engineers think equations approximate reality. Physicists think reality approximates the equations. Mathematicians never make the connection.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s