Search code examples
cmakeninja

CMake/Ninja: Recursive "cleaning" of output directories when contents are unknown...?


Colleagues!

We use "cmake" for our build and regression system. Some users use "ninja" and others "make" for cmake's underlying generator.

We have some custom targets that call external tools that generate DIRECTORIES with a variety of sub-directories and files.

When attempting to "clean", Ninja users get an error like:

ninja: error: remove(<dirABC>/<subdirXYZ>): Directory not empty
ninja: error: remove(<dirABC>): Directory not empty
...

I list the output directory as an OUTPUT of my custom build target ("dirABC" above). That doesn't enable recursive deletion though.

The list of files and subdirectory names (below "dirABC") cannot be known at configure or generate time. Names are really only known after running.

My question is:

How can ninja users get these output directories and their contents deleted when they run "ninja clean"?

Note that "make" users don't have this problem. I tag the directory with an "ADDITIONAL_MAKE_CLEAN_FILES" property. Make does an "rm -rf" on the top-level output directory and we're clean. The key is that "-r"!

In cmake, I'd like to add a dependency to the top-level "clean" target to tell how to actually clean up, but cmake doesn't allow that...


Solution

  • You can set ADDITIONAL_CLEAN_FILES property on a target. This works for both ninja and make.

    set_property(
      TARGET <my_target>
      APPEND
      PROPERTY ADDITIONAL_CLEAN_FILES <dir_to_remove>)