We use a third-party library with a lot of headers which a good amount of libraries in our codebase have a transitive dependency on (unfortunately). However, because the third-party library’s headers are INTERFACE sources, this makes the 200 or so headers from the third-party library show up in every downstream target’s “Header Files” folder in VS Solutions.
We’ve made sure SYSTEM was set to TRUE in the third-party’s target settings & looked at setting the source_group of all of the files to _hidden to collapse them at the very least, but neither help. SYSTEM is ignored when duplicating the 200 headers across every downstream target’s “Header Files” folder, and source_group is specific to the directory it’s called in so it doesn’t apply to the downstream targets.
How can we prevent these sources from being duplicated to every downstream target’s “Header Files” folder?
Since CMake 3.19, INTERFACE libraries can have sources specified in the call to add_library(), or sources can be added as PRIVATE with target_sources(). That will associate those headers with the INTERFACE library but not force them as transitive dependencies on anything linking to that INTERFACE library.
If you can convince the maintainers of the third party library to add the sources as described above (possibly behind a check for the CMake version), that’s the best outcome. If that isn’t an option, a workaround might be to read the INTERFACE_SOURCES property of that third party library target, copy that to the target’s SOURCES property, and then set its INTERFACE_SOURCES property to an empty string. I think that’s going to give you the same end result as if the third party maintainer fixed their target, but you’d need to try it to confirm.