user2065276
user2065276

Reputation: 311

If Else syntax evaluating wrong clause in GNU Makefile

I am trying to get if else syntax working in Makefile

TYPE=src
RTL=src

program_%:
ifeq ($(TYPE),$(RTL))  
        echo "RTL"      
else                    
        echo "Test"         
endif

Here is the command line

$make -f test.make prog_src

and I get the following in the output

echo "RTL"
RTL

However, when i change the if statement such that instead of hard-coded variables I do something like

program_%:
ifeq ($(TYPE),$*)  
        echo "RTL"      
else                     
        echo "Test"
endif

and run as follows

$make -f test.make prog_src

I get the wrong clause evaluated

echo "Test"
Test

Upvotes: 0

Views: 261

Answers (1)

ensc
ensc

Reputation: 6984

remove the whitespaces in the condition

ifeq ($(TYPE),$(RTL))  

Regarding the second part of the question:

No; this is not possible ifeq is evaluated in the context of the makefile and $* is not expanded there. You can try $(if ...) but checking equality is not directly possible:

program_%:
    @echo $(if $(filter ${TYPE},$*),"RTL","Test")

"Context of makefile" means that if* sees only global make variables. Special variables like $* are available in the rule context only.

Complex rules

complex rules are possible by

define complex_rule_A
@echo "complex_rule_A"
@echo "done"
endef

define complex_rule_B
@echo "complex_rule_B"
@echo "failed"
endef


program_%:
    $(if $(filter ${TYPE},$*),$(call complex_rule_A),$(call complex_rule_B))

Alternative solutions

When ${TYPE} can be enumerated, a more clean solution might be

program_%:  .program_${TYPE}_%
    :

.program_RTL_%:
    echo else

.program_RTL_RTL:
    echo RTL

Upvotes: 2

Related Questions