Created
February 27, 2021 21:43
-
-
Save rpav/77c06df8b8cdc80d685073801482a9cd to your computer and use it in GitHub Desktop.
If you want cmake GLOB changes to automatically rebuild
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Is this worth the time it took to write? I doubt it. | |
# | |
# Usage: | |
# | |
# include(autoglob) | |
# | |
# autoglob(MY_GLOB [RECURSE] | |
# *.cpp | |
# *.h) | |
# | |
# add_executable(myexe ${MY_GLOB}) | |
# add_dependencies(myexe MY_GLOB_autoglob) | |
# | |
# Probably not perfect, I haven't used it much. | |
# | |
set(AUTOGLOB_SCRIPT_PATH ${CMAKE_CURRENT_LIST_FILE}) | |
function(autoglob var) | |
set(ag_mode GLOB) | |
cmake_parse_arguments(ag "RECURSE" "" "" ${ARGN}) | |
if(ag_RECURSE) | |
set(ag_mode GLOB_RECURSE) | |
endif() | |
file(${ag_mode} ${var} ${ARGN}) | |
set(glob "${${var}}") | |
set(${var} "${${var}}" PARENT_SCOPE) | |
set(glob_file "${CMAKE_CURRENT_BINARY_DIR}/${var}_glob") | |
set(files_file "${CMAKE_CURRENT_BINARY_DIR}/${var}_files") | |
set(stamp_file "${CMAKE_CURRENT_BINARY_DIR}/${var}_stamp") | |
file(WRITE "${glob_file}" "${ARGN}") | |
file(WRITE "${files_file}" "${glob}") | |
if(NOT EXISTS "${stamp_file}") | |
file(WRITE "${stamp_file}" "") | |
endif() | |
add_custom_target(${var}_autoglob | |
COMMAND ${CMAKE_COMMAND} -DAUTOGLOB_CHECK=${var} -DAUTOGLOB_MODE=${ag_mode} -DTOPLEVEL=${CMAKE_BINARY_DIR} -DAUTOGLOB_PATH=${CMAKE_CURRENT_BINARY_DIR} -P ${AUTOGLOB_SCRIPT_PATH} | |
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} | |
) | |
include(${stamp_file}) | |
endfunction(autoglob) | |
function(autoglob_check var) | |
set(ag_mode ${AUTOGLOB_MODE}) | |
set(glob_file "${AUTOGLOB_PATH}/${var}_glob") | |
set(files_file "${AUTOGLOB_PATH}/${var}_files") | |
set(stamp_file "${CMAKE_CURRENT_BINARY_DIR}/${var}_stamp") | |
if(EXISTS ${glob_file}) | |
file(READ ${glob_file} glob) | |
file(${ag_mode} files ${glob}) | |
endif() | |
if(EXISTS ${files_file}) | |
file(SHA1 "${files_file}" hash) | |
endif() | |
string(SHA1 newhash "${files}") | |
if(NOT "${hash}" STREQUAL "${newhash}") | |
file(WRITE "${stamp_file}" "") | |
execute_process(COMMAND ${CMAKE_COMMAND} . -B . WORKING_DIRECTORY ${TOPLEVEL}) | |
endif() | |
endfunction() | |
if(AUTOGLOB_CHECK) | |
autoglob_check(${AUTOGLOB_CHECK}) | |
endif() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment