Search code examples
if-statementtestingmakefileconditional-statementscomparison

Can I use make conditionals with elif too?


I can do simple checks with make's conditionals, like that:

var = yes
ifeq $(var) "yes"; then
    echo "yes"
else
    echo "no"
fi

But the docs say nothing about elif. Can I do it like the following ?

var = yes
ifeq $(var) "yes"; then
    echo "yes"
elifeq $(var) "no"; then
    echo "no"
else
    echo "invalid"
fi

If not, is that possible at all, or do I have to make nested conditions or use test ?


Solution

  • Can I do it like the following ?

    No. You cannot use elifeq.

    do I have to make nested conditions or use test ?

    No. The documentation says:

    The syntax of a complex conditional is as follows: ... or:

    conditional-directive-one
    text-if-one-is-true
    else conditional-directive-two
    text-if-two-is-true
    else
    text-if-one-and-two-are-false
    endif
    

    There can be as many “else conditional-directive” clauses as necessary.

    Note here it says else conditional-directive-two. So, you can write:

    var = yes
    ifeq ($(var),yes)
        $(info "yes")
    else ifeq ($(var),no)
        $(info "no")
    else
        $(info "invalid")
    endif
    

    Note your original syntax is not valid makefile syntax. It looks like you're trying to use shell syntax: a makefile is not a shell script and doesn't use the same syntax.