如何在 Makefile 中获取 make 命令的 pid?

发布于 2024-09-25 18:29:06 字数 271 浏览 1 评论 0原文

我想使用此构建特有的临时目录。如何在 Makefile 中获取 make 命令的 pid?

我尝试过:

TEMPDIR = /tmp/myprog.$$$$

但这似乎将 TEMPDIR 存储为 /tmp/myprog.$$ ,然后将 eval 作为引用此的每个命令的新 pid !我如何获得所有这些的一个pid(我更喜欢make pid,但任何独特的东西都可以)。

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

谢绝鈎搭 2024-10-02 18:29:06

尝试使用 mktemp 创建唯一的临时文件名。 -d 选项将创建一个目录而不是文件。

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

注意冒号。 (编者注:这会导致 make 对函数求值一次并为其赋值,而不是为每个对 $(TEMPDIR) 的引用重新求表达式。)

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).)

半边脸i 2024-10-02 18:29:06

make 为它启动的每个命令启动一个新的 shell。使用 bash(未检查其他 shell)echo $PPID 给出父进程 ID(即 make)。

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

subtarget:
        echo $PPID

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
喜爱纠缠 2024-10-02 18:29:06
TEMPDIR := $(shell mktemp)

问题是:每次运行 make 时,它​​都会创建一个临时文件。无论您是否使用它,也无论您使用的 make 目标如何。这意味着:要么您在每个目标中删除该文件,要么它们随时都不会被删除。

我更喜欢添加 -u 参数:

TEMPDIR := $(shell mktemp -u)

这使得 mktemp 创建唯一的文件名而不创建文件。现在您可以在需要的目标中创建文件。当然,有可能出现竞争条件,即文件在创建之前已被另一个进程使用。这不太可能,但是不要在提升的命令中使用它,例如make install

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.

小红帽 2024-10-02 18:29:06

这是您问题的答案,似乎没有人愿意给您:

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

当然,这不能保证是唯一的。

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.

别念他 2024-10-02 18:29:06

您可以使用日期字符串。除非您同时启动多个构建,否则这应该非常接近。

类似于以下

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

file: 
    echo $(pid_standin)

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

更新:如评论中所述,如果您使用 name = val 语法设置变量,则它是 每次使用时都会重新评估。 := 语法设置它并且不重新评估该值,尽管使用反引号 `` 似乎可以以某种方式解决这个问题。您确实想使用 $(shell CMD) 构造来完成这样的事情。

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.

樱花坊 2024-10-02 18:29:06

另外,你可以像这样得到你的 make pid。

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

Also, you can get your make pid like this.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文