[CMake] comparing strings

Alexander Neundorf a.neundorf-work at gmx.net
Fri Feb 15 13:13:16 EST 2013


On Thursday 14 February 2013, Shaun Williams wrote:
> I'm slowly realizing the gravity of this behavior:
> 
> if(build_system STREQUAL "windows")
> ...
> endif()
> 
> If someone creates a variable named "windows", then this code will not work
> as intended.
> 
> I'm starting to convert our scripts to use this hopefully foolproof
> alternative:
> 
> string(COMPARE EQUAL "${build_system}" windows _cmp)
> if (_cmp)
> ...
> endif()
> 
> It will make it ugly to create a nested if else block, but it'll work.

You can do 
if("${myvar}" MATCHES "somestring")
or more strict

if("${myvar}" MATCHES "^somestring$")

Alex


More information about the CMake mailing list