find_package包含组件的配置文件

时间:2017-05-18 12:17:31

标签: cmake

我有一个cmake包,其中包含支持组件的Config文件,例如

find_package(myPack REQUIRED COMPONENTS foo bar)

在myPackConfig.cmake中我通常会

foreach(comp ${myPack_FIND_COMPONENTS})
    if(TARGET myPack::${comp})
         find_package(${comp})
    endif()
endif()

即。循环所有组件并找到它们。但如果没有给出所有组件,是否可以包含所有组件?例如。做什么时

find_package(myPack)

我想拥有所有组件(foo,bar,baz et.c。)

感谢

1 个答案:

答案 0 :(得分:3)

通常,您需要迭代所有目标的列表,并找到与给定模式匹配的目标(myPack::*)。

CMake有财产BUILDSYSTEM_TARGETS ...但它只列出非 IMPORTED 目标,因此它不适合。

用于获取已创建"对象列表的常用CMake模式"是拦截命令创建此类对象并强制它们将对象添加到列表中。此方法适用于收集目标列表:

<强> myPackConfig.cmake

# ... Somewhere at the beginnning, before including other files.
set(targets_list) # It will be maintained as a list of targets created
# Replace `add_library()` call
function (add_library name)
    # Add library target name to the list
    list(APPEND targets_list ${name})
    # And perform original actions
   _add_library (${name} ${ARGN})
endfunction (add_library)

# Include `*.cmake` files created by CMake.
...

# After all 'include()'s iterate over targets list and collect needed ones.
set(all_component_targets)
foreach(t ${targets_list})
    if(t MATCHES "myPack::.*")
        list(APPEND all_component_targets ${t})
    endif()
endforeach()

# Now 'all_component_targets' contains all targets prefixed with "myPack::".

(CMake mailing list中已经提出了这种方法。)