Skip to content

Instantly share code, notes, and snippets.

@bit-hack
Last active July 23, 2017 23:02
Show Gist options
  • Save bit-hack/6368a85e79894d38724692aad92e4221 to your computer and use it in GitHub Desktop.
Save bit-hack/6368a85e79894d38724692aad92e4221 to your computer and use it in GitHub Desktop.
Nice Recursive CMake globbing
project(residualvm)
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
# recursively glob files
#
file(GLOB_RECURSE _FILES "*.cpp" "*.h")
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
# file exclusion step
#
set(_EXCLUDE "example.cpp")
foreach(_FILE ${_FILES})
get_filename_component(_NAME ${_FILE} NAME)
if (${_NAME} IN_LIST _EXCLUDE)
list(REMOVE_ITEM _FILES ${_FILE})
endif()
endforeach(_FILE)
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
# set the source grouping based on directory path
#
foreach(_FILE ${_FILES})
file(RELATIVE_PATH _REL ${CMAKE_CURRENT_SOURCE_DIR} ${_FILE})
get_filename_component(_FOLDER ${_REL} DIRECTORY)
if (_FOLDER)
string(REPLACE "/" "\\" _FOLDER ${_FOLDER})
source_group(${_FOLDER} FILES ${_FILE})
else()
source_group("" FILES ${_FILE})
endif()
endforeach(_FILE)
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
# create project for globbed files
#
get_filename_component(PROJNAME ${CMAKE_CURRENT_SOURCE_DIR} NAME)
add_library(${PROJNAME} ${_FILES})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment