GNU 使用前导空格进行测试输出

GNU make test output with leading spaces

我想在生成文件 (GNU make) 的规则帮助中输出一些文本。

help:
  $(info Help:)
  $(info )
  $(info make all   Build all targets)
  $(info make this  Build target this)
  $(info make long  Build target long that has)
  $(info            a very long description)
  $(info            that must be shown on)
  $(info            several lines)
  $(info make that  Build target that)

调用 make help 时,我希望:

Help:

make all   Build all targets
make this  Build target this
make long  Build target long that has
           a very long description
           that must be shown on
           several lines
make that  Build target that

但我得到:

Help:

make all   Build all targets
make this  Build target this
make long  Build target long that has
a very long description
that must be shown on
several lines
make that  Build target that

如何保留前导 space?

编辑:

该解决方案也必须适用于 Windows。

解法:

诀窍是添加一个包含 space 的变量。然后 make 不剥离字符串。

SPACE := $(subst ,, )

help:
  $(info Help:)
  $(info )
  $(info make all   Build all targets)
  $(info make this  Build target this)
  $(info make long  Build target long that has)
  $(info $(SPACE)          a very long description)
  $(info $(SPACE)          that must be shown on)
  $(info $(SPACE)          several lines)
  $(info make that  Build target that)

不要使用 make 的 info 函数。通常很少有理由在菜谱中使用这样的 make 函数:菜谱具有 shell 可用的全部功能,并且比 GNU make 函数功能强大得多。请改用 shell 的 echo 函数:

help:
        echo "Help:"
        echo
        echo "make all   Build all targets"
        echo "make this  Build target this"
        echo "make long  Build target long that has"
        echo "           a very long description"
        echo "           that must be shown on"
        echo "           several lines"
        echo "make that  Build target that"

预计到达时间

如果你真的想使用$(info ...)来实现便携性,那你就得耍点小把戏了:

E :=

help:
        $(info $E        indented text)

这会生成一个空变量 $E,然后您可以使用它,它会标记将函数名称与其参数分隔开的空格的结尾。