# Race condition when multi add\_custom\_target deps on same file generated by add\_custom\_command

**URL:** https://discourse.cmake.org/t/race-condition-when-multi-add-custom-target-deps-on-same-file-generated-by-add-custom-command/2358
**Category:** Usage
**Created:** [December 12, 2020, 4:54pm UTC](https://discourse.cmake.org/t/race-condition-when-multi-add-custom-target-deps-on-same-file-generated-by-add-custom-command/2358 "2020-12-12T16:54:56Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![hehaoqian](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@hehaoqian](https://discourse.cmake.org/u/hehaoqian)
#### Post date: [December 12, 2020, 4:54pm UTC](https://discourse.cmake.org/t/race-condition-when-multi-add-custom-target-deps-on-same-file-generated-by-add-custom-command/2358/1 "2020-12-12T16:54:56Z")

</div>

**TLDR** : I do not understand the behavior of add\_custom\_command/add\_custom\_target when multiple of add\_custom\_command/add\_custom\_target depends on the same generated file created by add\_custom\_command in the same directory. It is causing commands unnessariliy run multiple times and may cause random build error when build is done in parallel, and CMake gives no warning when this occurs. Is this an intended behavior?

The current CMake document describes the “DEPENDS” keyword of add\_custom\_command as the following

> If any dependency is an OUTPUT of another custom command in the same directory (CMakeLists.txt file), CMake automatically brings the other custom command into the target in which this command is built.

Consider the following CMakeFiles.txt example. I am using CMake 3.19.1 and CentOS 7.3. Old CMake version has the same issue.

```
add_custom_command(
  OUTPUT output.txt
  DEPENDS input.txt
  COMMAND touch output.txt
  COMMENT “Generating output.txt”)

foreach (i RANGE 100)
  add_custom_target(job${i} ALL DEPENDS output.txt)
endforeach (i)

```

Run this CMakeLists.txt by

`cmake . && touch input.txt && make -j`

So I created 100 targets. All of them depends on output.txt.  
I would like to generate output.txt only once first, then run all 100 targets in parallel.

However, the output contains lots of “Generating output.txt”, implying the output.txt  
is generated multiple times in parallel. In this simple CMakeLists.txt, no build error would occur. But if output is generated by a complex command, build could fail randomly, just because two identical commands running in parallel and writing into the same path. This behavior is confusing and CMake gives no warning about this.

I know the issue can be workaround by ensuring generated file is only directly used by  
add\_custom\_target/add\_custom\_command

```
add_custom_command(
  OUTPUT output.txt
  DEPENDS input.txt
  COMMAND touch output.txt
  COMMENT “Generating output.txt”)

add_custom_target(gen_output DEPENDS output.txt)

foreach (i RANGE 100)
  add_custom_target(job${i} ALL DEPENDS gen_output)
endforeach (i)

```

But I wonder is this the indended usage? I do not want to add one target every time  
when the output of add\_custom\_command is the argument to the DEPENDS keyword of add\_custom\_command/add\_custom\_target multiple times in the current CMakeLists.txt

Is there a way to workaround this without adding a custom target?

Edit history

1. Reformat the post
2. Update TLDR
3. Add questions at the end

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [December 12, 2020, 10:02pm UTC](https://discourse.cmake.org/t/race-condition-when-multi-add-custom-target-deps-on-same-file-generated-by-add-custom-command/2358/2 "2020-12-12T22:02:57Z")

</div>

The current CMake documentation also says

> Do not list the output in more than one independent target that may build in parallel or the two instances of the rule may conflict (instead use the add\_custom\_target() command to drive the command and make the other targets depend on that one).

So adding a custom target seems to be the way.

---

<div class="post-metadata">

### Author: ![hehaoqian](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@hehaoqian](https://discourse.cmake.org/u/hehaoqian)
#### Post date: [December 13, 2020, 1:28am UTC](https://discourse.cmake.org/t/race-condition-when-multi-add-custom-target-deps-on-same-file-generated-by-add-custom-command/2358/3 "2020-12-13T01:28:10Z")

</div>

Why CMake raises no warning about this? I do think this is a common error. Should be easy to be detected by cmake

---

<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: [December 14, 2020, 1:01pm UTC](https://discourse.cmake.org/t/race-condition-when-multi-add-custom-target-deps-on-same-file-generated-by-add-custom-command/2358/4 "2020-12-14T13:01:03Z")

</div>

It sounds reasonable to me. Please file an issue to get such a warning. I suspect it will also involve a policy to make it a hard error.

---

<div class="post-metadata">

### Author: ![hehaoqian](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@hehaoqian](https://discourse.cmake.org/u/hehaoqian)
#### Post date: [December 14, 2020, 5:39pm UTC](https://discourse.cmake.org/t/race-condition-when-multi-add-custom-target-deps-on-same-file-generated-by-add-custom-command/2358/5 "2020-12-14T17:39:44Z")

</div>

I have posted an issue on Kitware Gitlab: [https://gitlab.kitware.com/cmake/cmake/-/issues/21596](https://gitlab.kitware.com/cmake/cmake/-/issues/21596)
