Search code examples
cmakepreset

CMake : Compile only one subdirectory with preset


I have a project with this structure, where Components are subdirectories :

CMakeList.txt
CMakePresets.json
|
---Component1/CMakeList.txt
|
---Component2/CMakeList.txt
|
---Component3/CMakeList.txt

I would like to compile only Component1 with the root preset. (I mean compile all targets under Component1).

Normally, to configure and compile all the project i use this commands :

#Configuration
cd myBuildDir
cmake mySourcedDir --preset=myPreset
#Compilation
cd mySourcedDir 
cmake --build --preset=myPreset

Problems :

  • With ninja, after configuration, the myBuildDir/Component1 directory doesn't contain build.ninja file
  • If i try to do cmake --build in the mySourcedDir/Component1 directory, i have an error message : CMake Error: Could not read presets from...

Solution

  • Try editing Component1/CMakeList.txt with:

    get_property(ALL_BUILDSYSTEM_TARGETS DIRECTORY PROPERTY BUILDSYSTEM_TARGETS)
    add_custom_target(Component1 DEPENDS ${ALL_BUILDSYSTEM_TARGETS})
    

    And then do:

    cmake --build --preset=myPreset --target component1