Possible to match Xcode source groups to folder structure?

I’m trying to keep my source code organized in folders, but since I migrated my project to CMake I just get all the headers and source files in separate but completely flat source groups.

Is there a way to make it look more like the old Projucer builds, ie a Source tree that reflects the physical directory structure? And also preferably with the source and header files together like before.

I tried a lot of variations with source_group(TREE “${CMAKE_SOURCE_DIR}/Source” FILES …) etc, but none of it seems to make any difference. I’m not that familiar with CMake yet, so I could be doing it totally wrong. But if anyone has a working example of this it would be really helpful!

I use this and have all project sources in a subfolder called Source

# a tree of sources for project sources
get_target_property(SOURCES_SRC ${CMAKE_PROJECT_NAME} SOURCES)
list(FILTER SOURCES_SRC INCLUDE REGEX "Source/*")
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/Source PREFIX "Source" FILES ${SOURCES_SRC})

# Move the INTERFACE auto-created JUCE library stuff into its own folder
source_group("JUCE Library Code" 
    FILES 
        build/${PROJECT_NAME}_artefacts/JuceLibraryCode/JuceHeader.h 
    REGULAR_EXPRESSION 
        "juce_"
)

Maybe it needs to be in right spot to work. I have it before target_compile_definitions(), after all files have been added.

Yes, this did the trick – Thank you!

I think my mistake was that I was trying to group the folders as I went along (I have a hierarchy of CMakeLists.txt), instead of creating the groups after adding all the files.