Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 890
user2164888 Avatar asked Dec 09 '25 11:12

user2164888


1 Answers

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.

like image 88
Renaud Pacalet Avatar answered Dec 12 '25 08:12

Renaud Pacalet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!