thanks to this answer regarding source_group and this answer regarding target_sources. I really cleaned up a lot of the way I managed my code base with calling nested CMakeLists.txt files rather than <subdirectory_name>.cmake
files. However, I wanted to know if there was a way to further filter source_group(TREE ...)
?
As I have it set up right now I have the source_group
filters at the root, or parent, CMakeLists.txt file and I have a giant mess of the following:
source_group("subdirectory_name/Header Files" FILES ${libName_subdirectory_name_HEADER_FILES})
source_group("subdirectory_name/Source Files" FILES ${libName_subdirectory_name_SOURCES})
And I have at least 21 subdirectories in this current library. And some of these subdirectories have subdirectories within them. I would like to keep the same pattern if I can since the rest of my project is already set up like this. So it was really nice to know that I could do:
get_target_property(libName_SOURCES libName SOURCES)
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIRECTORY} FILES ${libName_SOURCES})
And it neatly organizes the subdirectories with their sources and header files. However, I would like to further filter that so the headers and files are not combined in the same folder.
Right now, the Visual Studio Project looks like this:
libName
-- subDir1
-- header.hpp
-- source.cpp
-- subDir2
-- header.hpp
-- header.cpp
What I want to achieve:
libName
-- subDir1
-- Header Files
-- header.hpp
-- Source Files
-- source.cpp
-- subDir2
-- Header Files
-- header.hpp
-- Source Files
-- source.cpp
Is there a way to accomplish this with source_group(TREE
? or do I have to create a custom source_group
in a for loop that will parse everything in a way that I would like it?
Here is a small example of what my project currently looks like:
cmake_minimum_reqiured(VERSION 3.18)
project(myLib)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
add_library(myLib SHARED)
add_subdirectory(subDir1)
add_subdirectory(subDir2)
get_target_property(myLib_SOURCES myLib SOURCES)
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${myLib_SOURCES})
set_target_properties(myLib PROPERTIES FOLDER "Lib")
subDir1/2 CMakeLists.txt file:
target_sources(myLib PUBLIC header.hpp source.cpp)
Thanks!