# add\_custom\_command rebuilds every time

**URL:** https://discourse.cmake.org/t/add-custom-command-rebuilds-every-time/8213
**Category:** Usage
**Created:** [May 25, 2023, 9:40am UTC](https://discourse.cmake.org/t/add-custom-command-rebuilds-every-time/8213 "2023-05-25T09:40:35Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Pospelove](https://discourse.cmake.org/user_avatar/discourse.cmake.org/pospelove/32/2712_2.png) [@Pospelove](https://discourse.cmake.org/u/Pospelove)
#### Post date: [May 25, 2023, 9:40am UTC](https://discourse.cmake.org/t/add-custom-command-rebuilds-every-time/8213/1 "2023-05-25T09:40:35Z")

</div>

Dear CMake forums folks,

I run into issues with add\_custom\_command. I’d kindly appreciate if you give me a direction.

The issue is that **the target rebuilds every time, regardless of dependencies**.

Expected behavior, **the target rebuilds only if dependency files were modified**

> <https://github.com/skyrim-multiplayer/skymp/blob/main/skymp5-front/CMakeLists.txt#L21>

```auto
# TODO: consider fetching list of outputs from ExpectedDistContents.json instead of hardcoding
set(out
  ${CMAKE_BINARY_DIR}/dist/server/data/ui/build.js
  ${CMAKE_BINARY_DIR}/dist/server/data/ui/index.html
)
add_custom_command(
  OUTPUT ${out}
  COMMAND yarn --cwd "\"${CMAKE_CURRENT_LIST_DIR}\"" build
  DEPENDS ${sources}
)
add_custom_target(skymp5-front ALL
  DEPENDS ${out}
  SOURCES ${sources}
)

```

---

<div class="post-metadata">

### Author: ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)
#### Post date: [May 25, 2023, 10:21pm UTC](https://discourse.cmake.org/t/add-custom-command-rebuilds-every-time/8213/2 "2023-05-25T22:21:09Z")

</div>

Try to use Ninja with `-d explain`, so it should tell you why it’s rebuilding.

I guess you get a rebuild at least every time CMake was called.  
You glob the source directory (which is usually discouraged) and you write that config.js to the list dir (which is the same as source dir).  
Using `file(WRITE)` and `file(APPEND)` do write the file every time CMake is executed.  
You should use `file(CONFIGURE)` or `file(GENERATE)` instead, which write the file at generation time only if the content differs.

---

<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 29, 2023, 1:09pm UTC](https://discourse.cmake.org/t/add-custom-command-rebuilds-every-time/8213/4 "2023-05-29T13:09:53Z")

</div>

Ignore my previous reply; I completely missed the `DEPENDS` sitting right in front of my face.
