Makefile 中的名词集等价物是什么?

What is the nounset equivalent in Makefiles?

Bash 中的

set -o nounset 非常有用,但我不想 运行 我的 Makefile 中的每一行。是否可以通过 Makefile 中的 global 设置获得相同的效果?

您可以在 .SHELLFLAGS 变量 since GNU make 3.82 中提供全局 shell 设置。像这样:

$ cat Makefile
.SHELLFLAGS := -o nounset -c

all:
        echo "Foo is $${FOO}"

unset:
        set -o nounset; echo "Foo is $${FOO}"

make-resolved:
        echo "Foo is ${FOO}"

输出:

$ make all
echo "Foo is ${FOO}"
/bin/sh: FOO: unbound variable
make: *** [Makefile:4: all] Error 127

$ make unset
set -o nounset; echo "Foo is ${FOO}"
/bin/sh: FOO: unbound variable
make: *** [Makefile:7: unset] Error 127

没有.SHELLFLAGS第一次调用成功:

$ make all .SHELLFLAGS=-c
echo "Foo is ${FOO}"
Foo is

$ make unset .SHELLFLAGS=-c
set -o nounset; echo "Foo is ${FOO}"
/bin/sh: FOO: unbound variable
make: *** [Makefile:7: unset] Error 127

请注意,您需要转义 $ 才能让 shell 实际解析它,而不是 make。最后一个目标显示 make 将解决 ${FOO} 而 shell 不会抱怨:

$ make make-resolved
echo "Foo is "
Foo is

在这种情况下,您也可以从 make 得到警告(不是错误)since GNU make 3.68:

$ make --warn-undefined-variables make-resolved
Makefile:10: warning: undefined variable 'FOO'
echo "Foo is "
Foo is

哦,我多么渴望 --error-undefined-variables 旗帜与现有的 --warn-undefined-variables 旗帜搭配。空变量在我的 makefile 中始终是一个错误。

在此期间,对于更复杂的扩展,我经常使用 $(call expand,var) 之类的东西而不是普通的 ${var} 来捕捉拼写错误等。

expand = $(or ${},$(error [] is empty!))

有助于检查您的哈希是否具有正确的键。

codename<0.0.1> := Initial
codename<0.2.0> := Anniversary
codename<1.0.a> := Beta

codename := $(call expand,codename<${version}>)

等等