[CMake] [Config-file Package] The best practice of finding package dependencies?

P F pfultz2 at yahoo.com
Thu Jun 22 06:21:53 EDT 2017


> On Jun 20, 2017, at 10:46 PM, Konstantin Podsvirov <konstantin at podsvirov.pro> wrote:
> 
> Hello community!
> 
> I want to give a little discussion :)
> 
> If I export a package containing several targets.
> And these target are publicly linked to the targets imported from another package.
> Where is the best place to look for package dependencies?
> 
> I assume the following options:
> - In the Config-file of the package itself;

I believe this is the best practice, and it is why the `find_dependency` macro exists. 

> - The user himself must find them in his project.
> 
> It seems to me that to force the user to look for the dependencies of the package, which he himself seeks there is bad tone, but also to impose on the user, then how I myself found these dependencies in Config-file may not appeal to everyone.
> 
> Perhaps there should be some option for setting the behavior of the `find_package` command in the part of finding package dependencies? Or she already is, but I have not yet studied it.

The user can either use `<package>_DIR` to tell cmake exactly where the package is, or they can call `find_package(…)` beforehand. However, if the user wants something completely different then they would either need to provide a Find module or override `find_package`. 

For example, this is common for projects that build their dependencies in the same project:

set(as_subproject Foo)
macro(find_package)
  if(NOT "${ARG0}" IN_LIST as_subproject)
    _find_package(${ARGV})
  endif()
endmacro()
add_subdirectory(Foo)
add_subdirectory(App)

This makes `find_package(Foo)` do nothing since the target `Foo::Foo` would be part of the project.




More information about the CMake mailing list