Skip to content

Proposal for TREE [and PREFIX] arguments in the source_group command

stryku requested to merge stryku/cmake:source_group-tree into master

Hello again,

I've created small proposal for new argument in source_group command. The new argument would be 'TREE [with optional PREFIX]'.

The goal is to provide more user-friendly syntax for creating source groups which matches actual directories tree where user have his source files.

Its syntax looks like this source_group(TREE ${root} FILES ${source_files}) or source_group(TREE ${root} PREFIX some/prefix FILES ${source_files})

Motivation: Lets say that user have source files organized in such way:

${CMAKE_CURRENT_LIST_DIR}/sources/src/main.cpp
${CMAKE_CURRENT_LIST_DIR}/sources/src/foo/bar.cpp
${CMAKE_CURRENT_LIST_DIR}/sources/src/foo/baz.cpp
${CMAKE_CURRENT_LIST_DIR}/sources/src/foo/fooer/barer.cpp

${CMAKE_CURRENT_LIST_DIR}/sources/inc/foo/bar.hpp
${CMAKE_CURRENT_LIST_DIR}/sources/inc/foo/baz.hpp
${CMAKE_CURRENT_LIST_DIR}/sources/inc/foo/fooer/barer.hpp

and cmake variables set like this

set(root ${CMAKE_CURRENT_LIST_DIR}/sources)

set(inc_files   ${root}/inc/foo/bar.hpp
                     ${root}/inc/foo/baz.hpp
                     ${root}/inc/foo/fooer/barer.hpp)

set(src_files   ${root}/src/main.cpp
                     ${root}/src/foo/bar.cpp
                     ${root}/src/foo/baz.cpp
                     ${root}/src/foo/fooer/barer.cpp)

and now, when user will call source_group like this

source_group(TREE ${root} FILES ${src_files} ${inc_files})

cmake will create such source groups

inc
inc/foo/
inc/foo/fooer
src
src/foo
src/foo/fooer

which will contain files respectively to its location on filesystem.

PREFIX argument is optional and can be useful in case when user have such files

${CMAKE_CURRENT_LIST_DIR}/src/main.cpp
${CMAKE_CURRENT_LIST_DIR}/src/foo/bar.cpp
${CMAKE_CURRENT_LIST_DIR}/src/foo/baz.cpp
${CMAKE_CURRENT_LIST_DIR}/src/foo/fooer/barer.cpp

${CMAKE_CURRENT_LIST_DIR}/inc/foo/bar.hpp
${CMAKE_CURRENT_LIST_DIR}/inc/foo/baz.hpp
${CMAKE_CURRENT_LIST_DIR}/inc/foo/fooer/barer.hpp

but don't want to have src and inc group directly in root filter. Then he can add prefix e.g. sources and inc and src groups will be created in sources filter, not root.

whole command call would look like this: source_group(TREE ${CMAKE_CURRENT_LIST_DIR} PREFIX sources FILES ${src_files} ${inc_files}) and such groups would be created:

sources/inc
sources/inc/foo/
sources/inc/foo/fooer
sources/src
sources/src/foo
sources/src/foo/fooer

Of course my implementation is backward-compatybile.

It's not tested yet. I decided to send this mail first to get feedback if it's the thing. If you'll like this, I'll write tests apply reviews feedback and update documentation.

So if anyone have something to point, add, reject feel free (:

Merge request reports