[CMake] Only build a subset of all targets in ALL_BUILD

Brad King brad.king at kitware.com
Tue Aug 23 13:39:01 EDT 2005


wedekind wrote:
> But what if I only want to build a subset of all components? I want to
> choose the components to build and let cmake find out the dependent targets
> that must also be build in order to build the chosen components. Only those
> components will be built that are really necessary, not all.

You can use the OPTION command to turn on/off features:

OPTION(BUILD_PART_A "Build component A" ON)
OPTION(BUILD_PART_B "Build component B" ON)
IF(BUILD_PART_A)
   ADD_SUBDIRECTORY(A)
ENDIF(BUILD_PART_A)
IF(BUILD_PART_B)
   ADD_SUBDIRECTORY(B)
ENDIF(BUILD_PART_B)

CMake only knows how to trace dependencies of targets that are added by 
the CMakeLists.txt code.  In order to add/not-add targets you'll have to 
write your own IF blocks to automatically turn on needed parts.

Alternatively you could always enable all targets and add your own 
version of ALL_BUILD that depends only on the targets you want:

ADD_CUSTOM_TARGET(SUB_BUILD)
IF(BUILD_PART_A)
   ADD_DEPENDENCIES(SUB_BUILD PartA)
ENDIF(BUILD_PART_A)
# ...

Then building SUB_BUILD would build just the parts you want.  I have not 
tried this approach before though.

-Brad


More information about the CMake mailing list