[CMake] Adding dependencies to the `clean` target in 2016?

Dan Liew dan at su-root.co.uk
Sat Dec 3 06:52:00 EST 2016


Hi,

As part of a project I work on we use CMake's `ExternalProject()` to
build a set of runtime libraries that the project needs via a second
build system (Makefile based) that knows how to build the runtime
libraries. The code (which is full of hacks... sorry) is at [1].

What I want to do is when the `clean` target is invoked in the CMake
generated build system is have the command `make clean` invoked on the
project being built by `ExternalProject()`.

There was a post about this 10 years ago [1], has anything changed since then?

Using `ADDITIONAL_MAKE_CLEAN_FILES` is not really an option because
CMake doesn't know what files are generated by the other build system.
Even if CMake did know what those files were I don't think it should
be removing them itself. It should be using the other build system
because that knows best how to do a clean.

I realise the `clean` target won't be available for all generators but
for those that do, it would be really nice to able to do something
like this.

```
if (TARGET clean)
  add_custom_target(CleanKLEERuntimes
    COMMAND
    make -f mycustom_makefile clean
    USES_TERMINAL
)
add_dependencies(clean CleanKLEERuntimes)
endif()
```
Right now this doesn't work though.

* `if (TARGET clean)` is always false
* `add_dependencies()` does not work on the `clean` target.

[1] https://github.com/klee/klee/blob/28a3d47fa03b9ed98f1bbb4f1b38ecbd44ab1a00/runtime/CMakeLists.txt#L104
[2] https://cmake.org/pipermail/cmake/2006-October/011477.html


Thanks,
Dan.


More information about the CMake mailing list