joetde
joetde

Reputation: 1636

Cygwin's cmake does not match for CMAKE_SYSTEM_NAME

I'm having a stange behavior on cygwin using cmake. I try to check the OS in my CMakeLists.txt but is seems that this particular case does not work... Here is an example that raises my problem:

set (FOO "BAR")

message(${CMAKE_SYSTEM_NAME})

if (${CMAKE_SYSTEM_NAME} STREQUAL "CYGWIN")
    message("EQUALS CYGWIN")
endif()

if (${CMAKE_SYSTEM_NAME} MATCHES "CYGWIN")
    message("MATCHES CYGWIN")
endif()

if (${FOO} MATCHES "BAR")
    message("MATCHES BAR")
endif()

CMake prints:

CYGWIN
EQUALS CYGWIN
MATCHES BAR

And not the expected "MATCHES CYGWIN". I find this weird that it works for other variables (like FOO here). Is there something I am doing wrong?

Configuration:

PS: I also checked with FOO = "CYGWIN", and it does not match neither. It seems that just this particular string does not work with MATCHES...

Upvotes: 5

Views: 4658

Answers (2)

user2288008
user2288008

Reputation:

Cause

Signature of the if cmake command is

if(<variable|string> MATCHES regex)

but not

if(<string> MATCHES regex)

This means that variable CYGWIN checked first, and if there is no such variable, then CYGWIN string used.

Prove

Just try to print CYGWIN variable (see documentation):

> cat CMakeLists.txt
cmake_minimum_required(VERSION 2.8.12)
project(foo)
message("cygwin variable: ${CYGWIN}")
> cmake -H. -B_builds
...
cygwin variable: 1
...

Detailed

if (${CMAKE_SYSTEM_NAME} STREQUAL "CYGWIN")
    # variable CYGWIN vs variable CYGWIN, i.e. 1 == 1
    message("EQUALS CYGWIN") # success
endif()

if (${CMAKE_SYSTEM_NAME} MATCHES "CYGWIN")
    # variable CYGWIN (i.e. 1) vs regex CYGWIN
    message("MATCHES CYGWIN") # fail
endif()

if (${FOO} MATCHES "BAR")
    # string BAR vs string BAR
    message("MATCHES BAR") # success
endif()

Antidote

Avoid using commands with <variable|string> signature as much as possible, because any name can be applied to variable:

> cat CMakeLists.txt
cmake_minimum_required(VERSION 2.8.12)
project(foo)
set("/this/is/definitely/not/a/variable/name" "surprise!")
message("${/this/is/definitely/not/a/variable/name}")
> cmake -H. -B_builds
...
surprise!
...

For example you can use string command:

string(COMPARE EQUAL "${CMAKE_SYSTEM_NAME}" "CYGWIN" is_cygwin)
if(is_cygwin)
  message("Hello, cygwin!")
endif()

Note that in your particular case you can simply use CYGWIN variable check:

if(CYGWIN)
  message("Hello, cygwin!")
endif()

Related

Upvotes: 4

Yaakov
Yaakov

Reputation: 1703

Changing this:

if (${CMAKE_SYSTEM_NAME} MATCHES "CYGWIN")

to:

if (CMAKE_SYSTEM_NAME MATCHES "CYGWIN")

has the desired effect.

Upvotes: 2

Related Questions