FetchContent issue with find_package and dependency chains

Hi all,

I’m working on a C++ project using CMake, where I manage all my third-party dependencies using FetchContent exclusively (i.e no other package manager is installed nor run).

I use a combination of FetchContent_Declare and FetchContent_Populate (as outlined in the documentation here), as so far this has worked without any problems for dependencies that are independent from each other, i.e only my own code required them (this includes libraries like SDL2, Dear ImGui, and glm)

Recently, I tried the same approach with msdf-atlas-gen. This library uses FreeType, which in turn uses zlib and libpng. Using the above approach, I added all dependencies, making sure to fetch them in the correct order so each dependency would be ready by the time their dependents were being fetched.

This did not work at first. The fetch succeeded, but the calls to find_package in the CMake scripts of the above libraries could not find their dependencies. I then found this part of the documentation, and using OVERRIDE_FIND_PACKAGE and FetchContent_MakeAvailable, I was able to run the entire CMake script with no apparent issues (i.e no more error messages from find_package)

When I try to build the project, however, anything below zlib in the dependency chain is unable to find the zlib headers, and a similar issue happens with the dependencies that require libpng. When generating with Visual Studio, I checked the vcxproj files generated by CMake, and it seems neither the include directories, nor the libraries themselves were added.

I attached a minimal reproduction of the problem. I primarily use Visual Studio 2022 as my IDE and generator, though I have tested with Ninja as well. Am I missing something? Or is this an inherent limitation of FetchContent, and/or a problem with how the CMake scripts of the dependencies are configured?
FetchContentTest.zip (3.0 KB)

If you are unable to solve the problem of FetchContent, you could alternatively try superbuild, see https://gitlab.kitware.com/paraview/common-superbuild

We’re managing a reasonably complex build with several third party interdependencies with it. And ParaView is also built with it of course.

A number of CMakeLists.txt still rely on <PACKAGE>_INCLUDE_DIRS and <PACKAGE>_LIBRARIES instead of using targets, these are set by the find-module or config but not by the project it self so they are missing here → empty var = ‘’

Looking at freetype/CMakeLists.txt at master · freetype/freetype · GitHub this is the case. You will have to set(ZLIB_LIBRARIES zlib) manually (adding and using a zlib::zlib alias would probably also be good). You can ignore the include dirs as that will be handled by the target.

p.s. If projects are using namespaced targets that are not also created in the project cmake (e.g. zlib::zlib) you will have to add those too.