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?
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.
macro(add_clean_target dir)
add_custom_target(clean-${dir} COMMAND ${CMAKE_MAKE_PROGRAM} clean WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${dir})
endmacro(add_clean_target)
Now you can use it in such way:
add_clean_target(app-A)
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:
projectroot/
applications/
appA
libraries/
libA
libB
In projectroot I set up a CMakeLists.txt like this:
project(MyProject)
add_subdirectory(libraries)
add_subdirectory(applications)
In libraries/
, I do the following:
project(Libraries)
add_subdirectory(libA)
add_subdirectory(libB)
add_subdirectory(libC)
And then in applications/
:
project(Applications)
add_subdirectory(appA)
In this case, I can step into projectroot/applications/appA
and call make
or msbuild appA.sln
and it will start building libA, B, C and then appA. Similarly you can call make clean
or msbuild /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
or msbuild libraries.sln
in the libraries/
dir.