makefile中的“ifeq”条件语法

时间:2015-08-24 07:53:24

标签: makefile conditional gnu-make

由于条件指令ifeq经常用于比较从变量(通常包含空格)扩展的单词,我们可能需要,实际上需要,制作以删除任何前导尾随空格。

事实上,你可能有一个相反的观点,即Make应该逐字记录ifeq条件的所有参数,因为用户可能已经将这些空格作为“test”的一部分,并且意图将那些空格用于在评估此ifeq指令时,将 true false 作为决定因素。

我无法决定,哪些 更多 正确。

事实上,我并不孤单!

让自己 无法 决定哪些是正确的。因此,它可能会也可能不会删除前导尾随空格。

事实上,有时它会 仅剥离空白

不令人失望,Make有时 仅剥离空白

当然,检查的案例太多了,所以我只会“做”一些案例。



一个makefile(版本1),是:

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



执行,我得到:

$ make -r
echo 'false'
false



一个makefile(版本2),是:

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



执行,我得到:

$ make -r
echo 'false'
false



一个makefile(版本3),是:

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



执行,我得到:

$ make -r
echo 'false'
false



一个makefile(VERSION 4),是:

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



执行,我得到:

$ make -r
echo 'true'
true



一个makefile(VERSION 5),是:

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



执行,我得到:

$ make -r
echo 'true'
true



总结一下,只有少数情况,我们有:

# 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.

因此,这种用于评估ifeq条件指令的真实性的方法肯定会转变为:

  • 不太一致。
  • 维护不力。
  • 难以调试。
  • 容易出错。
  • 最后,很多“有趣”!

我们同意吗?

1 个答案:

答案 0 :(得分:7)

您应该阅读this

  

逗号和不匹配的括号或大括号不能出现在写入的参数文本中;前导空格不能出现在第一个参数的文本中。可以通过变量替换将这些字符放入参数值中。首先定义变量commaspace,其值是孤立的逗号和空格字符,然后将这些变量替换为需要这些字符的位置,如下所示:

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