# add\_custom\_command at the top level directory

**URL:** <https://discourse.cmake.org/t/add-custom-command-at-the-top-level-directory/1210>\
**Category:** Code\
**Created:** [May 13, 2020, 9:48pm UTC](https://discourse.cmake.org/t/add-custom-command-at-the-top-level-directory/1210 "2020-05-13T21:48:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![developer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@developer](https://discourse.cmake.org/u/developer)\
**Post date:** [May 13, 2020, 9:48pm UTC](https://discourse.cmake.org/t/add-custom-command-at-the-top-level-directory/1210/1 "2020-05-13T21:48:10Z")

</div>

I would like to iterate all my project targets with top level scripts. Then, I would like to add pre-build steps for all of them using `add_custom_command`. I realize that by default in newer versions of cmake this does not work because the documentation states that

> The \<target\> must be defined in the current directory; targets defined in other directories may not be specified.

I am tempted to set [CMP0040](https://cmake.org/cmake/help/latest/policy/CMP0040.html) to allow me to do this. What might the tradeoffs or negatives aspects of me doing this? It seems like a common usage to me to what to add certain steps/configuration to multiple targets. Not sure why this functionality is being deprecated?

---

<div class="post-metadata">

**Author:** ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)\
**Post date:** [May 14, 2020, 10:53am UTC](https://discourse.cmake.org/t/add-custom-command-at-the-top-level-directory/1210/2 "2020-05-14T10:53:03Z")

</div>

Note that at least according to the docs, setting the policy to `OLD` will do nothing to enable the commands to actualy work. They will just be silently ignored (as before) instead of an error being raised (as the `NEW` behaviour dictates).
