'CMake - Install Find script for depencency together with script

I am making a CMake library around some installable SDK. So the dependency tree looks like:

Application   -->   MyLibrary   -->   OfficialSDK

This SDK is installed by some setup.exe and does not have a CMake module.
So instead I include a custom find script inside MyLibrary: MyLibrary/cmake/FindOfficialSDK.cmake. Then inside the CMakeLists.txt of MyLibrary I can use find_package(OfficialSDK).

This works well for MyLibrary. I can build it and install it, together with the CMake export. So then Application can run find_package(MyLibrary) out of the box, since MyLibrary was installed properly using CMake.

However, when configuring Application I get an error:

Target "Application" links to target "OfficialSDK" but the target was not found.

Okay, so MyLibrary remembers it needs OfficialSDK, but it cannot find it in this CMake project.
I could solve this by including cmake/FindOfficialSDK.cmake in Application, but I would rather not make my users to copy the find script in case I need to update it in the future.

Is there some way of including the imported target OfficialSDK and install it together with MyLibrary, so Application doesn't need to search for it?



Solution 1:[1]

I found a solution, largely based on https://discourse.cmake.org/t/install-findpackage-script/5307.

Another example I found inside Pagmo2, for a custom FindBoost script: https://github.com/esa/pagmo2/blob/master/pagmo-config.cmake.in#L10

In a nutshell, I've added the following:

  • Added an install for my custom find script: install(FILE ${CMAKE_CURRENT_DIR}/cmake/FindOfficialSDK ...)
  • Added a custom *-config.cmake.in script that will be installed, aside from the more automatic *-targets.cmake export
  • Added an explicit find_dependency(OfficialSDK) (not find_package) inside this config script, but inside a clause that adds the MyLibrary cmake install directory to the CMake module path, so the custom find script is used.

A complete example can be seen this PR: https://github.com/ET-BE/AdsClient/pull/1/files (as well as a bunch of other stuff). The OfficialSDK is TwinCAT's ADS library.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1