在GNU Make中创建以逗号分隔的列表 [英] Create comma-separated lists in GNU Make

查看:79
本文介绍了在GNU Make中创建以逗号分隔的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一组布尔值的Makefile,必须将其用于控制​​外部应用程序的标志.问题在于该标志必须作为逗号分隔的字符串传递.

I have a Makefile with a set of booleans which must be used to control the flags for an external application. The problem is that the flag must be passed as a comma-separated string.

类似这样的东西(无效的伪代码):

Something like this (non-working pseudo code):

WITH_LIST = ""
WITHOUT_LIST = ""

ifeq ($(BOOL_A),y)
    # Append A to list "WITH_LIST"
else
    # Append A to list "WITHOUT_LIST"
endif

ifeq ($(BOOL_B),y)
    # Append B to list "WITH_LIST"
else
    # Append B to list "WITHOUT_LIST"
endif

ifeq ($(BOOL_C),y)
    # Append C to list "WITH_LIST"
else
    # Append C to list "WITHOUT_LIST"
endif

现在假设BOOL_A == y,BOOL_B == n和BOOL_C == y,我需要运行以下命令:

Now assuming BOOL_A == y, BOOL_B == n and BOOL_C == y, I need to run the following command:

./app --with=A,C --with-out=B

如何使用Gnu Make生成这些字符串?

How can I generate these string using Gnu Make?

推荐答案

首先,使用方法或thiton方法创建两个用空格分隔的列表. 然后,您可以使用GNU make手册的第6.2节末尾的小技巧. 创建一个包含一个空格的变量,一个包含逗号的变量.然后,您可以在$(subst ...)中使用它们将两个列表更改为逗号分隔.

First you create the two white-space separated lists, either using your method, or thiton's. Then you use the little trick from the end of section 6.2 of the GNU make manual to create a variable holding a single space, and one holding a comma. You can then use these in $(subst ...) to change the two lists to comma-separated.


PARTS  := A B C

BOOL_A := y
BOOL_B := n
BOOL_C := y

WITH_LIST    := $(foreach part, $(PARTS), $(if $(filter y, $(BOOL_$(part))), $(part)))
WITHOUT_LIST := $(filter-out $(WITH_LIST), $(PARTS))

null  :=
space := $(null) #
comma := ,

WITH_LIST    := $(subst $(space),$(comma),$(strip $(WITH_LIST)))
WITHOUT_LIST := $(subst $(space),$(comma),$(strip $(WITHOUT_LIST)))

all:
    ./app --with=$(WITH_LIST) --with-out=$(WITHOUT_LIST)

这篇关于在GNU Make中创建以逗号分隔的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆