# How to rebuild a specific subdirectory?

**URL:** https://discourse.cmake.org/t/how-to-rebuild-a-specific-subdirectory/547
**Category:** Usage
**Tags:** gen:ninja
**Created:** [January 28, 2020, 4:08pm UTC](https://discourse.cmake.org/t/how-to-rebuild-a-specific-subdirectory/547 "2020-01-28T16:08:36Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![DavidA](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davida/32/128_2.png) [@DavidA](https://discourse.cmake.org/u/DavidA)
#### Post date: [January 28, 2020, 4:08pm UTC](https://discourse.cmake.org/t/how-to-rebuild-a-specific-subdirectory/547/1 "2020-01-28T16:08:36Z")

</div>

Hi,

I have a CMakeLists.txt file that builds an executable and includes add\_subdirectory() calls to build a series of dynamic libraries - one per subdirectory. This runs on Linux and uses the Ninja generator.

With this we can, of course, do a clean build using:

ninja clean  
ninja

Is it possible to do a clean of one specific subdirectory?

I realise I could just delete the appropriate object code folder in CMakeFiles, but I wondered whether there is a more elegant way?

Best regards  
David

---

<div class="post-metadata">

### Author: ![sjoubert](https://discourse.cmake.org/user_avatar/discourse.cmake.org/sjoubert/32/110_2.png) [@sjoubert](https://discourse.cmake.org/u/sjoubert)
#### Post date: [January 28, 2020, 7:36pm UTC](https://discourse.cmake.org/t/how-to-rebuild-a-specific-subdirectory/547/2 "2020-01-28T19:36:55Z")

</div>

There are `<sub>/<dir>/all` targets to build/rebuild a specific folder, unfortunately no `<sub>/<dir>/clean` but you can use the ninja `clean` tool on that target to clean it before:  
`$ ninja -t clean <sub>/<dir>/all`  
`$ ninja <sub>/<dir>/all`

---

<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: [January 28, 2020, 7:40pm UTC](https://discourse.cmake.org/t/how-to-rebuild-a-specific-subdirectory/547/3 "2020-01-28T19:40:25Z")

</div>

Running `ninja -t clean all` won’t actually clean all targets, as ones that have [`EXCLUDE_FROM_ALL`](https://cmake.org/cmake/help/latest/prop_tgt/EXCLUDE_FROM_ALL.html) set will not be cleaned.

This would require first-class support in CMake itself, but it is doable - we took a similar approach in the [Ninja Multi-Config](https://cmake.org/cmake/help/git-master/generator/Ninja%20Multi-Config.html) generator to only clean targets for a specific configuration.

---

<div class="post-metadata">

### Author: ![DavidA](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davida/32/128_2.png) [@DavidA](https://discourse.cmake.org/u/DavidA)
#### Post date: [January 29, 2020, 10:04am UTC](https://discourse.cmake.org/t/how-to-rebuild-a-specific-subdirectory/547/4 "2020-01-29T10:04:40Z")

</div>

Thank you for your answers.
