MantisBT - CMake
View Issue Details
0015795CMakeModulespublic2015-10-16 10:412016-06-10 14:31
Sam Thursfield 
Rolf Eike Beer 
normalminoralways
closedmoved 
CMake 3.3.2 
CMake 3.7 
0015795: pkg_check_modules produces incorrect results depending on contents of CMakeCache.txt
I got bitten by a bug involving pkg_check_modules.

If I run cmake, then edit my CMakeLists.txt to change the parameters passed to pkg_check_modules, and rerun cmake, it ignores the change that I made to CMakeLists.txt. I needed to delete the CMakeCache.txt file to get the right results.

This seems like a cache invalidation bug to me -- a change in the parameters passed to to pkg_check_modules() should cause the cached values to be invalidated.

I've attached a small script that reproduces the issue.
Use attached script. It uses the 'avahi' library as an example, so needs the avahi-devel or libavahi-dev package (or whatever else your distro calls it) to be installed. Any two pkg-config modules will trigger the same error.

Output on my machine:

Creating initial CMakeLists.txt
Avahi libs should be: avahi-gobject
AVAHI LIBS: avahi-gobject;avahi-common;avahi-client;avahi-glib;glib-2.0

Updating pkg_check_modules call
Avahi libs should be: avahi-gobject;avahi-common;avahi-client;avahi-glib;glib-2.0
AVAHI LIBS: avahi-gobject;avahi-common;avahi-client;avahi-glib;glib-2.0

Removing cache and trying again
Avahi libs should be: avahi-gobject;avahi-common;avahi-client;avahi-glib;glib-2.0
AVAHI LIBS: avahi-gobject;avahi-common;avahi-client;avahi-glib;glib-2.0
No tags attached.
? testcase.sh (854) 2015-10-16 10:41
https://public.kitware.com/Bug/file/5546/testcase.sh
Issue History
2015-10-16 10:41Sam ThursfieldNew Issue
2015-10-16 10:41Sam ThursfieldFile Added: testcase.sh
2015-10-16 10:45Sam ThursfieldNote Added: 0039622
2016-06-02 15:49FlorianMNote Added: 0041131
2016-06-02 15:51FlorianMNote Edited: 0041131bug_revision_view_page.php?bugnote_id=41131#r2126
2016-06-02 15:52Brad KingAssigned To => Rolf Eike Beer
2016-06-02 15:52Brad KingStatusnew => assigned
2016-06-02 15:52Brad KingTarget Version => CMake 3.7
2016-06-10 14:29Kitware RobotNote Added: 0042858
2016-06-10 14:29Kitware RobotStatusassigned => resolved
2016-06-10 14:29Kitware RobotResolutionopen => moved
2016-06-10 14:31Kitware RobotStatusresolved => closed

Notes
(0039622)
Sam Thursfield   
2015-10-16 10:45   
Oops, the output of the test case I posted is with an existing CMakeCache.txt. If you run with no CMakeCache.txt you will see this:

Creating initial CMakeLists.txt
Avahi libs should be: avahi-gobject
AVAHI LIBS: avahi-gobject

Updating pkg_check_modules call
Avahi libs should be: avahi-gobject;avahi-common;avahi-client;avahi-glib;glib-2.0
AVAHI LIBS: avahi-gobject

Removing cache and trying again
Avahi libs should be: avahi-gobject;avahi-common;avahi-client;avahi-glib;glib-2.0
AVAHI LIBS: avahi-gobject;avahi-common;avahi-client;avahi-glib;glib-2.0

Both examples show that something is wrong!
(0041131)
FlorianM   
2016-06-02 15:49   
(edited on: 2016-06-02 15:51)
Recently this issue came up again on SO, see http://stackoverflow.com/questions/37026867/how-to-make-cmake-recheck-versions-of-dependencies-automatically [^]

After doing some testing, I believe that adding `${ARGV}` to `__pkg_config_checked_${_prefix}` variable would retrigger the scanning for the package when arguments to the `pkg_check_modules()` or `pkg_search_module()` calls are changed.

Thanks in advance for any feedback on this proposed fix.

Here is my patch for FindPkgConfig.cmake version as of 21th March, 2016:

--- FindPkgConfig.cmake
+++ FindPkgConfig.cmake
@@ -523,11 +523,13 @@
 #]========================================]
 macro(pkg_check_modules _prefix _module0)
   # check cached value
- if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
+ if (NOT DEFINED __pkg_config_checked_${_prefix} OR
+ NOT __pkg_config_checked_${_prefix} STREQUAL "${PKG_CONFIG_VERSION},${ARGV}" OR
+ NOT ${_prefix}_FOUND)
     _pkgconfig_parse_options (_pkg_modules _pkg_is_required _pkg_is_silent _no_cmake_path _no_cmake_environment_path "${_module0}" ${ARGN})
     _pkg_check_modules_internal("${_pkg_is_required}" "${_pkg_is_silent}" ${_no_cmake_path} ${_no_cmake_environment_path} "${_prefix}" ${_pkg_modules})

- _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
+ _pkgconfig_set(__pkg_config_checked_${_prefix} "${PKG_CONFIG_VERSION},${ARGV}")
   endif()
 endmacro()

@@ -550,7 +552,9 @@
 #]========================================]
 macro(pkg_search_module _prefix _module0)
   # check cached value
- if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
+ if (NOT DEFINED __pkg_config_checked_${_prefix} OR
+ NOT __pkg_config_checked_${_prefix} STREQUAL "${PKG_CONFIG_VERSION},${ARGV}" OR
+ NOT ${_prefix}_FOUND)
     set(_pkg_modules_found 0)
     _pkgconfig_parse_options(_pkg_modules_alt _pkg_is_required _pkg_is_silent _no_cmake_path _no_cmake_environment_path "${_module0}" ${ARGN})

@@ -575,7 +579,7 @@
       endif()
     endif()

- _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
+ _pkgconfig_set(__pkg_config_checked_${_prefix} "${PKG_CONFIG_VERSION},${ARGV}")
   endif()
 endmacro()

(0042858)
Kitware Robot   
2016-06-10 14:29   
Resolving issue as `moved`.

This issue tracker is no longer used. Further discussion of this issue may take place in the current CMake Issues page linked in the banner at the top of this page.