cmakecmake-custom-command

add_custom_command on xml files


our system uses some xml files to generate some code. So I've created a custom command to scan and parse these files, and generate stuff as accorded. This is what I did:

file(GLOB BEAN_XML_FILES "../../*.xml")

add_custom_command(TARGET communication PRE_BUILD
                   COMMAND python
                   ARGS bean_maker.py --input-directory ${SOURCE_DIR}/docs/beans --output-directory ${SOURCE_DIR}/beans
                   WORKING_DIRECTORY ${SOURCE_DIR}/tools/bean_maker
                   COMMENT "Running bean maker..."
                   DEPENDS ${BEAN_XML_FILES})

The problem is that add_custom_command only runs when I run cmake, even when I modified some xml inside the folder.

How could I do this to run when changes are made to the files?


Solution

  • Use the add_custom_command signature for adding a custom command to produce output files.

    add_custom_command(OUTPUT ${GENERATED_SOURCE_FILES}
                       COMMAND command1 [ARGS] [args1...]
                       DEPENDS ${BEAN_XML_FILES})
    

    At build time, the command will execute if the generated files are older than the files they depend on.