Best way to check with CMake whether list containts a specific entry

I have been using one liner like if ("${PLATFORM}" MATCHES "^(os|ios|android|linux|win32)$") to check if PLATFORM is in the list


Fewer lines:

if (";${ARGN};" MATCHES ";bar;")
  #  ...
endif()

But see the IN_LIST syntax from @sakra for a more-modern syntax.


With CMake 3.3 or later, the if command supports an IN_LIST operator, e.g.:

if ("bar" IN_LIST _list)
 ...
endif()

For older versions of CMake, you can use the built-in list(FIND) function:

list (FIND _list "bar" _index)
if (${_index} GREATER -1)
  ...
endif()

Tags:

Cmake