Generic way to enable cpp files multiprocessing for any project (MSBuild)

-j flag in cmake --build enabled multiprocessing for different Visual Studio projects (e.g. different targets), but typically user needs multiprocessing for different .cpp files and projects, not just projects.

Providing /MP flag to cl resolves that. It can be provided using CXXFLAGS environment variable, but some cmake project might override it - it’s a bad practice, but it does happen. Alternative is to chage cmake file, but it’s also undesirable.

So the solution would be to provide it somehow to --build command. Providing it as cmake --build . -v -- /p:MultiProcessorCompilation=true doesn’t seems to work (though there are reports that it did worked before or works in some cases, see). Which is kind of odd, since /p:WholeProgramOptimization=true works fine and sets /GL flag correctly. Not sure if there’s some other property to pass additional options to cl.

As a workaround I’ve found /p:UseMultiToolTask=true - by default msbuild starts a single cl process for each project passing multiple .cpp files to it. But with this option, it creates separate cl process for each .cpp file. By default it creates them as many as there are effective processors on the computer. It can be adjusted using /p:CL_MPCount=N.

So the ultimate command for enabling multiprocessing seems to be this. It will build multiple projects and .cpp files in parallel, limiting the number of processes across all projects.

cmake --build . -- /p:UseMultiToolTask=true /m /p:EnforceProcessCountAcrossBuilds=true

Questions:

  1. Is this the way to do it? Are there any downsides to UseMultiToolTask vs /MP?
  2. Is there a generic way to provide some cl option using msbuild arguments?
  3. Is there a way to set /MP from msbuild arguments?