0

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?

3
  • The file names are not cached. The command is expanded to COMMAND /usr/bin/echo a-file.txt. Re-run cmake. Commented Aug 28 at 2:27
  • You may use shell wildcards when form COMMAND: COMMAND /usr/bin/echo ${CMAKE_CURRENT_SOURCE_DIR}/*.txt. With file(GLOB_RECURSE) you may use CONFIGURE_DEPENDS option which triggers cmake re-running whenever content of the directory changes; that way variable myTxtFiles will be actualized every build. Note, however, that option CONFIGURE_DEPENDS works only for selective set of generators. Commented Aug 28 at 5:17
  • This Answer suggests using CONFIGURE_DEPENDS within the file glob, 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. Commented Aug 28 at 19:28

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.