eonnu
eonnu

Reputation: 133

Make: detect platform with regex

I'm writing a makefile that looks at the uname output, but for some reason the uname system name is too specific e.g. it is CYGWIN-NT-1.2.3. What I need is to compare that to a regular expression.

Currently I can detect only specific strings e.g.

ifeq (${value},CYGWIN-NT-4.5)
   do something
elif

How can I compare with a regex e.g. CYGWIN*?

Upvotes: 1

Views: 103

Answers (2)

Vroomfondel
Vroomfondel

Reputation: 2898

For simple cases one often gets away with globs instead of full regular expressions. gmtt is a GNUmake library which implements them. Your example could look like this:

include gmtt/gmtt.mk

PLATFORM := CYGWIN-NT-3.5

ifneq ($(call glob-match,$(PLATFORM),CYGWIN-*-4.?),)
$(info We are on Cygwin 4.x)
endif

ifneq ($(call glob-match,$(PLATFORM),CYGWIN-*-3.?),)
$(info We are on Cygwin 3.x)
endif

ifneq ($(call glob-match,$(PLATFORM),Ubuntu*),)
$(info We are on Ubuntu)
endif

Output:

$ make
We are on Cygwin 3.x

To streamline and lean down such selections, gmtt has tabular data and selection functions, which often make it clearer what is happening:

include gmtt/gmtt.mk

PLATFORM := CYGWIN-NT-3.5

define AVAILABLE-PLATFORMS :=
2
CYGWIN-*-4.?     toolX
CYGWIN-*-3.?     toolY
Ubuntu*          toolZ
endef


# select column 2 from the table line(s) which glob-match the current platform:
USED-TOOL := $(call select,2,$(AVAILABLE-PLATFORMS),$$(call glob-match,$(PLATFORM),$$1))

$(info We are using $(USED-TOOL))

Output:

We are using toolY

There is a caveat when using table cells with spaces in them (you must escape the spaces with spc-mask and convert them back with spc-unmask when using the value) but most of the time it is rather straightforward programming.

Upvotes: 0

MadScientist
MadScientist

Reputation: 100836

You can't use regular expressions in GNU make (without using shell or similar to invoke a shell script that handles regex's).

But you don't need regex's for the comparison you are looking for, which is just to see if the value starts with a given string. You can use the filter function:

ifneq (,$(filter CYGWIN%,$(value)))
  ...on cygwin...
endif

Upvotes: 1

Related Questions