3

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vivek Goel
  • 22,942
  • 29
  • 114
  • 186

2 Answers2

4

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)
arrowd
  • 33,231
  • 8
  • 79
  • 110
  • is there any short hand to app-A-clean target for adding add_cutoms_target – Vivek Goel Nov 15 '11 at 11:00
  • Nope, but i've updated my answer with code snippet. It may need some refinements though. – arrowd Nov 15 '11 at 11:16
  • Thanks for the useful answer! Is "${CMAKE_BUILD_TOOL} clean" a valid command for every possible value of ${CMAKE_BUILD_TOOL} (which is now used instead of ${CMAKE_MAKE_PROGRAM}) on every platform? Or do we need to differentiate between the different build tools if we want a platform-independent solution? – PiQuer Jan 23 '14 at 18:02
  • Right this isn't gonna work =\ Well, there is no portable way to do such thing, then. – arrowd Jan 24 '14 at 09:54
2

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
André
  • 18,348
  • 6
  • 60
  • 74
  • My apologies. Are you working with MSVC? When I wrote the answer I tested it with MinGW but not MSVC. I just tested this again on my MVSC 2010 setup and it indeed cleans the dependencies... – André Nov 16 '11 at 08:20
  • no . I am on linux and using Make target of cmake. I think it only works for MVSC 2010 – Vivek Goel Nov 16 '11 at 08:42
  • @Vivek: Too bad, then I suggest doing something like arrowdodger: adding a custom clean-target per project. I will keep my answer here "for the record"... – André Nov 16 '11 at 08:52