How to handle install+target_link_libraries+FetchContent

I am having some trouble understanding how this scenario should be handled.

I have a header only library DepPkg that is downloaded automatically via FetchContent if it is not found on the system. Then my target MyPkg has a target_link_libraries(PUBLIC) against DepPkg becaue it uses #include <DepPkg> in the code. Then how do I correctly handle its installation because as far as I’ve tried when installing MyPkg, DepPkg must also be installed, but that I understand is bad practice because it might overwrite system installed version without it being evident.

Is there a generator expression way to make DepPkg an external target that is known to not be installed? In the it is sufficient to specify find_dependency(DepPkg) right?

You could install the DepPkg dependency in its own install component. Then you can choose at install time whether to install that separate component or not. This should satisfy CMake’s requirement that you install dependencies built by the project.

It is a minimum requirement. If you combine that with my suggestion to install DepPkg as its own component, you should end up where you’re trying to get to.

Unfortunately that will not work because DepPkg is used in the main #include and that one is the only target of the project. But even so, the issue is that when you want to configure the CMakeLists with install enable, the configuration fails with “nothing exports…” Because of the public target_link_libraries .

I was thinking if there is a generator expression way of altering that part of target_link_libraries and maybe some way to convert the dependency targets to an IMPORTED one in order to satisfy install(EXPORT)