Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

cmake

I want to check whether a lists contains a specific entry like in the following code snipplet:

macro(foo) if ($(ARGN} contains "bar")   ... endif endmacro() 

CMake does not offer a contains. What is best / easiest way to get the desired result?

In CMake's wiki I found a LIST_CONTAINS macro, but the wiki page is outdated. Is this still the best way to go or has CMake gained new capabilities?

like image 985
usr1234567 Avatar asked Apr 27 '14 12:04

usr1234567


2 Answers

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() 
like image 74
sakra Avatar answered Sep 28 '22 17:09

sakra


Fewer lines:

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

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

like image 23
steveire Avatar answered Sep 28 '22 17:09

steveire