# Directory creation by ninja when using BYPRODUCTS of add\_custom\_command

**URL:** https://discourse.cmake.org/t/directory-creation-by-ninja-when-using-byproducts-of-add-custom-command/10859
**Category:** Usage
**Created:** [May 15, 2024, 11:21am UTC](https://discourse.cmake.org/t/directory-creation-by-ninja-when-using-byproducts-of-add-custom-command/10859 "2024-05-15T11:21:54Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Chardrazle](https://discourse.cmake.org/user_avatar/discourse.cmake.org/chardrazle/32/4362_2.png) [@Chardrazle](https://discourse.cmake.org/u/Chardrazle)
#### Post date: [May 15, 2024, 11:21am UTC](https://discourse.cmake.org/t/directory-creation-by-ninja-when-using-byproducts-of-add-custom-command/10859/1 "2024-05-15T11:21:54Z")

</div>

Further to [issue 25808](https://gitlab.kitware.com/cmake/cmake/-/issues/25808#note_1499441), I experimented a little more with the pattern…bear with 🙂

@brad.king on cursory reading, the comment you made [here](https://gitlab.kitware.com/cmake/cmake/-/issues/25808#note_1499440) _is_ what was being performed in the example cmake configuration as given. I think the detail that needs emphasis is “…If you use a custom command to **generate** the…”.  
As you point out earlier, in the example there is nothing explicitly tying the imported binary. Reading the docs for `add_custom_command`/`BYPRODUCTS` this sounds like something that would give that binding to the imported binary. It sounds like one of its purposes is to help the Ninja generator.

So, taking the example and adding the extra line to the custom command: `BYPRODUCTS ${importedBinary}` the rule link is provided for Ninja (we’ll gloss over the _clean_ step potentially removing this file for the point of this).

Now, here’s the thing. If the steps are followed in the example, when _ninja_ is invoked and finds it needs to recreate the (_indirection_) symlink, it _first_ creates a directory of the same name. The outcome is the subsequent cmake _create\_symlink_ fails.

This is mainly an academic exercise now (as an alternative approach has been taken), but it could be highlighting some underlying issue.

---

<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: [June 10, 2024, 8:08pm UTC](https://discourse.cmake.org/t/directory-creation-by-ninja-when-using-byproducts-of-add-custom-command/10859/2 "2024-06-10T20:08:13Z")

</div>

`ninja` is [defined to create the directories for any outputs that do not exist](https://github.com/ninja-build/ninja/blob/0b4b43aa3e2fee391443dcc0c961c9d2354d8954/src/build.cc#L892-L905) when it starts work on an edge.
