views:

61

answers:

2

How can I get the pid of my make command in the Makefile?

Specifically, I want to use a temp directory that will be unique to this build.

I tried:

TEMPDIR = /tmp/myprog.$$$$

but this seems to store TEMPDIR as "/tmp/myprog.$$" and then eval as a new pid for every command which refs this! How do I get one pid for all of them (I'd prefer the make pid, but anything unique will do).

Thanks, -Shawn

+3  A: 

Try mktemp for creating unique temporary filenames. The -d option will create a directory instead of a file.

Matthew Iselin
Thanks, this looks like a good alternative. I'm rather new to Makefile syntax, how can I define a variable (TEMPDIR) using a shell command (mktemp)?
sligocki
TEMPDIR := $(shell mktemp). Note the colon.
Matthew Iselin
+1  A: 

You could use a date string. Unless you're kicking off multiple builds at the same time this should be pretty close.

Something like the following

pid_standin := $(shell date +%Y-%m-%d_%H-%M-%S)

file: 
    echo $(pid_standin)

$ Make
2010-10-04_21-01-58

Update: As noted in the comment if you set a variable with name = val syntax it is re-evaluated each time it's used. The := syntax sets it and doesn't re-evaluate the value, though using back-ticks `` seems to get around this somehow. You really want to use the $(shell CMD) construct for stuff like this.

Paul Rubel
This does not work. It still evals `date ...` each time you use $(pid_standin) and thus will create different directories.
sligocki
Shows what happens when I try a simple test case, updated the question with a fix
Paul Rubel