Test whether a directory exists inside a 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
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
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
?
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
Or
foo.bak: foo.bar
echo "foo"
if [ -d "~/Dropbox" ]; then
echo "Dir exists";
fi
这种方法具有最小的回声功能:
.PHONY: all
all:
ifneq ($(wildcard ~/Dropbox/.*),)
@echo "Found ~/Dropbox."
else
@echo "Did not find ~/Dropbox."
endif
Act upon the absence of a directory
If you only need to know if a directory does not exist and want to act upon that by for example creating it, you can use ordinary Makefile targets:
directory = ~/Dropbox
all: | $(directory)
@echo "Continuation regardless of existence of ~/Dropbox"
$(directory):
@echo "Folder $(directory) does not exist"
mkdir -p $@
.PHONY: all
Remarks:
|
indicates that make shouldn't care about the timestamp (it's an order-only-prerequisite). mkdir -p $@
, you can write false
to exit, or solve your case differently. If you also need to run a particular series of instructions upon the existence of a directory, you cannot use the above. In other words, it is equivalent to:
if [ ! -d "~/Dropbox" ]; then
echo "The ~/Dropbox folder does not exist"
fi
There is no else
statement.
Act upon the presence of a directory
If you want the opposite if-statement this is also possible:
directory = $(wildcard ~/Dropbox)
all: | $(directory)
@echo "Continuation regardless of existence of ~/Dropbox"
$(directory):
@echo "Folder $(directory) exists"
.PHONY: all $(directory)
This is equivalent to:
if [ -d "~/Dropbox" ]; then
echo "The ~/Dropbox folder does exist"
fi
Again, there is no else
statement.
Act upon both the presence and the absence of a directory
This becomes a bit more cumbersome, but in the end gives you nice targets for both cases:
directory = ~/Dropbox
dir_target = $(directory)-$(wildcard $(directory))
dir_present = $(directory)-$(directory)
dir_absent = $(directory)-
all: | $(dir_target)
@echo "Continuation regardless of existence of ~/Dropbox"
$(dir_present):
@echo "Folder $(directory) exists"
$(dir_absent):
@echo "Folder $(directory) does not exist"
.PHONY: all
This is equivalent to:
if [ -d "~/Dropbox" ]; then
echo "The ~/Dropbox folder does exist"
else
echo "The ~/Dropbox folder does not exist"
fi
Naturally, the wildcard expansion might be slower than an if-else-statement. However, the third case is probably quite rare and is just added for completeness.
链接地址: http://www.djcxy.com/p/17486.html上一篇: Bash脚本,循环浏览文件夹中的文件失败
下一篇: 测试一个目录是否存在于makefile中