# Feature Request: add\_custom\_command(ALL)

**URL:** https://discourse.cmake.org/t/feature-request-add-custom-command-all/7609
**Category:** Development
**Created:** [March 6, 2023, 6:46pm UTC](https://discourse.cmake.org/t/feature-request-add-custom-command-all/7609 "2023-03-06T18:46:22Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![milianw](https://discourse.cmake.org/user_avatar/discourse.cmake.org/milianw/32/2035_2.png) [@milianw](https://discourse.cmake.org/u/milianw)
#### Post date: [March 6, 2023, 6:46pm UTC](https://discourse.cmake.org/t/feature-request-add-custom-command-all/7609/1 "2023-03-06T18:46:22Z")

</div>

Hey all,

would it be possible to add the `ALL` flag from `add_custom_target` for `add_custom_command` too? Or otherwise get the ability to manually add a dependency for the `ALL` pseudo target for a custom command?

The motivation comes from generation of runtime artifacts that are consumed by multiple targets, like tests or libraries or … Having to manually list these dependencies everywhere is cumbersome.

In practice, many projects just opt for using `add_custom_target` instead then to get the `ALL` dependency chain. But that has the really unfortunate side-effect of always-dirty builds. I.e. from the documentation from `add_custom_target`:

> The target has no output file and is _always considered out of date_

Due to this issue, `add_custom_target` is even banned at work to ensure we get a `ninja: no work to do.` after invoking it once (and not changing anything else).

To give a concrete example for a widely used cmake snippet that causes issues in that regard: the KDE i18n framework (ab)uses `add_custom_target` to generate mo/ts translation files, which is a costly operation. And because it’s always dirty, this runs every time we run `ninja` ☹ See  
[cmake/KF6I18nMacros.cmake.in · master · Frameworks / Ki18n · GitLab](https://invent.kde.org/frameworks/ki18n/-/blob/master/cmake/KF6I18nMacros.cmake.in#L137) and [460245 – ki18n\_install(po) slows down each build, even if nothing has changed](https://bugs.kde.org/show_bug.cgi?id=460245)

I would love to improve that situation, but the only hack that I could think of is adding a `add_custom_command` for each of these `add_custom_target` that do the actual generation, and then keeping the `add_custom_target` for the `ALL` convenience. This would at least prevent us from wasting cycles in re-generating the translations all the time, but it would still keep the `ALL` target always-dirty which is still sub-optimal.

As such: Could we just get a `add_custom_command(ALL)` instead? Or how do other projects handle situations like this?

---

<div class="post-metadata">

### Author: ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)
#### Post date: [March 6, 2023, 6:49pm UTC](https://discourse.cmake.org/t/feature-request-add-custom-command-all/7609/2 "2023-03-06T18:49:18Z")

</div>

The traditional way to handle this is:

```cmake
add_custom_command(OUTPUT file.txt COMMAND ${CMAKE_COMMAND} -E touch file.txt)
add_custom_target(tgt ALL DEPENDS file.txt)

```

---

<div class="post-metadata">

### Author: ![milianw](https://discourse.cmake.org/user_avatar/discourse.cmake.org/milianw/32/2035_2.png) [@milianw](https://discourse.cmake.org/u/milianw)
#### Post date: [March 6, 2023, 6:51pm UTC](https://discourse.cmake.org/t/feature-request-add-custom-command-all/7609/3 "2023-03-06T18:51:07Z")

</div>

Right, but as I said this has a really bad side-effect: Your normal `ninja` run will never be able to get into the `no work to do` state, right? I find this really not acceptable myself, do you simply not care?

---

<div class="post-metadata">

### Author: ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)
#### Post date: [March 6, 2023, 6:53pm UTC](https://discourse.cmake.org/t/feature-request-add-custom-command-all/7609/4 "2023-03-06T18:53:35Z")

</div>

The sample I gave above works differently from:

```cmake
add_custom_target(tgt ALL COMMAND ${CMAKE_COMMAND} -E touch file.txt BYPRODUCTS file.txt)

```

In the `add_custom_command()` + `add_custom_target()` example I gave above, once `file.txt` is created, when you run the build the next time around, it will say “no work to do” because `file.txt` already exists. Whereas with a standalone `add_custom_target()`, it always creates `file.txt` even if it already exists. Try the example I gave above - I think you’ll find that it works the way you want.
