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
?
Solution 1:
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
Solution 2:
This approach functions with minimal echos:
.PHONY: all
all:
ifneq ($(wildcard ~/Dropbox/.*),)
@echo "Found ~/Dropbox."
else
@echo "Did not find ~/Dropbox."
endif