Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exiting from a make file if the state of two shell variables are a certain state

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).

like image 803
MakeDummy Avatar asked Sep 10 '10 18:09

MakeDummy


People also ask

How do you check if a variable is defined in Makefile?

Check if variable is defined in a Makefilecheck_defined = \ $(strip $(foreach 1,$1, \ $(call __check_defined,$1,$(strip $(value 2))))) __check_defined = \ $(if $(value $1),, \ $(error Undefined $1$(if $2, ($2)))) install: $(call check_defined, var1) $(call check_defined, var2) # do stuff here..

How do I use IFEQ in Makefile?

The ifeq directive begins the conditional, and specifies the condition. It contains two arguments, separated by a comma and surrounded by parentheses. Variable substitution is performed on both arguments and then they are compared.

What is ?= In Makefile?

?= indicates to set the KDIR variable only if it's not set/doesn't have a value. For example: KDIR ?= "foo" KDIR ?= "bar" test: echo $(KDIR) Would print "foo"


2 Answers

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.

like image 155
Jack Kelly Avatar answered Sep 23 '22 03:09

Jack Kelly


Doing it in make is always better than using the shell (whether via $(shell) or a recipe). If you do do the check in a recipe, then it means that the Makefile can contain other targets that do not need this particular assert.

assert = $(if $(filter whatever,${CROSS_COMPILE}),$(if ${VARIABLE},,$(error Urk! Variable problem)))

checksource:
        ${assert}some shell commands...

P.S. If you ran your original make with --warn-undefined-variables you may have got some clue why your macros were not expanding properly:

$ make -f 1.mak CROSS_COMPILE=whatever --warn-undefined-variables
1.mak:6: warning: undefined variable `ifeq (whatever, whatever)'
make: *** No rule to make target `default', needed by `all'.  Stop.
like image 41
bobbogo Avatar answered Sep 20 '22 03:09

bobbogo