I want to generate a clean target for a subdirectory.
My project structure is like this:
app/
A
B
lib/
A
B
C
Sometimes I want to run clean on just app/A and don't want to clean the library.
Is it possible to tell CMake to generate a clean target for each directory?
Or a custom target like app-clean which will call clean on each application sub-directory?
Perhaps try to set up your CMakeLists.txt to have a project in each directory, and a "root" CMakeLists.txt which simply does add_subdirectory for all the sub-projects... then CMake will generate a solution-file or makefile in each project-directory. You can then "step into" such a directory and build and and clean one project.
For example, in one of my projects I have the following structure:
In projectroot I set up a CMakeLists.txt like this:
In
libraries/
, I do the following:And then in
applications/
:In this case, I can step into
projectroot/applications/appA
and callmake
ormsbuild appA.sln
and it will start building libA, B, C and then appA. Similarly you can callmake clean
ormsbuild /t:Clean appA.sln
.The added benefit of an extra directory for all libraries is that I can build them at once by running
make
ormsbuild libraries.sln
in thelibraries/
dir.You can just cd to ${CMAKE_BINARY_DIR}/app/A and run
make clean
there.Of course, you can add_custom_target(app-A-clean ...) which would do this for you.
Now you can use it in such way: