# Rerun custom command if project was reconfigured

**URL:** <https://discourse.cmake.org/t/rerun-custom-command-if-project-was-reconfigured/3328>\
**Category:** Code\
**Created:** [May 14, 2021, 7:00pm UTC](https://discourse.cmake.org/t/rerun-custom-command-if-project-was-reconfigured/3328 "2021-05-14T19:00:05Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Person-93](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/7bcc69/32.png) [@Person-93](https://discourse.cmake.org/u/Person-93)\
**Post date:** [May 14, 2021, 7:00pm UTC](https://discourse.cmake.org/t/rerun-custom-command-if-project-was-reconfigured/3328/1 "2021-05-14T19:00:05Z")

</div>

I want a custom command to be considered out of date if the project was reconfigured. My current solution is to touch a specific file in the build tree and make the command depend on it, but I get a warning that byproducts have to be listed explicitly.  
What is the correct way to do this?

---

<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:** [May 14, 2021, 7:06pm UTC](https://discourse.cmake.org/t/rerun-custom-command-if-project-was-reconfigured/3328/2 "2021-05-14T19:06:37Z")

</div>

You might be able to depend on `CMakeCache.txt` instead?

---

<div class="post-metadata">

**Author:** ![Person-93](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/7bcc69/32.png) [@Person-93](https://discourse.cmake.org/u/Person-93)\
**Post date:** [May 14, 2021, 7:22pm UTC](https://discourse.cmake.org/t/rerun-custom-command-if-project-was-reconfigured/3328/3 "2021-05-14T19:22:50Z")

</div>

That only re-runs it if cache variables were changed.

---

<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:** [May 14, 2021, 8:05pm UTC](https://discourse.cmake.org/t/rerun-custom-command-if-project-was-reconfigured/3328/4 "2021-05-14T20:05:43Z")

</div>

That file is unconditionally written when CMake runs. I don’t know if that’s guaranteed, but it is the behavior of today.

---

<div class="post-metadata">

**Author:** ![ferdnyc](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ferdnyc/32/267_2.png) [@ferdnyc](https://discourse.cmake.org/u/ferdnyc)\
**Post date:** [May 15, 2021, 1:10am UTC](https://discourse.cmake.org/t/rerun-custom-command-if-project-was-reconfigured/3328/5 "2021-05-15T01:10:45Z")

</div>

Another option might be to _delete_ an output file whenever CMake is run, and include rules for regenerating it as part of your custom command(s). e.g.

```cmake
set(marker ${CMAKE_CURRENT_BINARY_DIR}/.rebuilt)
add_custom_command(
  OUTPUT ${marker}
  COMMAND
    ${CMAKE_COMMAND} -E touch ${marker}
)
add_custom_command(
  OUTPUT ${build_file}
  COMMAND
    ${command_to_generate_build_file}
  DEPENDS ${marker}
)
file(REMOVE ${marker})

```

Untested, but that _should_ ensure that `${command_to_generate_build_file}` gets rerun for sure anytime the project is reconfigured, because the build will have to re-create the `.rebuilt` file it depends on. And there are no undeclared byproducts to worry about, so that shouldn’t be an issue.

---

<div class="post-metadata">

**Author:** ![Person-93](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/7bcc69/32.png) [@Person-93](https://discourse.cmake.org/u/Person-93)\
**Post date:** [May 15, 2021, 1:41pm UTC](https://discourse.cmake.org/t/rerun-custom-command-if-project-was-reconfigured/3328/6 "2021-05-15T13:41:53Z")

</div>

I double-checked. The timestamp on the file is not updated and the target doesn’t re-run.

---

<div class="post-metadata">

**Author:** ![Person-93](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/7bcc69/32.png) [@Person-93](https://discourse.cmake.org/u/Person-93)\
**Post date:** [May 15, 2021, 1:43pm UTC](https://discourse.cmake.org/t/rerun-custom-command-if-project-was-reconfigured/3328/7 "2021-05-15T13:43:11Z")

</div>

I ended up using a file generated using the configure\_file command. It seems that such files are recognized as byproducts of cmake itself.

---

<div class="post-metadata">

**Author:** ![ferdnyc](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ferdnyc/32/267_2.png) [@ferdnyc](https://discourse.cmake.org/u/ferdnyc)\
**Post date:** [May 15, 2021, 1:47pm UTC](https://discourse.cmake.org/t/rerun-custom-command-if-project-was-reconfigured/3328/8 "2021-05-15T13:47:22Z")

</div>

They are, but note that you may have the same troubles since, according to the `configure_file()` docs…:

> The generated file is modified and its timestamp updated on subsequent  
> cmake runs only if its content is changed.

---

<div class="post-metadata">

**Author:** ![ferdnyc](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ferdnyc/32/267_2.png) [@ferdnyc](https://discourse.cmake.org/u/ferdnyc)\
**Post date:** [May 15, 2021, 1:53pm UTC](https://discourse.cmake.org/t/rerun-custom-command-if-project-was-reconfigured/3328/9 "2021-05-15T13:53:57Z")

</div>

> [@Person-93](#):
>
> I double-checked. The timestamp on the file is not updated and the target doesn’t re-run.

Surprisingly enough I’m seeing the same thing, with CMake 3.19.7 on Linux x86\_64. When `build` is already configured and nothing has changed, a subsequent `cmake -B build -S .` leaves the `build/CMakeCache.txt` timestamp untouched. (But there really has to be _nothing_ changed.)

…Of course, if **nothing’s** changed, it’s difficult to see what kind of imperative there would be to force re-running of a custom command. Unless it’s being used for un-accounted-for side effects, in which case it would arguably be useful to instead account for those explicitly as dependencies in the build config. 😉

---

<div class="post-metadata">

**Author:** ![Person-93](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/7bcc69/32.png) [@Person-93](https://discourse.cmake.org/u/Person-93)\
**Post date:** [May 15, 2021, 2:10pm UTC](https://discourse.cmake.org/t/rerun-custom-command-if-project-was-reconfigured/3328/10 "2021-05-15T14:10:24Z")

</div>

I figured out a way to put the info that I really care about into the configured file.
