Alex Rivera | Logout

Exiting from a Makefile based on the state of two shell variables

Asked 2010-09-10T18:27:41.303
13

During make checksource, $CROSS_COMPILE should be "whatever". If $CROSS_COMPILE is not set, make should throw an error and exit.

I have the following rule in my Makefile:

.PHONY: checksource

all: checksource default

checksource:
    $(if $(and $(ifeq ($(CROSS_COMPILE), whatever)), $(ifeq ($(VARIABLE),))), \
    ($(shell echo "Error! VARIABLE not defined!") \
     $(shell exit 2)))

Right now if $CROSS_COMPILE is set to whatever and $VARIABLE is undefined, the make doesn't exit.

$CROSS_COMPILE:

$> echo $CROSS_COMPILE
whatever
$>

$VARIABLE is not defined:

$> echo $VARIABLE
$>

I could use a nested ifeq, but I want to make it pretty (and learn a bit more about Makefile operations).

Edit
Report

1 Answer

16

There is no such thing as $(ifeq). I still think you should do the check in the makefile itself, not as one of the targets:

ifeq ($(CROSS_COMPILE),whatever)
ifeq ($(VARIABLE),)
$(error Variables not set correctly.)
endif
endif

And if you're set on avoiding nested ifeq:

ifeq ($(or $(subst whatever,,$(CROSS_COMPILE)),$(VARIABLE)),)
$(error Variables not set correctly.)
endif

But I fail to see how that's an improvement. If you want to do it in a target, just use the shell and don't bother with make functions:

checksource:
    @if [ "$(CROSS_COMPILE)" = whatever -a -z "$(VARIABLE)" ]; then \
        echo "Error: Variables not set correctly"; exit 2; \
    else true; fi

I'd still go with the first option, because you can stop make before it stat s all the files names in Makefile and decides to start executing checksource.

answered 2010-09-13T01:31:07.200

Your Answer