Awalias
Awalias

Reputation: 2137

Only make target if file exists

In my makefile, I want to do something like this:

all: foo bar python

python:
    if /usr/bin/someprogram
        do some stuff
    else
        echo "not doing some stuff, coz someprogram ain't there"
    endif

What is the simplest way to achieve this?

Upvotes: 0

Views: 2146

Answers (2)

devnull
devnull

Reputation: 123608

A simple way would be to use test:

python:
    @test -s /usr/bin/someprogram && echo "someprogram exists" || echo "someprogram does not exist"
    @test -s /bin/ls && echo "ls exists" || echo "ls does not exist"

As @MadScientist says, you might need an if statement in case you want to do multiple things:

python:
        if [ -s /bin/ls ]; then \
          echo "ls exists"; \
        fi;

Upvotes: 5

seanmcl
seanmcl

Reputation: 9946

You can use the 'if' and 'shell' make functions:

all: foo bar python

CMD=/some/missing/command

foo:
    echo "foo"

bar:
    echo "bar"

python:
    $(if $(shell $(CMD) 2>/dev/null), \
    echo "yes", \
    echo "no")

This echos 'no'. If you change CMD to, say, /bin/ls, it echos 'yes'.

Upvotes: 2

Related Questions