source_groups() should be able to work with SOURCES property
My understanding of source_groups()
in v3.11 of CMake is that the source files to group must be explicitly specified to this command. However, I personally believe that it should be able to take source files from the SOURCES
target property. For example, any source files added to my target should be automatically grouped using source_groups(TREE)
. So maybe add an option to it to tell it to use sources from a target:
add_executable(foo source/one/one.cpp source/two/two.cpp source/three/three.cpp)
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} TARGET foo)
The TARGET foo
parameters to source_group()
would tell CMake to read the SOURCES
property from the specified target to obtain the list of source files it requires.
The reason this change is important, IMHO, is it allows avoiding usage of variables to define targets. For example, to get the desired behavior today as I have described above, I would have to do something like this:
set(source_files source/one/one.cpp source/two/two.cpp source/three/three.cpp)
add_executable(foo ${source_files})
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${source_files})
And when these examples get complex enough, you end up creating helper functions to define targets to automate some of these things which ends up making usage of CMake more complex. I like avoiding variables in CMake in favor of properties and commands.