-1

As a developer, I want to reduce file clutter on my machines. For example, I often run make clean when I temporarily put away a software project. This reduces noise in the project directory tree, and frees up disk space.

However, when run cmake --build . --target clean, then cmake leaves behind a TON of junk files.

I can work around most of these junk files, by appending a rather extensive boilerplate list of the names in an ADDITIONAL_CLEAN_FILES directory property:

set_property(
    DIRECTORY .
    APPEND PROPERTY
    ADDITIONAL_CLEAN_FILES
    "html;${EXECUTABLE_OUTPUT_PATH};debug;debug.log;Makefile;build.ninja;.ninja_deps;Testing;CTestTestfile.cmake;CMakeFiles;CMakeCache.txt;cmake_install.cmake;conanbuildinfo.cmake;conanbuildinfo.txt;conaninfo.txt;graph_info.json;conan.lock"
)

However, appending ;.ninja_log in this string has no effect. cmake appears to forcibly regenerate this file after every task, including cmake --build . --target clean. So there is that one annoying junk file leftover in all my cmake + ninja projects: .ninja_log.

I also tried working around this by declaring a custom cmake cleanup task:

add_custom_target(clean-ninja-log COMMAND "${CMAKE_COMMAND}" -E rm -f .ninja_log)

With the same results.

Currently, my operational workaround is to wrap cmake in yet another build system:

https://github.com/mcandre/rez

And have the outer build system run some C++ standard library calls to remove this file.

Another similar workaround involves writing a custom clean.ninja, clean.mk, clean.sh, or similar script. But these tend to limit portability, assuming either rm or del based commands. And it's annoying to have to use TWO scripting systems for basic things. I want a cmake task that works like make clean, for full project cleaning.

But if at all possible, I would prefer a solution that works in 100% cmake. Maybe I can configure cmake to stop running ninja -t restat after every single cmake task?

mcandre
  • 22,868
  • 20
  • 88
  • 147
  • You seem confused what CMake is and what is a `clean` target. CMake generates files, which can be used by a **build tool** for build the project. E.g., if you specify "Unix Makefiles" as [cmake generator](https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html), then after running `cmake` you could build the project using `make`. Construction `cmake --build . --target clean` is just a synonym for `make clean`. Or it is a synonym for `ninja -t clean` in case of Ninja generator. `make clean` doesn't remove `Makefile`, `ninja -t clean` doesn't remove `.ninja_log`. – Tsyvarev Apr 16 '23 at 09:10
  • If you want to remove all files generated by CMake for a build tool, then simply drop the entire build directory: `rm -r `. (Assuming you have performed out-of-source build). – Tsyvarev Apr 16 '23 at 09:12
  • rm is not portable. It fails on Command Prompt. – mcandre Apr 17 '23 at 13:11
  • Well, if you want a portable way to delete a directory, then you could use [command line tool of cmake](https://cmake.org/cmake/help/latest/manual/cmake.1.html#cmdoption-cmake-E-arg-rm): `cmake -E rm -r `. – Tsyvarev Apr 17 '23 at 14:19

1 Answers1

0

Out of source builds significantly improve cmake littering.

  • conan install --install-folder <build directory> allows conan to integrate with cmake out of source builds.
  • cmske -B <custom build directory> organizes the artifacts into a convenient directory, that is easier to remove.
  • cmake -E rm -rf <build directory> removes the cmske artifacts in a portable way.
  • cmake -P allows custom clean targets to be scripted in a way that doesn't have to trigger .ninja-log repopulation
  • cmake_path() and EXPAND_TILDAenable portable path construction during cmake shell command targets.

But I mostly just use rez.

mcandre
  • 22,868
  • 20
  • 88
  • 147