# How to run \`add\_custom\_target\` when source file changes

**URL:** https://discourse.cmake.org/t/how-to-run-add-custom-target-when-source-file-changes/13763
**Category:** Code
**Created:** [March 17, 2025, 11:13am UTC](https://discourse.cmake.org/t/how-to-run-add-custom-target-when-source-file-changes/13763 "2025-03-17T11:13:44Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![buildtool](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/7bcc69/32.png) [@buildtool](https://discourse.cmake.org/u/buildtool)
#### Post date: [March 17, 2025, 11:13am UTC](https://discourse.cmake.org/t/how-to-run-add-custom-target-when-source-file-changes/13763/1 "2025-03-17T11:13:44Z")

</div>

How do I ensure that when a source file changes, the custom target with command is re-run?

This is what I’m using

```cmake
add_custom_target("gen_code"
    COMMAND run.py ${INPUT_FILE} -o ${OUTPUT_FILE}
    SOURCES ${INPUT_FILE}
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    DEPENDS ${INPUT_FILE}
    BYPRODUCTS ${OUTPUT_FILE}
    # OUTPUT ${OUTPUT_FILE}
)

```

This does not re-run the command when the source file changes. Using the `OUTPUT` does not work either. What gives?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [March 17, 2025, 12:41pm UTC](https://discourse.cmake.org/t/how-to-run-add-custom-target-when-source-file-changes/13763/2 "2025-03-17T12:41:17Z")

</div>

You should really use `add_custom_command` for commands with inputs and outputs. You can then have a nice `gen_code` custom target that depends on its outputs.
