ideasman42
ideasman42

Reputation: 48128

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

Given the following ifeq statements, how would this be condensed so strings checks could be handled in one ifeq block?

OS:=$(shell uname -s)

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

I've looked into similar Q&A but not sure how it would apply exactly to this question.


Something like this:

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

Upvotes: 1

Views: 1576

Answers (2)

Vroomfondel
Vroomfondel

Reputation: 2898

You could also use the GNUmake table toolkit although it docs are quite beta still. Your code will look like this:

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),$$1%))

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

Upvotes: 0

MadScientist
MadScientist

Reputation: 100956

You can use the filter function for this:

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

Upvotes: 8

Related Questions