I am working on a project to build&package 3rd party dependencies for an application, by calling ExternalProject_Add exclusively. This runs in linux vm-s using both Ninja and Unix Makefile generators.
The problem:
Ninja automagically detects parallelism. Well, in practice it detects the number of host CPUs instead of the ones allocated to the VM → overcommitting and competing for resources → very slow build
So the question is, how can I override the number of cores for the individual builds? I only need to support Unix MakeFile and Ninja generators, so an if (CMAKE_GENERATOR EQUALS “Ninja”) … else … kind of solution is still acceptable for me (but something generic would be OK too ).
At the end figured out something simpler. One can chain the cmake commands to set CMAKE_BUILD_PARALLEL_LEVEL and this works (maybe I am doing something wrong but passing --parallel 2 is failing with argument “2” not understood).
This is not a general solution but works for me because it’s always --build . for my project. TBH, things would be easier if the commands could be modified for example with ExternalProject_Get_Property/ExternalProject_Set_Property. Something like this:
# Do not specify BUILD_COMMAND, so the default will be generated
ExternalProject_Add(${target_name} ...)
ExternalProject_Get_Property(${target_name} BUILD_COMMAND)
ExternalProject_Set_Property(${target_name} BUILD_COMMAND ${CMAKE_COMMAND} -E env CMAKE_BUILD_PARALLEL_LEVEL=${NCORES} ${BUILD_COMMAND})