0

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...
Sedji Aka
  • 217
  • 2
  • 10
  • Soo `cmake --build --preset=myPreset --target component1`? – KamilCuk Nov 04 '22 at 09:24
  • it doesn't work because component1 is a subdirectory and not a target : ```ninja: error: unknown target component1``` – Sedji Aka Nov 04 '22 at 09:27
  • Then pick a target from the subdirectory? Edit Component1/CMakeList.txt , add a Component1 target that has all the targets from that file, and then compile. – KamilCuk Nov 04 '22 at 09:27
  • How do you do a target which contains other targets ? by doing a target_link_libraries ? – Sedji Aka Nov 04 '22 at 09:31
  • Something along `add_custom_target(Component1 DEPENDS target1 target2 etc)`. I also found out https://cmake.org/cmake/help/latest/prop_dir/BUILDSYSTEM_TARGETS.html – KamilCuk Nov 04 '22 at 09:31

1 Answers1

1

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
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 1
    Thanks, the BUILDSYSTEM_TARGETS is not yet a GLOBAL PROPERTY, we can only use it like a DIRECTORY PROPERTY, but by using function and macros i can have my solution : https://stackoverflow.com/a/62311397/7079226 – Sedji Aka Nov 04 '22 at 09:56