Can CMake tell me which Module or Config file is used for find_package()?

Viewed 244

I sometimes find myself wondering which Find<package>.cmake file CMake is actually using. For instance I have two FindOpenMP.cmake files on my system, and I would like to know which one is used – if any – with

find_package(OpenMP)

Can CMake print the location of the module file used to resolve a find_package() call?

I am using CMake 3.18.2.

1 Answers

Config mode

If CMake uses config script (FooConfig.cmake or config-foo.cmake) for process find_package(Foo) request, then it sets:

  1. Foo_DIR cache variable pointed to the directory with the script. You may check this variable outside of the project, in the CMakeCache.txt file created in the build directory.

  2. Foo_CONFIG variable contained absolute path to the script. You may print content of this variable in the CMakeLists.txt after the find_package() call.

Module mode

If CMake uses module script (FindFoo.cmake) for process find_package(Foo) request, then it sets none variable pointed to the script.

But search procedure for such script is quite simple:

  1. CMake firstly checks a directory with modules, shipped with CMake itself.
  2. CMake iterates in order over directories listed in CMAKE_MODULE_PATH CMake variable, and stops after finding the first file with the expected name (FindFoo.cmake).

That is, you may print the value of CMAKE_MODULE_PATH variable, and manually (or by some other scripting) iterate over this value and find the first directory which contains required script.

Related