Using cmake, I have a add_custom_target statement and I would like some of the arguments passed to the COMMAND to be dynamic or "fresh" (not cached by cmake).
For example, given
file(GLOB_RECURSE myTxtFiles "*.txt")
add_custom_target(
echo_files
COMMAND /usr/bin/echo ${myTxtFiles}
)
Given a file a-file.txt, then the file glob matches file a-file.txt.
A call to cmake -t echo_files will print the files matching the glob. In this case it prints
a-file.txt
However, if I add a new file, another-file.txt, then the printed output of cmake -t echo_files does not change.
The problem is myTxtFiles is set once during the first run of cmake and then the value of myTxtFiles is cached. Later, if a new .txt file is created, it will not be reflected in myTxtFiles.
How can I force the arguments passed to COMMAND /usr/bin/echo to be dynamic or refreshed?
COMMAND /usr/bin/echo a-file.txt. Re-run cmake.COMMAND /usr/bin/echo ${CMAKE_CURRENT_SOURCE_DIR}/*.txt. Withfile(GLOB_RECURSE)you may use CONFIGURE_DEPENDS option which triggerscmakere-running whenever content of the directory changes; that way variablemyTxtFileswill be actualized every build. Note, however, that option CONFIGURE_DEPENDS works only for selective set of generators.CONFIGURE_DEPENDSwithin thefileglob, i.e.file(GLOB myTxtFiles CONFIGURE_DEPENDS "*.txt"). Unfortunately this Question was marked as DUPLICATE so I cannot add that Answer here. But looked at the linked Answer.