Verifying cmake string token

In cmake, how can I check if a token is included on another line?

In my case, I would like to know if the compiler name contains the string "Clang" (for example, "clang", "AppleClang", ...). All I could do so far:

if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") ... elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang") ... 

I need a more flexible approach, like checking for substring.

This is what I can find in the documentation:

if (MATCHES regex) True if the given value of the string or variables matches the given regular expression. if (LESS) True if the given value of the string or variables is a valid number and less than the right.
if (BIG) True if the given value of the string or variables is a valid number and greater than the right.
if (EQUAL) True if the given value of the string or variables is a valid number and is equal to the value on the right.
if (STRLESS) True if the given value of the string or variables is lexicographically less than the string or variable on the right.
if (STRGREATER) True if the given value of the string or variables is lexicographically larger than the string or variable on the right.
if (STREQUAL) True if the given value of a string or variables is lexicographically equal to the string or variable on the right.

+7
string cmake string-comparison
source share
1 answer

if(<variable|string> MATCHES regex) will probably be what you are looking for.

In this particular case (if you do the same inside the block for Clang and AppleClang ), you can replace:

 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") ... elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang") ... 

from:

 if(CMAKE_CXX_COMPILER_ID MATCHES "^(Apple)?Clang$") 
+8
source share

All Articles