【发布时间】:2023-03-07 20:29:01
【问题描述】:
由于条件指令ifeq 经常用于比较从变量扩展而来的单词,这些单词通常包含空格,因此我们可能希望并且实际上需要去掉任何 前导 或 尾随 空格。
事实上,您可能有相反的看法,即 Make 应该逐字保留 ifeq 条件的所有参数,因为用户可能已经将这些空格作为“测试”的一部分,目的是让这些空格在评估此 ifeq 指令时,作为 true 或 false 发挥决定性作用。
我无法确定,其中哪一个是更多正确的。
事实上,我并不孤单!
让自己无法决定,哪一个是正确的。因此,它可能会或可能不会去除 leading 或 trailing 空格。
事实上,有时它会只去除前导空格。
并不令人失望,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(版本 4)是:
ifeq (a , a)
all::
echo 'true'
else
all::
echo 'false'
endif
执行,我得到:
$ make -r
echo 'true'
true
makefile(版本 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.
因此,Make 用来评估ifeq 条件指令的真实性 的这种方法肯定会变成:
- 不太一致。
- 不易维护。
- 更难调试。
- 容易出错。
- 终于有很多“乐趣”了!
我们同意吗?
【问题讨论】:
标签: makefile conditional gnu-make