23

In my CMake project I'd like to have some custom behavior happen when the clean target is run. However, if I do

add_dependencies(clean extra-clean)

it fails with

add_dependencies Adding dependency to non-existent target: clean

Is it even possible to have custom clean behavior?

EDIT: I am running some external tools as part of the build. These tools generate lots of files, but they can be cleaned up simply by asking the tool to do so. Ideally, I could add a custom target to the clean target to call the tool to do its cleanup.

3
  • 2
    I'd still like to know the answer to the clarified question! None of the current answers actually answer it. How to run some extra commands when "make clean" is run, e.g. the "make clean" of an external tool?
    – Ben Farmer
    Feb 6, 2015 at 13:12
  • 5
    Same issue I want to add a custom "action" to the global clean target. or a custom command or a custom target clean is dependent upon so it will get called when "clean" is requested.
    – peterk
    Nov 8, 2018 at 15:14
  • 2
    Same here... there are still no answers that explain how to execute a custom command whenever the clean target is run.
    – Carlo Wood
    Mar 26, 2023 at 22:24

1 Answer 1

3

What is the custom behavior you are looking for? If you need to remove files you can use set_directory_properties.

7
  • Fortunately, the external tool I'm using can be configured to put everything in one directory, which allows me to use set_directory_properties. :)
    – paleozogt
    Oct 15, 2010 at 16:08
  • 6
    It is not clear to me which property to set that will allow for the desired behavior. Jul 27, 2015 at 20:35
  • 1
    Bear in mind it only works in Makefile generators (as the name suggests)
    – wmamrak
    Sep 8, 2016 at 8:44
  • 9
    Please give a complete example of how to add a directory to the clean target. Jun 16, 2020 at 15:23
  • 1
    That's not good enough. For example, when the user needs to run a custom command to clean up leftover Conan packages.
    – mcandre
    Apr 16, 2023 at 0:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.