Conditional "ifeq" syntax in makefile

Since the conditional directive is ifeqoften used to compare words (s) extended from variables that often contain spaces, we might want and, in fact, require that Make separate any leading or trailing white space characters.

In fact, you may have the opposite view, that is, Make should store verbatim all the arguments in the ifeq conditional expression, since the user can have these spaces as part of the "test", with the intention for these spaces play a decisive factor in evaluating this directive ifeqas true or false .

I can’t decide which one is more correct.

In fact, I'm not alone!

Make yourself can not decide which one is right. Thus, it may or may not divide leading or trailing spaces.

In fact, sometimes it will only separate leading spaces .

Not disappointing, Make will sometimes only separate whitespace .

Of course, there are too many cases to check, so I will only do some of them.



Make file (VERSION 1):

ifeq ( a, a)
all::
    echo 'true'
else
all::
    echo 'false'
endif



Running, I get:

$ make -r
echo 'false'
false



Make file (VERSION 2):

ifeq (a ,a )
all::
    echo 'true'
else
all::
    echo 'false'
endif



Running, I get:

$ make -r
echo 'false'
false



Make file (VERSION 3):

ifeq ( a , a )
all::
    echo 'true'
else
all::
    echo 'false'
endif



Running, I get:

$ make -r
echo 'false'
false



Make file (VERSION 4):

ifeq (a , a)
all::
    echo 'true'
else
all::
    echo 'false'
endif



Running, I get:

$ make -r
echo 'true'
true



Make file (VERSION 5):

ifeq (a, a)
all::
    echo 'true'
else
all::
    echo 'false'
endif



Running, I get:

$ make -r
echo 'true'
true



To summarize, just a few cases, we have:

# Both, have only leading whitespace.
ifeq( a, a)    as: false.

# Both, have only trailing whitespace.
ifeq(a ,a )    as: false.

# Both, have trailing AND trailing whitespace.
ifeq( a , a )  as: false.

# Left-hand-size has only trailing, and right-hand-size has only leading whitepsace.
ifeq(a , a)    as: true.

# Left-hand-size has NO whitespace at-all, and right-hand-size has only leading whitepsace.
ifeq(a, a)     as: true.

Thus, this methodology that Make uses to evaluate the veracity of a conditional directive ifeqdefinitely turns it into:

  • Less consistent.
  • Less comfortable.
  • It’s harder to debug.
  • .
  • , ""!

?

+4
1

this:

, ; , . . comma space, , , :

comma:= ,
empty:=
space:= $(empty) $(empty)
foo:= a b c
bar:= $(subst $(space),$(comma),$(foo))
# bar is now ‘a,b,c’.

strip .


Makefile:

empty:=
space:= $(empty) $(empty)

x := $(space)a$(space)
y := $(space)a$(space)

ifeq ($(x),$(y))
all::
        @echo 'regular: true'
else
all::
        @echo 'regular: false'
endif

ifeq ($(strip $(x)),$(strip $(y)))
all::
        @echo 'strip:   true'
else
all::
        @echo 'strip:   false'
endif

:

1

x = $(space)a
y = $(space)a
regular: true
strip:   true

2

x = a$(space)
y = a$(space)
regular: true
strip:   true

3

x = $(space)a$(space)
y = $(space)a$(space)
regular: true
strip:   true

4

x = a$(space)
y = $(space)a
regular: false
strip:   true

4

x = a
y = $(space)a
regular: false
strip:   true
+3

All Articles