Is it somehow possible to be able to have a parallel build no matter which build tool is used?
Under Unix we can add make -jN
where N are the number of threads
and under Windows I added to the CXX_FLAG "/MP"
which is then used in Visual Studio to parallel build...? How can I make my version such that CMAKE_MAKE_PROGRAM
is not always extended when I run CMake?
What is a general solution?
I came up with this:
#Add some multithreaded build support
MARK_AS_ADVANCED(MULTITHREADED_BUILD)
set(MULTITHREADED_BUILD 12 CACHE STRING "How many threads are used to build the project")
if(MULTITHREADED_BUILD)
if(${CMAKE_GENERATOR} MATCHES "Unix Makefiles")
message(STATUS ${CMAKE_BUILD_TOOL})
set(CMAKE_MAKE_PROGRAM "${CMAKE_MAKE_PROGRAM} -j${MULTITHREADED_BUILD}")
message(STATUS "Added arguments to CMAKE_BUILD_TOOL: ${CMAKE_MAKE_PROGRAM}")
elseif(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
message(STATUS "Added parallel build arguments to CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
endif()
endif()
You can't do this cross-platform. The -jN option is a parameter to make, and not part of the generated Makefile. However, you could have CMake generate a bash script that runs make for your project using -jN (where the script looks up the number of cores you have).
As this post is already a bit old:
My approach i have settled down to is writing a
parallelmake.sh
script forUnix Makefiles
based Generators This is done here: https://github.com/gabyx/ApproxMVBBAnd the relevant parts in the the cmake file:
https://github.com/gabyx/ApproxMVBB/blob/master/CMakeLists.txt#L89
If you have
cmake
v2.8.8 or higher, you may useninja
as an alternative of GNUmake
or
As you can see, no need to use
CMAKE_MAKE_PROGRAM
, the build is run in parallel by default, optimizing the number of jobs depending on available CPU cores.ninja
is based on a low-level JSON configuration to speed up the startup phase. Therefore its JSON configuration is not easy to write by hand and I always generate it using a high-level tool/IDE:ninja
configuration at https://github.com/ninja-build/ninja/wiki/List-of-generators-producing-ninja-build-filesAs C++ build often requires lots of memory, your computer must provide as much memory as the number of CPU cores.
With CMake 3.12 this is possible. From the release notes:
Edit: As mentioned by @dkg you can also set the environment variable
CMAKE_BUILD_PARALLEL_LEVEL
.Links to CMake's documentation: