测试makefile中是否存在目录 [英] Test whether a directory exists inside a makefile
问题描述
@Grundlefleck在他的答案中解释了如何检查目录是否存在.我尝试了一些在makefile
中使用它的方法,如下所示:
In his answer @Grundlefleck explains how to check whether a directory exists or not. I tried some to use this inside a makefile
as follow:
foo.bak: foo.bar
echo "foo"
if [ -d "~/Dropbox" ]; then
echo "Dir exists"
fi
运行make foo.bak
(假设存在foo.bar
)会产生以下错误:
Running make foo.bak
(given that foo.bar
exists) yields the following error:
echo "foo"
foo
if [ -d "~/Dropbox" ]; then
/bin/sh: -c: line 1: syntax error: unexpected end of file
make: *** [foo.bak] Error 2
我所做的解决方法是使用一个独立的bash脚本来实现测试,然后我从makefile
调用了该脚本.但是,这听起来很麻烦.有没有更好的方法可以检查makefile
中是否存在目录?
The workaround I made was to have a standalone bash script where the test is implemented and I called the script from the makefile
. This, however, sounds very cumbersome. Is there a nicer way to check whether a directory exists from within a makefile
?
推荐答案
如果使用shell命令,则使命令必须在一行中,或者使用反斜杠进行行扩展时在多行中.因此,这种方法将起作用:
Make commands, if a shell command, must be in one line, or be on multiple lines using a backslash for line extension. So, this approach will work:
foo.bak: foo.bar
echo "foo"
if [ -d "~/Dropbox" ]; then echo "Dir exists"; fi
或
foo.bak: foo.bar
echo "foo"
if [ -d "~/Dropbox" ]; then \
echo "Dir exists"; \
fi
这篇关于测试makefile中是否存在目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!