In my GNUmakefile, I would like to have a rule that uses a temporary directory. For example:
out.tar: TMP := $(shell mktemp -d)
echo hi $(TMP)/hi.txt
tar -C $(TMP) cf $@ .
rm -rf $(TMP)
As written, the above rule creates the temporary directory at the time that the rule is parsed. This means that, even I don't make out.tar all the time, many temporary directories get created. I would like to avoid my /tmp being littered with unused temporary directories.
Is there a way to cause the variable to only be defined when the rule is fired, as opposed to whenever it is defined?
My main thought is to dump the mktemp and tar into a shell script but that seems somewhat unsightly.
Best Answer
In your example, the
TMP
variable is set (and the temporary directory created) whenever the rules forout.tar
are evaluated. In order to create the directory only whenout.tar
is actually fired, you need to move the directory creation down into the steps:The eval function evaluates a string as if it had been typed into the makefile manually. In this case, it sets the
TMP
variable to the result of theshell
function call.edit (in response to comments):
To create a unique variable, you could do the following:
This would prepend the name of the target (out.tar, in this case) to the variable, producing a variable with the name
out.tar_TMP
. Hopefully, that is enough to prevent conflicts.