【问题标题】:makefile process variable with list of items where one item is a quoted string with spaces带有项目列表的makefile过程变量,其中一项是带空格的带引号的字符串
【发布时间】:2019-06-09 09:00:27
【问题描述】:

取下面的makefile sn -p:

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 这样的函数作用于空格...

【问题讨论】:

  • 你想用这个解决什么更大的问题?
  • @MaximEgorushkin 我想要一个 makefile 用户可以填写的 doxygen 配置变量。这些项目看起来像 "VAR=Some Value"。然后我需要将它们中的每一个转换成如下所示的内容:; echo "VAR=Some Value",其中分号是列表分隔符。然后我将它输入到 doxygen 命令中。在此处查看第一个答案:stackoverflow.com/questions/11032280/…。这有意义吗?
  • @MaximEgorushkin 我可以让用户添加这样的项目:; echo "VAR=some value" ...但这看起来像是强加给用户的可怕语法...目前我说他们可以使用只有没有空格的值(大部分都可以)。
  • 不确定你需要的东西是否可以通过 make 轻松实现。我会为此使用 shell 或 python 脚本。
  • @MaximEgorushkin 是的,我想我和你在一起。可能有一些可怕的 make 函数可以应用,但它真的很复杂(如果可能的话)。我已经看到了一些非常聪明/可怕的代码!...但是正如你所说,使用脚本! - 我只是没想到,好主意 - 随时添加作为答案:)

标签: linux makefile gnu


【解决方案1】:

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

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

【讨论】:

  • 这是可行的(见我的回答)——人们可以争论复杂性,但恕我直言,包含电池的属性胜过缺乏优雅。
【解决方案2】:

您可以在gmtt 的帮助下完全在 GNUmake 中完成此操作。它不像完整的编程语言那样简单,但至少它是可移植的并且独立于外部 shell 风格和工具。

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]

【讨论】:

  • 哇......你能做到这一点真是太神奇了......但代码真的不好 - 我几乎无法理解它,所以为了将来的维护它不是我的第一选择。但是为代码 +1!
  • 你的意思是这段代码还是lib中它背后的代码? All make 编程基本上是一个非常简单的函数范例,具有一种数据类型,字符串。前缀列表是处理集合的一种方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多