Cmake parallel build

How do I set an option for a parallel build?
My approach so far:
add_library(myLib ${SRCS} ${HDRS})
add_custom_command(TARGET myLib COMMAND ${CMAKE_COMMAND} -j5})

It doesnt have any effect like this. Any other suggestion ?

Parallel build is a build tool parameter. Depending on which generator you use you have nothing to do (ninja) or you may specify it.
You may read this article to know more: https://blog.kitware.com/cmake-building-with-all-your-cores/

Thanks. Ive read it already. It says nothing of the way, I would like to trigger the parallel build.

I would like to force a parallel build for make (not ninja or sth else) over cmake

It may be that using cmake --build . -j is sufficient for the level of parallelism you are looking for.

It would have been, if the above custom_command is the way to achieve it ?

I just type cmake --build . -j at the command line when I want to build. You mentioned using make; you would just type make -j. There is no need for add -j via a custom command.

Right, I mentioned I do not want to type over the command line, but over a CMAKE_CUSTOM_COMMAND or whatever else possible. So, any suggestion of best practices of doing it, would be appreciated.

I don’t think you can do that but you can call make -j 5 or cmake --build -j5 the latter may use an environment var:
https://cmake.org/cmake/help/latest/envvar/CMAKE_BUILD_PARALLEL_LEVEL.html

Thanks, but as I mentioned already, Im looking for a solution, not using the command line.

How do you trigger your build?
you said:

so you use make but don’t call it?
I miss something.

I would like to set Generator Unix Makefiles, and than tell make to use the -j N parallel option.

1 Like

You do not really answer my question.

  • If you do invoke make then you may define make options in your environment MAKEFLAGS=-j5
  • if you do invoke cmake --build then you may CMAKE_BUILD_PARALLEL_LEVEL=5

In any case this is a user choice so such config ought to stay in user’s hand not in the CMakeLists.txt

You may invoke such parallel build using a custom_target if you prefer that:

add_custom_target(my_parallel_build 
                      COMMAND ${CMAKE_COMMAND} --build -j 5
                      WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
                      COMMENT "My parallel build with 5 cores")

and then you would invoke:
make my_parallel_build

I didnt know how to answer better your questions.

My goal is, that if a user sets to use a Unix Makefiles generator, and he if he forgets to set the parallel build flag, that than I add this flag over CMake.

The question is, if this is possible, and if yes, what is the way?

It is not currently possible to enforce parallelism of the build from within CMakeLists.txt.

1 Like

Except for setting the flag for Win VS. For Unix Makefile it is not supported.

If you are referring to /MP, that’s a flag of the compiler, not of the build tool.

GCC and clang simply don’t have such a flag. You could make a feature request on these projects.

1 Like

Wasnt aware of it. Part of the compiler? So the MS compiler is also a build tool and it parallelizes its compilation process? Interesting.

The build tool used by Visual Studio is MSBuild. But you can also use MSVC (the compiler) with Ninja, or NMake Makefiles.

When you pass /MP and several input files to MSVC, it “multiplies” itself to run in parallel. MSVC doesn’t track modification dates and dependencies, it is not a build tool.

1 Like

I have to ask again, why isnt it possible to set some BUILD_ARGS over the CMakeLists.txt?

It doesnt seem to me technically difficult, to set there a variable, which is expanded after cmake --build to .e.g. an additional --parallel N or --j N as I would like to in my case.

From the CMake documentation for --parallel [<jobs>], -j [<jobs>]:
The CMAKE_BUILD_PARALLEL_LEVEL environment variable, if set, specifies a default parallel level when this option is not given.