83

A variable returns MINGW32_NT-5.1 or CYGWIN_NT-5.1. (yea, dot at the end)

Need to compare that given var contains NT-5.1 positioned anywhere.

Using cygwin and would like to be compatible with pretty much any *nix.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Pablo
  • 28,133
  • 34
  • 125
  • 215

2 Answers2

152

The findstring function is what your heart desires:

$(findstring find,in)

Searches in for an occurrence of find. If it occurs, the value is find; otherwise, the value is empty. You can use this function in a conditional to test for the presence of a specific substring in a given string. Thus, the two examples,

$(findstring a,a b c)
$(findstring a,b c)

produce the values "a" and "" (the empty string), respectively. See Testing Flags, for a practical application of findstring.

Something like:

ifneq (,$(findstring NT-5.1,$(VARIABLE)))
    # Found
else
    # Not found
endif

What is the comma here for ifneq (,$(...?

Parse it as ifneq(A,B) where A is the empty string and B is $(findstring...). It looks odd because you don't quote strings in Makefiles.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 3
    Is `findstring` case sensitive? If so, is there a simple way to do case insensitive matching? The manual wasn't clear... – Isaac Turner Feb 16 '14 at 17:45
  • 3
    @IsaacTurner: Yes, `make` functions are _invariably_ case-_sensitive_. Sadly, there are no case-insensitive variants, but as a workaround you could use the `$(shell ...)` function to use a shell command for case conversion - clunky, but it works; e.g.: `$(findstring $(shell echo 'BC' | tr '[:upper:]' '[:lower:]'), 'abcd')`. If you don't mind specifying `SHELL := bash` to have `make` use bash as the shell, you can take advantage of `shopt -s nocasematch` and perform entire comparisons case-insensitively inside a single `$(shell ...)` call. – mklement0 Dec 01 '14 at 18:27
  • @Invictus You should post that as a new question. – John Kugelman Jul 26 '21 at 11:56
23
VARIABLE=NT-5.1_Can_be_any_string
ifeq ($(findstring NT-5.1,$(VARIABLE)),NT-5.1)
    # Found
    RESULT=found
else
    # Not found
    RESULT=notfound
endif

all:
    @echo "RESULT=${RESULT} , output=$(findstring NT-5.1,$(VARIABLE))"

It matches the given string and returns

vimjet
  • 341
  • 3
  • 9