带有项目列表的makefile过程变量,其中一个项目是带空格的带引号的字符串

时间:2019-01-15 10:47:06

标签: linux makefile gnu

获取以下makefile片段:

VAR_LIST = "item1" "item2" "item 3 that has spaces" "item4"
ARGS = $(addprefix echo ,$(VAR_LIST))

我要达到的目的是让ARGS包含以下内容:

echo "item1" echo "item2" echo "item 3 that has spaces" echo "item4"

我不知道如何解决,是像addprefix这样的功能作用于空格...

2 个答案:

答案 0 :(得分:1)

不确定make是否可以轻松实现所需的内容,因为引号字符串对处理单词的make函数没有影响:"是单词的一部分。

我会为此使用shell或python脚本。

答案 1 :(得分:1)

您完全可以在GNUmake内的gmtt的帮助下进行操作。它不像完整的编程语言那样简单明了,但至少它是可移植的,并且独立于外部外壳样式和工具。

include gmtt/gmtt.mk

VAR_LIST = "item1" "item2" "item 3 that has spaces" "item4"

# make a prefix-list by splitting at ". This will yield superfluous space 
# characters between the items, but we can eliminate them later
prefix-list := $(call chop-str-spc,$(VAR_LIST),A $(-alnum-as-str))
$(info $(prefix-list))

# Now we select the data payload from the prefix-list. Spaces inside items 
# are still encoded as $(-spacereplace) characters, which is good as we have  
# a normal make list this way
string-list := $(call get-sufx-val,$(prefix-list),A,,100)
$(info $(string-list))

# Using get-sufx-val() is fine, but we can have it even simpler, by dropping
# the prefix, as we have only one in the list anyway:
string-list := $(call drop-prfx,$(prefix-list))

# Now step through the list with a normal for loop, converting $(-spacereplace)
# back to real spaces 
$(foreach item,$(string-list),$(if $(strip $(call spc-unmask,$(item))),\
   $(info [$(call spc-unmask,$(item))])))

输出:

$ make
 A¤item1 A¤§ A¤item2 A¤§ A¤item§3§that§has§spaces A¤§ A¤item4
item1 § item2 § item§3§that§has§spaces § item4
[item1]
[item2]
[item 3 that has spaces]
[item4]