I have following structure
Main (dir)
+-- CMakeLists.txt
+-- File.cpp
+-- File.hpp
+-- Dir (dir)
+-- CMakeLists.txt
I know that using the CMAKE glob function is usually frowned upon: Why is CMAKE glob evil, but in my case I found it to better than explicitly naming each file. I figured I would include a modified version of Florian's answer using GLOB.
# This code sorts the project files as they appear in the root directory
# Generate a list of all .c & .h files in the current directory and sub directores.
file(
GLOB_RECURSE source_list RELATIVE
"${CMAKE_CURRENT_SOURCE_DIR}"
*.c *.h
)
foreach(source IN LISTS source_list)
get_filename_component(source_path "${source}" PATH)
string(REPLACE "/" "\\" source_path_msvc "${source_path}")
source_group("${source_path_msvc}" FILES "${source}")
endforeach()
message(STATUS "Tree reorganized")