# Why CMake add\_custom\_command with DEPFILE reruns every time?

**URL:** https://discourse.cmake.org/t/why-cmake-add-custom-command-with-depfile-reruns-every-time/6978
**Category:** Usage
**Created:** [November 29, 2022, 10:47am UTC](https://discourse.cmake.org/t/why-cmake-add-custom-command-with-depfile-reruns-every-time/6978 "2022-11-29T10:47:37Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Crazy\_Sage](https://discourse.cmake.org/user_avatar/discourse.cmake.org/crazy_sage/32/2962_2.png) [@Crazy\_Sage](https://discourse.cmake.org/u/Crazy_Sage)
#### Post date: [November 29, 2022, 10:47am UTC](https://discourse.cmake.org/t/why-cmake-add-custom-command-with-depfile-reruns-every-time/6978/1 "2022-11-29T10:47:38Z")

</div>

I’m trying to write my cmake script as follows:

```auto
set(OUTPUT_FILE "${CMAKE_BINARY_DIR}/static_init/generated/${target}/static_init.cpp")
set(DEP_FILE "${CMAKE_BINARY_DIR}/static_init/input/${target}.d")
write_file(${DEP_FILE} "${OUTPUT_FILE}: ")

add_custom_command(
     OUTPUT ${OUTPUT_FILE}
     COMMAND ${Python3_EXECUTABLE} myscript.py
     DEPFILE ${DEP_FILE}
     VERBATIM)

```

I expect, that I will populate .d file during my script first launch and later custom command will be rerun only when one of files listed after colon will change. Instead command is running during every compilation, even with empty dependencies list. Printing my own sources during cmake run also leads me to constant command rerunning. What am I doing (or understanding) wrong?

CMake  
3.18.5  
Ninja Clang

---

<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: [November 29, 2022, 1:41pm UTC](https://discourse.cmake.org/t/why-cmake-add-custom-command-with-depfile-reruns-every-time/6978/2 "2022-11-29T13:41:23Z")

</div>

Due to `Ninja` behavior, it seems that a dep file without any dependencies (so exactly what you have in your example) imply that the command is rerun at each launch.

Now, I jus tested that, when a depfile have some dependencies, the command is run just the first time. In your case, are you sure that the depfile you generate has the correct format?

---

<div class="post-metadata">

### Author: ![Crazy\_Sage](https://discourse.cmake.org/user_avatar/discourse.cmake.org/crazy_sage/32/2962_2.png) [@Crazy\_Sage](https://discourse.cmake.org/u/Crazy_Sage)
#### Post date: [November 29, 2022, 2:03pm UTC](https://discourse.cmake.org/t/why-cmake-add-custom-command-with-depfile-reruns-every-time/6978/3 "2022-11-29T14:03:45Z")

</div>

My first try was absolute source paths separated by spaces, it was the same. Maybe separator should be \n?

---

<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: [November 29, 2022, 2:09pm UTC](https://discourse.cmake.org/t/why-cmake-add-custom-command-with-depfile-reruns-every-time/6978/4 "2022-11-29T14:09:56Z")

</div>

The format is explained [here](https://cmake.org/cmake/help/latest/command/add_custom_command.html), under `DEPFILE` option.

Space is OK as separator. But are you sure that all files specified as dependencies exist? Moreover, the depfile is transformed by `CMake` before to be consumed by `ninja`. Can you check the effective file used by ninja? This file is under `<build-dir>/CMakeFiles/d` directory.

---

<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: [November 30, 2022, 9:23am UTC](https://discourse.cmake.org/t/why-cmake-add-custom-command-with-depfile-reruns-every-time/6978/5 "2022-11-30T09:23:51Z")

</div>

You can try Ninja to tell you why it’s rebuilding: `ninja -d explain`

---

<div class="post-metadata">

### Author: ![Crazy\_Sage](https://discourse.cmake.org/user_avatar/discourse.cmake.org/crazy_sage/32/2962_2.png) [@Crazy\_Sage](https://discourse.cmake.org/u/Crazy_Sage)
#### Post date: [November 30, 2022, 1:52pm UTC](https://discourse.cmake.org/t/why-cmake-add-custom-command-with-depfile-reruns-every-time/6978/6 "2022-11-30T13:52:11Z")

</div>

Yes, also found that option. Problem was that DEPFILE parameter and target in dep file should be relative to build dir, not absolute.

---

<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: [November 30, 2022, 3:08pm UTC](https://discourse.cmake.org/t/why-cmake-add-custom-command-with-depfile-reruns-every-time/6978/7 "2022-11-30T15:08:07Z")

</div>

Not at all. Absolute paths are perfectly valid and supported. See also [CMP0116](https://cmake.org/cmake/help/latest/policy/CMP0116.html#policy:CMP0116) which is relative to paths in depfile.

---

<div class="post-metadata">

### Author: ![Crazy\_Sage](https://discourse.cmake.org/user_avatar/discourse.cmake.org/crazy_sage/32/2962_2.png) [@Crazy\_Sage](https://discourse.cmake.org/u/Crazy_Sage)
#### Post date: [November 30, 2022, 5:04pm UTC](https://discourse.cmake.org/t/why-cmake-add-custom-command-with-depfile-reruns-every-time/6978/8 "2022-11-30T17:04:13Z")

</div>

Maybe toolchain we are using isn’t the latest, but ninja -d explain messages was clear, they said, that there was absolute path where relative one was expected and when I fixed it - everything was ok.

---

<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: [December 1, 2022, 10:42am UTC](https://discourse.cmake.org/t/why-cmake-add-custom-command-with-depfile-reruns-every-time/6978/9 "2022-12-01T10:42:22Z")

</div>

AFAIK you need CMake 3.20 for the handling of absolute paths. @Crazy_Sage uses 3.18
