Testing if a file exists in makefile target, and quitting if not present

I realize this is a bit old at this point, but you don’t need to even use a subshell to test if a file exists in Make.

It also depends on how you want/expect it to run.

Using the wildcard function, like so:

all: foo
foo:
ifeq (,$(wildcard /opt/local/bin/gsort))
    $(error GNU Sort does not exist!)
endif

is one good way to do it. Note here that the ifeq clause is not indented because it is evaluated before the target itself.

If you want this to happen unconditionally for every target, you can just move it outside of a target:

ifeq (,$(wildcard /opt/local/bin/gsort))
$(error GNU Sort does not exist!)
endif

Leave a Comment