CMake 按照文件目录组织VS工程的筛选器
macro(group_src_by_dir src_files) foreach(file_path ${${src_files}}) string(REGEX REPLACE ${CMAKE_CURRENT_SOURCE_DIR}/\(.*\) \\1 relative_path ${file_path}) string(REGEX MATCH ".+\\.c.*" is_match ${relative_path}) if(is_match) string(REGEX REPLACE "(.+)\\.c.*" \\1 cpp_relative_dir ${relative_path}) if(NOT cpp_relative_dir STREQUAL "main") list(APPEND c_groups ${cpp_relative_dir}) endif() endif(is_match) endforeach(file_path) foreach(file_path ${${src_files}}) message(${file_path}) string(REGEX REPLACE ${CMAKE_CURRENT_SOURCE_DIR}/\(.*\) \\1 relative_path ${file_path}) string(REGEX REPLACE "(.+)\\..*" \\1 relative_group ${relative_path}) list(FIND c_groups ${relative_group} index) if(NOT index LESS 0) message(${relative_group}) string(REPLACE "/" "\\" relative_group ${relative_group}) source_group(${relative_group} FILES ${file_path}) else() string(REGEX MATCH "(.+)/.*" has_group ${relative_path}) if(has_group) string(REGEX REPLACE "(.+)/.*" \\1 group ${relative_path}) string(REPLACE "/" "\\" group ${group}) message(${group}) source_group(${group} FILES ${file_path}) else() message("") source_group("" FILES ${file_path}) endif() endif() endforeach(file_path) endmacro(group_src_by_dir)
该函数有两个效果:
1.按照代码文件在磁盘的目录结构来组织VS的筛选器结构
2.如果同一个目录下包含同名的.cpp和.h文件,则会将这两个文件组织到同一个筛选器中.
效果: