在 GNU Make 中创建逗号分隔列表

发布于 2024-12-06 06:09:14 字数 613 浏览 1 评论 0原文

我有一个带有一组布尔值的 Makefile,必须使用它们来控制外部应用程序的标志。问题是该标志必须作为逗号分隔的字符串传递。

像这样的东西(非工作伪代码):

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,我需要运行以下命令:

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

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

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

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

How can I generate these string using Gnu Make?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嗳卜坏 2024-12-13 06:09:14

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

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)

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)
梦途 2024-12-13 06:09:14

像这样的构造

OPTIONS+=$(if $(filter y,$(BOOL_A)),--with=A,--with-out=A)

应该可以工作。

编辑:抱歉,忽略了必要的整理。

PARTS=A B C
YESSES=$(foreach i,$(PARTS),$(if $(filter y,$(BOOL_$(i))),$(i)))

all:
        echo with=$(shell echo $(YESSES) | tr ' ' ',')

这个想法是检查每个可能的部分 X 是否设置为“是”,如果是,则将其插入列表中。该列表以空格分隔,很难用 make 进行逗号分隔,但在 shell 中很容易做到这一点。

A construct like

OPTIONS+=$(if $(filter y,$(BOOL_A)),--with=A,--with-out=A)

should work.

Edit: Sorry, overlooked the necessary collation.

PARTS=A B C
YESSES=$(foreach i,$(PARTS),$(if $(filter y,$(BOOL_$(i))),$(i)))

all:
        echo with=$(shell echo $(YESSES) | tr ' ' ',')

The idea is to check for each possible part X whether it's set to yes and insert it into a list if it is yes. This list is whitespace-separated and hard to comma-separate with make, but easy to do this in shell.

温暖的光 2024-12-13 06:09:14

或者只使用 sed:丑陋(且未经测试)但简单

WITH_LIST = $(shell echo A$(BOOL_A) B$(BOOL_B) C$(BOOL_C) | sed -e 's/[ABC][^yABC]*//g' -e 's/y//g' -e 's/ /,/g')
WITHOUT_LIST = $(shell echo A$(BOOL_A) B$(BOOL_B) C$(BOOL_C) | sed -e 's/[ABC]y[^ABC]*//g' -e 's/[^ABC ]//g' -e 's/ /,/g')

Or just use sed: ugly (and untested) but straightforward

WITH_LIST = $(shell echo A$(BOOL_A) B$(BOOL_B) C$(BOOL_C) | sed -e 's/[ABC][^yABC]*//g' -e 's/y//g' -e 's/ /,/g')
WITHOUT_LIST = $(shell echo A$(BOOL_A) B$(BOOL_B) C$(BOOL_C) | sed -e 's/[ABC]y[^ABC]*//g' -e 's/[^ABC ]//g' -e 's/ /,/g')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文