1

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
ideasman42
  • 42,413
  • 44
  • 197
  • 320

2 Answers2

8

You can use the filter function for this:

ifeq ($(OS), Linux)
    foo
endif
ifneq (,$(filter $(OS),Darwin FreeBSD NetBSD))
    bar
endif
MadScientist
  • 92,819
  • 9
  • 109
  • 136
0

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)<)
Vroomfondel
  • 2,704
  • 1
  • 15
  • 29