sometimes find_package() does not look for files in passed PATHS parameter

Hello,

We’ve a repository running for a long while, recently, without any related changes, a spurious cmake error happened, only on some build environments.

The environment is Ubuntu 18.04. CMake version is 3.23.0 or 3.23.3. Compiler is GCC 7.5 or 9.

We have a toolchain file, for both x86_64 build and arm64 build, where it sets:

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH) # host and install
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

When run cmake configure, we pass -DCMAKE_FIND_ROOT_PATH=<cmake_install_prefix_value> as every module build its package and install for later module to use.

We use Poco library from here:
https:github.com/pocoproject/poco/blob/devel/cmake/PocoConfig.cmake.in

And the PocoConfig.cmake.in file content is exactly the same as the one we stored in our repo.

The call to use the Poco package is as follows:

find_package(Poco REQUIRED Foundation)

Occasionally, when building either x86_64 natively or crossing building for ARM64, this error would happen:

CMake Error at .../install/lib/cmake/Poco/PocoConfig.cmake:30 (find_package):
  Could not find a package configuration file provided by "PocoFoundation"
  with any of the following names:

    PocoFoundationConfig.cmake
    pocofoundation-config.cmake

  Add the installation prefix of "PocoFoundation" to CMAKE_PREFIX_PATH or set
  "PocoFoundation_DIR" to a directory containing one of the above files.  If
  "PocoFoundation" provides a separate development package or SDK, be sure it
  has been installed.
Call Stack (most recent call first):
  CMakeLists.txt:19 (find_package)

The relevant part from PocoConfig.cmake is as follows:

get_filename_component(_Poco_install_prefix "${CMAKE_CURRENT_LIST_DIR}" ABSOLUTE)

set(_Poco_NOTFOUND_MESSAGE)

# Let components find each other, but don't overwrite CMAKE_PREFIX_PATH
set(_Poco_CMAKE_PREFIX_PATH_old ${CMAKE_PREFIX_PATH})
set(CMAKE_PREFIX_PATH ${_Poco_install_prefix})

foreach(module ${Poco_FIND_COMPONENTS})
    find_package(Poco${module}
        ${_Poco_FIND_PARTS_QUIET}
        ${_Poco_FIND_PARTS_REQUIRED}
        PATHS "${_Poco_install_prefix}" NO_DEFAULT_PATH
    )
  # More omitted...

The overall PocoConfig.cmake and component file PocoFoundationConfig.cmake are at the same path.
PocoConfig.cmake tries to set up some paths (like CMAKE_PREFIX_PATH), and also provide the parameter PATHS in the find_package() call with intention to let cmake find the PocoFoundationConfig.cmake file at the same path.

But on some build environment that I just can’t figure out the actual difference, above error would occur, and when in such environment this error happens, it can be reproduced always.

As the error happens, I tried the following actions, it can fix the issue:

  1. Inside PocoConfig.cmake, add set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE NEVER)
  2. Inside PocoConfig.cmake, add unset(CMAKE_FIND_ROOT_PATH CACHE)
  3. In toolchain file, change ONLY to BOTH.

It seems that either of above options can help to fix the issue.

My question is:
Under what circumstances, the PATHS parameter passed to find_package() is not used at all?

Because without above said “fix”, the build has been running for a long time under many people’s daily work environment and on Jenkins. But yesterday suddenly this error happened on two people’s work environment, I hope to find out the actual root cause of this failure.

Thanks!

Does passing --debug-find to the configure step give more context?

Thanks for the hint. I don’t have the reproducing environment for now. I will try this flag to see if there is more info.