如何使用 $(info) 打印前导空格
How to print leading whitespace with $(info)
我想使用 $(info ...)
打印缩进输出
Make 会忽略 info
后的空格,因此以下内容不起作用:
$(info There is a space and then a tab before this text)
它将打印:
There is a space and then a tab before this text
所以现在我正在这样做(这不是我想要的,但足够接近):
$(info - Here there is a space, a dash, and then a tab)
它将打印:
- Here there is a space, a dash, and then a tab
是否可以打印以下内容?:
Is it possible to print me?
我知道我可以 echo(1)
,但我已经看到这样做会造成相当大的性能损失,所以我更愿意使用 $(info)
,即使我必须打印前导破折号。
你可以在你的 spaces:
前面放一些扩展为空字符串的东西
NULL :=
$(info $(NULL) X)
$(NULL)
标记在 info
和字符串参数之间起到分隔符的作用。
而不是 NULL
你甚至可以使用一个宏来扩展指定数量的 spaces。它将扮演前面示例中 NULL
变量的角色,加上请求的 space 数量。
$ cat Makefile
info-spaces = $(subst -, ,$(subst $(eval) ,,$(wordlist 1,$(1),\
$(foreach n,0 1 2 3 4 5 6 7 8 9,- - - - - - - - - -))))
.PHONY: all
N ?= 0
all:
$(info 0123456789)
$(info $(call info-spaces,$(N))X)
@:
$ make
0123456789
X
$ make N=10
0123456789
X
$ make N=5
0123456789
X
Note: you can add up to 100 spaces, no more. Modify the foreach
call to increase or reduce this maximum.
解释:
$(foreach ...)
调用创建了一个包含 100 个单词的列表,全部等于 -
。
$(wordlist ...)
returns N
第一个词 其中 N
是传递给 info-spaces
宏的参数(如果 N
=0它 returns 空字符串,如果 N
>100 它 returns 100 个字)。
$(subst $(eval) ,,...)
从单词列表中删除 space,只留下 min(N,100)
乘以 -
字符的单个单词。这里 $(eval)
什么都不做,它只是使用 space 作为 subst
的第一个参数的一种方式。与方案一中的$(NULL)
相同。我们可能会使用其他东西,只要它扩展为空字符串即可。
- 最后,最外层的
subst
将每个 -
字符替换为一个 space 和 returns 一串 min(N,100)
space。
Note: We could also use $(shell printf '%$(1)s' "")
instead of this complicated stuff but if you have performance issues spawning a new shell for each info
call is probably not a good idea.
如果$(info)
的文本参数为空,它将不输出任何内容并且
扩展为空,使其可用作零宽度字符,例如
$(info $(info) indented text)
\t := $(info) $(info)
输出 indented text
并将 \t
定义为制表符。
我想使用 $(info ...)
Make 会忽略 info
后的空格,因此以下内容不起作用:
$(info There is a space and then a tab before this text)
它将打印:
There is a space and then a tab before this text
所以现在我正在这样做(这不是我想要的,但足够接近):
$(info - Here there is a space, a dash, and then a tab)
它将打印:
- Here there is a space, a dash, and then a tab
是否可以打印以下内容?:
Is it possible to print me?
我知道我可以 echo(1)
,但我已经看到这样做会造成相当大的性能损失,所以我更愿意使用 $(info)
,即使我必须打印前导破折号。
你可以在你的 spaces:
前面放一些扩展为空字符串的东西NULL :=
$(info $(NULL) X)
$(NULL)
标记在 info
和字符串参数之间起到分隔符的作用。
而不是 NULL
你甚至可以使用一个宏来扩展指定数量的 spaces。它将扮演前面示例中 NULL
变量的角色,加上请求的 space 数量。
$ cat Makefile
info-spaces = $(subst -, ,$(subst $(eval) ,,$(wordlist 1,$(1),\
$(foreach n,0 1 2 3 4 5 6 7 8 9,- - - - - - - - - -))))
.PHONY: all
N ?= 0
all:
$(info 0123456789)
$(info $(call info-spaces,$(N))X)
@:
$ make
0123456789
X
$ make N=10
0123456789
X
$ make N=5
0123456789
X
Note: you can add up to 100 spaces, no more. Modify the
foreach
call to increase or reduce this maximum.
解释:
$(foreach ...)
调用创建了一个包含 100 个单词的列表,全部等于-
。$(wordlist ...)
returnsN
第一个词 其中N
是传递给info-spaces
宏的参数(如果N
=0它 returns 空字符串,如果N
>100 它 returns 100 个字)。$(subst $(eval) ,,...)
从单词列表中删除 space,只留下min(N,100)
乘以-
字符的单个单词。这里$(eval)
什么都不做,它只是使用 space 作为subst
的第一个参数的一种方式。与方案一中的$(NULL)
相同。我们可能会使用其他东西,只要它扩展为空字符串即可。- 最后,最外层的
subst
将每个-
字符替换为一个 space 和 returns 一串min(N,100)
space。
Note: We could also use
$(shell printf '%$(1)s' "")
instead of this complicated stuff but if you have performance issues spawning a new shell for eachinfo
call is probably not a good idea.
如果$(info)
的文本参数为空,它将不输出任何内容并且
扩展为空,使其可用作零宽度字符,例如
$(info $(info) indented text)
\t := $(info) $(info)
输出 indented text
并将 \t
定义为制表符。