如何将字符串与 makefile 中的多个文字匹配?

How to match a string against multiple literals in a makefile?

给定以下 ifeq 语句,如何将其压缩以便在一个 ifeq 块中处理字符串检查?

OS:=$(shell uname -s)

ifeq ($(OS), Linux)
    foo
endif
ifeq ($(OS), Darwin)
    bar
endif
ifeq ($(OS), FreeBSD)
    bar
endif
ifeq ($(OS), NetBSD)
    bar
endif

我调查了 similar Q&A 但不确定它如何准确地应用于这个问题。


像这样:

ifeq ($(OS), Linux)
    foo
endif
ifeq ($(OS) in (Darwin, FreeBSD, NetBSD))  # <- something like this
    bar
endif

您可以为此使用 filter 函数:

ifeq ($(OS), Linux)
    foo
endif
ifneq (,$(filter $(OS),Darwin FreeBSD NetBSD))
    bar
endif

您也可以使用 the GNUmake table toolkit,尽管它的文档仍处于测试阶段。您的代码将如下所示:

include gmtt.mk

OS := $(shell uname -s)

# define a gmtt table with 2 columns
define os-table :=
2
Linux      foo
Windows    bar
Darwin     baz
FreeBSD    bof
NetBSD     baf
CYGWIN     foobar
endef


my-var := $(call select,$(os-table),2,$$(call str-match,$$(OS),$%))

$(info I'm on $(OS) and I selected >$(my-var)<)