# Executing a custom command on modified files.

**URL:** <https://discourse.cmake.org/t/executing-a-custom-command-on-modified-files/3240>\
**Category:** Code\
**Created:** [April 29, 2021, 10:46pm UTC](https://discourse.cmake.org/t/executing-a-custom-command-on-modified-files/3240 "2021-04-29T22:46:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JRR](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@JRR](https://discourse.cmake.org/u/JRR)\
**Post date:** [April 29, 2021, 10:46pm UTC](https://discourse.cmake.org/t/executing-a-custom-command-on-modified-files/3240/1 "2021-04-29T22:46:19Z")

</div>

From what I understand compilation works something like:

1. If the first time compile all files
2. else only compile modified files

I’d like to do something similar but instead of compile I’d like to run our companies style checker. I’d like the logic to be similar

1. If the first time check all files
2. else only check modified files

Is this possible?

I remember doing something like this long ago with classic `makefile` patterns, like:

```auto
%.stvn : %.cpp
	myExe $< -o $@

```

In the beginning there are no .stvn files so it will run `myExe` one by one for each .cpp file creating a subsequent .stvn file.

Later if a .cpp is updated it’s found to be newer and so myExe is executed on just the newer file.

So I get and understand and use this in makefiles.

How can I do something similar in Cmake?

---

<div class="post-metadata">

**Author:** ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)\
**Post date:** [April 30, 2021, 7:26am UTC](https://discourse.cmake.org/t/executing-a-custom-command-on-modified-files/3240/2 "2021-04-30T07:26:41Z")

</div>

Here’s the basic skeleton to get you started:

```auto
add_custom_command(
  OUTPUT myfile1.stvn
  COMMAND myExe myfile1.cpp -o myfile1.stvn
  DEPENDS myfile1.cpp
  VERBATIM
)
add_custom_command(
  OUTPUT myfile2.stvn
  COMMAND myExe myfile2.cpp -o myfile2.stvn
  DEPENDS myfile2.cpp
  VERBATIM
)
add_custom_target(
  RunChecker ALL
  COMMENT "Running Checker where necessary"
  DEPENDS
    ${CMAKE_CURRENT_BINARY_DIR}/myfile1.stvn
    ${CMAKE_CURRENT_BINARY_DIR}/myfile2.stvn
)

```

See docs of [`add_custom_command()`](https://cmake.org/cmake/help/latest/command/add_custom_command.html) and [`add_custom_target()`](https://cmake.org/cmake/help/latest/command/add_custom_target.html) for deeper explanation and for other potential options.

---

<div class="post-metadata">

**Author:** ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)\
**Post date:** [April 30, 2021, 7:55am UTC](https://discourse.cmake.org/t/executing-a-custom-command-on-modified-files/3240/3 "2021-04-30T07:55:39Z")

</div>

You can also use option `DEPFILE` of `add_custom_command` for more fine-grain dependency management.

Be aware that `DEPFILE` is only supported by `Ninja` until `3.19`. `Makefiles` generators were added in `3.20`. And `Xcode` will be available in next release (`3.21`).

---

<div class="post-metadata">

**Author:** ![JRR](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@JRR](https://discourse.cmake.org/u/JRR)\
**Post date:** [April 30, 2021, 3:35pm UTC](https://discourse.cmake.org/t/executing-a-custom-command-on-modified-files/3240/4 "2021-04-30T15:35:10Z")

</div>

Thank you for sharing the skeleton Peter.

It appears that this requires that we need to explicitly add a custom command for each and every file in the product? That CMake doesn’t have an equivalent to the pattern paradigm in make? Am I correct?

---

<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:** [April 30, 2021, 5:59pm UTC](https://discourse.cmake.org/t/executing-a-custom-command-on-modified-files/3240/5 "2021-04-30T17:59:32Z")

</div>

No, there’s no pattern support (because that would still only support the Makefiles generators). I recommend wrapping it up in a CMake function then calling it in a loop.
