How to rebuild a specific subdirectory?


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

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

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

1 Like

Running ninja -t clean all won’t actually clean all targets, as ones that have EXCLUDE_FROM_ALL 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 generator to only clean targets for a specific configuration.

Thank you for your answers.