WHOLE_ARCHIVE with transitive Dependencies

After reading the discussion in https://gitlab.kitware.com/cmake/cmake/-/issues/24504 I decided to post some follow up question here.

The mentioned discussion talks about cyclic dependencies but I actually have an issue with normal transitive dependencies. Let’s say we have static libraries A, U and an executable T.

A which “links” against U
T which links against U A

A of course only “links” by using target_link_libraries to get the include paths from U.

When I now specify to link against U with the WHOLE_ARCHIVE option (using the override in my case) the final linker command will link against U twice, both times with the whole-archive option. This results in a multiple definition error.

Is there a solution to this?

P.S. The background of using WHOLE_ARCHIVE at all is that T is a test executable for testing the library U. To generate coverage data (uncovered) also for unused object files I want to include the full library.

@marc.chevrier @brad.king Maybe we want to dedupe libraries like Xcode 15 wants for WHOLE_ARCHIVE as well?

As I already explain in issue #24504, for now, because CMake do not have any knowledge about semantics attached to features provided by $<LINK_LIBRARY> genex, it is not possible to do any specific treatment.

And I strongly disagree to start to hack the code to resolve a specific problem. The only valid and long-term solution is to elaborate some way to attach semantics to the features, generic enough to be useful for any feature (and usable by users which can introduce features as well).

Thank you for the replies @ben.boeckel and @marc.chevrier . I fully agree that a “hack” for this situation is not what we want. But still I think from user’s perspective it is quite unexpected that using the WHOLE_ARCHIVE option causes linking errors due to the library being linked twice.