Skip to content

Instantly share code, notes, and snippets.

@Zeex
Last active June 3, 2021 12:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zeex/5291602 to your computer and use it in GitHub Desktop.
Save Zeex/5291602 to your computer and use it in GitHub Desktop.
group_target_sources(target) - group target's source files on based on their directory.
# group_target_sources(target) - group target's source files on based on
# their directory.
function(group_target_sources target)
get_target_property(sources ${target} SOURCES)
foreach(file ${sources})
get_filename_component(path "${file}" ABSOLUTE)
get_filename_component(path "${path}" PATH)
if(file MATCHES "${PROJECT_BINARY_DIR}")
file(RELATIVE_PATH path ${PROJECT_BINARY_DIR} "${path}")
else()
file(RELATIVE_PATH path ${PROJECT_SOURCE_DIR} "${path}")
endif()
string(REGEX REPLACE "/" "\\\\" win_path "${path}")
source_group("${win_path}" REGULAR_EXPRESSION "${path}/[^/\\]+\\..*")
endforeach()
endfunction()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment