CMAKE generates clean target for subdirectory

I want to create a clean target for a subdirectory.

my Project strucuter looks like this

app/
  A
  B

lib/
   A
   B
   C 

      

Sometimes I only want to run cleanup for application / A and don't want to clean up the library.

Is it possible to tell cmake to create a clean target for each directory.

or a custom object like app-clean that will invoke cleanup in every subdirectory of the application

+1


source to share


2 answers


You can just cd to $ {CMAKE_BINARY_DIR} / app / A and run there make clean

.

You can of course add_custom_target (app-A-clean ...) which will 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 like this:

add_clean_target(app-A)

      

+4


source


Maybe try setting your CMakeLists.txt for project in every directory and "root" CMakeLists.txt which is just add_subdirectory for all subprojects ... then CMake will generate a solution file or makefile in every project directory. You can then "step" into that directory and build and clean one project.

eg. in one of my projects i have the following structure:

projectroot/
  applications/
    appA
  libraries/
    libA
    libB

      

In projectroot, I am setting 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 go in 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

ormsbuild /t:Clean appA.sln

An added benefit of an additional directory for all libraries is that I can create them immediately with runnning make

or msbuild libraries.sln

in a directory libraries/

.

+2


source







All Articles