如何在 Makefile 中自动创建(和删除)临时目录?
是否可以让 make
在执行第一个目标之前创建一个临时目录? 也许使用一些黑客,一些额外的目标等?
Makefile 中的所有命令都可以引用自动创建的目录 $TMPDIR
,并且当 make
命令结束时该目录将自动删除。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我似乎记得能够递归地调用 make ,大致如下:
我已经完成了在子目录中调用 make 的类似技巧:
刚刚检查了一下,效果很好:
I seem to recall being able to call make recursively, something along the lines of:
I've done similar tricks for calling make in subdirectories:
Just checked it and this works fine:
将其放在 Makefile 的顶部
当
make
进程使用 shell 的父 PID ($PPID) 完成时,就会发生清理。 使用trap
设置,当make未正确完成或被中断时也会发生清理。我按以下方式在
ifeq
中使用它:这确保只有顶级 make 才会创建该目录,并且子 make 可以使用同一目录。 因此
导出
。可能仅适用于以
bash
作为 shell 的 Linux,因此SHELL := /bin/bash
。希望有帮助。
Put this at the top of your Makefile
The cleanup happens when the
make
-process finishes using the parent PID ($PPID) of the shell. Using thetrap
-setup the cleanup happens also when make does not finish properly or is interrupted.I use this inclosed in an
ifeq
in the following way:This ensures that only the top-level make will create the directory and the sub-makes can then use the same directory. Thus the
export
.Likely only works on linux with
bash
as the shell, thus theSHELL := /bin/bash
.Hope that helps.
以前的这些答案要么不起作用,要么看起来过于复杂。 这是我能想到的一个更直接的例子:
These previous answers either didn't work or seemed overly complicated. Here is a far more straight forward example I was able to figure out:
至少使用 GNU make
可以获得临时目录。 除了作为
all
目标一部分的明显的rmdir "$(TMPDIR)"
之外,我想不出一个在最后清理它的好方法。With GNU make, at least,
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 theall
target.请参阅 从 makefile 获取 makefile 的名称 的
$(self)
技巧See Getting the name of the makefile from the makefile for the
$(self)
trick