Search code examples
cmakepkg-config

How to make FeatureSummary include packages found by pkg_check_modules


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

Running cmake . gives me:

-- The following REQUIRED packages have been found:

 * OpenSSL

Can anyone explain the trick to make FeatureSummary also include packages found by pkg_check_modules?

UPDATE

If I create a file named FindJSON.cmake with the following code:

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)

and change CMakeLists.txt to:

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)

I get:

-- The following REQUIRED packages have been found:

 * OpenSSL
 * JSON

That is fine. What has changed?

I use find_package_handle_standard_args. Alright let me just copy the content of FindJSON.cmake to CMakeLists.txt, instead of using it through find_package.

The new CMakeLists.txt will look like this:

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)

and the output:

-- The following REQUIRED packages have been found:

 * OpenSSL

JSON has disappeared again.

So find_package does some magic of which I am unaware.


Solution

  • There is magic happening inside find_package. It's storing the names of packages into global properties.

    You can mess with those properties yourself:

    set_property(GLOBAL APPEND PROPERTY PACKAGES_FOUND MyJunkPackage)
    

    Check the source of FeatureSummary.cmake to see which other properties and variables it refers to in order to produce. For example, to make this package show up in the "required" list,

    set_property(GLOBAL APPEND PROPERTY _CMAKE_MyJunkPackage_TYPE REQUIRED)