How can I automatically create (and remove) a temp directory in a Makefile?

Frank picture Frank · Feb 26, 2009 · Viewed 24.4k times · Source

Is it possible to have make create a temp directory before it executes the first target? Maybe using some hack, some additional target etc.?

All commands in the Makefile would be able to refer to the automatically created directory as $TMPDIR, and the directory would be automatically removed when the make command ends.

Answer

derobert picture derobert · Feb 26, 2009

With GNU make, at least,

TMPDIR := $(shell mktemp -d)

will get you your temporary directory. I can't come up with a good way to clean it up at the end, other than the obvious rmdir "$(TMPDIR)" as part of the all target.