用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不提供包含。什么是获得所需结果的最佳/最简单方法?

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

在CMake的 Wiki 中,我找到了LIST_CONTAINS宏,但是Wiki页面已过时

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?

对于CMake 3.3或更高版本, if 命令支持 IN_LIST 运算符,例如:

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

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

对于较旧版本的CMake,您可以使用内置的 list(FIND)函数:

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

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