RSFalcon7
RSFalcon7

Reputation: 2311

Makefile prerequisites before rules

Is it possible to force a specific condition before the rules are evaluated?

Ex:

FOOPATH = $(shell which foo)
ifndef $(FOOPATH)
    echo "Error: foo is not avaliable."
    # abort make
endif

PS: I'm not looking for a solution to add prerequisites do every rule that depends on foo (because the entire Makefile should depend on foo)

Upvotes: 0

Views: 95

Answers (2)

RSFalcon7
RSFalcon7

Reputation: 2311

For further information, this is my final solution, with a lot a help from @Beta

FOOPATH = $(shell which ls 2>/dev/null)

ifeq ($(FOOPATH),)
$(error foo is not avaliable)
else
$(warning foo is avaliable in $(FOOPATH))
endif

all:
    @echo "rule all executed"

Upvotes: 0

Beta
Beta

Reputation: 99094

FOOPATH = $(shell which foo)

ifeq ($(FOOPATH),)
$(error foo is not available)
endif

(Note that FOOPATH is defined, even if it is empty.)

EDIT: Correction-- I was wrong about ifndef; it returns true if the variable is defined but has an empty value. But it won't work as written because of the way FOOPATH is assigned a value:

FOOPATH = $(shell which foo)

The value of FOOPATH is literally "$(shell which foo)", so the ifndef will never evaluate as true, and you'll never see the error. The solution is to use a simply expanded variable:

FOOPATH := $(shell which foo)   # <-- note the colon

Also, ifndef should be applied to a variable name, not its value (e.g. FOOPATH not $(FOOPATH)).

ifndef FOOPATH
$(error foo is not available)
endif

Upvotes: 2

Related Questions