How to get pid of my make command in the Makefile?
Asked Answered
T

6

17

I want to use a temp directory that will be unique to this build. How can I get the pid of my make command in the Makefile?

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 in advance.

Translunar answered 5/10, 2010 at 0:36 Comment(1)
Original question: How to get process id in Makefile. If anyone knows, I'd love an answer. However, this solved the root problem I had.Translunar
D
27

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

TEMPFILE := $(shell mktemp)
TEMPDIR := $(shell mktemp -d)

Note the colon. (Editor's note: This causes make to evaluate the function once and assign its value instead of re-evaluating the expression for every reference to $(TEMPDIR).)

Debrief answered 5/10, 2010 at 0:43 Comment(5)
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)?Translunar
TEMPDIR := $(shell mktemp). Note the colon.Debrief
This does not answer the question.Confocal
Please note, that the call mktemp without arguments does not work on MacOS/BSD-compatible systems. Using mktemp -t tempfile.XXXXXX would be a better portable option.Gambia
This cannot be used in a recipe: make: TEMPDIR: Command not foundSeaman
D
12

make starts a new shell for each command it starts. Using bash (didn't check for other shells) echo $PPID gives the parent process ID (which is make).

all: subtarget 
        echo $$PPID
        echo $(shell echo $$PPID)

subtarget:
        echo $$PPID
Duration answered 10/11, 2011 at 14:41 Comment(0)
D
12
TEMPDIR := $(shell mktemp)

The problem is: every time you run make, it will create a temporary file. Regardless of you use it or not and regardless of the make target you use. That means: either you delete this file in every target or you they will not be deleted anytime.

I prefer to add the -u parameter:

TEMPDIR := $(shell mktemp -u)

This makes mktemp to create an unique filename without creating the file. Now you can create the file in the targets you need them. Of course there is a chance of a race conditions, where the file is used by another process before you create it. That is very unlikely, but do not use it in an elevated command, like make install.

Duration answered 10/11, 2011 at 14:52 Comment(0)
S
6

Here is the answer to your question, which nobody seemed to want to give you:

TEMPDIR := /tmp/myprog.$(shell ps -o ppid $$$$)

Of course, this is not guaranteed to be unique.

Sickener answered 19/11, 2017 at 6:0 Comment(1)
This gives a stable TEMPDIR variable (because := rather than = to make a simply expanded rather than recursively expanded variable), but it gives the PID of that subshell, not the make command itself (in case anyone was actually looking for the PID of the make command)Unpen
H
1

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.

Heracles answered 5/10, 2010 at 0:43 Comment(2)
This does not work. It still evals date ... each time you use $(pid_standin) and thus will create different directories.Translunar
Shows what happens when I try a simple test case, updated the question with a fixHeracles
A
-3

Also, you can get your make pid like this.

PID := $(shell ps | tail -n 6 | head -n 1 | cut -f1 -d' ')

Animadversion answered 24/3, 2018 at 11:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.