如何使FeatureSummary包含pkg_check_modules找到的包

时间:2016-07-24 17:24:36

标签: cmake pkg-config

我的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做了一些我不知道的魔法。

2 个答案:

答案 0 :(得分:0)

使用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>)
     

使用此宏添加   有关具有给定<name>的要素的信息。 <enabled>包含   无论是否启用此功能,<description>都是文本   描述这个特征。[..]

     

设置功能信息的示例:

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

答案 1 :(得分:0)

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

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

set_property(GLOBAL APPEND PROPERTY PACKAGES_FOUND MyJunkPackage)

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

set_property(GLOBAL APPEND PROPERTY _CMAKE_MyJunkPackage_TYPE REQUIRED)