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

前端 未结 4 1299
粉色の甜心
粉色の甜心 2021-02-06 21:26

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()


        
相关标签:
4条回答
  • 2021-02-06 21:48

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

    0 讨论(0)
  • 2021-02-06 21:51

    If the intention here is to add a value to a list but only if it's not already in the list, then an alternative approach is to just add it to the list and immediately remove possible duplicates again:

    list(APPEND            SOME_LIST "value")
    list(REMOVE_DUPLICATES SOME_LIST)
    
    0 讨论(0)
  • 2021-02-06 21:55

    Fewer lines:

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

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

    0 讨论(0)
  • 2021-02-06 22:05

    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()
    
    0 讨论(0)
提交回复
热议问题