如何在Makefile中自动创建(和删除)临时目录? [英] How can I automatically create (and remove) a temp directory in a Makefile?

查看:881
本文介绍了如何在Makefile中自动创建(和删除)临时目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在执行第一个目标之前,是否可以让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.?

Makefile中的所有命令都可以将自动创建的目录称为$TMPDIR,并且当make命令结束时,该目录将被自动删除.

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.

推荐答案

我似乎想起了能够递归调用make的方法,

I seem to recall being able to call make recursively, something along the lines of:

all:
    -mkdir $(TEMPDIR)
    $(MAKE) $(MLAGS) old_all
    -rm -rf $(TEMPDIR)

old_all: ... rest of stuff.

我在子目录中也做了类似的技巧:

I've done similar tricks for making in subdirectories:

all:
    @for i in $(SUBDIRS); do \
        echo "make all in $$i..."; \
        (cd $$i; $(MAKE) $(MLAGS) all); \
    done

只需检查一下,即可正常运行

Just checked it and this works fine:

$ cat Makefile
all:
    -mkdir tempdir
    -echo hello >tempdir/hello
    -echo goodbye >tempdir/goodbye
    $(MAKE) $(MFLAGS) old_all
    -rm -rf tempdir

old_all:
    ls -al tempdir

$ make all
mkdir tempdir
echo hello >tempdir/hello
echo goodbye >tempdir/goodbye
make  old_all
make[1]: Entering directory '/home/pax'
ls -al tempdir
total 2
drwxr-xr-x+ 2 allachan None 0 Feb 26 15:00 .
drwxrwxrwx+ 4 allachan None 0 Feb 26 15:00 ..
-rw-r--r--  1 allachan None 8 Feb 26 15:00 goodbye
-rw-r--r--  1 allachan None 6 Feb 26 15:00 hello
make[1]: Leaving directory '/home/pax'
rm -rf tempdir

$ ls -al tempdir
ls: cannot access tempdir: No such file or directory

这篇关于如何在Makefile中自动创建(和删除)临时目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆