FindPkgConfig: Some variables are not getting set upon subsequent builds if using new version of pkg-config
Consider the following code:
find_package(PkgConfig REQUIRED)
pkg_check_modules(SpiderMonkey mozjs-102)
...
include_directories(SYSTEM ${SpiderMonkey_INCLUDE_DIRS})
Depending on the version of pkg-config
installed on the system the FindPkgConfig module behaves differently.
For older versions of pkg-config
(e.g. 0.29.2 in Ubuntu 22.04) the variables ${_prefix}_INCLUDE_DIRS
and ${_prefix}_CFLAGS_OTHER
are saved to the cmake cache on the first run along with all the other variables (see this line of code), and then are available upon subsequent runs.
For newer versions of pkg-config
(e.g. 1.8.1 in Ubuntu 23.04) the variables ${_prefix}_INCLUDE_DIRS
and ${_prefix}_CFLAGS_OTHER
are set but not saved to the cache (see this line of code), and in subsequent runs they are not set. This leads to build failures, in my example above ${SpiderMonkey_INCLUDE_DIRS}
becomes undefined (empty string).
The behaviour across versions of pkg-config
is inconsistent. This could be fixed in two ways:
- Always cache the variables (see my MR)
- Never cache the variables, and re-populate them each time
pkg_check_modules
is called
Since all variables are already written to the cache except ${_prefix}_INCLUDE_DIRS
and ${_prefix}_CFLAGS_OTHER
, and only when using a new version of pkg-config, option 1 makes more sense I think.
Option 2 is the "correct" way according to the documentation, but right now we also cache ${_prefix}_LIBRARIES
and ${_prefix}_LIBRARY_DIRS
which also shouldn't be done according to the same documentation.