如何使 FeatureSummary 包含 pkg_check_modules 找到的包

How to make FeatureSummary include packages found by pkg_check_modules

我的CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
project (foo)
include(FeatureSummary)
find_package(OpenSSL REQUIRED)
find_package(PkgConfig QUIET)
pkg_check_modules(JSON REQUIRED json-c)
feature_summary(WHAT ALL)

运行 cmake . 给我:

-- The following REQUIRED packages have been found:

 * OpenSSL

谁能解释一下让 FeatureSummary 也包含 pkg_check_modules 找到的包的技巧?

更新

如果我使用以下代码创建一个名为 FindJSON.cmake 的文件:

find_package(PkgConfig QUIET)
# --> Still using pkg_check_modules
pkg_check_modules(JSON REQUIRED QUIET json-c)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
  JSON
  DEFAULT_MSG
  JSON_LIBRARIES
  JSON_INCLUDE_DIRS)

并将 CMakeLists.txt 更改为:

cmake_minimum_required(VERSION 2.8)
project (foo)
include(FeatureSummary)
find_package(OpenSSL REQUIRED)
set(CMAKE_MODULE_PATH . ${CMAKE_MODULE_PATH})
# --> Now using find_package which still uses pkg_check_modules
find_package(JSON REQUIRED)
feature_summary(WHAT ALL)

我得到:

-- The following REQUIRED packages have been found:

 * OpenSSL
 * JSON

没关系。发生了什么变化?

我用find_package_handle_standard_args。好吧,让我把FindJSON.cmake的内容复制到CMakeLists.txt,而不是通过find_package.

使用它

新的 CMakeLists.txt 将如下所示:

cmake_minimum_required(VERSION 2.8)
project (foo)
include(FeatureSummary)
find_package(OpenSSL REQUIRED)
# --> The code from from FindJSON.cmake
find_package(PkgConfig QUIET)
# --> Still using pkg_check_modules
pkg_check_modules(JSON REQUIRED QUIET json-c)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
  JSON
  DEFAULT_MSG
  JSON_LIBRARIES
  JSON_INCLUDE_DIRS)
# <-- end of code from FindJSON.cmake
feature_summary(WHAT ALL)

和输出:

-- The following REQUIRED packages have been found:

 * OpenSSL

JSON又消失了

所以 find_package 做了一些我不知道的魔法。

使用 find_package_handle_standard_args 并传递 find_package 中的相应变量。

pkg_check_modules(FOO)
find_package_handle_standard_args(FOO
  DEFAULT_MSG
  FOO_FOUND
  )

在您的情况下,将 FOO 替换为 SQLITE3

文档: https://cmake.org/cmake/help/v3.6/module/FindPackageHandleStandardArgs.html

或者,要将自定义条目添加到功能摘要,请使用 add_feature_info

文档指出:

add_feature_info(<name> <enabled> <description>)

Use this macro to add information about a feature with the given <name>. <enabled> contains whether this feature is enabled or not, <description> is a text describing the feature.[..]

Example for setting the info for a feature:

option(WITH_FOO "Help for foo" ON)
add_feature_info(Foo WITH_FOO "The Foo feature provides very cool stuff.")

https://cmake.org/cmake/help/v3.6/module/FeatureSummary.html

在find_package里面发生的魔法。它将包的名称存储到全局属性中。

你可以自己弄乱这些属性:

set_property(GLOBAL APPEND PROPERTY PACKAGES_FOUND MyJunkPackage)

检查 FeatureSummary.cmake 的来源以查看它引用了哪些其他属性和变量以便生成。例如,要使此包显示在 "required" 列表中,

set_property(GLOBAL APPEND PROPERTY _CMAKE_MyJunkPackage_TYPE REQUIRED)