CMake: Enable parallel builds by default when using `--build` command line option
I want to pick up the discussion from https://blog.kitware.com/cmake-building-with-all-your-cores/ where @bill-hoffman commented:
The only part of CMake that could learn these flags would be the
--build
part of CMake. CMake is a meta build tool, and at the end of the day it creates some build files, and then you don’t run CMake again. So, there really would not be a way to force parallelism from CMake other than in the--build
option.
I think it would be a nice touch for CMake users if we take the good hints from the blog post's discussion and make it the default. And I would restrict this defaulting to "Target Level Parallelism".
For me CMake is an great abstraction layer for the build environment and users may not want to learn the make
flag details. So more advanced users could still overwrite the defaults with their own flags if they want to (or already have).
I'm willing to do the necessary CMake code changes, but I wanted to discuss possible concerns first.
My summary of what could be a starting point (with N being the number of cores determined by CMake):
- GNU Make: -j -l N
- MSBuild: /m
- JOM / Ninja: already the default
And some reference:
- https://cmake.org/pipermail/cmake/2011-November/047783.html
- https://stackoverflow.com/questions/10688549/how-to-configure-portable-parallel-builds-in-cmake
- https://stackoverflow.com/questions/36633074/how-to-setup-the-number-of-threads-in-cmake-build-from-the-command-line
- https://stackoverflow.com/questions/44433285/parallel-commands-in-cmake