How to easily add CMake files to be visibile in IDE generators (Visual Studio)?

Throughout my code base I have this re-ocurring pattern to provide friendly Visual Studio Support.

target_sources(foo PRIVATE
        # Add CMakeLists.txt so it shows up in Visual Studio generated projects
	CMakeLists.txt
	...
)

I even do this for my custom modules:

target_sources(foo PRIVATE
        # Add modules so they shows up in Visual Studio generated projects
	foo.cmake
	...
)

Is there an easy, good, fast way to do this? Adding them manually as shown above does work but is tedious and error prone.

I tried globbing but our project is pretty big and that adds 10 seconds to the configuration time.

I think how you’ve described it is how I’d do it. If you wrapped add_library, you could probably do this in such a wrapper.

FWIW, you don’t have to add them extra via target_sources. You can just list them directly in the add_library/add_executable command. All our projects at work do it this way, and it works just fine.

Interesting, we do not add them but they are automagically added to the targets in the generated Visual Studio solution. But I guess it only works when you define a target in a CMakeLists.txt.

The CMakeLists.txt for a target should be added automatically, but this is only the one for the directory owning the add_library or add_executable call. If you gather sources in subdirectories or have helper files, they’d need manually listed.