user2164888
user2164888

Reputation: 31

Nested conditionals in makefiles

A makefile here has nested if statements to assign variable values and targets to be built. The current nested structure isn't really a nest - it is a series of if statements: There isn't any use of else.

Is this because makefiles don't have an equivalent of elseIf?

Current structure (indentations added for readability in this post)

If condition x
    if condition x.x
        blah
    endif
    if condition x.y
        blah blah
    endif
endif
if condition y
    if condition y.x
        blah
    endif
    if condition y.y
        blah blah
    endif
endif 

Pseudocod-ish version of desired structure:

If condition x
    if condition x.x
        blah
    else
    if condition x.y
        blah blah
    endif
else
if condition y
    if condition y.x
        blah
    else
    if condition y.y
        blah blah
    endif
endif

Upvotes: 3

Views: 5952

Answers (1)

Renaud Pacalet
Renaud Pacalet

Reputation: 29345

Does this answer your question?

ifeq ($(VAR1),x)
    ifeq ($(VAR2),x)
        $(info x.x)
    else ifeq ($(VAR2),y)
        $(info x.y)
    endif
else ifeq ($(VAR1),y)
    ifeq ($(VAR2),x)
        $(info y.x)
    else ifeq ($(VAR2),y)
        $(info y.y)
    endif
endif

all:;

Demo:

$ make VAR1=x VAR2=y
x.y
make: 'all' is up to date.

But you can also:

ifeq ($(VAR1).$(VAR2),x.x)
    $(info x.x)
else ifeq ($(VAR1).$(VAR2),x.y)
    $(info x.y)
else ifeq ($(VAR1).$(VAR2),y.x)
    $(info y.x)
else ifeq ($(VAR1).$(VAR2),y.y)
    $(info y.y)
endif

all:;

Demo:

$ make VAR1=y VAR2=x
y.x
make: 'all' is up to date.

For more information the best source is the GNU make manual, section Syntax of Conditionals.

Upvotes: 2

Related Questions